Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(test-utils): orm expr query behavior for $eq, $ne and so on #343

Merged
merged 4 commits into from
Aug 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions packages/koishi-test-utils/src/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ const queryOperators: QueryOperators = {
$regexFor: (query, data) => new RegExp(data, 'i').test(query),
$in: (query, data) => query.includes(data),
$nin: (query, data) => !query.includes(data),
$ne: (query, data) => data !== query,
$eq: (query, data) => data === query,
$gt: (query, data) => data > query,
$gte: (query, data) => data >= query,
$lt: (query, data) => data < query,
$lte: (query, data) => data <= query,
$ne: (query, data) => data.valueOf() !== query.valueOf(),
$eq: (query, data) => data.valueOf() === query.valueOf(),
$gt: (query, data) => data.valueOf() > query.valueOf(),
$gte: (query, data) => data.valueOf() >= query.valueOf(),
$lt: (query, data) => data.valueOf() < query.valueOf(),
$lte: (query, data) => data.valueOf() <= query.valueOf(),
$el: (query, data) => data.some(item => executeFieldQuery(query, item)),
$size: (query, data) => data.length === query,
$bitsAllSet: (query, data) => (query & data) === query,
Expand All @@ -65,6 +65,8 @@ const queryOperators: QueryOperators = {
}

function executeFieldQuery(query: Query.FieldQuery, data: any) {
if (!data) return false
NWYLZW marked this conversation as resolved.
Show resolved Hide resolved

// shorthand syntax
if (Array.isArray(query)) {
return query.includes(data)
Expand Down
17 changes: 16 additions & 1 deletion packages/koishi-test-utils/tests/memory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface FooData {
bar: string
baz?: number
list?: number[]
datetime?: Date
NWYLZW marked this conversation as resolved.
Show resolved Hide resolved
}

Tables.extend('foo')
Expand Down Expand Up @@ -63,7 +64,7 @@ describe('Memory Database', () => {

describe('complex expression', () => {
before(async () => {
await db.createFoo({ bar: 'awesome foo', baz: 3, list: [] })
await db.createFoo({ bar: 'awesome foo', baz: 3, list: [], datetime: new Date('2000-01-01') })
await db.createFoo({ bar: 'awesome bar', baz: 4, list: [1] })
await db.createFoo({ bar: 'awesome foo bar', baz: 7, list: [100] })
})
Expand All @@ -73,6 +74,20 @@ describe('Memory Database', () => {
db.memory.$store.foo = []
})

it('should check in addition to basic types.', async () => {
NWYLZW marked this conversation as resolved.
Show resolved Hide resolved
await expect(db.get('foo', {
datetime: { $eq: new Date('2000-01-01') },
})).eventually.to
.have.nested.property('[0].bar')
.equal('awesome foo')

await expect(db.get('foo', {
datetime: { $gte: new Date('2000-01-01') },
})).eventually.to
.have.nested.property('[0].bar')
.equal('awesome foo')
})

it('compile expr query', async () => {
await expect(db.get('foo', {
id: 1,
Expand Down