swaf/test/ModelQuery.test.ts

17 lines
733 B
TypeScript

import ModelQuery, {WhereOperator} from "../src/db/ModelQuery";
import ModelFactory from "../src/db/ModelFactory";
import Model from "../src/db/Model";
describe('Test ModelQuery', () => {
test('groupWhere generates proper query', () => {
const query = ModelQuery.select({table: 'model'} as unknown as ModelFactory<Model>, '*');
query.where('f1', 'v1');
query.groupWhere(q => q.where('f2', 'v2').where('f3', 'v3')
.groupWhere(q => q.where('f4', 'v4'), WhereOperator.OR))
.where('f5', 'v5');
expect(query.toString(true)).toBe('SELECT * FROM model WHERE `f1`=? AND (`f2`=? AND `f3`=? OR (`f4`=?)) AND `f5`=? ');
expect(query.variables.length).toBe(5);
});
});