1
0
Fork 0
mirror of synced 2024-08-05 13:21:26 +12:00

Down to 4 failures.

This commit is contained in:
Sam Rose 2024-06-13 16:23:16 +01:00
parent 746ee711ae
commit c01c2c7cc3
No known key found for this signature in database
2 changed files with 315 additions and 296 deletions

View file

@ -313,351 +313,359 @@ describe.each([
}) })
}) })
// Ensure all bindings resolve and perform as expected // We've decided not to try and support binding for in-memory search just now.
describe("bindings", () => { !isInMemory &&
let globalUsers: any = [] describe("bindings", () => {
let globalUsers: any = []
const serverTime = new Date() const serverTime = new Date()
// In MariaDB and MySQL we only store dates to second precision, so we need // In MariaDB and MySQL we only store dates to second precision, so we need
// to remove milliseconds from the server time to ensure searches work as // to remove milliseconds from the server time to ensure searches work as
// expected. // expected.
serverTime.setMilliseconds(0) serverTime.setMilliseconds(0)
const future = new Date(serverTime.getTime() + 1000 * 60 * 60 * 24 * 30) const future = new Date(serverTime.getTime() + 1000 * 60 * 60 * 24 * 30)
const rows = (currentUser: User) => { const rows = (currentUser: User) => {
return [ return [
{ name: "foo", appointment: "1982-01-05T00:00:00.000Z" }, { name: "foo", appointment: "1982-01-05T00:00:00.000Z" },
{ name: "bar", appointment: "1995-05-06T00:00:00.000Z" }, { name: "bar", appointment: "1995-05-06T00:00:00.000Z" },
{ name: currentUser.firstName, appointment: future.toISOString() }, { name: currentUser.firstName, appointment: future.toISOString() },
{ name: "serverDate", appointment: serverTime.toISOString() }, { name: "serverDate", appointment: serverTime.toISOString() },
{ {
name: "single user, session user", name: "single user, session user",
single_user: JSON.stringify(currentUser), single_user: JSON.stringify(currentUser),
},
{
name: "single user",
single_user: JSON.stringify(globalUsers[0]),
},
{
name: "deprecated single user, session user",
deprecated_single_user: JSON.stringify([currentUser]),
},
{
name: "deprecated single user",
deprecated_single_user: JSON.stringify([globalUsers[0]]),
},
{
name: "multi user",
multi_user: JSON.stringify(globalUsers),
},
{
name: "multi user with session user",
multi_user: JSON.stringify([...globalUsers, currentUser]),
},
{
name: "deprecated multi user",
deprecated_multi_user: JSON.stringify(globalUsers),
},
{
name: "deprecated multi user with session user",
deprecated_multi_user: JSON.stringify([...globalUsers, currentUser]),
},
]
}
beforeAll(async () => {
// Set up some global users
globalUsers = await Promise.all(
Array(2)
.fill(0)
.map(async () => {
const globalUser = await config.globalUser()
const userMedataId = globalUser._id
? dbCore.generateUserMetadataID(globalUser._id)
: null
return {
_id: globalUser._id,
_meta: userMedataId,
}
})
)
table = await createTable({
name: { name: "name", type: FieldType.STRING },
appointment: { name: "appointment", type: FieldType.DATETIME },
single_user: {
name: "single_user",
type: FieldType.BB_REFERENCE_SINGLE,
subtype: BBReferenceFieldSubType.USER,
},
deprecated_single_user: {
name: "deprecated_single_user",
type: FieldType.BB_REFERENCE,
subtype: BBReferenceFieldSubType.USER,
},
multi_user: {
name: "multi_user",
type: FieldType.BB_REFERENCE,
subtype: BBReferenceFieldSubType.USER,
constraints: {
type: "array",
}, },
}, {
deprecated_multi_user: { name: "single user",
name: "deprecated_multi_user", single_user: JSON.stringify(globalUsers[0]),
type: FieldType.BB_REFERENCE,
subtype: BBReferenceFieldSubType.USERS,
constraints: {
type: "array",
}, },
}, {
}) name: "deprecated single user, session user",
await createRows(rows(config.getUser())) deprecated_single_user: JSON.stringify([currentUser]),
}) },
{
name: "deprecated single user",
deprecated_single_user: JSON.stringify([globalUsers[0]]),
},
{
name: "multi user",
multi_user: JSON.stringify(globalUsers),
},
{
name: "multi user with session user",
multi_user: JSON.stringify([...globalUsers, currentUser]),
},
{
name: "deprecated multi user",
deprecated_multi_user: JSON.stringify(globalUsers),
},
{
name: "deprecated multi user with session user",
deprecated_multi_user: JSON.stringify([
...globalUsers,
currentUser,
]),
},
]
}
// !! Current User is auto generated per run beforeAll(async () => {
it("should return all rows matching the session user firstname", async () => { // Set up some global users
await expectQuery({ globalUsers = await Promise.all(
equal: { name: "{{ [user].firstName }}" }, Array(2)
}).toContainExactly([ .fill(0)
{ .map(async () => {
name: config.getUser().firstName, const globalUser = await config.globalUser()
appointment: future.toISOString(), const userMedataId = globalUser._id
}, ? dbCore.generateUserMetadataID(globalUser._id)
]) : null
}) return {
_id: globalUser._id,
_meta: userMedataId,
}
})
)
it("should parse the date binding and return all rows after the resolved value", async () => { table = await createTable({
await tk.withFreeze(serverTime, async () => { name: { name: "name", type: FieldType.STRING },
await expectQuery({ appointment: { name: "appointment", type: FieldType.DATETIME },
range: { single_user: {
appointment: { name: "single_user",
low: "{{ [now] }}", type: FieldType.BB_REFERENCE_SINGLE,
high: "9999-00-00T00:00:00.000Z", subtype: BBReferenceFieldSubType.USER,
},
deprecated_single_user: {
name: "deprecated_single_user",
type: FieldType.BB_REFERENCE,
subtype: BBReferenceFieldSubType.USER,
},
multi_user: {
name: "multi_user",
type: FieldType.BB_REFERENCE,
subtype: BBReferenceFieldSubType.USER,
constraints: {
type: "array",
}, },
}, },
deprecated_multi_user: {
name: "deprecated_multi_user",
type: FieldType.BB_REFERENCE,
subtype: BBReferenceFieldSubType.USERS,
constraints: {
type: "array",
},
},
})
await createRows(rows(config.getUser()))
})
// !! Current User is auto generated per run
it("should return all rows matching the session user firstname", async () => {
await expectQuery({
equal: { name: "{{ [user].firstName }}" },
}).toContainExactly([ }).toContainExactly([
{ {
name: config.getUser().firstName, name: config.getUser().firstName,
appointment: future.toISOString(), appointment: future.toISOString(),
}, },
])
})
it("should parse the date binding and return all rows after the resolved value", async () => {
await tk.withFreeze(serverTime, async () => {
await expectQuery({
range: {
appointment: {
low: "{{ [now] }}",
high: "9999-00-00T00:00:00.000Z",
},
},
}).toContainExactly([
{
name: config.getUser().firstName,
appointment: future.toISOString(),
},
{ name: "serverDate", appointment: serverTime.toISOString() },
])
})
})
it("should parse the date binding and return all rows before the resolved value", async () => {
await expectQuery({
range: {
appointment: {
low: "0000-00-00T00:00:00.000Z",
high: "{{ [now] }}",
},
},
}).toContainExactly([
{ name: "foo", appointment: "1982-01-05T00:00:00.000Z" },
{ name: "bar", appointment: "1995-05-06T00:00:00.000Z" },
{ name: "serverDate", appointment: serverTime.toISOString() }, { name: "serverDate", appointment: serverTime.toISOString() },
]) ])
}) })
})
it("should parse the date binding and return all rows before the resolved value", async () => { it("should parse the encoded js snippet. Return rows with appointments up to 1 week in the past", async () => {
await expectQuery({ const jsBinding = "return snippets.WeeksAgo();"
range: { const encodedBinding = encodeJSBinding(jsBinding)
appointment: {
low: "0000-00-00T00:00:00.000Z", await expectQuery({
high: "{{ [now] }}", range: {
appointment: {
low: "0000-00-00T00:00:00.000Z",
high: encodedBinding,
},
}, },
}, }).toContainExactly([
}).toContainExactly([ { name: "foo", appointment: "1982-01-05T00:00:00.000Z" },
{ name: "foo", appointment: "1982-01-05T00:00:00.000Z" }, { name: "bar", appointment: "1995-05-06T00:00:00.000Z" },
{ name: "bar", appointment: "1995-05-06T00:00:00.000Z" }, ])
{ name: "serverDate", appointment: serverTime.toISOString() }, })
])
})
it("should parse the encoded js snippet. Return rows with appointments up to 1 week in the past", async () => { it("should parse the encoded js binding. Return rows with appointments 2 weeks in the past", async () => {
const jsBinding = "return snippets.WeeksAgo();" const jsBinding = `const currentTime = new Date(${Date.now()})\ncurrentTime.setDate(currentTime.getDate()-14);\nreturn currentTime.toISOString();`
const encodedBinding = encodeJSBinding(jsBinding) const encodedBinding = encodeJSBinding(jsBinding)
await expectQuery({ await expectQuery({
range: { range: {
appointment: { appointment: {
low: "0000-00-00T00:00:00.000Z", low: "0000-00-00T00:00:00.000Z",
high: encodedBinding, high: encodedBinding,
},
}, },
}, }).toContainExactly([
}).toContainExactly([ { name: "foo", appointment: "1982-01-05T00:00:00.000Z" },
{ name: "foo", appointment: "1982-01-05T00:00:00.000Z" }, { name: "bar", appointment: "1995-05-06T00:00:00.000Z" },
{ name: "bar", appointment: "1995-05-06T00:00:00.000Z" }, ])
]) })
})
it("should parse the encoded js binding. Return rows with appointments 2 weeks in the past", async () => { it("should match a single user row by the session user id", async () => {
const jsBinding = `const currentTime = new Date(${Date.now()})\ncurrentTime.setDate(currentTime.getDate()-14);\nreturn currentTime.toISOString();` await expectQuery({
const encodedBinding = encodeJSBinding(jsBinding) equal: { single_user: "{{ [user]._id }}" },
}).toContainExactly([
await expectQuery({ {
range: { name: "single user, session user",
appointment: { single_user: { _id: config.getUser()._id },
low: "0000-00-00T00:00:00.000Z",
high: encodedBinding,
}, },
}, ])
}).toContainExactly([ })
{ name: "foo", appointment: "1982-01-05T00:00:00.000Z" },
{ name: "bar", appointment: "1995-05-06T00:00:00.000Z" },
])
})
it("should match a single user row by the session user id", async () => { it("should match a deprecated single user row by the session user id", async () => {
await expectQuery({ await expectQuery({
equal: { single_user: "{{ [user]._id }}" }, equal: { deprecated_single_user: "{{ [user]._id }}" },
}).toContainExactly([ }).toContainExactly([
{ {
name: "single user, session user", name: "deprecated single user, session user",
single_user: { _id: config.getUser()._id }, deprecated_single_user: [{ _id: config.getUser()._id }],
}, },
]) ])
}) })
it("should match a deprecated single user row by the session user id", async () => { // TODO(samwho): fix for SQS
await expectQuery({ !isSqs &&
equal: { deprecated_single_user: "{{ [user]._id }}" }, it("should match the session user id in a multi user field", async () => {
}).toContainExactly([ const allUsers = [...globalUsers, config.getUser()].map(
{ (user: any) => {
name: "deprecated single user, session user", return { _id: user._id }
deprecated_single_user: [{ _id: config.getUser()._id }], }
}, )
])
})
// TODO(samwho): fix for SQS await expectQuery({
!isSqs && contains: { multi_user: ["{{ [user]._id }}"] },
it("should match the session user id in a multi user field", async () => { }).toContainExactly([
const allUsers = [...globalUsers, config.getUser()].map((user: any) => { {
return { _id: user._id } name: "multi user with session user",
multi_user: allUsers,
},
])
}) })
await expectQuery({ // TODO(samwho): fix for SQS
contains: { multi_user: ["{{ [user]._id }}"] }, !isSqs &&
}).toContainExactly([ it("should match the session user id in a deprecated multi user field", async () => {
{ const allUsers = [...globalUsers, config.getUser()].map(
name: "multi user with session user", (user: any) => {
multi_user: allUsers, return { _id: user._id }
}, }
]) )
})
// TODO(samwho): fix for SQS await expectQuery({
!isSqs && contains: { deprecated_multi_user: ["{{ [user]._id }}"] },
it("should match the session user id in a deprecated multi user field", async () => { }).toContainExactly([
const allUsers = [...globalUsers, config.getUser()].map((user: any) => { {
return { _id: user._id } name: "deprecated multi user with session user",
deprecated_multi_user: allUsers,
},
])
}) })
// TODO(samwho): fix for SQS
!isSqs &&
it("should not match the session user id in a multi user field", async () => {
await expectQuery({
notContains: { multi_user: ["{{ [user]._id }}"] },
notEmpty: { multi_user: true },
}).toContainExactly([
{
name: "multi user",
multi_user: globalUsers.map((user: any) => {
return { _id: user._id }
}),
},
])
})
// TODO(samwho): fix for SQS
!isSqs &&
it("should not match the session user id in a deprecated multi user field", async () => {
await expectQuery({
notContains: { deprecated_multi_user: ["{{ [user]._id }}"] },
notEmpty: { deprecated_multi_user: true },
}).toContainExactly([
{
name: "deprecated multi user",
deprecated_multi_user: globalUsers.map((user: any) => {
return { _id: user._id }
}),
},
])
})
it("should match the session user id and a user table row id using helpers, user binding and a static user id.", async () => {
await expectQuery({ await expectQuery({
contains: { deprecated_multi_user: ["{{ [user]._id }}"] }, oneOf: {
single_user: [
"{{ default [user]._id '_empty_' }}",
globalUsers[0]._id,
],
},
}).toContainExactly([ }).toContainExactly([
{ {
name: "deprecated multi user with session user", name: "single user, session user",
deprecated_multi_user: allUsers, single_user: { _id: config.getUser()._id },
},
{
name: "single user",
single_user: { _id: globalUsers[0]._id },
}, },
]) ])
}) })
// TODO(samwho): fix for SQS it("should match the session user id and a user table row id using helpers, user binding and a static user id. (deprecated single user)", async () => {
!isSqs &&
it("should not match the session user id in a multi user field", async () => {
await expectQuery({ await expectQuery({
notContains: { multi_user: ["{{ [user]._id }}"] }, oneOf: {
notEmpty: { multi_user: true }, deprecated_single_user: [
"{{ default [user]._id '_empty_' }}",
globalUsers[0]._id,
],
},
}).toContainExactly([ }).toContainExactly([
{ {
name: "multi user", name: "deprecated single user, session user",
multi_user: globalUsers.map((user: any) => { deprecated_single_user: [{ _id: config.getUser()._id }],
return { _id: user._id } },
}), {
name: "deprecated single user",
deprecated_single_user: [{ _id: globalUsers[0]._id }],
}, },
]) ])
}) })
// TODO(samwho): fix for SQS it("should resolve 'default' helper to '_empty_' when binding resolves to nothing", async () => {
!isSqs &&
it("should not match the session user id in a deprecated multi user field", async () => {
await expectQuery({ await expectQuery({
notContains: { deprecated_multi_user: ["{{ [user]._id }}"] }, oneOf: {
notEmpty: { deprecated_multi_user: true }, single_user: [
"{{ default [user]._idx '_empty_' }}",
globalUsers[0]._id,
],
},
}).toContainExactly([ }).toContainExactly([
{ {
name: "deprecated multi user", name: "single user",
deprecated_multi_user: globalUsers.map((user: any) => { single_user: { _id: globalUsers[0]._id },
return { _id: user._id }
}),
}, },
]) ])
}) })
it("should match the session user id and a user table row id using helpers, user binding and a static user id.", async () => { it("should resolve 'default' helper to '_empty_' when binding resolves to nothing (deprecated single user)", async () => {
await expectQuery({ await expectQuery({
oneOf: { oneOf: {
single_user: [ deprecated_single_user: [
"{{ default [user]._id '_empty_' }}", "{{ default [user]._idx '_empty_' }}",
globalUsers[0]._id, globalUsers[0]._id,
], ],
}, },
}).toContainExactly([ }).toContainExactly([
{ {
name: "single user, session user", name: "deprecated single user",
single_user: { _id: config.getUser()._id }, deprecated_single_user: [{ _id: globalUsers[0]._id }],
}, },
{ ])
name: "single user", })
single_user: { _id: globalUsers[0]._id },
},
])
}) })
it("should match the session user id and a user table row id using helpers, user binding and a static user id. (deprecated single user)", async () => {
await expectQuery({
oneOf: {
deprecated_single_user: [
"{{ default [user]._id '_empty_' }}",
globalUsers[0]._id,
],
},
}).toContainExactly([
{
name: "deprecated single user, session user",
deprecated_single_user: [{ _id: config.getUser()._id }],
},
{
name: "deprecated single user",
deprecated_single_user: [{ _id: globalUsers[0]._id }],
},
])
})
it("should resolve 'default' helper to '_empty_' when binding resolves to nothing", async () => {
await expectQuery({
oneOf: {
single_user: [
"{{ default [user]._idx '_empty_' }}",
globalUsers[0]._id,
],
},
}).toContainExactly([
{
name: "single user",
single_user: { _id: globalUsers[0]._id },
},
])
})
it("should resolve 'default' helper to '_empty_' when binding resolves to nothing (deprecated single user)", async () => {
await expectQuery({
oneOf: {
deprecated_single_user: [
"{{ default [user]._idx '_empty_' }}",
globalUsers[0]._id,
],
},
}).toContainExactly([
{
name: "deprecated single user",
deprecated_single_user: [{ _id: globalUsers[0]._id }],
},
])
})
})
describe.each([FieldType.STRING, FieldType.LONGFORM])("%s", () => { describe.each([FieldType.STRING, FieldType.LONGFORM])("%s", () => {
beforeAll(async () => { beforeAll(async () => {
table = await createTable({ table = await createTable({

View file

@ -325,17 +325,28 @@ export const runQuery = (
return false return false
} }
if (_.isObject(testValue.low) && _.isEmpty(testValue.low)) {
testValue.low = undefined
}
if (_.isObject(testValue.high) && _.isEmpty(testValue.high)) {
testValue.high = undefined
}
if (testValue.low == null && testValue.high == null) { if (testValue.low == null && testValue.high == null) {
return false return false
} }
if (!isNaN(+docValue)) { const docNum = +docValue
if (!isNaN(+testValue.low) && !isNaN(+testValue.high)) { if (!isNaN(docNum)) {
return +docValue >= testValue.low && +docValue <= testValue.high const lowNum = +testValue.low
} else if (!isNaN(+testValue.low)) { const highNum = +testValue.high
return +docValue >= testValue.low if (!isNaN(lowNum) && !isNaN(highNum)) {
} else if (!isNaN(+testValue.high)) { return docNum >= lowNum && docNum <= highNum
return +docValue <= testValue.high } else if (!isNaN(lowNum)) {
return docNum >= lowNum
} else if (!isNaN(highNum)) {
return docNum <= highNum
} }
} }