Skip to content

Commit

Permalink
test: new schema tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MXPOL committed Nov 29, 2023
1 parent db83410 commit 67df2e3
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 24 deletions.
54 changes: 35 additions & 19 deletions libs/velo-external-db-core/src/service/schema.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Chance from 'chance'
import { Uninitialized } from '@wix-velo/test-commons'
import { errors } from '@wix-velo/velo-external-db-commons'
import { PrimaryKeyFieldName, errors } from '@wix-velo/velo-external-db-commons'
import SchemaService from './schema'
import * as driver from '../../test/drivers/schema_provider_test_support'
import * as schema from '../../test/drivers/schema_information_test_support'
Expand All @@ -11,13 +11,15 @@ import {
compareColumnsInDbAndRequest,
InputFieldsToWixFormatFields,
InputFieldToWixFormatField,
WixFormatFieldsToInputFields
} from '../utils/schema_utils'
import {
Table,
InputField
InputField,
} from '@wix-velo/velo-external-db-types'
import { FieldType as VeloFieldTypeEnum } from '../spi-model/collection'

const { collectionsListFor } = matchers
const { collectionsListFor, collectionsWithReadWriteCapabilitiesInWixFormatFor, collectionsWithReadOnlyCapabilitiesInWixFormatFor } = matchers
const chance = Chance()

