Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lolopinto committed Aug 2, 2022
1 parent d6395f2 commit d4ad62b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
55 changes: 45 additions & 10 deletions ts/src/action/orchestrator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import {
Table,
} from "../testutils/db/temp_db";
import { Dialect } from "../core/db";
import { convertList } from "../core/convert";
import { convertDate, convertList } from "../core/convert";
import { v4 } from "uuid";

jest.mock("pg");
Expand Down Expand Up @@ -294,6 +294,18 @@ const SensitiveValuesSchema = getBuilderSchemaFromFields(
SensitiveUser,
);

class NullableEvent extends Event {}

const SchemaWithNullFields = getBuilderSchemaFromFields(
{
startTime: TimestampType(),
endTime: TimestampType({
nullable: true,
}),
},
NullableEvent,
);

describe("postgres", () => {
commonTests();
});
Expand Down Expand Up @@ -321,6 +333,7 @@ describe("sqlite", () => {
CustomUserSchema,
ContactEmailSchema,
SensitiveValuesSchema,
SchemaWithNullFields,
].map((s) => tables.push(getSchemaTable(s, Dialect.SQLite)));
return tables;
};
Expand Down Expand Up @@ -516,14 +529,6 @@ function commonTests() {
});

test("schema with null fields", async () => {
const SchemaWithNullFields = getBuilderSchemaFromFields(
{
startTime: TimestampType(),
endTime: TimestampType({ nullable: true }),
},
User,
);

const d = new Date();
const builder = new SimpleBuilder(
new LoggedOutViewer(),
Expand All @@ -539,12 +544,20 @@ function commonTests() {
validateFieldsExist(fields, "id", "created_at", "updated_at");
validateFieldsDoNotExist(fields, "end_time");

await builder.saveX();
const evt = await builder.editedEntX();
expect(convertDate(evt.data.start_time).toISOString()).toBe(
d.toISOString(),
);
// undefined in fake postgres, null in sqlite
expect(evt.data.end_time).toBeFalsy();

const builder2 = new SimpleBuilder(
new LoggedOutViewer(),
SchemaWithNullFields,
new Map([
["startTime", d],
["endTime", null],
["endTime", d],
]),
WriteOperation.Insert,
null,
Expand All @@ -554,6 +567,28 @@ function commonTests() {
expect(fields2["start_time"]).toEqual(d.toISOString());

validateFieldsExist(fields2, "id", "created_at", "updated_at");

await builder2.saveX();
const evt2 = await builder2.editedEntX();
expect(convertDate(evt2.data.start_time).toISOString()).toBe(
d.toISOString(),
);
expect(convertDate(evt2.data.end_time).toISOString()).toBe(d.toISOString());

const builder3 = new SimpleBuilder(
new LoggedOutViewer(),
SchemaWithNullFields,
new Map([["endTime", null]]),
WriteOperation.Edit,
evt2,
);

await builder3.saveX();
const evt3 = await builder3.editedEntX();
expect(convertDate(evt3.data.start_time).toISOString()).toBe(
d.toISOString(),
);
expect(evt3.data.end_time).toBeNull();
});

test("schema_with_overriden_storage_key", async () => {
Expand Down
11 changes: 10 additions & 1 deletion ts/src/core/ent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,7 @@ export function buildUpdateQuery(
let valsString: string[] = [];
let values: any[] = [];
let logValues: any[] = [];
const dialect = DB.getDialect();

let idx = 1;
for (const key in options.fields) {
Expand All @@ -1433,7 +1434,15 @@ export function buildUpdateQuery(
if (options.fieldsToLog) {
logValues.push(options.fieldsToLog[key]);
}
valsString.push(clause.Eq(key, val).clause(idx));
// TODO would be nice to use clause here. need update version of the queries so that
// we don't have to handle dialect specifics here
// can't use clause because of IS NULL
// valsString.push(clause.Eq(key, val).clause(idx));
if (dialect === Dialect.Postgres) {
valsString.push(`${key} = $${idx}`);
} else {
valsString.push(`${key} = ?`);
}
idx++;
}

Expand Down

0 comments on commit d4ad62b

Please sign in to comment.