Skip to content

Commit fcc2f17

Browse files
committed
Added tests for batch
1 parent 4053598 commit fcc2f17

File tree

2 files changed

+158
-15
lines changed

2 files changed

+158
-15
lines changed

tests/integration/batch.test.ts

Lines changed: 90 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import makeServer from './drivers/default/server';
22
import Post from '../stubs/models/post';
33
import { Batch } from '../../src/batch';
4+
import { Orion } from '../../src/orion';
45

56
let server: any;
67

78
beforeEach(() => {
89
server = makeServer();
10+
Orion.setBaseUrl("https://api-mock.test/api")
911
});
1012

1113
afterEach(() => {
@@ -14,25 +16,98 @@ afterEach(() => {
1416

1517
describe('Batch tests', () => {
1618
test('saving a couple of models', async () => {
17-
await Batch.store(
18-
[
19-
new Post({title: "item1", id: 10, created_at: "", updated_at: ""}),
20-
new Post({title: "item2", id: 12, created_at: "", updated_at: ""}),
21-
]
22-
)
19+
const posts = [
20+
new Post(),
21+
new Post(),
22+
]
23+
posts[0].$attributes.title = "First";
24+
posts[1].$attributes.title = "Second";
25+
26+
const res = await Batch.store(posts);
27+
28+
expect(server.schema.posts.all()).toHaveLength(2);
29+
expect(server.schema.posts.find('1').attrs.title).toBe("First")
30+
expect(server.schema.posts.find('2').attrs.title).toBe("Second")
31+
expect(server.schema.posts.find('1').attrs.title).toEqual(res[0].$attributes.title)
32+
expect(server.schema.posts.find('1').attrs.created_at).toEqual(res[0].$attributes.created_at)
33+
expect(server.schema.posts.find('2').attrs.title).toEqual(res[1].$attributes.title)
34+
expect(server.schema.posts.find('2').attrs.created_at).toEqual(res[1].$attributes.created_at)
2335
});
2436

2537
test('updating a couple of models', async () => {
26-
const post1 = await Post.$query().find(1);
27-
const post2 = await Post.$query().find(2);
38+
const posts = [
39+
new Post(),
40+
new Post(),
41+
new Post(),
42+
]
43+
posts[0].$attributes.title = "First";
44+
posts[1].$attributes.title = "Second";
45+
posts[2].$attributes.title = "Third";
46+
47+
let res = await Batch.store(posts);
48+
49+
res[0].$attributes.title = "NewFirst";
50+
res[1].$attributes.title = "NewSecond";
51+
52+
res = await Batch.update([res[0],res[1]]);
53+
54+
expect(res).toHaveLength(2);
55+
expect(server.schema.posts.find('1').attrs.title).toBe("NewFirst")
56+
expect(server.schema.posts.find('2').attrs.title).toBe("NewSecond")
57+
expect(server.schema.posts.find('1').attrs.title).toEqual(res[0].$attributes.title)
58+
expect(server.schema.posts.find('2').attrs.title).toEqual(res[1].$attributes.title)
59+
expect(server.schema.posts.find('3').attrs.title).toEqual("Third");
60+
61+
});
62+
63+
test('deleting a couple of models', async () => {
64+
const posts = [
65+
new Post(),
66+
new Post(),
67+
new Post(),
68+
]
69+
posts[0].$attributes.title = "First";
70+
posts[1].$attributes.title = "Second";
71+
posts[2].$attributes.title = "Third";
72+
73+
let res = await Batch.store(posts);
74+
75+
let ModelDelete = await Batch.delete([res[1]]);
76+
let idDelete = await Batch.delete([3], new Post);
77+
78+
expect(server.schema.posts.find('1').attrs.deleted_at).toBeUndefined();
79+
expect(server.schema.posts.find('2').attrs.deleted_at).toBeDefined();
80+
expect(server.schema.posts.find('3').attrs.deleted_at).toBeDefined();
81+
expect(server.schema.posts.find('2').attrs.title).toEqual(ModelDelete[0].$attributes.title)
82+
expect(server.schema.posts.find('3').attrs.title).toEqual(idDelete[0].$attributes.title)
83+
84+
85+
});
86+
87+
test('restoring a couple of models', async () => {
88+
const posts = [
89+
new Post(),
90+
new Post(),
91+
new Post(),
92+
]
93+
posts[0].$attributes.title = "First";
94+
posts[1].$attributes.title = "Second";
95+
posts[2].$attributes.title = "Third";
96+
97+
let res = await Batch.store(posts);
98+
99+
// delete ID 2 & 3
100+
let ModelDelete = await Batch.delete([res[1]]);
101+
let idDelete = await Batch.delete([3], new Post);
102+
103+
res = await Batch.restore([...ModelDelete, ...idDelete]);
104+
105+
expect(server.schema.posts.find('1').attrs.deleted_at).toBeFalsy();
106+
expect(server.schema.posts.find('2').attrs.deleted_at).toBeFalsy();
107+
expect(server.schema.posts.find('3').attrs.deleted_at).toBeFalsy();
108+
expect(server.schema.posts.find('2').attrs.title).toEqual(res[0].$attributes.title);
109+
expect(server.schema.posts.find('3').attrs.title).toEqual(res[1].$attributes.title);
28110

29-
post1.$attributes.title = "new title";
30-
post2.$attributes.title = "newer title";
31111

32-
await Batch.update(
33-
[
34-
post1, post2
35-
]
36-
)
37112
});
38113
});

tests/integration/drivers/default/server.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,74 @@ export default function makeServer() {
129129
updated: [request.params.tag_id],
130130
};
131131
});
132+
133+
this.post('/api/posts/batch', (schema: any, request) => {
134+
const body: {
135+
resources: any[]
136+
} = JSON.parse(request.requestBody);
137+
138+
const rval: any[] = [];
139+
for (let i = 0; i < body.resources.length; i++) {
140+
rval.push(schema.posts.create(body.resources[i]));
141+
}
142+
143+
return {data: rval};
144+
})
145+
146+
this.patch('/api/posts/batch', (schema: any, request) => {
147+
const body: {
148+
resources: object
149+
} = JSON.parse(request.requestBody);
150+
151+
const rval: any[] = [];
152+
for (const key in body.resources) {
153+
const attrs = body.resources[key];
154+
155+
const post = schema.posts.find(key);
156+
157+
158+
rval.push(post.update(attrs));
159+
}
160+
161+
return {data: rval};
162+
})
163+
164+
this.delete('/api/posts/batch', (schema: any, request) => {
165+
const body: {
166+
resources: number[]
167+
} = JSON.parse(request.requestBody);
168+
169+
const rval: any[] = [];
170+
for (let i = 0; i < body.resources.length; i++) {
171+
const id = body.resources[i];
172+
const post = schema.posts.find(id);
173+
174+
post.update({ deleted_at: '2021-01-01' });
175+
176+
rval.push(post);
177+
}
178+
179+
return {data: rval};
180+
})
181+
182+
this.post('/api/posts/batch/restore', (schema: any, request) => {
183+
const body: {
184+
resources: number[]
185+
} = JSON.parse(request.requestBody);
186+
187+
const rval: any[] = [];
188+
189+
for (let i = 0; i < body.resources.length; i++) {
190+
const id = body.resources[i];
191+
const post = schema.posts.find(id);
192+
193+
post.update({ deleted_at: null });
194+
195+
rval.push(post);
196+
}
197+
198+
return {data: rval};
199+
})
132200
},
133201
});
134202
}

0 commit comments

Comments
 (0)