Skip to content

Commit 7a31dd1

Browse files
committed
Fix tests
1 parent ea75ae7 commit 7a31dd1

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

src/query-string.test.ts

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
import { UnsafeQueryLiteral, sql, joinSQL, commaJoinSQL, commaJoinStringsToSQL } from "./query-string";
1+
import { describe, expect, test } from "vitest";
2+
import {
3+
UnsafeQueryLiteral,
4+
sql,
5+
joinSQL,
6+
commaJoinSQL,
7+
commaJoinStringsToSQL,
8+
} from "./query-string";
29

310
test("sql template with simple values", () => {
411
const str = "foo";
512
const num = 42;
613
const bln = false;
7-
const [queryText, values] = sql`insert into MyTable(col1, col2) values (${str}, ${num}, ${bln})`.build();
14+
const [queryText, values] =
15+
sql`insert into MyTable(col1, col2) values (${str}, ${num}, ${bln})`.build();
816

9-
expect(queryText).toEqual("insert into MyTable(col1, col2) values ($1, $2, $3)");
17+
expect(queryText).toEqual(
18+
"insert into MyTable(col1, col2) values ($1, $2, $3)"
19+
);
1020
expect(values).toEqual([str, num, bln]);
1121
});
1222

@@ -17,7 +27,9 @@ test("appending sql templates", () => {
1727
query.append(sql` and bong = ${99}`);
1828

1929
const [queryText, values] = query.build();
20-
expect(queryText).toEqual("select * from MyTable where foo = $1 and baz = $2 and bong = $3");
30+
expect(queryText).toEqual(
31+
"select * from MyTable where foo = $1 and baz = $2 and bong = $3"
32+
);
2133
expect(values).toEqual(["bar", true, 99]);
2234
});
2335

@@ -27,11 +39,17 @@ test("appending literal strings", () => {
2739
const value = "floof";
2840

2941
const query = sql`select data from GoodStuff`;
30-
query.append(sql` order by `.appendRawString(`${coercion}(data->'${columnName}') asc`));
42+
query.append(
43+
sql` order by `.appendRawString(
44+
`${coercion}(data->'${columnName}') asc`
45+
)
46+
);
3147
query.append(sql` where foo = ${value}`);
3248

3349
const [queryText, values] = query.build();
34-
expect(queryText).toEqual("select data from GoodStuff order by as_number(data->'foof') asc where foo = $1");
50+
expect(queryText).toEqual(
51+
"select data from GoodStuff order by as_number(data->'foof') asc where foo = $1"
52+
);
3553
expect(values).toEqual([value]);
3654
});
3755

@@ -60,7 +78,9 @@ test("interpolating UnsafeQueryLiteral", () => {
6078
const query = sql`select ${columnName} from ${tableName} where answer = ${constant}`;
6179

6280
const [queryText, values] = query.build();
63-
expect(queryText).toEqual("select my_column from my_table where answer = $1");
81+
expect(queryText).toEqual(
82+
"select my_column from my_table where answer = $1"
83+
);
6484
expect(values).toEqual([constant]);
6585
});
6686

@@ -77,11 +97,15 @@ describe("build() is idempotent", () => {
7797
});
7898

7999
test("two", () => {
80-
const builder = sql`SELECT ${new UnsafeQueryLiteral("bla")} FROM ${new UnsafeQueryLiteral("table")}`;
100+
const builder = sql`SELECT ${new UnsafeQueryLiteral(
101+
"bla"
102+
)} FROM ${new UnsafeQueryLiteral("table")}`;
81103
builder.append(sql` ORDER BY row_id LIMIT ${123}`);
82104

83105
const [queryText1, values1] = builder.build();
84-
expect(queryText1).toEqual("SELECT bla FROM table ORDER BY row_id LIMIT $1");
106+
expect(queryText1).toEqual(
107+
"SELECT bla FROM table ORDER BY row_id LIMIT $1"
108+
);
85109
expect(values1).toEqual([123]);
86110

87111
const [queryText2, values2] = builder.build();
@@ -95,18 +119,29 @@ describe("join", () => {
95119
const more = [sql`a`, sql`${"b"}`, sql`c`];
96120

97121
describe("joinSQL", () => {
98-
test("one", () => expect(joinSQL(one, "|").build()).toEqual(["$1", [123]]));
99-
test("more", () => expect(joinSQL(more, "|").build()).toEqual(["a|$1|c", ["b"]]));
122+
test("one", () =>
123+
expect(joinSQL(one, "|").build()).toEqual(["$1", [123]]));
124+
test("more", () =>
125+
expect(joinSQL(more, "|").build()).toEqual(["a|$1|c", ["b"]]));
100126
});
101127

102128
describe("commaJoinSQL", () => {
103-
test("one", () => expect(commaJoinSQL(one).build()).toEqual(["$1", [123]]));
104-
test("more", () => expect(commaJoinSQL(more).build()).toEqual(["a, $1, c", ["b"]]));
129+
test("one", () =>
130+
expect(commaJoinSQL(one).build()).toEqual(["$1", [123]]));
131+
test("more", () =>
132+
expect(commaJoinSQL(more).build()).toEqual(["a, $1, c", ["b"]]));
105133
});
106134

107135
describe("commaJoinStringsToSQL", () => {
108-
test("one", () => expect(commaJoinStringsToSQL(["a"]).build()).toEqual(["$1", ["a"]]));
136+
test("one", () =>
137+
expect(commaJoinStringsToSQL(["a"]).build()).toEqual([
138+
"$1",
139+
["a"],
140+
]));
109141
test("more", () =>
110-
expect(commaJoinStringsToSQL(["a", "b", "c"]).build()).toEqual(["$1, $2, $3", ["a", "b", "c"]]));
142+
expect(commaJoinStringsToSQL(["a", "b", "c"]).build()).toEqual([
143+
"$1, $2, $3",
144+
["a", "b", "c"],
145+
]));
111146
});
112147
});

0 commit comments

Comments
 (0)