Skip to content

Commit

Permalink
fix: support odata filter with tolower or toupper when mockdata conta…
Browse files Browse the repository at this point in the history
…ins null (#898)

* fix: support odata filter with tolower or toupper when mockdata contains null

* chore: changeset

* chore: sonar
  • Loading branch information
nlunets authored Nov 27, 2024
1 parent 3511b1d commit e49df89
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-rocks-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sap-ux/fe-mockserver-core': patch
---

We now properly support filter with string values null
20 changes: 10 additions & 10 deletions packages/fe-mockserver-core/src/data/entitySets/entitySet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ function makeTransformationFn(type: string, preparedArgs: PreparedFunction[]) {
function transformationFn(type: string, check?: any) {
switch (type) {
case 'tolower':
return (data: string) => data.toLowerCase();
return (data: string) => data?.toLowerCase();
case 'toupper':
return (data: string) => data.toUpperCase();
return (data: string) => data?.toUpperCase();
case 'trim':
return (data: string) => data.trim();
return (data: string) => data?.trim();
case 'length':
return (data: string) => {
return data && data.length;
Expand All @@ -45,7 +45,7 @@ function transformationFn(type: string, check?: any) {
return (data: string) => {
switch (check) {
case 'Edm.String':
return data.toString();
return data?.toString();
case 'Edm.Boolean':
return data === 'true';
case 'Edm.Byte':
Expand All @@ -64,31 +64,31 @@ function transformationFn(type: string, check?: any) {
};
case 'startswith':
return (data: string) => {
return data.startsWith(prepareLiteral(check, 'Edm.String') as string);
return data?.startsWith(prepareLiteral(check, 'Edm.String') as string);
};
case 'endswith':
return (data: string) => {
return data.endsWith(prepareLiteral(check, 'Edm.String') as string);
return data?.endsWith(prepareLiteral(check, 'Edm.String') as string);
};
case 'substringof':
return (data: string) => {
return check.indexOf(prepareLiteral(data, 'Edm.String') as string) !== -1;
return check?.indexOf(prepareLiteral(data, 'Edm.String') as string) !== -1;
};
case 'contains':
return (data: string) => {
return data.indexOf(prepareLiteral(check, 'Edm.String') as string) !== -1;
return data?.indexOf(prepareLiteral(check, 'Edm.String') as string) !== -1;
};
case 'concat':
return (data: string) => {
return data + prepareLiteral(check, 'Edm.String');
};
case 'indexof':
return (data: string) => {
return data.indexOf(prepareLiteral(check, 'Edm.String') as string);
return data?.indexOf(prepareLiteral(check, 'Edm.String') as string);
};
case 'substring':
return (data: string) => {
return data.substring(check);
return data?.substring(check);
};
case 'matchesPattern':
const regExp = new RegExp(prepareLiteral(check, 'Edm.String') as string);
Expand Down
61 changes: 32 additions & 29 deletions packages/fe-mockserver-core/test/unit/v4/dataAccess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ describe('Data Access', () => {
dataAccess
);
let countryData = await dataAccess.getData(odataRequest);
expect(countryData.length).toEqual(6);
expect(odataRequest.dataCount).toEqual(6);
expect(countryData.length).toEqual(7);
expect(odataRequest.dataCount).toEqual(7);
odataRequest = new ODataRequest(
{
url: '/Countries?$skip=0&$top=3',
Expand All @@ -47,7 +47,7 @@ describe('Data Access', () => {
);
countryData = await dataAccess.getData(odataRequest);
expect(countryData.length).toEqual(3);
expect(odataRequest.dataCount).toEqual(6);
expect(odataRequest.dataCount).toEqual(7);
expect(countryData[0].Country_Code).toEqual('DE');
expect(countryData[0].PeopleCount).toEqual(70);
expect(countryData[0].SuperHeroCount).toEqual(7);
Expand Down Expand Up @@ -87,7 +87,7 @@ describe('Data Access', () => {
dataAccess
)
);
expect(countryData.length).toEqual(1);
expect(countryData.length).toEqual(2);
expect(countryData[0].Country_Code).toEqual('DE');

countryData = await dataAccess.getData(
Expand All @@ -96,15 +96,15 @@ describe('Data Access', () => {
dataAccess
)
);
expect(countryData.length).toEqual(0);
expect(countryData.length).toEqual(1);

countryData = await dataAccess.getData(
new ODataRequest(
{ method: 'GET', url: "/Countries?$filter=contains(tolower(Country_Code), 'de')" },
dataAccess
)
);
expect(countryData.length).toEqual(1);
expect(countryData.length).toEqual(2);
expect(countryData[0].Country_Code).toEqual('DE');

countryData = await dataAccess.getData(
Expand Down Expand Up @@ -143,7 +143,7 @@ describe('Data Access', () => {
expect(countryData[0].Country_Code).toEqual('DE');
countryData = await dataAccess.getData(
new ODataRequest(
{ method: 'GET', url: "/Countries?$filter=tolower(Country_Code) eq tolower('dE')" },
{ method: 'GET', url: "/Countries?$filter=toupper(Country_Code) eq toupper('dE')" },
dataAccess
)
);
Expand All @@ -162,28 +162,31 @@ describe('Data Access', () => {
countryData = await dataAccess.getData(
new ODataRequest({ method: 'GET', url: '/Countries?$orderby=PeopleCount,Country_Code' }, dataAccess)
);
expect(countryData.length).toEqual(6);
expect(countryData[0].Country_Code).toEqual('LV');
expect(countryData[1].Country_Code).toEqual('IR');
expect(countryData[2].Country_Code).toEqual('FR');
expect(countryData[3].Country_Code).toEqual('DE');
expect(countryData[4].Country_Code).toEqual('US');
expect(countryData[5].Country_Code).toEqual('IN');
expect(countryData.length).toEqual(7);
expect(countryData[0].Country_Code).toEqual(null);
expect(countryData[1].Country_Code).toEqual('LV');
expect(countryData[2].Country_Code).toEqual('IR');
expect(countryData[3].Country_Code).toEqual('FR');
expect(countryData[4].Country_Code).toEqual('DE');
expect(countryData[5].Country_Code).toEqual('US');
expect(countryData[6].Country_Code).toEqual('IN');
countryData = await dataAccess.getData(
new ODataRequest({ method: 'GET', url: '/Countries?$orderby=PeopleCount,Country_Code desc' }, dataAccess)
);
expect(countryData.length).toEqual(6);
expect(countryData[0].Country_Code).toEqual('LV');
expect(countryData[1].Country_Code).toEqual('IR');
expect(countryData[2].Country_Code).toEqual('FR');
expect(countryData[3].Country_Code).toEqual('US');
expect(countryData[4].Country_Code).toEqual('DE');
expect(countryData[5].Country_Code).toEqual('IN');
expect(countryData.length).toEqual(7);
expect(countryData[0].Country_Code).toEqual(null);
expect(countryData[1].Country_Code).toEqual('LV');
expect(countryData[2].Country_Code).toEqual('IR');
expect(countryData[3].Country_Code).toEqual('FR');
expect(countryData[4].Country_Code).toEqual('US');
expect(countryData[5].Country_Code).toEqual('DE');
expect(countryData[6].Country_Code).toEqual('IN');
countryData = await dataAccess.getData(
new ODataRequest({ method: 'GET', url: '/Countries?$orderby=PeopleCount desc,Country_Code' }, dataAccess)
);
expect(countryData.length).toEqual(6);
expect(countryData[5].Country_Code).toEqual('LV');
expect(countryData.length).toEqual(7);
expect(countryData[6].Country_Code).toEqual('LV');
expect(countryData[5].Country_Code).toEqual(null);
expect(countryData[4].Country_Code).toEqual('IR');
expect(countryData[3].Country_Code).toEqual('FR');
expect(countryData[2].Country_Code).toEqual('US');
Expand Down Expand Up @@ -421,7 +424,7 @@ describe('Data Access', () => {

test('v4metadata - it can POST data for an entity', async () => {
let countryData = await dataAccess.getData(new ODataRequest({ method: 'GET', url: '/Countries' }, dataAccess));
expect(countryData.length).toEqual(6);
expect(countryData.length).toEqual(7);
countryData = await dataAccess.getData(
new ODataRequest({ method: 'GET', url: '/Countries?$filter=PeopleCount gt 750' }, dataAccess)
);
Expand Down Expand Up @@ -458,12 +461,12 @@ describe('Data Access', () => {
countryData = await dataAccess.getData(
new ODataRequest({ method: 'GET', url: '/Countries?$filter=PeopleCount lt 3' }, dataAccess)
);
expect(countryData.length).toEqual(1);
expect(countryData.length).toEqual(2);
// LE
countryData = await dataAccess.getData(
new ODataRequest({ method: 'GET', url: '/Countries?$filter=PeopleCount le 3' }, dataAccess)
);
expect(countryData.length).toEqual(2);
expect(countryData.length).toEqual(3);
// EQ
countryData = await dataAccess.getData(
new ODataRequest({ method: 'GET', url: '/Countries?$filter=PeopleCount eq 3' }, dataAccess)
Expand All @@ -473,7 +476,7 @@ describe('Data Access', () => {
countryData = await dataAccess.getData(
new ODataRequest({ method: 'GET', url: '/Countries?$filter=PeopleCount ne 3' }, dataAccess)
);
expect(countryData.length).toEqual(5);
expect(countryData.length).toEqual(6);
// Boolean
countryData = await dataAccess.getData(
new ODataRequest({ method: 'GET', url: '/Countries?$filter=IsHot eq true' }, dataAccess)
Expand All @@ -494,7 +497,7 @@ describe('Data Access', () => {
dataAccess
)
);
expect(countryData.length).toEqual(3);
expect(countryData.length).toEqual(4);
});
test('v4 metadata with draft, POST', async () => {
// Create Empty Element
Expand Down Expand Up @@ -1051,7 +1054,7 @@ describe('Data Access', () => {
let odataRequest = new ODataRequest({ method: 'GET', url: '/Countries?$skip=0&$top=3' }, dataAccess);
let countryData = await dataAccess.getData(odataRequest);
expect(countryData.length).toEqual(3);
expect(odataRequest.dataCount).toEqual(6);
expect(odataRequest.dataCount).toEqual(7);
expect(countryData[0].Country_Code).toEqual('DE');
expect(countryData[0].PeopleCount).toEqual(70);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,13 @@
"SpokenLanguages": ["Lettish"],
"MainLanguage": "Lettish",
"IsHot": false
},
{
"Country_Code": null,
"Name": "Null",
"PeopleCount": 2,
"SpokenLanguages": ["Nullish"],
"MainLanguage": "Nullish",
"IsHot": false
}
]

0 comments on commit e49df89

Please sign in to comment.