-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: named query date parameters aren't behaving as expected (#2694)
* Initial commit. Added userQueryFields configuration option that will allow us to pass the logged in user's attributes in a namedQuery * Added logged in user parameter support for solr queries * Added mocha tests for the parameter building functions for solr and named queries inside the vocab service. Added a feature to expose all service methods when in mocha testing mode * Fixed issue where default values for date params were not being set. Fixed issue where date parameters with values of type days were not being set correctly on the request. * Fixed PopulateExportedMethods decorator so that it doesn't override the mochaTesting exportedMethods * Added mocha tests to ensure the parameter setting logic works correctly for named queries * Fixed format parameter for ISODate Added default format check to ensure it uses ISODate --------- Co-authored-by: Mark Cottman-Fields <[email protected]>
- Loading branch information
1 parent
307f92c
commit 771d58c
Showing
2 changed files
with
181 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
describe('The Named Query Service', function () { | ||
before(function (done) { | ||
done() | ||
}) | ||
|
||
it("Build a valid query with default values", function (done) { | ||
|
||
const namedQueryConfig = { | ||
collectionName: "record", | ||
brandIdFieldPath: "metaMetadata.brandId", | ||
resultObjectMapping: { | ||
|
||
}, | ||
mongoQuery: { | ||
"daysDatePath": null, | ||
"isoDatePath": null, | ||
"defaultIsoDatePath": null, | ||
"stringPath": null, | ||
"numberPath": null | ||
}, | ||
queryParams: { | ||
|
||
daysDateParam: { | ||
|
||
type: "date", | ||
path: "daysDatePath", | ||
queryType: "<=", | ||
format: "days", | ||
whenUndefined: "defaultValue", | ||
defaultValue: "-1" | ||
}, | ||
isoDateParam: { | ||
type: "date", | ||
queryType: "=>", | ||
path: "isoDatePath", | ||
format: "ISODate", | ||
whenUndefined: "defaultValue", | ||
defaultValue: "2024-12-17T00:00:00.000Z" | ||
}, | ||
defaultIsoDateParam: { | ||
type: "date", | ||
queryType: "=>", | ||
path: "defaultIsoDatePath", | ||
whenUndefined: "defaultValue", | ||
defaultValue: "2024-12-18T00:00:00.000Z" | ||
}, | ||
stringParam: { | ||
type: "string", | ||
path: "stringPath", | ||
queryType: 'contains', | ||
whenUndefined: "defaultValue", | ||
defaultValue: "defaultString" | ||
}, | ||
//TODO: number doesn't support queryType of default value like the others | ||
numberParam: { | ||
type: "number", | ||
path: "numberPath" | ||
} | ||
|
||
} | ||
|
||
}; | ||
|
||
const numberParamValue = 1; | ||
let paramMap = {numberParam: numberParamValue}; | ||
|
||
NamedQueryService.setParamsInQuery(namedQueryConfig.mongoQuery, namedQueryConfig.queryParams, paramMap); | ||
expect(namedQueryConfig.mongoQuery.numberPath).to.equal(numberParamValue); | ||
expect(namedQueryConfig.mongoQuery.isoDatePath).to.deep.equal({ "=>": '2024-12-17T00:00:00.000Z'}); | ||
expect(namedQueryConfig.mongoQuery.defaultIsoDatePath).to.deep.equal({ "=>": '2024-12-18T00:00:00.000Z'}); | ||
expect(namedQueryConfig.mongoQuery.stringPath).to.deep.equal({contains: 'defaultString'}); | ||
|
||
const daysDateString = namedQueryConfig.mongoQuery.daysDatePath["<="] | ||
expect(daysDateString).to.be.a('string'); | ||
let yesterdayIsoString = moment().subtract(1, 'days').format('YYYY-MM-DD'); | ||
expect(daysDateString).to.contain(yesterdayIsoString); | ||
|
||
done() | ||
}) | ||
|
||
|
||
it("Build a valid query with passed values", function (done) { | ||
|
||
const namedQueryConfig = { | ||
collectionName: "record", | ||
brandIdFieldPath: "metaMetadata.brandId", | ||
resultObjectMapping: { | ||
|
||
}, | ||
mongoQuery: { | ||
"daysDatePath": null, | ||
"isoDatePath": null, | ||
"stringPath": null, | ||
"numberPath": null | ||
}, | ||
queryParams: { | ||
|
||
daysDateParam: { | ||
|
||
type: "date", | ||
path: "daysDatePath", | ||
queryType: "<=", | ||
format: "days", | ||
whenUndefined: "defaultValue", | ||
defaultValue: "-1" | ||
}, | ||
isoDateParam: { | ||
type: "date", | ||
queryType: "=>", | ||
path: "isoDatePath", | ||
format: "ISODate", | ||
whenUndefined: "defaultValue", | ||
defaultValue: "2024-12-17T00:00:00.000Z" | ||
}, | ||
defaultIsoDateParam: { | ||
type: "date", | ||
queryType: "=>", | ||
path: "defaultIsoDatePath", | ||
whenUndefined: "defaultValue", | ||
defaultValue: "2024-12-18T00:00:00.000Z" | ||
}, | ||
stringParam: { | ||
type: "string", | ||
path: "stringPath", | ||
queryType: 'contains', | ||
whenUndefined: "defaultValue", | ||
defaultValue: "defaultString" | ||
}, | ||
numberParam: { | ||
type: "number", | ||
path: "numberPath" | ||
} | ||
|
||
} | ||
|
||
}; | ||
|
||
const daysDateParamValue = -2; | ||
const isoDateParamValue = "2024-12-18T00:00:00.000Z"; | ||
const defaultIsoDateParamValue = "2024-12-18T00:00:00.000Z"; | ||
const stringParamValue = "passedString"; | ||
const numberParamValue = 2; | ||
let paramMap = {numberParam: numberParamValue, isoDateParam: isoDateParamValue, stringParam: stringParamValue, daysDateParam: daysDateParamValue, defaultIsoDateParamValue: defaultIsoDateParamValue}; | ||
|
||
NamedQueryService.setParamsInQuery(namedQueryConfig.mongoQuery, namedQueryConfig.queryParams, paramMap); | ||
expect(namedQueryConfig.mongoQuery.numberPath).to.equal(numberParamValue); | ||
expect(namedQueryConfig.mongoQuery.isoDatePath).to.deep.equal({ "=>": isoDateParamValue}); | ||
expect(namedQueryConfig.mongoQuery.defaultIsoDatePath).to.deep.equal({ "=>": defaultIsoDateParamValue}); | ||
expect(namedQueryConfig.mongoQuery.stringPath).to.deep.equal({contains: stringParamValue}); | ||
|
||
const daysDateString = namedQueryConfig.mongoQuery.daysDatePath["<="] | ||
expect(daysDateString).to.be.a('string'); | ||
let dayBeforeYesterdayIsoString = moment().subtract(2, 'days').format('YYYY-MM-DD'); | ||
expect(daysDateString).to.contain(dayBeforeYesterdayIsoString); | ||
|
||
done() | ||
}) | ||
|
||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters