Skip to content

Commit

Permalink
feat(where-parser): standardize the where input object to ease the qu…
Browse files Browse the repository at this point in the history
…erying (#1601)
  • Loading branch information
brunozoric committed May 27, 2021
1 parent c9cd4b7 commit f23f9fb
Show file tree
Hide file tree
Showing 33 changed files with 1,655 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,16 @@ describe("latest entries", function() {
await until(
() => previewListArticles().then(([data]) => data),
({ data }) => {
const categories = data?.listArticles?.data[0]?.categories || [];
const targetArticle = data?.listArticles?.data[0];
if (targetArticle.savedOn !== article.savedOn) {
return false;
}
const categories = targetArticle.categories || [];
return categories.some(category => {
return category.id === updatedFruitCategory.id;
});
},
{ name: "list all articles", tries: 5 }
{ name: "list all articles", tries: 10 }
);

const [listResponse] = await previewListArticles();
Expand Down
1 change: 1 addition & 0 deletions packages/where-parser/.babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("../../.babel.node")({ path: __dirname });
21 changes: 21 additions & 0 deletions packages/where-parser/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Webiny

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 18 additions & 0 deletions packages/where-parser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# @webiny/where-parser

[![](https://img.shields.io/npm/dw/@webiny/where-parser.svg)](https://www.npmjs.com/package/@webiny/where-parser)
[![](https://img.shields.io/npm/v/@webiny/where-parser.svg)](https://www.npmjs.com/package/@webiny/where-parser)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)

## Install

```
npm install --save @webiny/where-parser
```

Or if you prefer yarn:

```
yarn add @webiny/where-parser
```
41 changes: 41 additions & 0 deletions packages/where-parser/__tests__/conditions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import availableConditions from "../src/conditions";

const requiredConditions = [
"eq",
"not_eq",
"in",
"not_in",
"gt",
"not_gt",
"gte",
"not_gte",
"lt",
"not_lt",
"lte",
"not_lte",
"contains",
"not_contains",
"between",
"not_between"
];

describe("conditions", () => {
test.each(requiredConditions)(
"all required conditions should exist - %s",
(required: string) => {
const found = availableConditions.all().filter(c => {
return c.key === required;
});

expect(found).toHaveLength(1);
}
);

it("should not contain conditions that are not in the list", () => {
const conditions = availableConditions.all();
for (const c of conditions) {
const isRequired = requiredConditions.includes(c.key);
expect(isRequired).toBe(true);
}
});
});
74 changes: 74 additions & 0 deletions packages/where-parser/__tests__/conditions/between.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import availableConditions from "../../src/conditions";

const supportedValues = [[[1, 4]]];

const unsupportedValues = [
["number", 1],
["string", "text"],
["date", new Date()],
["boolean:true", true],
["boolean:false", false],
["null", null],
["array:0", []],
["array:1", [1]],
["array:3", [1, 2, 3]],
["object", {}]
];

describe("condition - between", () => {
test.each(supportedValues)(
"between should not throw an error on validation when value is supported",
(value: any) => {
const betweenCondition = availableConditions.get("between");

expect(() => {
betweenCondition.validate({
attr: "id",
value
});
}).not.toThrow();
}
);

test.each(unsupportedValues)(
"between should throw an error on validation when value is not supported - %s",
(name: string, value: any) => {
const betweenCondition = availableConditions.get("between");

expect(() => {
betweenCondition.validate({
attr: "id",
value
});
}).toThrow();
}
);

test.each(supportedValues)(
"not_between should not throw an error on validation when value is supported",
(value: any) => {
const notBetweenCondition = availableConditions.get("not_between");

expect(() => {
notBetweenCondition.validate({
attr: "id",
value
});
}).not.toThrow();
}
);

test.each(unsupportedValues)(
"not_between should throw an error on validation when value is not supported - %s",
(name: string, value: any) => {
const notBetweenCondition = availableConditions.get("not_between");

expect(() => {
notBetweenCondition.validate({
attr: "id",
value
});
}).toThrow();
}
);
});
75 changes: 75 additions & 0 deletions packages/where-parser/__tests__/conditions/contains.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import availableConditions from "../../src/conditions";

const supportedValues = [
["number", 1],
["string", "text"]
];

const unsupportedValues = [
["date", new Date()],
["boolean:true", true],
["boolean:false", false],
["null", null],
["array:0", []],
["array:1", [1]],
["array:3", [1, 2, 3]],
["object", {}]
];

describe("condition - contains", () => {
test.each(supportedValues)(
"contains should not throw an error on validation when value is supported - %s",
(name: string, value: any) => {
const containsCondition = availableConditions.get("contains");

expect(() => {
containsCondition.validate({
attr: "id",
value
});
}).not.toThrow();
}
);

test.each(unsupportedValues)(
"contains should throw an error on validation when value is not supported - %s",
(name: string, value: any) => {
const containsCondition = availableConditions.get("contains");

expect(() => {
containsCondition.validate({
attr: "id",
value
});
}).toThrow();
}
);

test.each(supportedValues)(
"not_contains should not throw an error on validation when value is supported - %s",
(name: string, value: any) => {
const notContainsCondition = availableConditions.get("not_contains");

expect(() => {
notContainsCondition.validate({
attr: "id",
value
});
}).not.toThrow();
}
);

test.each(unsupportedValues)(
"not_contains should throw an error on validation when value is not supported - %s",
(name: string, value: any) => {
const notContainsCondition = availableConditions.get("not_contains");

expect(() => {
notContainsCondition.validate({
attr: "id",
value
});
}).toThrow();
}
);
});
73 changes: 73 additions & 0 deletions packages/where-parser/__tests__/conditions/eq.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import availableConditions from "../../src/conditions";

const supportedValues = [
["number", 1],
["string", "text"],
["date", new Date()],
["boolean:true", true],
["boolean:false", false],
["null", null]
];

const unsupportedValues = [
["array", []],
["object", {}]
];

describe("condition - eq", () => {
test.each(supportedValues)(
"eq should not throw an error on validation when value is supported - %s",
(name: string, value: any) => {
const eqCondition = availableConditions.get("eq");

expect(() => {
eqCondition.validate({
attr: "id",
value
});
}).not.toThrow();
}
);

test.each(unsupportedValues)(
"eq should throw an error on validation when value is not supported - %s",
(name: string, value: any) => {
const eqCondition = availableConditions.get("eq");

expect(() => {
eqCondition.validate({
attr: "id",
value
});
}).toThrow("EQ condition value is of non-supported type.");
}
);

test.each(supportedValues)(
"not_eq should not throw an error on validation when value is supported - %s",
(name: string, value: any) => {
const notEqCondition = availableConditions.get("not_eq");

expect(() => {
notEqCondition.validate({
attr: "id",
value
});
}).not.toThrow();
}
);

test.each(unsupportedValues)(
"not_eq should throw an error on validation when value is not supported - %s",
(name: string, value: any) => {
const notEqCondition = availableConditions.get("not_eq");

expect(() => {
notEqCondition.validate({
attr: "id",
value
});
}).toThrow("EQ condition value is of non-supported type.");
}
);
});

0 comments on commit f23f9fb

Please sign in to comment.