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 21, 2019
1 parent 3521dae commit 451bea1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
45 changes: 41 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,24 @@ 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', 'email'], pk: true },
],
}
}

storageManager.registry.registerCollections(collectionDefinitions)
await storageManager.finishInitialization()
await storageManager.backend.migrate()
return { storageManager }
Expand Down Expand Up @@ -621,7 +638,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 451bea1

Please sign in to comment.