This repository was archived by the owner on Aug 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathupdate.test.ts
More file actions
171 lines (149 loc) · 5.62 KB
/
Copy pathupdate.test.ts
File metadata and controls
171 lines (149 loc) · 5.62 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { describe, expect, it } from 'vitest';
import {
createReturningUsersCollection,
createUsersCollection,
createUsersCollectionWithoutReturning,
timeouts,
withCollectionRuntime,
} from './helpers';
import { seedPosts, seedUsers } from './runtime-helpers';
describe('integration/update', () => {
it(
'updateCount() returns matched row count and updates data',
async () => {
await withCollectionRuntime(async (runtime) => {
const users = createUsersCollection(runtime);
await seedUsers(runtime, [
{ id: 1, name: 'Stale', email: 'a@example.com' },
{ id: 2, name: 'Stale', email: 'b@example.com' },
{ id: 3, name: 'Fresh', email: 'c@example.com' },
]);
const count = await users.where({ name: 'Stale' }).updateCount({ name: 'Updated' });
expect(count).toBe(2);
const rows = await runtime.query<{ id: number; name: string }>(
'select id, name from users order by id',
);
expect(rows).toEqual([
{ id: 1, name: 'Updated' },
{ id: 2, name: 'Updated' },
{ id: 3, name: 'Fresh' },
]);
});
},
timeouts.spinUpPpgDev,
);
it(
'update() affects only one row even when where() matches several',
async () => {
await withCollectionRuntime(async (runtime) => {
const users = createReturningUsersCollection(runtime);
await seedUsers(runtime, [
{ id: 1, name: 'Stale', email: 'a@example.com' },
{ id: 2, name: 'Stale', email: 'b@example.com' },
{ id: 3, name: 'Fresh', email: 'c@example.com' },
]);
const returned = await users.where({ name: 'Stale' }).update({ name: 'Updated' });
expect(returned).not.toBeNull();
expect(returned?.name).toBe('Updated');
expect([1, 2]).toContain(returned?.id);
const rows = await runtime.query<{ id: number; name: string }>(
'select id, name from users order by id',
);
const updatedRows = rows.filter((row) => row.name === 'Updated');
const staleRows = rows.filter((row) => row.name === 'Stale');
expect(updatedRows).toHaveLength(1);
expect(staleRows).toHaveLength(1);
expect(rows).toContainEqual({ id: 3, name: 'Fresh' });
expect(updatedRows[0]?.id).toBe(returned?.id);
});
},
timeouts.spinUpPpgDev,
);
it(
'updateAll() returns all updated rows',
async () => {
await withCollectionRuntime(async (runtime) => {
const users = createReturningUsersCollection(runtime);
await seedUsers(runtime, [
{ id: 1, name: 'Draft', email: 'a@example.com' },
{ id: 2, name: 'Draft', email: 'b@example.com' },
{ id: 3, name: 'Published', email: 'c@example.com' },
]);
const updated = await users.where({ name: 'Draft' }).updateAll({ name: 'Ready' });
expect(updated).toHaveLength(2);
expect(updated).toEqual(
expect.arrayContaining([
{ id: 1, name: 'Ready', email: 'a@example.com', invitedById: null, address: null },
{ id: 2, name: 'Ready', email: 'b@example.com', invitedById: null, address: null },
]),
);
const rows = await runtime.query<{ id: number; name: string }>(
'select id, name from users order by id',
);
expect(rows).toEqual([
{ id: 1, name: 'Ready' },
{ id: 2, name: 'Ready' },
{ id: 3, name: 'Published' },
]);
});
},
timeouts.spinUpPpgDev,
);
it(
'update() with include() and select() keeps selected scalars and relation rows',
async () => {
await withCollectionRuntime(async (runtime) => {
const users = createReturningUsersCollection(runtime);
await seedUsers(runtime, [{ id: 1, name: 'Alice', email: 'alice@example.com' }]);
await seedPosts(runtime, [
{ id: 10, title: 'Post A', userId: 1, views: 100 },
{ id: 11, title: 'Post B', userId: 1, views: 200 },
]);
const updated = await users
.where({ id: 1 })
.select('name')
.include('posts', (posts) => posts.orderBy((post) => post.id.asc()))
.update({ name: 'Alice Updated' });
expect(updated).toEqual({
name: 'Alice Updated',
posts: [
{ id: 10, title: 'Post A', userId: 1, views: 100, embedding: null },
{ id: 11, title: 'Post B', userId: 1, views: 200, embedding: null },
],
});
});
},
timeouts.spinUpPpgDev,
);
it(
'update() and updateAll() reject when returning capability is disabled',
async () => {
await withCollectionRuntime(async (runtime) => {
const users = createUsersCollectionWithoutReturning(runtime);
const filtered = users.where({ id: 1 });
await expect(filtered.update({ name: 'Blocked' })).rejects.toThrow(
/requires contract capability "returning"/,
);
expect(() => filtered.updateAll({ name: 'Blocked' })).toThrow(
/requires contract capability "returning"/,
);
});
},
timeouts.spinUpPpgDev,
);
it(
'updateAll({}) and updateCount({}) are no-ops',
async () => {
await withCollectionRuntime(async (runtime) => {
const users = createReturningUsersCollection(runtime);
runtime.resetExecutions();
const updated = await users.where({ id: 1 }).updateAll({});
const count = await users.where({ id: 1 }).updateCount({});
expect(updated).toEqual([]);
expect(count).toBe(0);
expect(runtime.executions).toHaveLength(0);
});
},
timeouts.spinUpPpgDev,
);
});