Skip to content

Commit

Permalink
test: add tests for raw
Browse files Browse the repository at this point in the history
  • Loading branch information
fratzinger committed May 13, 2024
1 parent df848da commit c65f84f
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ export class SequelizeAdapter<
} as UpdateOptions)
.catch(errorHandler) as [number, Model[]?];

if (instances?.length) {
// eslint-disable-next-line no-console
console.log('instanceof', instances[0] instanceof Model);
}

if (sequelizeOptions.returning === false) {
return []
}
Expand Down
1 change: 1 addition & 0 deletions test/connection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Sequelize } from 'sequelize';

export default (DB?: 'postgres' | 'mysql') => {
console.log('DB:', DB);
if (DB === 'postgres') {
return new Sequelize('sequelize', 'postgres', 'password', {
host: 'localhost',
Expand Down
191 changes: 183 additions & 8 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,18 +746,41 @@ describe('Feathers Sequelize Service', () => {
expect(results[0]).to.be.an.instanceof(Model);
});

it('find() with `params.sequelize.raw: true` returns raw data', async () => {
const results = await people.find({ sequelize: {
raw: true
} }) as any as any[];

expect(results[0]).to.be.an('object');
expect(results[0]).to.not.be.instanceOf(Model);
});

it('get() returns a model instance', async () => {
const instance = await people.get(david.id);

expect(instance).to.be.an.instanceOf(Model);
});

it('get() with `params.sequelize.raw: true` returns raw data', async () => {
const instance = await people.get(david.id, { sequelize: { raw: true } });

expect(instance).to.be.an('object');
expect(instance).to.not.be.instanceOf(Model);
});

it('create() returns a model instance', async () => {
const instance = await people.create({ name: 'Sarah' });

expect(instance).to.be.an.instanceOf(Model);
});

it('create() with `params.sequelize.raw: true` returns raw data', async () => {
const instance = await people.create({ name: 'Sarah' }, { sequelize: { raw: true } });

expect(instance).to.be.an('object');
expect(instance).to.not.be.instanceOf(Model);
});

it('bulk create() returns model instances', async () => {
const results = await people.create([
{ name: 'Sarah' },
Expand All @@ -770,15 +793,30 @@ describe('Feathers Sequelize Service', () => {
assert.ok(results[1].id);
});

it('bulk create() with `params.sequelize.raw: true` returns raw data', async () => {
const results = await people.create([
{ name: 'Sarah' },
{ name: 'Connor' }
], { sequelize: { raw: true } });

expect(results.length).to.equal(2);
expect(results[0]).to.be.an('object');
expect(results[0]).to.not.be.instanceOf(Model);
assert.ok(results[0].id);
assert.ok(results[1].id);
});

it('create() with returning=false returns empty array', async () => {
// does not work for single requests
const responseSingle = await people.create({ name: 'delete' }, {
sequelize: { returning: false }
});
expect(responseSingle).to.be.an.instanceOf(Model);

// works for bulk requests
const responseMulti = await people.create([{ name: 'delete' }], {
sequelize: { returning: false }
});

expect(responseSingle).to.be.an.instanceOf(Model);
expect(responseMulti).to.deep.equal([]);
});

Expand All @@ -788,16 +826,34 @@ describe('Feathers Sequelize Service', () => {
expect(instance).to.be.an.instanceOf(Model);
});

it('bulk patch() returns model instances', async () => {
const results = await people.patch(null, { name: 'Sarah' });

expect(results).to.be.an('array').with.lengthOf(1);
expect(results[0]).to.be.an.instanceOf(Model);
});

it('bulk patch() with `params.sequelize.raw: true` returns raw data', async () => {
const results = await people.patch(null, { name: 'Sarah' }, { sequelize: { raw: true } });

expect(results).to.be.an('array').with.lengthOf(1);
expect(results[0]).to.be.an('object');
expect(results[0]).to.not.be.instanceOf(Model);
});

it('patch() with returning=false returns empty array', async () => {
// does not work for single requests
const responseSingle = await people.patch(david.id, { name: 'Sarah' }, {
sequelize: { returning: false }
});
expect(responseSingle).to.be.an.instanceOf(Model);

// works for bulk requests
const responseMulti = await people.patch(null, { name: 'Sarah' }, {
query: { name: 'Sarah' },
sequelize: { returning: false }
});

expect(responseSingle).to.be.an.instanceOf(Model);
expect(responseMulti).to.deep.equal([]);
});

Expand All @@ -807,27 +863,125 @@ describe('Feathers Sequelize Service', () => {
expect(instance).to.be.an.instanceOf(Model);
});

it('update() with `params.sequelize.raw: true` returns raw data', async () => {
const instance = await people.update(david.id, david, { sequelize: { raw: true } });

expect(instance).to.be.an('object');
expect(instance).to.not.be.instanceOf(Model);
});

it('remove() returns a model instance', async () => {
const instance = await people.remove(david.id);

expect(instance).to.be.an.instanceOf(Model);
});

it('remove() with `params.sequelize.raw: true` returns raw data', async () => {
const instance = await people.remove(david.id, { sequelize: { raw: true } });

expect(instance).to.be.an('object');
expect(instance).to.not.be.instanceOf(Model);
});

it('bulk remove() returns model instances', async () => {
const results = await people.remove(null, { query: {} });

expect(results).to.be.an('array').with.lengthOf(1);
expect(results[0]).to.be.an.instanceOf(Model);
});

it('bulk remove() with `params.sequelize.raw: true` returns raw data', async () => {
const results = await people.remove(null, { query: {}, sequelize: { raw: true } });

expect(results).to.be.an('array').with.lengthOf(1);
expect(results[0]).to.be.an('object');
expect(results[0]).to.not.be.instanceOf(Model);
});

it('remove() with returning=false returns empty array', async () => {
// does not work for single requests
const responseSingle = await people.remove(david.id, {
sequelize: { returning: false }
});
expect(responseSingle).to.be.an.instanceOf(Model);

david = await people.create({ name: 'David' });
const responseMulti = await people.remove(null, {
query: { name: 'David' },
sequelize: { returning: false }
});

expect(responseSingle).to.be.an.instanceOf(Model);
expect(responseMulti).to.deep.equal([]);
});
});

describe('raw Service', () => {
let david: any;

beforeEach(async () => {
david = await rawPeople.create({ name: 'David' });
});

afterEach(() => rawPeople.remove(null, { query: {} }).catch(() => {}));

it('find() returns raw', async () => {
const results = await rawPeople.find() as any as any[];

expect(results).to.be.an('array').with.lengthOf(1);
expect(results[0]).to.not.be.an.instanceof(Model);
});

it('get() returns raw', async () => {
const instance = await rawPeople.get(david.id);

expect(instance).to.not.be.an.instanceof(Model);
});

it('create() returns raw', async () => {
const instance = await rawPeople.create({ name: 'Sarah' });

expect(instance).to.not.be.an.instanceof(Model);
});

it('bulk create() returns raw', async () => {
const results = await rawPeople.create([{ name: 'Sarah' }]);

expect(results).to.be.an('array').with.lengthOf(1);
expect(results[0]).to.not.be.an.instanceof(Model);
});

it('patch() returns raw', async () => {
const instance = await rawPeople.patch(david.id, { name: 'Sarah' });

expect(instance).to.not.be.an.instanceof(Model);
});

it('bulk patch() returns raw', async () => {
const results = await rawPeople.patch(null, { name: 'Sarah' }, { query: { id: { $in: [david.id] } } });

expect(results).to.be.an('array').with.lengthOf(1);
expect(results[0]).to.not.be.an.instanceof(Model);
});

it('update() returns raw', async () => {
const instance = await rawPeople.update(david.id, david);

expect(instance).to.not.be.an.instanceof(Model);
});

it('remove() returns raw', async () => {
const instance = await rawPeople.remove(david.id);

expect(instance).to.not.be.an.instanceof(Model);
});

it('bulk remove() returns raw', async () => {
const results = await rawPeople.remove(null);

expect(results).to.be.an('array').with.lengthOf(1);
expect(results[0]).to.not.be.an.instanceof(Model);
});
});

describe('Non-raw Service Method Calls', () => {
const NOT_RAW = { sequelize: { raw: false } };
let david: any;
Expand All @@ -838,6 +992,13 @@ describe('Feathers Sequelize Service', () => {

afterEach(() => rawPeople.remove(null, { query: {} }).catch(() => {}));

it('find() returns raw', async () => {
const results = await rawPeople.find() as any as any[];

expect(results).to.be.an('array').with.lengthOf(1);
expect(results[0]).to.be.not.an.instanceof(Model);
});

it('`raw: false` works for find()', async () => {
const results = await rawPeople.find(NOT_RAW) as any as any[];

Expand All @@ -859,7 +1020,7 @@ describe('Feathers Sequelize Service', () => {
it('`raw: false` works for bulk create()', async () => {
const results = await rawPeople.create([{ name: 'Sarah' }], NOT_RAW);

expect(results.length).to.equal(1);
expect(results).to.be.an('array').with.lengthOf(1);
expect(results[0]).to.be.an.instanceof(Model);
});

Expand All @@ -869,6 +1030,13 @@ describe('Feathers Sequelize Service', () => {
expect(instance).to.be.an.instanceof(Model);
});

it('`raw: false` works for bulk patch()', async () => {
const results = await rawPeople.patch(null, { name: 'Sarah' }, NOT_RAW);

expect(results).to.be.an('array').with.lengthOf(1);
expect(results[0]).to.be.an.instanceof(Model);
});

it('`raw: false` works for update()', async () => {
const instance = await rawPeople.update(david.id, david, NOT_RAW);

Expand All @@ -880,6 +1048,13 @@ describe('Feathers Sequelize Service', () => {

expect(instance).to.be.an.instanceof(Model);
});

it('`raw: false` works for bulk remove()', async () => {
const results = await rawPeople.remove(null, NOT_RAW);

expect(results).to.be.an('array').with.lengthOf(1);
expect(results[0]).to.be.an.instanceof(Model);
});
});
});

Expand Down Expand Up @@ -991,7 +1166,7 @@ describe('Feathers Sequelize Service', () => {
const results = await people.create([{ name: 'Sarah' }], params);
expect(params.sequelize.getModelCalls.count).to.gte(1);

expect(results.length).to.equal(1);
expect(results).to.be.an('array').with.lengthOf(1);
expect(results[0]).to.be.an.instanceof(Model);

const removeParams = getExtraParams();
Expand Down Expand Up @@ -1105,7 +1280,7 @@ describe('Feathers Sequelize Service', () => {
const results = await rawPeople.create([{ name: 'Sarah' }], params);
expect(params.sequelize.getModelCalls.count).to.gte(1);

expect(results.length).to.equal(1);
expect(results).to.be.an('array').with.lengthOf(1);
expect(results[0]).to.be.an.instanceof(Model);

const removeParams = getExtraParams();
Expand Down

0 comments on commit c65f84f

Please sign in to comment.