Skip to content

Commit

Permalink
fix(adapter-tests): Add tests for pagination in multi updates (#2472)
Browse files Browse the repository at this point in the history
  • Loading branch information
fratzinger committed Feb 15, 2022
1 parent 1e6e322 commit 98a811a
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 12 deletions.
3 changes: 2 additions & 1 deletion 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;

Expand Down
84 changes: 84 additions & 0 deletions 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';
17 changes: 12 additions & 5 deletions 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;

Expand Down Expand Up @@ -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);
}
99 changes: 98 additions & 1 deletion 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;
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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 = {
Expand Down
7 changes: 4 additions & 3 deletions 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;
Expand Down Expand Up @@ -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);
});
Expand All @@ -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), {
Expand Down
5 changes: 4 additions & 1 deletion packages/adapter-tests/test/index.test.ts
Expand Up @@ -18,6 +18,7 @@ const testSuite = adapterTests([
'.remove + $select',
'.remove + id + query',
'.remove + multi',
'.remove + multi no pagination',
'.update',
'.update + $select',
'.update + id + query',
Expand All @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion packages/memory/package.json
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions packages/memory/test/index.test.ts
Expand Up @@ -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',
Expand All @@ -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',
Expand Down

0 comments on commit 98a811a

Please sign in to comment.