Changeset View
Changeset View
Standalone View
Standalone View
lib/app/base-chips/field-types/cached-value.subtest.js
Show First 20 Lines • Show All 90 Lines • ▼ Show 20 Lines | collection.add_field({ | ||||
}, | }, | ||||
}, | }, | ||||
get_value: async (context, resource_id) => { | get_value: async (context, resource_id) => { | ||||
const [latest_action] = await app.Datastore.aggregate( | const [latest_action] = await app.Datastore.aggregate( | ||||
"actions", | "actions", | ||||
[ | [ | ||||
{ | { | ||||
$match: { | $match: { | ||||
"body.account": resource_id, | account: resource_id, | ||||
}, | }, | ||||
}, | }, | ||||
{ | { | ||||
$group: { | $group: { | ||||
_id: "$body.name", | _id: "$name", | ||||
timestamp: { | timestamp: { | ||||
$max: | $max: | ||||
"$last_modified_context.timestamp", | "$_metadata.last_modified_context.timestamp", | ||||
}, | }, | ||||
}, | }, | ||||
}, | }, | ||||
] | ] | ||||
); | ); | ||||
return action_to_status[latest_action._id]; | return action_to_status[latest_action._id]; | ||||
}, | }, | ||||
▲ Show 20 Lines • Show All 110 Lines • ▼ Show 20 Lines | with_running_app(async ({ app, rest_api }) => { | ||||
); | ); | ||||
} | } | ||||
); | ); | ||||
})); | })); | ||||
it("Respects filters of base field type", async () => | it("Respects filters of base field type", async () => | ||||
with_running_app(async ({ app, rest_api }) => { | with_running_app(async ({ app, rest_api }) => { | ||||
create_collections(app); | create_collections(app); | ||||
const ids = await add_a_few_accounts(app); | await add_a_few_accounts(app); | ||||
const { items: accounts } = await rest_api.get( | const { items: accounts } = await rest_api.get( | ||||
"/api/v1/collections/accounts?filter[number][>]=3" | "/api/v1/collections/accounts?filter[number][>]=3" | ||||
); | ); | ||||
assert.equal(accounts.length, 2); | assert.equal(accounts.length, 2); | ||||
})); | })); | ||||
it("Respects format of base field type", async () => | it("Respects format of base field type", async () => | ||||
with_running_app(async ({ app, rest_api }) => { | with_running_app(async ({ app, rest_api }) => { | ||||
create_collections(app); | create_collections(app); | ||||
const id = await add_account(rest_api, { username: "user_1" }); | const id = await add_account(rest_api, { username: "user_1" }); | ||||
const expected_datetime = getDateTime( | const expected_datetime = getDateTime( | ||||
new Date("2018-01-01"), | new Date("2018-01-01"), | ||||
"yyyy-mm-dd hh:mm:ss" | "yyyy-mm-dd hh:mm:ss" | ||||
); | ); | ||||
const actual_datetime = (await rest_api.get( | const actual_datetime = (await rest_api.get( | ||||
`/api/v1/collections/accounts/${id}?format[date_time]=human_readable` | `/api/v1/collections/accounts/${id}?format[date_time]=human_readable` | ||||
)).body.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( | const happy_numbers = await app.createChip( | ||||
Show All 11 Lines | with_running_app(async ({ app }) => { | ||||
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, | context, | ||||
number_id | number_id | ||||
) => { | ) => { | ||||
const { | const { | ||||
body: { number }, | number, | ||||
} = await app.run_action( | } = await app.run_action( | ||||
context, | context, | ||||
[ | [ | ||||
"collections", | "collections", | ||||
"happy-numbers", | "happy-numbers", | ||||
number_id, | number_id, | ||||
], | ], | ||||
"show" | "show" | ||||
Show All 34 Lines | |||||
function make_refresh_on(app) { | function make_refresh_on(app) { | ||||
return [ | return [ | ||||
{ | { | ||||
event_matcher: new app.Sealious.EventMatchers.Collection({ | event_matcher: new app.Sealious.EventMatchers.Collection({ | ||||
when: "after", | when: "after", | ||||
collection_name: "actions", | collection_name: "actions", | ||||
action: "create", | action: "create", | ||||
}), | }), | ||||
resource_id_getter: (emitted_event, resource) => | resource_id_getter: (emitted_event, resource) => resource.account, | ||||
resource.body.account, | |||||
}, | }, | ||||
{ | { | ||||
event_matcher: new app.Sealious.EventMatchers.Resource({ | event_matcher: new app.Sealious.EventMatchers.Resource({ | ||||
when: "after", | when: "after", | ||||
collection_name: "actions", | collection_name: "actions", | ||||
action: "edit", | action: "edit", | ||||
}), | }), | ||||
resource_id_getter: (emitted_event, resource) => | resource_id_getter: (emitted_event, resource) => resource.account, | ||||
resource.body.account, | |||||
}, | }, | ||||
]; | ]; | ||||
} | } | ||||
async function assert_status_equals(rest_api, account_id, expected_status) { | async function assert_status_equals(rest_api, account_id, expected_status) { | ||||
const account = (await rest_api.get( | const account = await rest_api.get( | ||||
`/api/v1/collections/accounts/${account_id}` | `/api/v1/collections/accounts/${account_id}` | ||||
)).body; | ); | ||||
assert.equal(account.status, expected_status); | assert.equal(account.status, expected_status); | ||||
} | } |