describe('Schema Service', () => {
Expand All @@ -31,36 +33,39 @@ describe('Schema Service', () => {
await expect( env.schemaService.list([]) ).resolves.toEqual(collectionsListFor(ctx.dbsWithIdColumn))
})

test('create new collection without fields', async() => {
test('create new collection without fields should throw', async() => {
driver.givenAllSchemaOperations()
driver.expectCreateOf(ctx.collectionName)
schema.expectSchemaRefresh()

await expect(env.schemaService.create({ id: ctx.collectionName, fields: [] })).resolves.toEqual({
collection: { id: ctx.collectionName, fields: [] }
})
await expect(env.schemaService.create({ id: ctx.collectionName, fields: [] })).rejects.toThrow(errors.InvalidRequest)
})

test('create new collection without _id field should throw', async() => {
driver.givenAllSchemaOperations()
schema.expectSchemaRefresh()
driver.expectCreateWithFieldsOf(ctx.collectionName, [ ctx.column ])

await expect(env.schemaService.create({ id: ctx.collectionName, fields: InputFieldsToWixFormatFields([ ctx.column ]) })).rejects.toThrow(errors.InvalidRequest)
})

test('create new collection with fields', async() => {
const fields = [{
key: ctx.column.name,
type: fieldTypeToWixDataEnum(ctx.column.type),
}]
const fields = [
{ key: PrimaryKeyFieldName, type: VeloFieldTypeEnum.text },
InputFieldToWixFormatField(ctx.column),
]

driver.givenAllSchemaOperations()
schema.expectSchemaRefresh()
driver.expectCreateWithFieldsOf(ctx.collectionName, fields)
driver.expectCreateWithFieldsOf(ctx.collectionName, WixFormatFieldsToInputFields(fields))

await expect(env.schemaService.create({ id: ctx.collectionName, fields })).resolves.toEqual({
collection: { id: ctx.collectionName, fields }
})
})

test('update collection - add new columns', async() => {
const newFields = [{
key: ctx.column.name,
type: fieldTypeToWixDataEnum(ctx.column.type),
}]

const newFields = InputFieldsToWixFormatFields([ ctx.column ])
driver.givenAllSchemaOperations()
schema.expectSchemaRefresh()
driver.givenFindResults([ { id: ctx.collectionName, fields: [] } ])
Expand Down Expand Up @@ -154,8 +159,19 @@ describe('Schema Service', () => {

})

// TODO: create a test for the case
// test('collections without _id column will have read-only capabilities', async() => {})
test('convert collection with read-write capabilities to Wix format', async() => {
schema.givenReadWriteOperationsCapabilitiesFor(ctx.collectionName, [])
await expect( env.schemaService.list([ctx.collectionName]) ).resolves.toEqual({
collections: [collectionsWithReadWriteCapabilitiesInWixFormatFor({ id: ctx.collectionName, fields: [] })]
})
})

test('convert collection with read-only capabilities to Wix format', async() => {
schema.givenReadOnlyOperationsCapabilitiesFor(ctx.collectionName, [])
await expect( env.schemaService.list([ctx.collectionName]) ).resolves.toEqual({
collections: [collectionsWithReadOnlyCapabilitiesInWixFormatFor({ id: ctx.collectionName, fields: [] })]
})
})

test('run unsupported operations should throw', async() => {
schema.expectSchemaRefresh()
Expand Down
6 changes: 3 additions & 3 deletions libs/velo-external-db-core/src/service/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export default class SchemaService {
await this.storage.list() :
await Promise.all(collectionIds.map((collectionName: string) => this.schemaInformation.schemaFor(collectionName)))

return { collections: collections.map(this.formatCollection.bind(this)) }
return { collections: collections.map(this.formatCollection.bind(this)) }
}

async create(collection: collectionSpi.Collection): Promise<collectionSpi.CreateCollectionResponse> {
// await this.validateFields(collection.fields)
await this.validateFields(collection.fields)
await this.storage.create(collection.id, WixFormatFieldsToInputFields(collection.fields))
await this.schemaInformation.refresh()
return { collection }
Expand Down Expand Up @@ -108,7 +108,7 @@ export default class SchemaService {

const fieldsName = fields.map(field => field.key)
if (!fieldsName.includes(PrimaryKeyFieldName)) {
throw new errors.InvalidRequest('_id field is missing')
throw new errors.InvalidRequest(`${PrimaryKeyFieldName} is field is missing`)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe ('Schema Aware Data Service', () => {
})

test('schema without _id - find will trigger find request with projection without _id', async() => {
schema.givenSchemaFor(ctx.collectionName, [ctx.column])
schema.givenSchemaFieldsFor(ctx.collectionName, [ctx.column])
queryValidator.givenValidFilterForDefaultFieldsOf(ctx.filter)
queryValidator.givenValidProjectionForDefaultFieldsOf([ctx.column.field])

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { SystemFields } from '@wix-velo/velo-external-db-commons'
import { ResponseField, DataOperation, PagingMode } from '@wix-velo/velo-external-db-types'
import { when } from 'jest-when'

export const schemaInformation = {
schemaFieldsFor: jest.fn(),
schemaFor: jest.fn(),
refresh: jest.fn(),
}

Expand All @@ -14,16 +16,42 @@ export const givenDefaultSchemaFor = (collectionName: any) => {
export const expectSchemaRefresh = () =>
when(schemaInformation.refresh).mockResolvedValue(undefined)

export const givenSchemaFor = (collectionName: string, fields: {field: string, type: string, subtype?: string}[]) => {
export const givenSchemaFieldsFor = (collectionName: string, fields: {field: string, type: string, subtype?: string}[]) => {
when(schemaInformation.schemaFieldsFor).calledWith(collectionName)
.mockResolvedValue( fields.map(field => ({ field: field.field, type: 'string' })) )
}

export const givenschemaFor = (collectionName: string, fields: ResponseField[], capabilities: any) => {
when(schemaInformation.schemaFor).calledWith(collectionName)
.mockResolvedValue({ id: collectionName, fields, capabilities })
}

export const givenReadWriteOperationsCapabilitiesFor = (collectionName: string, fields: ResponseField[]) => {
const readWriteCapabilities = Object.values(DataOperation)
givenCapabilitiesFor(collectionName, fields, readWriteCapabilities)
}

export const givenReadOnlyOperationsCapabilitiesFor = (collectionName: string, fields: ResponseField[]) => {
const { query, count, queryReferenced, aggregate, } = DataOperation
const ReadOnlyCapabilities = [query, count, queryReferenced, aggregate]
givenCapabilitiesFor(collectionName, fields, ReadOnlyCapabilities)
}

export const givenCapabilitiesFor = (collectionName: string, fields: ResponseField[], dataCapabilities: any) => {
const capabilities = {
dataOperations: dataCapabilities,
pagingMode: PagingMode.offset
}
when(schemaInformation.schemaFor).calledWith(collectionName)
.mockResolvedValue({ id: collectionName, fields, capabilities })
}

export const givenSchemaFieldsResultFor = (dbs: any[]) =>
dbs.forEach((db: { id: any; fields: any }) => when(schemaInformation.schemaFieldsFor).calledWith(db.id).mockResolvedValue(db.fields) )


export const reset = () => {
schemaInformation.schemaFieldsFor.mockClear()
schemaInformation.schemaFor.mockClear()
schemaInformation.refresh.mockClear()
}
23 changes: 23 additions & 0 deletions libs/velo-external-db-core/test/drivers/schema_matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ export const collectionsInWixFormatFor = (collection: Table) => {
})
}

export const collectionsWithReadWriteCapabilitiesInWixFormatFor = (collection: Table) => {
return expect.objectContaining({
id: collection.id,
fields: [],
capabilities: {
dataOperations: Object.values(collectionSpi.DataOperation),
},
pagingMode: collectionSpi.PagingMode.offset
})
}

export const collectionsWithReadOnlyCapabilitiesInWixFormatFor = (collection: Table) => {
const { query, count, queryReferenced, aggregate } = collectionSpi.DataOperation
return expect.objectContaining({
id: collection.id,
fields: [],
capabilities: {
dataOperations: [query, count, queryReferenced, aggregate]
},
pagingMode: collectionSpi.PagingMode.offset
})
}

export const collectionsListFor = (collections: Table[]) => {
return expect.objectContaining({ collections: expect.arrayContaining(collections.map(collectionsInWixFormatFor)) })
}
Expand Down

0 comments on commit 67df2e3

Please sign in to comment.