Skip to content

Commit 451bea1

Browse files
committed
Add support for compound PKs in test suite
- also added new backend feature support flag to check support of
1 parent 3521dae commit 451bea1

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

ts/index.tests.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as expect from 'expect'
22
import StorageManager from '.'
3-
import { StorageBackend, FieldType, CollectionFields, Relationship, PrimitiveFieldType } from './types'
3+
import { StorageBackend, FieldType, CollectionFields, Relationship, PrimitiveFieldType, CollectionDefinitionMap } from './types'
44
import { StorageBackendFeatureSupport } from './types/backend-features';
55
import { FieldTypeRegistry } from './fields';
66
import { Field } from './fields/types';
@@ -276,7 +276,7 @@ export function testStorageBackendOperations(backendCreator : StorexBackendTestB
276276
const relationshipType = options.relationshipType || 'childOf'
277277

278278
const storageManager = new StorageManager({ backend: options.backend })
279-
storageManager.registry.registerCollections({
279+
const collectionDefinitions: CollectionDefinitionMap = {
280280
user: {
281281
version: new Date(2019, 1, 1),
282282
fields: options.userFields || {
@@ -292,7 +292,24 @@ export function testStorageBackendOperations(backendCreator : StorexBackendTestB
292292
{ [relationshipType as any]: 'user', ...relationshipOptions } as any
293293
]
294294
}
295-
})
295+
}
296+
297+
if (options.backend.supports('compoundPrimaryKeys')) {
298+
collectionDefinitions.tag = {
299+
version: new Date(2019, 1, 1),
300+
fields: {
301+
name: { type: 'string' },
302+
},
303+
relationships: [
304+
{ childOf: 'email', reverseAlias: 'tags', alias: 'email' },
305+
],
306+
indices: [
307+
{ field: ['name', 'email'], pk: true },
308+
],
309+
}
310+
}
311+
312+
storageManager.registry.registerCollections(collectionDefinitions)
296313
await storageManager.finishInitialization()
297314
await storageManager.backend.migrate()
298315
return { storageManager }
@@ -621,7 +638,27 @@ export function testStorageBackendOperations(backendCreator : StorexBackendTestB
621638
await storageManager.operation('executeBatch', [])
622639
})
623640

624-
it('should support batch operations with compound primary keys')
641+
it('should support batch operations with compound primary keys', { shouldSupport: ['compoundPrimaryKeys'] }, async function (context) {
642+
const { storageManager } = await setupChildOfTest({ backend: context.backend })
643+
expect(storageManager.registry.collections['tag'].pkIndex)
644+
.toEqual(expect.arrayContaining(['name', 'email']))
645+
646+
const { object: user1 } = await storageManager.collection('user').createObject({displayName: 'Jack'})
647+
const { object: email1 } = await storageManager.collection('email').createObject({ user: user1.id, address: '[email protected]' })
648+
649+
await storageManager.operation('executeBatch', [
650+
{ operation: 'createObject', collection: 'tag', args: { email: email1.id, name: 'cool' } },
651+
{ operation: 'createObject', collection: 'tag', args: { email: email1.id, name: 'groovy' } },
652+
{ operation: 'createObject', collection: 'tag', args: { email: email1.id, name: 'rad' } },
653+
])
654+
655+
expect(await storageManager.collection('tag').findObjects({ email: email1.id }))
656+
.toEqual(expect.arrayContaining([
657+
{ email: email1.id, name: 'cool' },
658+
{ email: email1.id, name: 'groovy' },
659+
{ email: email1.id, name: 'rad' },
660+
]))
661+
})
625662

626663
it('should support batches with updateObjects operations', { shouldSupport: ['executeBatch'] }, async function (context : TestContext) {
627664
const { storageManager } = await setupChildOfTest({ backend: context.backend })

ts/types/backend-features.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface StorageBackendFeatureSupport {
77
updateWithRelationships? : boolean
88
relationshipFetching? : boolean
99
crossRelationshipQueries? : boolean
10+
compoundPrimaryKeys?: boolean
1011
executeBatch? : boolean
1112
batchCreates? : boolean
1213
resultLimiting? : boolean

0 commit comments

Comments
 (0)