From 467804d9d50ef657f67c465adbd9365426b4e7e7 Mon Sep 17 00:00:00 2001 From: zkldi Date: Tue, 1 Jun 2021 21:43:09 +0100 Subject: [PATCH] basic parser tests --- .../import-types/api/arc-ddr/parser.test.ts | 47 +++++++++++++++++++ .../import-types/api/arc-iidx/parser.test.ts | 47 +++++++++++++++++++ .../api/arc-jubeat/parser.test.ts | 47 +++++++++++++++++++ .../import-types/api/arc-sdvx/parser.test.ts | 47 +++++++++++++++++++ .../common/api-kai/traverse-api.test.ts | 13 +---- server/src/test-utils/misc.ts | 11 +++++ 6 files changed, 200 insertions(+), 12 deletions(-) create mode 100644 server/src/lib/score-import/import-types/api/arc-ddr/parser.test.ts create mode 100644 server/src/lib/score-import/import-types/api/arc-iidx/parser.test.ts create mode 100644 server/src/lib/score-import/import-types/api/arc-jubeat/parser.test.ts create mode 100644 server/src/lib/score-import/import-types/api/arc-sdvx/parser.test.ts create mode 100644 server/src/test-utils/misc.ts diff --git a/server/src/lib/score-import/import-types/api/arc-ddr/parser.test.ts b/server/src/lib/score-import/import-types/api/arc-ddr/parser.test.ts new file mode 100644 index 000000000..63926536f --- /dev/null +++ b/server/src/lib/score-import/import-types/api/arc-ddr/parser.test.ts @@ -0,0 +1,47 @@ +import t from "tap"; +import { CloseAllConnections } from "../../../../../test-utils/close-connections"; +import { agta } from "../../../../../test-utils/misc"; +import { MockJSONFetch } from "../../../../../test-utils/mock-fetch"; +import ResetDBState from "../../../../../test-utils/reset-db-state"; +import CreateLogCtx from "../../../../logger/logger"; +import { ParseArcDDR } from "./parser"; + +const logger = CreateLogCtx(__filename); + +t.test("#ParseArcDDR", (t) => { + t.beforeEach(ResetDBState); + + t.test("Should iterate over the API.", async (t) => { + const mockArcAPI = MockJSONFetch({ + "https://arc.example.com/api/v1/ddr/16/player_bests?profile_id=profile": { + _links: { + _next: + "https://arc.example.com/api/v1/ddr/16/player_bests?profile_id=profile&page=2", + }, + _items: [1, 2, 3], + }, + "https://arc.example.com/api/v1/ddr/16/player_bests?profile_id=profile&page=2": { + _links: { + _next: null, + }, + _items: [4, 5, 6], + }, + }); + + const res = ParseArcDDR("profile", logger, mockArcAPI); + + t.equal(res.game, "ddr"); + t.strictSame(res.context, {}); + t.equal(res.classHandler, null); + + const iter = await agta(res.iterable); + + t.strictSame(iter, [1, 2, 3, 4, 5, 6]); + + t.end(); + }); + + t.end(); +}); + +t.teardown(CloseAllConnections); diff --git a/server/src/lib/score-import/import-types/api/arc-iidx/parser.test.ts b/server/src/lib/score-import/import-types/api/arc-iidx/parser.test.ts new file mode 100644 index 000000000..56529288b --- /dev/null +++ b/server/src/lib/score-import/import-types/api/arc-iidx/parser.test.ts @@ -0,0 +1,47 @@ +import t from "tap"; +import { CloseAllConnections } from "../../../../../test-utils/close-connections"; +import { agta } from "../../../../../test-utils/misc"; +import { MockJSONFetch } from "../../../../../test-utils/mock-fetch"; +import ResetDBState from "../../../../../test-utils/reset-db-state"; +import CreateLogCtx from "../../../../logger/logger"; +import { ParseArcIIDX } from "./parser"; + +const logger = CreateLogCtx(__filename); + +t.test("#ParseArcIIDX", (t) => { + t.beforeEach(ResetDBState); + + t.test("Should iterate over the API.", async (t) => { + const mockArcAPI = MockJSONFetch({ + "https://arc.example.com/api/v1/iidx/27/player_bests?profile_id=profile": { + _links: { + _next: + "https://arc.example.com/api/v1/iidx/27/player_bests?profile_id=profile&page=2", + }, + _items: [1, 2, 3], + }, + "https://arc.example.com/api/v1/iidx/27/player_bests?profile_id=profile&page=2": { + _links: { + _next: null, + }, + _items: [4, 5, 6], + }, + }); + + const res = ParseArcIIDX("profile", logger, mockArcAPI); + + t.equal(res.game, "iidx"); + t.strictSame(res.context, {}); + t.equal(res.classHandler, null); + + const iter = await agta(res.iterable); + + t.strictSame(iter, [1, 2, 3, 4, 5, 6]); + + t.end(); + }); + + t.end(); +}); + +t.teardown(CloseAllConnections); diff --git a/server/src/lib/score-import/import-types/api/arc-jubeat/parser.test.ts b/server/src/lib/score-import/import-types/api/arc-jubeat/parser.test.ts new file mode 100644 index 000000000..ed80f26de --- /dev/null +++ b/server/src/lib/score-import/import-types/api/arc-jubeat/parser.test.ts @@ -0,0 +1,47 @@ +import t from "tap"; +import { CloseAllConnections } from "../../../../../test-utils/close-connections"; +import { agta } from "../../../../../test-utils/misc"; +import { MockJSONFetch } from "../../../../../test-utils/mock-fetch"; +import ResetDBState from "../../../../../test-utils/reset-db-state"; +import CreateLogCtx from "../../../../logger/logger"; +import { ParseArcJubeat } from "./parser"; + +const logger = CreateLogCtx(__filename); + +t.test("#ParseArcJubeat", (t) => { + t.beforeEach(ResetDBState); + + t.test("Should iterate over the API.", async (t) => { + const mockArcAPI = MockJSONFetch({ + "https://arc.example.com/api/v1/jb/8/player_bests?profile_id=profile": { + _links: { + _next: + "https://arc.example.com/api/v1/jb/8/player_bests?profile_id=profile&page=2", + }, + _items: [1, 2, 3], + }, + "https://arc.example.com/api/v1/jb/8/player_bests?profile_id=profile&page=2": { + _links: { + _next: null, + }, + _items: [4, 5, 6], + }, + }); + + const res = ParseArcJubeat("profile", logger, mockArcAPI); + + t.equal(res.game, "jubeat"); + t.strictSame(res.context, {}); + t.equal(res.classHandler, null); + + const iter = await agta(res.iterable); + + t.strictSame(iter, [1, 2, 3, 4, 5, 6]); + + t.end(); + }); + + t.end(); +}); + +t.teardown(CloseAllConnections); diff --git a/server/src/lib/score-import/import-types/api/arc-sdvx/parser.test.ts b/server/src/lib/score-import/import-types/api/arc-sdvx/parser.test.ts new file mode 100644 index 000000000..272aeb22e --- /dev/null +++ b/server/src/lib/score-import/import-types/api/arc-sdvx/parser.test.ts @@ -0,0 +1,47 @@ +import t from "tap"; +import { CloseAllConnections } from "../../../../../test-utils/close-connections"; +import { agta } from "../../../../../test-utils/misc"; +import { MockJSONFetch } from "../../../../../test-utils/mock-fetch"; +import ResetDBState from "../../../../../test-utils/reset-db-state"; +import CreateLogCtx from "../../../../logger/logger"; +import { ParseArcSDVX } from "./parser"; + +const logger = CreateLogCtx(__filename); + +t.test("#ParseArcSDVX", (t) => { + t.beforeEach(ResetDBState); + + t.test("Should iterate over the API.", async (t) => { + const mockArcAPI = MockJSONFetch({ + "https://arc.example.com/api/v1/sdvx/5/player_bests?profile_id=profile": { + _links: { + _next: + "https://arc.example.com/api/v1/sdvx/5/player_bests?profile_id=profile&page=2", + }, + _items: [1, 2, 3], + }, + "https://arc.example.com/api/v1/sdvx/5/player_bests?profile_id=profile&page=2": { + _links: { + _next: null, + }, + _items: [4, 5, 6], + }, + }); + + const res = ParseArcSDVX("profile", logger, mockArcAPI); + + t.equal(res.game, "sdvx"); + t.strictSame(res.context, {}); + t.equal(res.classHandler, null); + + const iter = await agta(res.iterable); + + t.strictSame(iter, [1, 2, 3, 4, 5, 6]); + + t.end(); + }); + + t.end(); +}); + +t.teardown(CloseAllConnections); diff --git a/server/src/lib/score-import/import-types/common/api-kai/traverse-api.test.ts b/server/src/lib/score-import/import-types/common/api-kai/traverse-api.test.ts index 5bd9a5c15..4885b994a 100644 --- a/server/src/lib/score-import/import-types/common/api-kai/traverse-api.test.ts +++ b/server/src/lib/score-import/import-types/common/api-kai/traverse-api.test.ts @@ -1,5 +1,6 @@ import t from "tap"; import { CloseAllConnections } from "../../../../../test-utils/close-connections"; +import { agta } from "../../../../../test-utils/misc"; import { MockJSONFetch } from "../../../../../test-utils/mock-fetch"; import ResetDBState from "../../../../../test-utils/reset-db-state"; import CreateLogCtx from "../../../../logger/logger"; @@ -10,18 +11,6 @@ const logger = CreateLogCtx(__filename); const fakeAuth = "bar"; -/** - * Async Generator To Array - */ -async function agta(ag: AsyncGenerator) { - const a = []; - for await (const el of ag) { - a.push(el); - } - - return a; -} - t.test("#TraverseKaiAPI", (t) => { t.beforeEach(ResetDBState); diff --git a/server/src/test-utils/misc.ts b/server/src/test-utils/misc.ts new file mode 100644 index 000000000..2962aeeb6 --- /dev/null +++ b/server/src/test-utils/misc.ts @@ -0,0 +1,11 @@ +/** + * Async Generator To Array + */ +export async function agta(ag: AsyncIterable) { + const a = []; + for await (const el of ag) { + a.push(el); + } + + return a; +}