-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
239 lines (214 loc) · 7.63 KB
/
test.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { describe, it } from "node:test";
import test from "node:test";
import assert from "node:assert";
import { Post, POST_KINDS, search } from "./search";
describe("Empty filter", () => {
it("should return all posts if filter is empty", async () => {
const posts = await search({});
assert(posts.length === 100_000);
assertPostsAreSorted(posts);
});
});
describe("Filter with title", () => {
it("should return all posts if title is empty", async () => {
const posts = await search({ title: "" });
assert(posts.length === 100_000);
assertPostsAreSorted(posts);
});
it('should return all posts if searching for "post" in lowercase', async () => {
const posts = await search({ title: "post" });
assert(posts.length === 100_000);
assertPostsAreSorted(posts);
});
it('should return all posts if searching for "Post" in uppercase', async () => {
const posts = await search({ title: "Post" });
assert(posts.length === 100_000);
assertPostsAreSorted(posts);
});
it('should return nothing if searching for "NON_EXISTING_POST"', async () => {
const posts = await search({ title: "Post NON_EXISTING_POST" });
assert(posts.length === 0);
});
it('should return matching posts if searching for "9"', async () => {
const posts = await search({ title: "9" });
assert(posts.length > 0);
assertPostsAreSorted(posts);
assert(posts.every((post) => post.title.includes("9")));
});
});
describe("Filter with kind", () => {
for (const kind of POST_KINDS) {
it(`should return matching posts if kind is "${kind}"`, async () => {
const posts = await search({ kind });
assert(posts.length > 0);
assertPostsAreSorted(posts);
assert(posts.every((post) => post.kind === kind));
});
}
});
describe("Filter with category", () => {
it('should return matching posts if searching for "Notícia"', async () => {
const posts = await search({ category: "Notícia" });
assert(posts.length > 0);
assertPostsAreSorted(posts);
assert(posts.every((post) => post.category.includes("Notícia")));
});
it('should return nothing if searching for "NON_EXISTING_CATEGORY"', async () => {
const posts = await search({ category: "NON_EXISTING_CATEGORY" });
assert(posts.length === 0);
});
});
describe("Filter with target", () => {
it('should return matching posts if searching for "Aluno"', async () => {
const posts = await search({ target: "Aluno" });
assert(posts.length > 0);
assertPostsAreSorted(posts);
assert(posts.every((post) => post.targets.includes("Aluno")));
});
it('should return nothing if searching for "NON_EXISTING_TARGET"', async () => {
const posts = await search({ target: "NON_EXISTING_TARGET" });
assert(posts.length === 0);
});
});
describe("Filter with publishedAtStart", () => {
it("should return matching posts if publishedAtStart", async () => {
const date = new Date("2024-06-01");
const posts = await search({ publishedAtStart: date });
assert(posts.length > 0);
assertPostsAreSorted(posts);
assert(posts.every((post) => new Date(post.publishedAt) >= date));
});
it("should return nothing if publishedAtStart is in the future", async () => {
const date = new Date("2025-06-01");
const posts = await search({ publishedAtStart: date });
assert(posts.length === 0);
});
});
describe("Filter with publishedAtEnd", () => {
it("should return matching posts if publishedAtEnd", async () => {
const date = new Date("2024-05-20");
const posts = await search({ publishedAtEnd: date });
assert(posts.length > 0);
assertPostsAreSorted(posts);
assert(posts.every((post) => new Date(post.publishedAt) <= date));
});
it("should return nothing if publishedAtEnd is in the past", async () => {
const date = new Date("1995-05-20");
const posts = await search({ publishedAtEnd: date });
assert(posts.length === 0);
});
});
describe("Filter with createdAtStart", () => {
it("should return matching posts if createdAtStart", async () => {
const date = new Date("2023-10-10");
const posts = await search({ createdAtStart: date });
assert(posts.length > 0);
assertPostsAreSorted(posts);
assert(posts.every((post) => new Date(post.createdAt) >= date));
});
it("should return nothing if createdAtStart is in the future", async () => {
const date = new Date("2025-10-10");
const posts = await search({ createdAtStart: date });
assert(posts.length === 0);
});
});
describe("Filter with createdAtEnd", () => {
it("should return matching posts if createdAtEnd", async () => {
const date = new Date("2023-10-20");
const posts = await search({ createdAtEnd: date });
assert(posts.length > 0);
assertPostsAreSorted(posts);
assert(posts.every((post) => new Date(post.createdAt) <= date));
});
it("should return nothing if createdAtEnd is in the past", async () => {
const date = new Date("1995-10-20");
const posts = await search({ createdAtEnd: date });
assert(posts.length === 0);
});
});
describe("Combining filters", () => {
it("Using title and kind", async () => {
const posts = await search({ title: "9", kind: "report" });
assert(posts.length > 0);
assertPostsAreSorted(posts);
assert(
posts.every((post) => post.title.includes("9") && post.kind === "report")
);
});
it("Using title and category", async () => {
const posts = await search({ title: "9", category: "Notícia" });
assert(posts.length > 0);
assertPostsAreSorted(posts);
assert(
posts.every(
(post) => post.title.includes("9") && post.category.includes("Notícia")
)
);
});
it("Using all filters", async () => {
const posts = await search({
title: "9",
kind: "report",
category: "Notícia",
target: "Aluno",
publishedAtStart: new Date("2024-01-01"),
publishedAtEnd: new Date("2024-06-20"),
createdAtStart: new Date("2023-08-01"),
createdAtEnd: new Date("2023-12-20"),
});
assert(posts.length > 0);
assertPostsAreSorted(posts);
assert(
posts.every(
(post) =>
post.title.includes("9") &&
post.kind === "report" &&
post.category.includes("Notícia") &&
post.targets.includes("Aluno") &&
new Date(post.publishedAt) >= new Date("2024-01-01") &&
new Date(post.publishedAt) <= new Date("2024-06-20") &&
new Date(post.createdAt) >= new Date("2023-08-01") &&
new Date(post.createdAt) <= new Date("2023-12-20")
)
);
});
it("Using publishedAtStart and publishedAtEnd", async () => {
const publishedAtStart = new Date("2024-03-01");
const publishedAtEnd = new Date("2024-04-01");
const posts = await search({
publishedAtStart,
publishedAtEnd,
});
assert(posts.length > 0);
assertPostsAreSorted(posts);
assert(
posts.every(
(post) =>
new Date(post.publishedAt) >= publishedAtStart &&
new Date(post.publishedAt) <= publishedAtEnd
)
);
});
it("Using createdAtStart and createdAtEnd", async () => {
const createdAtStart = new Date("2023-08-01");
const createdAtEnd = new Date("2023-12-20");
const posts = await search({
createdAtStart,
createdAtEnd,
});
assert(posts.length > 0);
assertPostsAreSorted(posts);
assert(
posts.every(
(post) =>
new Date(post.createdAt) >= createdAtStart &&
new Date(post.createdAt) <= createdAtEnd
)
);
});
});
function assertPostsAreSorted(posts: Post[]) {
for (let i = 0; i < posts.length - 1; i++) {
assert(posts[i].publishedAt >= posts[i + 1].publishedAt);
}
}