Changeset View
Changeset View
Standalone View
Standalone View
lib/app/base-chips/field-types/cached-value.subtest.js
Show First 20 Lines • Show All 141 Lines • ▼ Show 20 Lines | describe("cached-value", () => { | ||||
} | } | ||||
it("Correctly fills in cached-values if such field is added later", async () => { | it("Correctly fills in cached-values if such field is added later", async () => { | ||||
const account_ids = []; | const account_ids = []; | ||||
await with_stopped_app( | await with_stopped_app( | ||||
async ({ app, dont_clear_database_on_stop, rest_api }) => { | async ({ app, dont_clear_database_on_stop, rest_api }) => { | ||||
create_collections(app, "create_status_field" && false); | create_collections(app, "create_status_field" && false); | ||||
await app.start(); | await app.start(); | ||||
for (username of ["user_1", "user_2"]) { | for (const username of ["user_1", "user_2"]) { | ||||
account_ids.push(await add_account(rest_api, { username })); | account_ids.push(await add_account(rest_api, { username })); | ||||
} | } | ||||
await rest_api.post("/api/v1/collections/actions", { | await rest_api.post("/api/v1/collections/actions", { | ||||
name: "suspend", | name: "suspend", | ||||
account: account_ids[1], | account: account_ids[1], | ||||
}); | }); | ||||
dont_clear_database_on_stop(); | dont_clear_database_on_stop(); | ||||
} | } | ||||
); | ); | ||||
await with_stopped_app(async ({ app, rest_api }) => { | await with_stopped_app(async ({ app, rest_api }) => { | ||||
create_collections(app); | create_collections(app); | ||||
await app.start(); | await app.start(); | ||||
await assert_status_equals(rest_api, account_ids[0], "created"); | await assert_status_equals(rest_api, account_ids[0], "created"); | ||||
await assert_status_equals(rest_api, account_ids[1], "suspended"); | await assert_status_equals(rest_api, account_ids[1], "suspended"); | ||||
}); | }); | ||||
}); | }); | ||||
it("Correctly updates cached-value on create", async () => | it("Correctly updates cached-value on create", async () => | ||||
with_running_app(async ({ app, rest_api }) => { | with_running_app(async ({ app, rest_api }) => { | ||||
create_collections(app); | create_collections(app); | ||||
const account_ids = []; | const account_ids = []; | ||||
for (username of ["user_1", "user_2"]) { | for (const username of ["user_1", "user_2"]) { | ||||
account_ids.push(await add_account(rest_api, { username })); | account_ids.push(await add_account(rest_api, { username })); | ||||
} | } | ||||
await assert_status_equals(rest_api, account_ids[0], "created"); | await assert_status_equals(rest_api, account_ids[0], "created"); | ||||
const actions = ["open", "suspend", "close"]; | const actions = ["open", "suspend", "close"]; | ||||
for (let action of actions) { | for (let action of actions) { | ||||
await rest_api.post("/api/v1/collections/actions", { | await rest_api.post("/api/v1/collections/actions", { | ||||
name: action, | name: action, | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | with_running_app(async ({ app, rest_api }) => { | ||||
`/api/v1/collections/accounts/${id}?format[date_time]=human_readable` | `/api/v1/collections/accounts/${id}?format[date_time]=human_readable` | ||||
)).date_time; | )).date_time; | ||||
assert.equal(actual_datetime, expected_datetime); | assert.equal(actual_datetime, expected_datetime); | ||||
})); | })); | ||||
it("Properly responds to recursive edits", async () => | it("Properly responds to recursive edits", async () => | ||||
with_running_app(async ({ app }) => { | with_running_app(async ({ app }) => { | ||||
await assert_throws_async( | await assert_throws_async( | ||||
async () => { | async () => | ||||
const happy_numbers = await app.createChip( | app.createChip(app.Sealious.Collection, { | ||||
app.Sealious.Collection, | |||||
{ | |||||
name: "happy-numbers", | name: "happy-numbers", | ||||
fields: [ | fields: [ | ||||
{ | { | ||||
name: "number", | name: "number", | ||||
type: "int", | type: "int", | ||||
required: true, | required: true, | ||||
}, | }, | ||||
{ | { | ||||
name: "double_number", | name: "double_number", | ||||
type: "cached-value", | type: "cached-value", | ||||
params: { | params: { | ||||
base_field_type: { name: "int" }, | base_field_type: { name: "int" }, | ||||
get_value: async ( | get_value: async (context, number_id) => { | ||||
context, | const { number } = await app.run_action( | ||||
number_id | |||||
) => { | |||||
const { | |||||
number, | |||||
} = await app.run_action( | |||||
context, | context, | ||||
[ | [ | ||||
"collections", | "collections", | ||||
"happy-numbers", | "happy-numbers", | ||||
number_id, | number_id, | ||||
], | ], | ||||
"show" | "show" | ||||
); | ); | ||||
return number * 2; | return number * 2; | ||||
}, | }, | ||||
refresh_on: [ | refresh_on: [ | ||||
{ | { | ||||
event_matcher: new app.Sealious.EventMatchers.Collection( | event_matcher: new app.Sealious.EventMatchers.Collection( | ||||
{ | { | ||||
when: "after", | when: "after", | ||||
collection_name: | collection_name: | ||||
"happy-numbers", | "happy-numbers", | ||||
action: "create", | action: "create", | ||||
} | } | ||||
), | ), | ||||
resource_id_getter: ( | resource_id_getter: ( | ||||
emitted_event, | emitted_event, | ||||
resource | resource | ||||
) => resource.id, | ) => resource.id, | ||||
}, | }, | ||||
], | ], | ||||
}, | }, | ||||
}, | }, | ||||
], | ], | ||||
} | }), | ||||
); | |||||
}, | |||||
error => | error => | ||||
assert.equal( | assert.equal( | ||||
error, | error, | ||||
`Error: In the happy-numbers collection definition you've tried to create the double_number cached-value field that refers to the collection itself. Consider using 'derived-value' field type to avoid problems with endless recurrence.` | `Error: In the happy-numbers collection definition you've tried to create the double_number cached-value field that refers to the collection itself. Consider using 'derived-value' field type to avoid problems with endless recurrence.` | ||||
) | ) | ||||
); | ); | ||||
})); | })); | ||||
}); | }); | ||||
Show All 28 Lines |