Page MenuHomeSealhub

json-object.subtest.js
No OneTemporary

json-object.subtest.js

const assert = require("assert");
const locreq = require("locreq")(__dirname);
const Promise = require("bluebird");
const { with_running_app } = locreq("test_utils/with-test-app.js");
const assert_throws_async = locreq("test_utils/assert_throws_async.js");
describe("json-object", () => {
function setup(app) {
app.createChip(app.Sealious.Collection, {
name: "seals",
fields: [
{
name: "name",
type: "text",
required: true,
},
{
name: "metadata",
type: "json-object",
required: true,
params: {},
},
],
});
}
it("Correctly adds and edits record with json field", async () =>
with_running_app(async ({ app, rest_api }) => {
setup(app);
const item = {
name: "Hoover",
metadata: {
gender: "male",
weight: 300,
},
};
const { id } = await rest_api.post(
"/api/v1/collections/seals",
item
);
assert.deepEqual(
item,
(await rest_api.get(`/api/v1/collections/seals/${id}`)).body
);
item.metadata.weight = 320;
await rest_api.patch(`/api/v1/collections/seals/${id}`, item);
assert.deepEqual(
item,
(await rest_api.get(`/api/v1/collections/seals/${id}`)).body
);
}));
it("Doesn't allow to post a primitive", async () =>
with_running_app(async ({ app, rest_api }) => {
setup(app);
await assert_throws_async(
() =>
rest_api.post("/api/v1/collections/seals", {
name: "Hoover",
metadata: "atadatem",
}),
e =>
assert.equal(
e.response.data.data.metadata.message,
"A primitive, not an object!"
)
);
}));
it("Respects filter passed to query", async () =>
with_running_app(async ({ app, rest_api }) => {
setup(app);
await rest_api.post("/api/v1/collections/seals", {
name: "Hoover",
metadata: {
gender: "male",
weight: 300,
},
});
await rest_api.post("/api/v1/collections/seals", {
name: "Maksiu",
metadata: {
gender: "male",
weight: 280,
},
});
let seals = await rest_api.get("/api/v1/collections/seals");
assert.equal(seals.length, 2);
seals = await rest_api.get(
"/api/v1/collections/seals?filter[name]=Hoover"
);
assert.equal(seals[0].body.name, "Hoover");
seals = await rest_api.get(
"/api/v1/collections/seals?filter[metadata][weight]=280"
);
assert.equal(seals.length, 1);
assert.equal(seals[0].body.name, "Maksiu");
}));
it("Respects filter passed to query when value can be parsed as number", async () =>
with_running_app(async ({ app, rest_api }) => {
setup(app);
const seals = [
{
name: "Hoover",
metadata: {
gender: "male",
weight: "300.253",
},
},
{
name: "Maksiu",
metadata: {
gender: "male",
weight: "280",
},
},
];
for (let seal of seals) {
await rest_api.post("/api/v1/collections/seals", seal);
}
for (let seal of seals) {
let actual_seals = await rest_api.get(
`/api/v1/collections/seals?filter[metadata][weight]=${
seal.metadata.weight
}`
);
assert.equal(actual_seals.length, 1);
assert.equal(actual_seals[0].body.name, seal.name);
}
}));
});

File Metadata

Mime Type
text/plain
Expires
Tue, Dec 24, 14:03 (10 h, 51 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
557259
Default Alt Text
json-object.subtest.js (3 KB)

Event Timeline