Changeset View
Changeset View
Standalone View
Standalone View
lib/app/base-chips/special_filters/matches.subtest.js
const assert = require("assert"); | const assert = require("assert"); | ||||
const locreq = require("locreq")(__dirname); | const locreq = require("locreq")(__dirname); | ||||
const Promise = require("bluebird"); | const Promise = require("bluebird"); | ||||
const { create_resource_as } = locreq("test_utils"); | |||||
const { with_running_app } = locreq("test_utils/with-test-app.js"); | const { with_running_app } = locreq("test_utils/with-test-app.js"); | ||||
describe("Matches", () => { | describe("Matches", () => { | ||||
async function setup(app) { | async function setup(app, rest_api) { | ||||
const port = app.ConfigManager.get("www-server.port"); | const port = app.ConfigManager.get("www-server.port"); | ||||
app.createChip(Sealious.Collection, { | app.createChip(Sealious.Collection, { | ||||
name: "numbers", | name: "numbers", | ||||
fields: [ | fields: [ | ||||
{ | { | ||||
name: "number", | name: "number", | ||||
type: "int", | type: "int", | ||||
}, | }, | ||||
], | ], | ||||
named_filters: { | named_filters: { | ||||
positive: app.SpecialFilter.Matches({ number: { ">": 0 } }), | positive: app.SpecialFilter.Matches({ number: { ">": 0 } }), | ||||
negative: app.SpecialFilter.Matches({ number: { "<": 0 } }), | negative: app.SpecialFilter.Matches({ number: { "<": 0 } }), | ||||
}, | }, | ||||
}); | }); | ||||
const numbers = [-2, -1, 0, 1, 2]; | const numbers = [-2, -1, 0, 1, 2]; | ||||
await Promise.map(numbers, n => | for (let number of numbers) { | ||||
create_resource_as({ | await rest_api.post("/api/v1/collections/numbers", { number }); | ||||
collection: "numbers", | } | ||||
resource: { number: n }, | |||||
port, | |||||
}) | |||||
); | |||||
} | } | ||||
it("returns only positive numbers when using filter", () => | it("returns only positive numbers when using filter", () => | ||||
with_running_app(async ({ app, rest_api }) => { | with_running_app(async ({ app, rest_api }) => { | ||||
await setup(app); | await setup(app, rest_api); | ||||
const result = await app.run_action( | const result = await app.run_action( | ||||
new app.Sealious.SuperContext(), | new app.Sealious.SuperContext(), | ||||
["collections", "numbers", "@positive"], | ["collections", "numbers", "@positive"], | ||||
"show" | "show" | ||||
); | ); | ||||
assert.deepEqual( | assert.deepEqual(result.items.map(resource => resource.number), [ | ||||
result.items.map(resource => resource.body.number), | 1, | ||||
[1, 2] | 2, | ||||
); | ]); | ||||
})); | })); | ||||
it("returns only positive numbers when using @positive filter", () => | it("returns only positive numbers when using @positive filter", () => | ||||
with_running_app(async ({ app, rest_api }) => { | with_running_app(async ({ app, rest_api }) => { | ||||
await setup(app); | await setup(app, rest_api); | ||||
const { items } = await rest_api.get( | const { items } = await rest_api.get( | ||||
"/api/v1/collections/numbers/@positive?sort[body.number]=asc" | "/api/v1/collections/numbers/@positive?sort[number]=asc" | ||||
); | ); | ||||
assert.deepEqual(items.map(resource => resource.body.number), [ | assert.deepEqual(items.map(resource => resource.number), [1, 2]); | ||||
1, | |||||
2, | |||||
]); | |||||
})); | })); | ||||
it("returns empty array when using both @positive and @negative filters", () => | it("returns empty array when using both @positive and @negative filters", () => | ||||
with_running_app(async ({ app, rest_api }) => { | with_running_app(async ({ app, rest_api }) => { | ||||
await setup(app); | await setup(app, rest_api); | ||||
const { items } = await rest_api.get( | const { items } = await rest_api.get( | ||||
"/api/v1/collections/numbers/@positive/@negative" | "/api/v1/collections/numbers/@positive/@negative" | ||||
); | ); | ||||
assert.deepEqual(items.map(resource => resource.body.number), []); | assert.deepEqual(items.map(resource => resource.number), []); | ||||
})); | })); | ||||
}); | }); |