Skip to content

Commit fd3b277

Browse files
committed
Page only tests (apple_reviews)
1 parent 11654dc commit fd3b277

1 file changed

Lines changed: 202 additions & 0 deletions

File tree

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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("apple_reviews", {
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 = "apple_reviews";
39+
const productId = "534220544";
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: number[] = [];
62+
let page;
63+
page = await getJson(engine, { product_id: productId });
64+
while (page) {
65+
ids.push(...page.reviews.map((r: any) => r.id));
66+
if (ids.length >= 50) break;
67+
page = await page.next?.();
68+
}
69+
assert(new Set(ids).size > 25, MSG_ASSERT_HAS_MULTIPLE_PAGES);
70+
});
71+
72+
await t.step("callback", async () => {
73+
const ids: number[] = [];
74+
await new Promise<void>((done) => {
75+
getJson(engine, { product_id: productId }, (page) => {
76+
ids.push(...page.reviews.map((r: any) => r.id));
77+
if (ids.length < 50 && page.next) {
78+
page.next();
79+
} else {
80+
assert(new Set(ids).size > 25, 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+
product_id: productId,
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+
product_id: productId,
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 page", {
131+
ignore: !HAS_API_KEY,
132+
}, async (t) => {
133+
const firstPage = await getJson(engine, { product_id: productId });
134+
const idsOnFirstPage = firstPage.reviews.map((r: any) => r.id);
135+
136+
await t.step("async/await", async () => {
137+
const ids: number[] = [];
138+
let page;
139+
page = await getJson(engine, { product_id: productId, page: "2" });
140+
while (page) {
141+
ids.push(...page.reviews.map((r: any) => r.id));
142+
if (ids.length >= 50) break;
143+
page = await page.next?.();
144+
}
145+
146+
assert(new Set(ids).size > 25, MSG_ASSERT_HAS_MULTIPLE_PAGES);
147+
assert(
148+
ids.some((id) => !idsOnFirstPage.includes(id)),
149+
MSG_ASSERT_HAS_NON_FIRST_PAGE_RESULT,
150+
);
151+
});
152+
153+
await t.step("callback", async () => {
154+
const ids: number[] = [];
155+
await new Promise<void>((done) => {
156+
getJson(engine, { product_id: productId, page: "2" }, (page) => {
157+
ids.push(...page.reviews.map((r: any) => r.id));
158+
if (ids.length < 50 && page.next) {
159+
page.next();
160+
} else {
161+
assert(new Set(ids).size > 25, MSG_ASSERT_HAS_MULTIPLE_PAGES);
162+
assert(
163+
ids.some((id) => !idsOnFirstPage.includes(id)),
164+
MSG_ASSERT_HAS_NON_FIRST_PAGE_RESULT,
165+
);
166+
done();
167+
}
168+
});
169+
});
170+
});
171+
});
172+
173+
it("getJson pagination has last page", {
174+
ignore: !HAS_API_KEY,
175+
}, async (t) => {
176+
await t.step("async/await", async () => {
177+
let page;
178+
let pageNum = 0;
179+
page = await getJson(engine, { product_id: productId, page: "99" });
180+
while (page && pageNum < 5) {
181+
pageNum++;
182+
page = await page.next?.();
183+
}
184+
assert(pageNum < 5, MSG_ASSERT_HAS_LAST_PAGE);
185+
});
186+
187+
await t.step("callback", async () => {
188+
let pageNum = 0;
189+
await new Promise<void>((done) => {
190+
getJson(engine, { product_id: productId, page: "99" }, (page) => {
191+
pageNum++;
192+
if (pageNum < 5 && page.next) {
193+
page.next();
194+
} else {
195+
assert(pageNum < 5, MSG_ASSERT_HAS_LAST_PAGE);
196+
done();
197+
}
198+
});
199+
});
200+
});
201+
});
202+
});

0 commit comments

Comments
 (0)