Skip to content

Commit

Permalink
Add support for compound PKs in test suite
Browse files Browse the repository at this point in the history
- also added new backend feature support flag to check support of
  • Loading branch information
poltak committed Jul 16, 2019
1 parent 3521dae commit 66f2c26
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
46 changes: 42 additions & 4 deletions ts/index.tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as expect from 'expect'
import StorageManager from '.'
import { StorageBackend, FieldType, CollectionFields, Relationship, PrimitiveFieldType } from './types'
import { StorageBackend, FieldType, CollectionFields, Relationship, PrimitiveFieldType, CollectionDefinitionMap } from './types'
import { StorageBackendFeatureSupport } from './types/backend-features';
import { FieldTypeRegistry } from './fields';
import { Field } from './fields/types';
Expand Down Expand Up @@ -276,7 +276,7 @@ export function testStorageBackendOperations(backendCreator : StorexBackendTestB
const relationshipType = options.relationshipType || 'childOf'

const storageManager = new StorageManager({ backend: options.backend })
storageManager.registry.registerCollections({
const collectionDefinitions: CollectionDefinitionMap = {
user: {
version: new Date(2019, 1, 1),
fields: options.userFields || {
Expand All @@ -292,7 +292,25 @@ export function testStorageBackendOperations(backendCreator : StorexBackendTestB
{ [relationshipType as any]: 'user', ...relationshipOptions } as any
]
}
})
}

if (options.backend.supports('compoundPrimaryKeys')) {
collectionDefinitions.tag = {
version: new Date(2019, 1, 1),
fields: {
name: { type: 'string' },
},
relationships: [
{ childOf: 'email', reverseAlias: 'tags', alias: 'email' },
],
indices: [
{ field: 'name', pk: true },
{ field: 'email', pk: true },
],
}
}

storageManager.registry.registerCollections(collectionDefinitions)
await storageManager.finishInitialization()
await storageManager.backend.migrate()
return { storageManager }
Expand Down Expand Up @@ -621,7 +639,27 @@ export function testStorageBackendOperations(backendCreator : StorexBackendTestB
await storageManager.operation('executeBatch', [])
})

it('should support batch operations with compound primary keys')
it('should support batch operations with compound primary keys', { shouldSupport: ['compoundPrimaryKeys'] }, async function (context) {
const { storageManager } = await setupChildOfTest({ backend: context.backend })
expect(storageManager.registry.collections['tag'].pkIndex)
.toEqual(expect.arrayContaining(['name', 'email']))

const { object: user1 } = await storageManager.collection('user').createObject({displayName: 'Jack'})
const { object: email1 } = await storageManager.collection('email').createObject({ user: user1.id, address: '[email protected]' })

await storageManager.operation('executeBatch', [
{ operation: 'createObject', collection: 'tag', args: { email: email1.id, name: 'cool' } },
{ operation: 'createObject', collection: 'tag', args: { email: email1.id, name: 'groovy' } },
{ operation: 'createObject', collection: 'tag', args: { email: email1.id, name: 'rad' } },
])

expect(await storageManager.collection('tag').findObjects({ email: email1.id }))
.toEqual(expect.arrayContaining([
{ email: email1.id, name: 'cool' },
{ email: email1.id, name: 'groovy' },
{ email: email1.id, name: 'rad' },
])
})

it('should support batches with updateObjects operations', { shouldSupport: ['executeBatch'] }, async function (context : TestContext) {
const { storageManager } = await setupChildOfTest({ backend: context.backend })
Expand Down
1 change: 1 addition & 0 deletions ts/types/backend-features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface StorageBackendFeatureSupport {
updateWithRelationships? : boolean
relationshipFetching? : boolean
crossRelationshipQueries? : boolean
compoundPrimaryKeys?: boolean
executeBatch? : boolean
batchCreates? : boolean
resultLimiting? : boolean
Expand Down

0 comments on commit 66f2c26

Please sign in to comment.