Skip to content

Commit 4e4fcd2

Browse files
committed
Token only tests (google_scholar_profiles)
1 parent ff773c9 commit 4e4fcd2

1 file changed

Lines changed: 203 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)