Skip to content

Commit 2ce4c04

Browse files
committed
Offset + page + size tests (home_depot)
1 parent 376bec4 commit 2ce4c04

1 file changed

Lines changed: 285 additions & 0 deletions

File tree

tests/engines/home_depot_test.ts

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
// deno-lint-ignore-file no-explicit-any
2+
import { loadSync } from "https://deno.land/std@0.170.0/dotenv/mod.ts";
3+
import {
4+
afterAll,
5+
afterEach,
6+
beforeAll,
7+
beforeEach,
8+
describe,
9+
it,
10+
} from "https://deno.land/std@0.170.0/testing/bdd.ts";
11+
import { spy, Stub, stub } from "https://deno.land/std@0.170.0/testing/mock.ts";
12+
import {
13+
assert,
14+
assertEquals,
15+
assertExists,
16+
assertMatch,
17+
} from "https://deno.land/std@0.170.0/testing/asserts.ts";
18+
import { _internals } from "../../src/utils.ts";
19+
import { config, getJson } from "../../mod.ts";
20+
import {
21+
MSG_ASSERT_HAS_LAST_PAGE,
22+
MSG_ASSERT_HAS_MULTIPLE_PAGES,
23+
MSG_ASSERT_HAS_NON_FIRST_PAGE_RESULT,
24+
} from "./constants.ts";
25+
26+
loadSync({ export: true });
27+
const SERPAPI_TEST_KEY = Deno.env.get("SERPAPI_TEST_KEY") ?? "";
28+
const HAS_API_KEY = SERPAPI_TEST_KEY.length > 0;
29+
const BASE_URL = Deno.env.get("ENV_TYPE") === "local"
30+
? "http://localhost:3000"
31+
: "https://serpapi.com";
32+
33+
describe("home_depot", {
34+
sanitizeOps: false, // TODO(seb): look into how we can avoid setting these to false
35+
sanitizeResources: false,
36+
}, () => {
37+
let urlStub: Stub;
38+
const engine = "home_depot";
39+
const q = "coffee";
40+
41+
beforeAll(() => {
42+
urlStub = stub(_internals, "getBaseUrl", () => BASE_URL);
43+
});
44+
45+
beforeEach(() => {
46+
config.api_key = SERPAPI_TEST_KEY;
47+
});
48+
49+
afterEach(() => {
50+
config.api_key = null;
51+
});
52+
53+
afterAll(() => {
54+
urlStub.restore();
55+
});
56+
57+
it("getJson pagination", {
58+
ignore: !HAS_API_KEY,
59+
}, async (t) => {
60+
await t.step("async/await", async () => {
61+
const ids: string[] = [];
62+
let page;
63+
page = await getJson(engine, { q });
64+
while (page) {
65+
ids.push(...page.products.map((r: any) => r.product_id));
66+
if (ids.length >= 48) break;
67+
page = await page.next?.();
68+
}
69+
assert(new Set(ids).size > 24, MSG_ASSERT_HAS_MULTIPLE_PAGES);
70+
});
71+
72+
await t.step("callback", async () => {
73+
const ids: string[] = [];
74+
await new Promise<void>((done) => {
75+
getJson(engine, { q }, (page) => {
76+
ids.push(...page.products.map((r: any) => r.product_id));
77+
if (ids.length < 48 && page.next) {
78+
page.next();
79+
} else {
80+
assert(new Set(ids).size > 24, MSG_ASSERT_HAS_MULTIPLE_PAGES);
81+
done();
82+
}
83+
});
84+
});
85+
});
86+
});
87+
88+
it("getJson pagination keeps original parameter keys", {
89+
ignore: !HAS_API_KEY,
90+
}, async (t) => {
91+
const executeSpy = spy(_internals, "execute");
92+
config.api_key = null;
93+
94+
await t.step("async/await", async () => {
95+
const page1 = await getJson(engine, {
96+
api_key: SERPAPI_TEST_KEY,
97+
no_cache: false,
98+
q,
99+
});
100+
assertMatch(executeSpy.calls[0].args[1].api_key as string, /[a-z0-9]+/);
101+
assertEquals(executeSpy.calls[0].args[1].no_cache, false);
102+
103+
assertExists(page1.next);
104+
await page1.next();
105+
assertMatch(executeSpy.calls[1].args[1].api_key as string, /[a-z0-9]+/);
106+
assertEquals(executeSpy.calls[1].args[1].no_cache, false);
107+
});
108+
109+
await t.step("callback", async () => {
110+
const page1 = await new Promise<Awaited<ReturnType<typeof getJson>>>(
111+
(res) =>
112+
getJson(engine, {
113+
api_key: SERPAPI_TEST_KEY,
114+
no_cache: false,
115+
q,
116+
}, res),
117+
);
118+
assertMatch(executeSpy.calls[0].args[1].api_key as string, /[a-z0-9]+/);
119+
assertEquals(executeSpy.calls[0].args[1].no_cache, false);
120+
121+
assertExists(page1.next);
122+
await new Promise((res) => page1.next?.(res));
123+
assertMatch(executeSpy.calls[1].args[1].api_key as string, /[a-z0-9]+/);
124+
assertEquals(executeSpy.calls[1].args[1].no_cache, false);
125+
});
126+
127+
executeSpy.restore();
128+
});
129+
130+
it("getJson pagination with offset + size", {
131+
ignore: !HAS_API_KEY,
132+
}, async (t) => {
133+
const firstPage = await getJson(engine, { q, ps: 3 });
134+
const idsOnFirstPage = firstPage.products.map((r: any) => r.product_id);
135+
136+
await t.step("async/await", async () => {
137+
const ids: string[] = [];
138+
let page;
139+
page = await getJson(engine, { q, nao: "6", ps: 3 });
140+
while (page) {
141+
ids.push(...page.products.map((r: any) => r.product_id));
142+
if (ids.length >= 6) break;
143+
page = await page.next?.();
144+
}
145+
assert(new Set(ids).size > 3, MSG_ASSERT_HAS_MULTIPLE_PAGES);
146+
assert(
147+
ids.some((id) => !idsOnFirstPage.includes(id)),
148+
MSG_ASSERT_HAS_NON_FIRST_PAGE_RESULT,
149+
);
150+
});
151+
152+
await t.step("callback", async () => {
153+
const ids: string[] = [];
154+
await new Promise<void>((done) => {
155+
getJson(engine, { q, nao: "6", ps: 3 }, (page) => {
156+
ids.push(...page.products.map((r: any) => r.product_id));
157+
if (ids.length < 6 && page.next) {
158+
page.next();
159+
} else {
160+
assert(new Set(ids).size > 3, MSG_ASSERT_HAS_MULTIPLE_PAGES);
161+
assert(
162+
ids.some((id) => !idsOnFirstPage.includes(id)),
163+
MSG_ASSERT_HAS_NON_FIRST_PAGE_RESULT,
164+
);
165+
done();
166+
}
167+
});
168+
});
169+
});
170+
});
171+
172+
it("getJson pagination with page + size", {
173+
ignore: !HAS_API_KEY,
174+
}, async (t) => {
175+
const firstPage = await getJson(engine, { q, ps: 3 });
176+
const idsOnFirstPage = firstPage.products.map((r: any) => r.product_id);
177+
178+
await t.step("async/await", async () => {
179+
const ids: string[] = [];
180+
let page;
181+
page = await getJson(engine, { q, page: "3", ps: 3 });
182+
while (page) {
183+
ids.push(...page.products.map((r: any) => r.product_id));
184+
if (ids.length >= 6) break;
185+
page = await page.next?.();
186+
}
187+
assert(new Set(ids).size > 3, MSG_ASSERT_HAS_MULTIPLE_PAGES);
188+
assert(
189+
ids.some((id) => !idsOnFirstPage.includes(id)),
190+
MSG_ASSERT_HAS_NON_FIRST_PAGE_RESULT,
191+
);
192+
});
193+
194+
await t.step("callback", async () => {
195+
const ids: string[] = [];
196+
await new Promise<void>((done) => {
197+
getJson(engine, { q, page: "3", ps: 3 }, (page) => {
198+
ids.push(...page.products.map((r: any) => r.product_id));
199+
if (ids.length < 6 && page.next) {
200+
page.next();
201+
} else {
202+
assert(new Set(ids).size > 3, MSG_ASSERT_HAS_MULTIPLE_PAGES);
203+
assert(
204+
ids.some((id) => !idsOnFirstPage.includes(id)),
205+
MSG_ASSERT_HAS_NON_FIRST_PAGE_RESULT,
206+
);
207+
done();
208+
}
209+
});
210+
});
211+
});
212+
});
213+
214+
it("getJson pagination with offset + page + size", {
215+
ignore: !HAS_API_KEY,
216+
}, async (t) => {
217+
const firstPage = await getJson(engine, { q, ps: 3 });
218+
const idsOnFirstPage = firstPage.products.map((r: any) => r.product_id);
219+
220+
await t.step("async/await", async () => {
221+
const ids: string[] = [];
222+
let page;
223+
page = await getJson(engine, { q, nao: "6", page: "3", ps: 3 });
224+
while (page) {
225+
ids.push(...page.products.map((r: any) => r.product_id));
226+
if (ids.length >= 6) break;
227+
page = await page.next?.();
228+
}
229+
assert(new Set(ids).size > 3, MSG_ASSERT_HAS_MULTIPLE_PAGES);
230+
assert(
231+
ids.some((id) => !idsOnFirstPage.includes(id)),
232+
MSG_ASSERT_HAS_NON_FIRST_PAGE_RESULT,
233+
);
234+
});
235+
236+
await t.step("callback", async () => {
237+
const ids: string[] = [];
238+
await new Promise<void>((done) => {
239+
getJson(engine, { q, nao: "6", page: "3", ps: 3 }, (page) => {
240+
ids.push(...page.products.map((r: any) => r.product_id));
241+
if (ids.length < 6 && page.next) {
242+
page.next();
243+
} else {
244+
assert(new Set(ids).size > 3, MSG_ASSERT_HAS_MULTIPLE_PAGES);
245+
assert(
246+
ids.some((id) => !idsOnFirstPage.includes(id)),
247+
MSG_ASSERT_HAS_NON_FIRST_PAGE_RESULT,
248+
);
249+
done();
250+
}
251+
});
252+
});
253+
});
254+
});
255+
256+
it("getJson pagination has last page", {
257+
ignore: !HAS_API_KEY,
258+
}, async (t) => {
259+
await t.step("async/await", async () => {
260+
let page;
261+
let pageNum = 0;
262+
page = await getJson(engine, { q, page: "17", ps: 48 });
263+
while (page && pageNum < 5) {
264+
pageNum++;
265+
page = await page.next?.();
266+
}
267+
assert(pageNum < 5, MSG_ASSERT_HAS_LAST_PAGE);
268+
});
269+
270+
await t.step("callback", async () => {
271+
let pageNum = 0;
272+
await new Promise<void>((done) => {
273+
getJson(engine, { q, page: "17", ps: 48 }, (page) => {
274+
pageNum++;
275+
if (pageNum < 5 && page.next) {
276+
page.next();
277+
} else {
278+
assert(pageNum < 5, MSG_ASSERT_HAS_LAST_PAGE);
279+
done();
280+
}
281+
});
282+
});
283+
});
284+
});
285+
});

0 commit comments

Comments
 (0)