Skip to content

Commit

Permalink
test: add test case for all Comparison Operators
Browse files Browse the repository at this point in the history
that is not covered by feathers adapter testSuite and available in Ottoman
  • Loading branch information
bwgjoseph committed Jun 13, 2021
1 parent dbdf008 commit 63a8ce5
Show file tree
Hide file tree
Showing 2 changed files with 279 additions and 102 deletions.
105 changes: 3 additions & 102 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from 'ottoman';
import { ModelOptions } from 'ottoman/lib/types/model/interfaces/create-model.interface';
import { Service } from '../src/index';
import customTestSuite from './methods.test';

const testSuite = adapterTests([
'.options',
Expand Down Expand Up @@ -160,107 +161,7 @@ describe('Feathers Ottoman Service', () => {
testSuite(app, errors, 'posts-customid', 'customid');
});

it('.update + multi', async () => {
try {
await app.service('posts').update(null, {});
throw new Error('Should never get here');
} catch (error) {
assert.strictEqual(error.name, 'BadRequest',
'You can not replace multiple instances. Did you mean \'patch\'');
}
});

it('.create + multi + $select', async () => {
const service = app.service('posts');
service.options.multi = ['create'];

const items = [
{
name: 'Gerald',
age: 18,
},
{
name: 'Herald',
age: 18,
},
];

const data = await service.create(items, {
query: { $select: ['name'] },
});

assert.ok(Array.isArray(data), 'data is an array');
assert.ok(typeof data[0].id !== 'undefined', 'id is set');
assert.strictEqual(data[0].name, 'Gerald', 'first name matches');
assert.ok(typeof data[1].id !== 'undefined', 'id is set');
assert.strictEqual(data[1].name, 'Herald', 'second name macthes');
assert.ok(!data[0].age, 'data.age is falsy');
assert.ok(!data[1].age, 'data.age is falsy');

await service.remove(data[0].id);
await service.remove(data[1].id);

service.options.multi = [];
});

it('.patch + multi + $select', async () => {
const service = app.service('posts');
service.options.multi = ['patch'];

await service.create({ name: 'Dave', age: 29, created: true });
await service.create({ name: 'David', age: 3, created: true });

const data = await service.patch(null, { age: 2 }, {
query: { created: true, $select: ['age'] },
});

assert.strictEqual(data.length, 2, 'returned two entries');
assert.strictEqual(data[0].age, 2, 'First entry age was updated');
assert.strictEqual(data[1].age, 2, 'Second entry age was updated');
assert.ok(!data[0].name, 'data.name is falsy');
assert.ok(!data[1].name, 'data.name is falsy');

await service.remove(data[0].id);
await service.remove(data[1].id);

service.options.multi = [];
});

it('.remove + multi + $select', async () => {
const service = app.service('posts');
service.options.multi = ['remove'];

await service.create({ name: 'Dave', age: 29, created: true });
await service.create({ name: 'David', age: 3, created: true });

const data = await service.remove(null, {
query: { created: true, $select: ['name'] },
});

const names = data.map((person: any) => person.name);

assert.strictEqual(data.length, 2, 'returned two entries');
assert.ok(names.includes('Dave'), 'Dave removed');
assert.ok(names.includes('David'), 'David removed');
assert.ok(!data[0].age, 'data.age is falsy');
assert.ok(!data[1].age, 'data.age is falsy');

await service.remove(null);

service.options.multi = [];
});

it('.find + $ignoreCase filter', async () => {
const service = app.service('posts');

await service.create({ name: 'Dave', age: 29, created: true });

const q = { name: 'dave', $ignoreCase: true };
const data = await service.find({ query: q });

assert.strictEqual(data.length, 1, 'returned one entries');
assert.strictEqual(data[0].name, 'Dave');

await service.remove(data[0].id);
it('run custom test suite', () => {
customTestSuite(app, 'posts');
});
});
276 changes: 276 additions & 0 deletions test/methods.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
import assert from 'assert';

const customTestSuite = (app: any, serviceName: string): void => {
describe('adapter methods', () => {
let service: any;

beforeEach(() => {
service = app.service(serviceName);
});

afterEach(async () => {
service.options.multi = ['remove'];
await service.remove(null);
service.options.multi = [];
service.options.whitelist = [];
});

it('.update + multi', async () => {
try {
await service.update(null, {});
throw new Error('Should never get here');
} catch (error) {
assert.strictEqual(error.name, 'BadRequest',
'You can not replace multiple instances. Did you mean \'patch\'');
}
});

it('.create + multi + $select', async () => {
service.options.multi = ['create'];

const items = [
{
name: 'Gerald',
age: 18,
},
{
name: 'Herald',
age: 18,
},
];

const data = await service.create(items, {
query: { $select: ['name'] },
});

assert.ok(Array.isArray(data), 'data is an array');
assert.ok(typeof data[0].id !== 'undefined', 'id is set');
assert.strictEqual(data[0].name, 'Gerald', 'first name matches');
assert.ok(typeof data[1].id !== 'undefined', 'id is set');
assert.strictEqual(data[1].name, 'Herald', 'second name macthes');
assert.ok(!data[0].age, 'data.age is falsy');
assert.ok(!data[1].age, 'data.age is falsy');

service.options.multi = [];
});

it('.patch + multi + $select', async () => {
service.options.multi = ['patch'];

await service.create({ name: 'Dave', age: 29, created: true });
await service.create({ name: 'David', age: 3, created: true });

const data = await service.patch(null, { age: 2 }, {
query: { created: true, $select: ['age'] },
});

assert.strictEqual(data.length, 2, 'returned two entries');
assert.strictEqual(data[0].age, 2, 'First entry age was updated');
assert.strictEqual(data[1].age, 2, 'Second entry age was updated');
assert.ok(!data[0].name, 'data.name is falsy');
assert.ok(!data[1].name, 'data.name is falsy');

service.options.multi = [];
});

it('.remove + multi + $select', async () => {
service.options.multi = ['remove'];

await service.create({ name: 'Dave', age: 29, created: true });
await service.create({ name: 'David', age: 3, created: true });

const data = await service.remove(null, {
query: { created: true, $select: ['name'] },
});

const names = data.map((person: any) => person.name);

assert.strictEqual(data.length, 2, 'returned two entries');
assert.ok(names.includes('Dave'), 'Dave removed');
assert.ok(names.includes('David'), 'David removed');
assert.ok(!data[0].age, 'data.age is falsy');
assert.ok(!data[1].age, 'data.age is falsy');

service.options.multi = [];
});

it('.find + $ignoreCase filter', async () => {
await service.create({ name: 'Dave', age: 29, created: true });

const q = { name: 'dave', $ignoreCase: true };
const data = await service.find({ query: q });

assert.strictEqual(data.length, 1, 'returned one entries');
assert.strictEqual(data[0].name, 'Dave');
});

it('.find + $eq', async () => {
service.options.whitelist = ['$eq'];

await service.create({ name: 'Dave', age: 29, created: true });

const q = { name: { $eq: 'Dave' } };
const data = await service.find({ query: q });

assert.strictEqual(data.length, 1, 'returned one entries');
assert.strictEqual(data[0].name, 'Dave');
});

it('.find + $eq + $ignoreCase', async () => {
service.options.whitelist = ['$eq', '$ignoreCase'];

await service.create({ name: 'Dave', age: 29, created: true });

const q = { name: { $eq: 'dave', $ignoreCase: true } };
const data = await service.find({ query: q });

assert.strictEqual(data.length, 1, 'returned one entries');
assert.strictEqual(data[0].name, 'Dave');
});

it('.find + $neq', async () => {
service.options.whitelist = ['$neq'];

await service.create({ name: 'Dave', age: 29, created: true });
await service.create({ name: 'David', age: 29, created: true });

const q = { name: { $neq: 'Dave' } };
const data = await service.find({ query: q });

assert.strictEqual(data.length, 1, 'returned one entries');
assert.strictEqual(data[0].name, 'David');
});

it('.find + $like', async () => {
service.options.whitelist = ['$like'];

await service.create({ name: 'Dave', age: 29, created: true });

const q = { name: { $like: 'D%' } };
const data = await service.find({ query: q });

assert.strictEqual(data.length, 1, 'returned one entries');
assert.strictEqual(data[0].name, 'Dave');
});

it('.find + $notLike', async () => {
service.options.whitelist = ['$notLike'];

await service.create({ name: 'Dave', age: 29, created: true });
await service.create({ name: 'Joseph', age: 29, created: true });

const q = { name: { $notLike: 'D%' } };
const data = await service.find({ query: q });

assert.strictEqual(data.length, 1, 'returned one entries');
assert.strictEqual(data[0].name, 'Joseph');
});

it('.find + $btw', async () => {
service.options.whitelist = ['$btw'];

await service.create({ name: 'Dave', age: 10, created: true });
await service.create({ name: 'Joseph', age: 30, created: true });

const q = { age: { $btw: [1, 20] } };
const data = await service.find({ query: q });

assert.strictEqual(data.length, 1, 'returned one entries');
assert.strictEqual(data[0].name, 'Dave');
});

it('.find + $notBtw', async () => {
service.options.whitelist = ['$notBtw'];

await service.create({ name: 'Dave', age: 10, created: true });
await service.create({ name: 'Joseph', age: 30, created: true });

const q = { age: { $notBtw: [1, 20] } };
const data = await service.find({ query: q });

assert.strictEqual(data.length, 1, 'returned one entries');
assert.strictEqual(data[0].name, 'Joseph');
});

// See https://github.com/bwgjoseph/mongoose-vs-ottoman/issues/95
// it('.find + $isNull', async () => {
// service.options.whitelist = ['$isNull'];

// const a = await service.create({ age: 10, created: true });
// console.log(a);
// const q = { name: { $isNull: true } };
// const data = await service.find({ query: q });

// assert.strictEqual(data.length, 1, 'returned one entries');
// assert.strictEqual(data[0].name, 'Dave');

// });

it('.find + $isNotNull', async () => {
service.options.whitelist = ['$isNotNull'];

await service.create({ age: 10, created: true });
await service.create({ name: 'Joseph', age: 30, created: true });

const q = { name: { $isNotNull: true } };
const data = await service.find({ query: q });

assert.strictEqual(data.length, 1, 'returned one entries');
assert.strictEqual(data[0].name, 'Joseph');
});

it('.find + $isMissing', async () => {
service.options.whitelist = ['$isMissing'];

await service.create({ age: 10, created: true });
await service.create({ name: 'David', age: 30, created: true });

const q = { name: { $isMissing: true } };
const data = await service.find({ query: q });

assert.strictEqual(data.length, 1, 'returned one entries');
assert.strictEqual(data[0].age, 10);
});

it('.find + $isNotMissing', async () => {
service.options.whitelist = ['$isNotMissing'];

await service.create({ name: 'Dave', age: 10, created: true });
await service.create({ age: 20, created: true });

const q = { name: { $isNotMissing: true } };
const data = await service.find({ query: q });

assert.strictEqual(data.length, 1, 'returned one entries');
assert.strictEqual(data[0].name, 'Dave');
});

it('.find + $isValued', async () => {
service.options.whitelist = ['$isValued'];

await service.create({ age: 10, created: true });
await service.create({ name: 'David', age: 30, created: true });

const q = { name: { $isValued: true } };
const data = await service.find({ query: q });

assert.strictEqual(data.length, 1, 'returned one entries');
assert.strictEqual(data[0].name, 'David');
});

it('.find + $isNotValued', async () => {
service.options.whitelist = ['$isNotValued'];

await service.create({ age: 10, created: true });
await service.create({ name: 'David', age: 30, created: true });

const q = { name: { $isNotValued: true } };
const data = await service.find({ query: q });

assert.strictEqual(data.length, 1, 'returned one entries');
assert.strictEqual(data[0].age, 10);
});
});
};

export default customTestSuite;

0 comments on commit 63a8ce5

Please sign in to comment.