Skip to content

Commit

Permalink
test: another integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
RutZap committed Jan 20, 2025
1 parent e09822b commit 7e526eb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
14 changes: 14 additions & 0 deletions integration/testdata/duration/functions/writeAndDuplicate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { WriteAndDuplicate, models, Duration } from "@teamkeel/sdk";

// To learn more about what you can do with custom functions, visit https://docs.keel.so/functions
export default WriteAndDuplicate(async (ctx, inputs) => {
const mod = await models.myDuration.create({ dur: inputs.dur });
const duplicate = await models.myDuration.create({
dur: Duration.fromISOString("PT1H"),
});

return {
model: mod,
duplicate: duplicate,
};
});
7 changes: 7 additions & 0 deletions integration/testdata/duration/schema.keel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ model MyDuration {
write writeCustomFunction(DurationMessage) returns (ModelMessage) {
@permission(expression: true)
}
write writeAndDuplicate(DurationMessage) returns (DuplicateModelMessage) {
@permission(expression: true)
}
}

@permission(expression: true, actions: [get, list, create, update])
Expand All @@ -29,3 +32,7 @@ message DurationMessage {
message ModelMessage {
model MyDuration
}
message DuplicateModelMessage {
model MyDuration
duplicate MyDuration
}
23 changes: 22 additions & 1 deletion integration/testdata/duration/tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ test("duration - write custom function", async () => {
expect(mydurs[0].dur?.toISOString()).toEqual("PT1H2M3S");
});

test("durs - create and store duration in hook", async () => {
test("duration - create and store duration in hook", async () => {
await actions.createDurationInHook({});

const mydurs = await useDatabase()
Expand All @@ -58,3 +58,24 @@ test("durs - create and store duration in hook", async () => {
expect(mydurs.length).toEqual(1);
expect(mydurs[0].dur?.toISOString()).toEqual("PT1H");
});

test("duration - write two in custom function", async () => {
// write and duplicate will create two models, one with the input and one with PT1H
const result = await actions.writeAndDuplicate({
dur: Duration.fromISOString("PT1H2M3S"),
});

expect(result.model.dur).toEqual("PT1H2M3S");
expect(result.duplicate.dur).toEqual("PT1H");

const mydurs = await useDatabase()
.selectFrom("my_duration")
.selectAll()
.execute();

expect(mydurs.length).toEqual(2);
expect(mydurs[0].id).toEqual(result.model.id);
expect(mydurs[0].dur?.toISOString()).toEqual("PT1H2M3S");
expect(mydurs[1].id).toEqual(result.duplicate.id);
expect(mydurs[1].dur?.toISOString()).toEqual("PT1H");
});
4 changes: 3 additions & 1 deletion packages/functions-runtime/src/camelCasePlugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const { CamelCasePlugin } = require("kysely");
const { isPlainObject, isRichType } = require("./type-utils");

// KeelCamelCasePlugin is a wrapper around kysely's camel case plugin.
// KeelCamelCasePlugin is a wrapper around kysely's CamelCasePlugin. The behaviour is the same apart from the fact that
// nested objects that are of a rich keel data type, such as Duration, are skipped so that they continue to be
// implementations of the rich data classes defined by Keel.
class KeelCamelCasePlugin {
constructor(opt) {
this.opt = opt;
Expand Down
5 changes: 2 additions & 3 deletions packages/functions-runtime/src/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ function createDatabaseClient({ connString } = {}) {
new AuditContextPlugin(),
// allows users to query using camelCased versions of the database column names, which
// should match the names we use in our schema.
// https://kysely-org.github.io/kysely/classes/CamelCasePlugin.html
// If they don't, then we can create a custom implementation of the plugin where we control
// the casing behaviour (see url above for example)
// We're using an extended version of Kysely's CamelCasePlugin which avoids changing keys of objects that represent
// rich data formats, specific to Keel (e.g. Duration)
new KeelCamelCasePlugin(),
],
log(event) {
Expand Down

0 comments on commit 7e526eb

Please sign in to comment.