fix: Symbol property could not be iterated in for-in (#39)

This commit is contained in:
Junyi 2020-12-12 22:10:44 +08:00 committed by GitHub
parent 5662509f4c
commit 787d2b611f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 30 deletions

View File

@ -1,4 +1,4 @@
import { literal } from 'sequelize';
import { literal, Op } from 'sequelize';
import { getDatabase } from '..';
import Database from '../../database';
import Model, { ModelCtor } from '../../model';
@ -55,6 +55,10 @@ beforeAll(() => {
db.table({
name: 'foos',
fields: [
{
type: 'int',
name: 'f_g61p'
},
{
type: 'hasMany',
name: 'bars',
@ -100,6 +104,19 @@ describe('parseApiJson', () => {
expect(data).toEqual({ where: { col1: 'co2' } });
});
it('filter with condition operator', () => {
const data = Foo.parseApiJson({
filter: {"and":[{"f_g61p.eq":23}]}
});
expect(data).toEqual({
where: {
[Op.and]: [
{ f_g61p: { [Op.eq]: 23 } }
]
}
});
});
it('fields', () => {
const data = Foo.parseApiJson({
fields: ['col1'],

View File

@ -1,10 +1,40 @@
import { Op } from "sequelize";
import Database from "../../database";
import Model, { ModelCtor } from '../../model';
import { toWhere } from "../../utils";
import { getDatabase } from "..";
describe('utils.toWhere', () => {
let db: Database;
beforeAll(() => {
db = getDatabase();
db.table({
name: 'users',
});
db.table({
name: 'posts',
fields: [
{
type: 'string',
name: 'title'
},
{
type: 'belongsTo',
name: 'user',
}
]
});
db.table({
name: 'categories',
fields: [
{
type: 'hasMany',
name: 'posts',
}
],
});
});
afterAll(() => db.close());
describe('single', () => {
it('=', () => {
const where = toWhere({
@ -213,6 +243,18 @@ describe('utils.toWhere', () => {
});
});
it('logical and with condition operator in field', () => {
const Post = db.getModel('posts');
const data = toWhere({"and":[{"title.eq": '23'}]}, {
model: Post
});
expect(data).toEqual({
[Op.and]: [
{ title: { [Op.eq]: '23' } }
]
});
});
// TODO: bug
it.skip('field as "or"', () => {
expect(toWhere({
@ -233,33 +275,6 @@ describe('utils.toWhere', () => {
});
describe('association', () => {
let db: Database;
beforeAll(() => {
db = getDatabase();
db.table({
name: 'users',
});
db.table({
name: 'posts',
fields: [
{
type: 'belongsTo',
name: 'user',
}
]
});
db.table({
name: 'categories',
fields: [
{
type: 'hasMany',
name: 'posts',
}
],
});
});
afterAll(() => db.close());
const toWhereExpect = (options: any) => {
const Category = db.getModel('categories');
const where = toWhere(options, {

View File

@ -273,7 +273,7 @@ export function toInclude(options: any, context: ToIncludeContext = {}) {
data.distinct = true;
}
if (Object.keys(where).length > 0) {
if (Reflect.ownKeys(where).length > 0) {
data.where = where;
}