-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathdelete.queries.spec.ts
104 lines (89 loc) · 3.41 KB
/
delete.queries.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { describe, it, beforeEach, expect } from 'bun:test';
import { newDb } from '../db';
import { IMemoryDb } from '../interfaces';
describe('Deletes', () => {
let db: IMemoryDb;
let many: (str: string) => any[];
let none: (str: string) => void;
beforeEach(() => {
db = newDb();
many = db.public.many.bind(db.public);
none = db.public.none.bind(db.public);
});
it('can delete with conditions simple', () => {
expect(many(`create table test(a text);
insert into test values ('a'), ('b'), ('c');
delete from test where a <= 'b';
select * from test;`))
.toEqual([{ a: 'c' }])
});
it('can delete with conditions within transaction', () => {
expect(many(`create table test(a text);
start transaction;
insert into test values ('a'), ('b'), ('c');
delete from test where a <= 'b';
commit;
select * from test;`))
.toEqual([{ a: 'c' }])
});
it('does not delete if rollback transaction', () => {
expect(many(`create table test(a text);
insert into test values ('a'), ('b'), ('c');
commit;
start transaction;
delete from test where a <= 'b';
rollback;
select * from test;`))
.toEqual([{ a: 'a' }, { a: 'b' }, { a: 'c' }])
});
it('delete if in same rollbacked transaction', () => {
expect(many(`create table test(a text);
commit;
start transaction;
insert into test values ('a'), ('b'), ('c');
delete from test where a <= 'b';
rollback;
select * from test;`))
.toEqual([])
});
it('can truncate table', () => {
expect(many(`create table test(a text);
insert into test values ('a'), ('b'), ('c');
truncate test;
select * from test;`))
.toEqual([])
});
it('does not restart identity by default on truncation', () => {
expect(many(`create table test_table (id INT GENERATED BY DEFAULT AS identity, val text);
insert into test_table(val) values ('a'), ('b'), ('c');
truncate test_table;
insert into test_table(val) values ('d'), ('e');
select * from test_table;`))
.toEqual([{ id: 4, val: 'd' }, { id: 5, val: 'e' }])
});
it('can restart identity on truncation', () => {
expect(many(`create table test_table (id INT GENERATED BY DEFAULT AS identity, val text);
insert into test_table(val) values ('a'), ('b'), ('c');
truncate test_table restart identity;
insert into test_table(val) values ('d'), ('e');
select * from test_table;`))
.toEqual([{ id: 1, val: 'd' }, { id: 2, val: 'e' }])
});
it('cannot query primary key condition after truncate', () => {
// this was a bug
expect(many(`create table test(a text primary key);
insert into test values ('a'), ('b'), ('c');
truncate test;
select * from test where a='a';`))
.toEqual([])
});
it('cannot query index condition after truncate', () => {
// this was a bug
expect(many(`create table test(a text);
create index on test(a);
insert into test values ('a'), ('b'), ('c');
truncate test;
select * from test where a='a';`))
.toEqual([])
});
});