diff --git a/packages/adapter-tests/src/basic.ts b/packages/adapter-tests/src/basic.ts index 5353ac1dcd..a9599a3a72 100644 --- a/packages/adapter-tests/src/basic.ts +++ b/packages/adapter-tests/src/basic.ts @@ -1,6 +1,7 @@ import assert from 'assert'; +import { AdapterBasicTest } from './declarations'; -export default (test: any, app: any, _errors: any, serviceName: string, idProp: string) => { +export default (test: AdapterBasicTest, app: any, _errors: any, serviceName: string, idProp: string) => { describe('Basic Functionality', () => { let service: any; diff --git a/packages/adapter-tests/src/declarations.ts b/packages/adapter-tests/src/declarations.ts new file mode 100644 index 0000000000..80bb142385 --- /dev/null +++ b/packages/adapter-tests/src/declarations.ts @@ -0,0 +1,84 @@ +export type AdapterTest = (name: AdapterTestName, runner: any) => void; + +export type AdapterBasicTest = (name: AdapterBasicTestName, runner: any) => void; +export type AdapterMethodsTest = (name: AdapterMethodsTestName, runner: any) => void; +export type AdapterSyntaxTest = (name: AdapterSyntaxTestName, runner: any) => void; + +export type AdapterTestName = AdapterBasicTestName | AdapterMethodsTestName | AdapterSyntaxTestName; + +export type AdapterBasicTestName = + '.id' | + '.options' | + '.events' | + '._get' | + '._find' | + '._create' | + '._update' | + '._patch' | + '._remove'; + +export type AdapterMethodsTestName = + '.get' | + '.get + $select' | + '.get + id + query' | + '.get + NotFound' | + '.get + id + query id' | + '.find' | + '.remove' | + '.remove + $select' | + '.remove + id + query' | + '.remove + multi' | + '.remove + multi no pagination' | + '.remove + id + query id' | + '.update' | + '.update + $select' | + '.update + id + query' | + '.update + NotFound' | + '.update + query + NotFound' | + '.update + id + query id' | + '.patch' | + '.patch + $select' | + '.patch + id + query' | + '.patch multiple' | + '.patch multiple no pagination' | + '.patch multi query same' | + '.patch multi query changed' | + '.patch + NotFound' | + '.patch + query + NotFound' | + '.patch + id + query id' | + '.create' | + '.create + $select' | + '.create multi' | + 'internal .find' | + 'internal .get' | + 'internal .create' | + 'internal .update' | + 'internal .patch' | + 'internal .remove'; + +export type AdapterSyntaxTestName = + '.find + equal' | + '.find + equal multiple' | + '.find + $sort' | + '.find + $sort + string' | + '.find + $limit' | + '.find + $limit 0' | + '.find + $skip' | + '.find + $select' | + '.find + $or' | + '.find + $in' | + '.find + $nin' | + '.find + $lt' | + '.find + $lte' | + '.find + $gt' | + '.find + $gte' | + '.find + $ne' | + '.find + $gt + $lt + $sort' | + '.find + $or nested + $sort' | + 'params.adapter + paginate' | + 'params.adapter + multi' | + '.find + paginate' | + '.find + paginate + query' | + '.find + paginate + $limit + $skip' | + '.find + paginate + $limit 0' | + '.find + paginate + params'; \ No newline at end of file diff --git a/packages/adapter-tests/src/index.ts b/packages/adapter-tests/src/index.ts index bfb0806e9c..51b3d31bd4 100644 --- a/packages/adapter-tests/src/index.ts +++ b/packages/adapter-tests/src/index.ts @@ -1,18 +1,19 @@ /* eslint-disable no-console */ import basicTests from './basic'; +import { AdapterTestName } from './declarations'; import methodTests from './methods'; import syntaxTests from './syntax'; -const adapterTests = (testNames: string[]) => { +const adapterTests = (testNames: AdapterTestName[]) => { return (app: any, errors: any, serviceName: any, idProp = 'id') => { if (!serviceName) { throw new Error('You must pass a service name'); } - const skippedTests: string[] = []; - const allTests: string[] = []; + const skippedTests: AdapterTestName[] = []; + const allTests: AdapterTestName[] = []; - const test = (name: string, runner: any) => { + const test = (name: AdapterTestName, runner: any) => { const skip = !testNames.includes(name); const its = skip ? it.skip : it; @@ -46,4 +47,10 @@ const adapterTests = (testNames: string[]) => { }; }; -export = adapterTests; +export * from './declarations' + +export default adapterTests; + +if (typeof module !== 'undefined') { + module.exports = Object.assign(adapterTests, module.exports); +} diff --git a/packages/adapter-tests/src/methods.ts b/packages/adapter-tests/src/methods.ts index 561336a447..219acb3c84 100644 --- a/packages/adapter-tests/src/methods.ts +++ b/packages/adapter-tests/src/methods.ts @@ -1,6 +1,7 @@ import assert from 'assert'; +import { AdapterMethodsTest } from './declarations'; -export default (test: any, app: any, _errors: any, serviceName: string, idProp: string) => { +export default (test: AdapterMethodsTest, app: any, _errors: any, serviceName: string, idProp: string) => { describe(' Methods', () => { let doug: any; let service: any; @@ -156,6 +157,51 @@ export default (test: any, app: any, _errors: any, serviceName: string, idProp: assert.ok(names.includes('David'), 'David removed'); }); + test('.remove + multi no pagination', async () => { + try { + await service.remove(doug[idProp]); + } catch (error: any) {} + + const count = 14; + const defaultPaginate = 10; + + assert.ok(count > defaultPaginate, 'count is bigger than default pagination'); + + const multiBefore = service.options.multi; + const paginateBefore = service.options.paginate; + + try { + service.options.multi = true; + service.options.paginate = { + 'default': defaultPaginate, + 'max': 100 + }; + + const emptyItems = await service.find({ paginate: false }); + assert.strictEqual(emptyItems.length, 0, 'no items before') + + const createdItems = await service.create( + Array.from(Array(count)).map((_, i) => ({ name: `name-${i}`, age: 3, created: true })) + ); + assert.strictEqual(createdItems.length, count, `created ${count} items`); + + const foundItems = await service.find({ paginate: false }); + assert.strictEqual(foundItems.length, count, `created ${count} items`); + + const foundPaginatedItems = await service.find({}); + assert.strictEqual(foundPaginatedItems.data.length, defaultPaginate, 'found paginated items'); + + const allItems = await service.remove(null, { query: { created: true } }); + + assert.strictEqual(allItems.length, count, `removed all ${ count } items`); + } finally { + await service.remove(null, { query: { created: true }, paginate: false }); + + service.options.multi = multiBefore; + service.options.paginate = paginateBefore; + } + }); + test('.remove + id + query id', async () => { const alice = await service.create({ name: 'Alice', @@ -357,6 +403,57 @@ export default (test: any, app: any, _errors: any, serviceName: string, idProp: await service.remove(david[idProp]); }); + test('.patch multiple no pagination', async () => { + try { + await service.remove(doug[idProp]); + } catch (error: any) {} + + const count = 14; + const defaultPaginate = 10; + + assert.ok(count > defaultPaginate, 'count is bigger than default pagination'); + + const multiBefore = service.options.multi; + const paginateBefore = service.options.paginate; + + let ids: any[]; + + try { + service.options.multi = true; + service.options.paginate = { + 'default': defaultPaginate, + 'max': 100 + }; + + const emptyItems = await service.find({ paginate: false }); + assert.strictEqual(emptyItems.length, 0, 'no items before') + + const createdItems = await service.create( + Array.from(Array(count)).map((_, i) => ({ name: `name-${i}`, age: 3, created: true })) + ); + assert.strictEqual(createdItems.length, count, `created ${count} items`); + ids = createdItems.map((item: any) => item[idProp]); + + const foundItems = await service.find({ paginate: false }); + assert.strictEqual(foundItems.length, count, `created ${count} items`); + + const foundPaginatedItems = await service.find({}); + assert.strictEqual(foundPaginatedItems.data.length, defaultPaginate, 'found paginated data') + + const allItems = await service.patch(null, { age: 4 }, { query: { created: true } }) + + assert.strictEqual(allItems.length, count, `patched all ${ count } items`); + } finally { + service.options.multi = multiBefore; + service.options.paginate = paginateBefore; + if (ids) { + await Promise.all( + ids.map(id => service.remove(id)) + ) + } + } + }); + test('.patch multi query same', async () => { const service = app.service(serviceName); const params = { diff --git a/packages/adapter-tests/src/syntax.ts b/packages/adapter-tests/src/syntax.ts index f6fe1b0e15..069025bd41 100644 --- a/packages/adapter-tests/src/syntax.ts +++ b/packages/adapter-tests/src/syntax.ts @@ -1,6 +1,7 @@ import assert from 'assert'; +import { AdapterSyntaxTest } from './declarations'; -export default (test: any, app: any, _errors: any, serviceName: string, idProp: string) => { +export default (test: AdapterSyntaxTest, app: any, _errors: any, serviceName: string, idProp: string) => { describe('Query Syntax', () => { let bob: any; let alice: any; @@ -283,7 +284,7 @@ export default (test: any, app: any, _errors: any, serviceName: string, idProp: paginate: { default: 3 } } }); - + assert.strictEqual(page.limit, 3); assert.strictEqual(page.skip, 0); }); @@ -307,7 +308,7 @@ export default (test: any, app: any, _errors: any, serviceName: string, idProp: const users = await service.create(items, multiParams); assert.strictEqual(users.length, 2); - + await service.remove(users[0][idProp]); await service.remove(users[1][idProp]); await assert.rejects(() => service.patch(null, { age: 2 }, multiParams), { diff --git a/packages/adapter-tests/test/index.test.ts b/packages/adapter-tests/test/index.test.ts index 725c7cae49..445c3d9d6c 100644 --- a/packages/adapter-tests/test/index.test.ts +++ b/packages/adapter-tests/test/index.test.ts @@ -18,6 +18,7 @@ const testSuite = adapterTests([ '.remove + $select', '.remove + id + query', '.remove + multi', + '.remove + multi no pagination', '.update', '.update + $select', '.update + id + query', @@ -26,7 +27,9 @@ const testSuite = adapterTests([ '.patch + $select', '.patch + id + query', '.patch multiple', - '.patch multi query', + '.patch multiple no pagination', + '.patch multi query changed', + '.patch multi query same', '.patch + NotFound', '.create', '.create + $select', diff --git a/packages/memory/package.json b/packages/memory/package.json index 89ffabbcc1..349549a92b 100644 --- a/packages/memory/package.json +++ b/packages/memory/package.json @@ -37,7 +37,7 @@ "prepublish": "npm run compile", "compile": "shx rm -rf lib/ && tsc", "test": "npm run compile && npm run mocha", - "mocha": "mocha --config ../../.mocharc.json --recursive test/**.test.ts test/**/*.test.ts" + "mocha": "mocha --config ../../.mocharc.json --recursive test/**/*.test.ts" }, "publishConfig": { "access": "public" diff --git a/packages/memory/test/index.test.ts b/packages/memory/test/index.test.ts index 5f95ee5403..c210610629 100644 --- a/packages/memory/test/index.test.ts +++ b/packages/memory/test/index.test.ts @@ -25,6 +25,7 @@ const testSuite = adapterTests([ '.remove + $select', '.remove + id + query', '.remove + multi', + '.remove + multi no pagination', '.remove + id + query id', '.update', '.update + $select', @@ -36,6 +37,7 @@ const testSuite = adapterTests([ '.patch + $select', '.patch + id + query', '.patch multiple', + '.patch multiple no pagination', '.patch multi query same', '.patch multi query changed', '.patch + query + NotFound',