Changeset View
Changeset View
Standalone View
Standalone View
lib/app/base-chips/field-types/reverse-single-reference.subtest.js
Show All 33 Lines | if (with_reverse) { | ||||
type: "reverse-single-reference", | type: "reverse-single-reference", | ||||
params: { collection: A, field_name: "reference_to_b" }, | params: { collection: A, field_name: "reference_to_b" }, | ||||
}); | }); | ||||
} | } | ||||
} | } | ||||
async function create_resources(app) { | async function create_resources(app) { | ||||
const numbers = [1, 2, 3]; | const numbers = [1, 2, 3]; | ||||
const bs = await Promise.map(numbers, number => | const bs = []; | ||||
app.run_action( | for (let number of numbers) { | ||||
bs.push( | |||||
await app.run_action( | |||||
new app.Sealious.SuperContext(), | new app.Sealious.SuperContext(), | ||||
["collections", "B"], | ["collections", "B"], | ||||
"create", | "create", | ||||
{ number } | { number } | ||||
) | ) | ||||
); | ); | ||||
} | |||||
for (let b of bs) { | for (let b of bs) { | ||||
for (let i = 1; i <= b.body.number; i++) { | for (let i = 1; i <= b.number; i++) { | ||||
await app.run_action( | await app.run_action( | ||||
new app.Sealious.SuperContext(), | new app.Sealious.SuperContext(), | ||||
["collections", "A"], | ["collections", "A"], | ||||
"create", | "create", | ||||
{ reference_to_b: b.id, pairity: i % 2 ? "odd" : "even" } | { reference_to_b: b.id, pairity: i % 2 ? "odd" : "even" } | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
Show All 19 Lines | await with_stopped_app(async ({ app, dont_clear_database_on_stop }) => { | ||||
dont_clear_database_on_stop(); | dont_clear_database_on_stop(); | ||||
}); | }); | ||||
await with_stopped_app(async ({ base_url, app, rest_api }) => { | await with_stopped_app(async ({ base_url, app, rest_api }) => { | ||||
await create_referencing_collections(app, "with_reverse" && true); | await create_referencing_collections(app, "with_reverse" && true); | ||||
await app.start(); | await app.start(); | ||||
const { | const { | ||||
items: [result], | items: [result], | ||||
} = await rest_api.get("/api/v1/collections/B?filter[number]=1"); | } = await rest_api.get("/api/v1/collections/B?filter[number]=1"); | ||||
assert(result.body.references_in_a); | assert(result.references_in_a); | ||||
assert.equal(result.body.references_in_a.length, 1); | assert.equal(result.references_in_a.length, 1); | ||||
const { | const { | ||||
items: [result2], | items: [result2], | ||||
} = await rest_api.get("/api/v1/collections/B?filter[number]=2"); | } = await rest_api.get("/api/v1/collections/B?filter[number]=2"); | ||||
assert(result2.body.references_in_a); | assert(result2.references_in_a); | ||||
assert.equal(result2.body.references_in_a.length, 2); | assert.equal(result2.references_in_a.length, 2); | ||||
}); | }); | ||||
}); | }); | ||||
it("updates the cached value when a new reference is created", async () => { | it("updates the cached value when a new reference is created", async () => { | ||||
await with_stopped_app(async ({ app, rest_api }) => { | await with_stopped_app(async ({ app, rest_api }) => { | ||||
await create_referencing_collections(app, "with_reverse" && true); | await create_referencing_collections(app, "with_reverse" && true); | ||||
await app.start(); | await app.start(); | ||||
await create_resources(app); | await create_resources(app); | ||||
const { | const { | ||||
items: [result], | items: [result], | ||||
} = await rest_api.get("/api/v1/collections/B?filter[number]=2"); | } = await rest_api.get("/api/v1/collections/B?filter[number]=2"); | ||||
assert(result.body.references_in_a instanceof Array); | assert(result.references_in_a instanceof Array); | ||||
assert.equal(result.body.references_in_a.length, 2); | assert.equal(result.references_in_a.length, 2); | ||||
}); | }); | ||||
}); | }); | ||||
it("updates the cached value when an old reference is deleted", async () => | it("updates the cached value when an old reference is deleted", async () => | ||||
with_reverse(async ({ app, rest_api }) => { | with_reverse(async ({ app, rest_api }) => { | ||||
const { | const { | ||||
items: [result], | items: [result], | ||||
} = await rest_api.get("/api/v1/collections/B?filter[number]=2"); | } = await rest_api.get("/api/v1/collections/B?filter[number]=2"); | ||||
const referencing_id = result.body.references_in_a[0]; | const referencing_id = result.references_in_a[0]; | ||||
await rest_api.delete(`/api/v1/collections/A/${referencing_id}`); | await rest_api.delete(`/api/v1/collections/A/${referencing_id}`); | ||||
const { | const { | ||||
items: [new_result2], | items: [new_result2], | ||||
} = await rest_api.get("/api/v1/collections/B?filter[number]=2"); | } = await rest_api.get("/api/v1/collections/B?filter[number]=2"); | ||||
assert.equal(new_result2.body.references_in_a.length, 1); | assert.equal(new_result2.references_in_a.length, 1); | ||||
})); | })); | ||||
it("updates the cached value when an old reference is edited to a new one", async () => | it("updates the cached value when an old reference is edited to a new one", async () => | ||||
with_reverse(async ({ app, rest_api }) => { | with_reverse(async ({ app, rest_api }) => { | ||||
const { | const { | ||||
items: [result1], | items: [result1], | ||||
} = await rest_api.get("/api/v1/collections/B?filter[number]=1"); | } = await rest_api.get("/api/v1/collections/B?filter[number]=1"); | ||||
const { | const { | ||||
items: [result2], | items: [result2], | ||||
} = await rest_api.get("/api/v1/collections/B?filter[number]=2"); | } = await rest_api.get("/api/v1/collections/B?filter[number]=2"); | ||||
const referencing_id = result2.body.references_in_a[0]; | const referencing_id = result2.references_in_a[0]; | ||||
await rest_api.patch(`/api/v1/collections/A/${referencing_id}`, { | await rest_api.patch(`/api/v1/collections/A/${referencing_id}`, { | ||||
reference_to_b: result1.id, | reference_to_b: result1.id, | ||||
}); | }); | ||||
const { | const { | ||||
items: [new_result2], | items: [new_result2], | ||||
} = await rest_api.get("/api/v1/collections/B?filter[number]=2"); | } = await rest_api.get("/api/v1/collections/B?filter[number]=2"); | ||||
assert.equal(new_result2.body.references_in_a.length, 1); | assert.equal(new_result2.references_in_a.length, 1); | ||||
const { | const { | ||||
items: [new_result1], | items: [new_result1], | ||||
} = await rest_api.get("/api/v1/collections/B?filter[number]=1"); | } = await rest_api.get("/api/v1/collections/B?filter[number]=1"); | ||||
assert.equal(new_result1.body.references_in_a.length, 2); | assert.equal(new_result1.references_in_a.length, 2); | ||||
})); | })); | ||||
it("updates the cached value when an old reference is edited to an empty one", async () => | it("updates the cached value when an old reference is edited to an empty one", async () => | ||||
with_reverse(async ({ app, rest_api }) => { | with_reverse(async ({ app, rest_api }) => { | ||||
const { | const { | ||||
items: [result1], | items: [result1], | ||||
} = await rest_api.get("/api/v1/collections/B?filter[number]=1"); | } = await rest_api.get("/api/v1/collections/B?filter[number]=1"); | ||||
const { | const { | ||||
items: [result2], | items: [result2], | ||||
} = await rest_api.get("/api/v1/collections/B?filter[number]=2"); | } = await rest_api.get("/api/v1/collections/B?filter[number]=2"); | ||||
const referencing_id = result2.body.references_in_a[0]; | const referencing_id = result2.references_in_a[0]; | ||||
await rest_api.patch(`/api/v1/collections/A/${referencing_id}`, { | await rest_api.patch(`/api/v1/collections/A/${referencing_id}`, { | ||||
reference_to_b: "", | reference_to_b: "", | ||||
}); | }); | ||||
const { | const { | ||||
items: [new_result2], | items: [new_result2], | ||||
} = await rest_api.get("/api/v1/collections/B?filter[number]=2"); | } = await rest_api.get("/api/v1/collections/B?filter[number]=2"); | ||||
assert.equal(new_result2.body.references_in_a.length, 1); | assert.equal(new_result2.references_in_a.length, 1); | ||||
})); | })); | ||||
it("allows to filter by a value of the referencing resource", async () => | it("allows to filter by a value of the referencing resource", async () => | ||||
with_reverse(async ({ app, rest_api }) => { | with_reverse(async ({ app, rest_api }) => { | ||||
let results = (await rest_api.get( | let results = (await rest_api.get( | ||||
"/api/v1/collections/B?filter[references_in_a][pairity]=non-existant" | "/api/v1/collections/B?filter[references_in_a][pairity]=non-existant" | ||||
)).items; | )).items; | ||||
assert.equal(results.length, 0); | assert.equal(results.length, 0); | ||||
results = (await rest_api.get( | results = (await rest_api.get( | ||||
"/api/v1/collections/B?filter[references_in_a][pairity]=odd" | "/api/v1/collections/B?filter[references_in_a][pairity]=odd" | ||||
)).items; | )).items; | ||||
assert.equal(results.length, 3); | assert.equal(results.length, 3); | ||||
results = (await rest_api.get( | results = (await rest_api.get( | ||||
"/api/v1/collections/B?filter[references_in_a][pairity]=even&filter[number]=3" | "/api/v1/collections/B?filter[references_in_a][pairity]=even&filter[number]=3" | ||||
)).items; | )).items; | ||||
assert.equal(results.length, 1); | assert.equal(results.length, 1); | ||||
})); | })); | ||||
it("allows to display the full body of the referencing resources", async () => | it("allows to display the full body of the referencing resources", async () => | ||||
with_reverse(async ({ app, rest_api }) => { | with_reverse(async ({ app, rest_api }) => { | ||||
let { items } = await rest_api.get( | let { items } = await rest_api.get( | ||||
"/api/v1/collections/B?format[references_in_a]=expand" | "/api/v1/collections/B?format[references_in_a]=expand" | ||||
); | ); | ||||
assert(items[0].body.references_in_a[0].body); | assert(items[0].references_in_a[0]); | ||||
})); | })); | ||||
}); | }); |