From 27a746e6fc9f3f6b81f7ca88d5bdda9d87febb0f Mon Sep 17 00:00:00 2001 From: zkldi Date: Tue, 11 May 2021 21:18:06 +0100 Subject: [PATCH] batch manual test --- server/src/internal-api/import/import.test.ts | 66 ++++++++- .../file/json_batch-manual/parser.test.ts | 126 +++++++++++++++++- 2 files changed, 190 insertions(+), 2 deletions(-) diff --git a/server/src/internal-api/import/import.test.ts b/server/src/internal-api/import/import.test.ts index 6e128bf7c..fca5db1cc 100644 --- a/server/src/internal-api/import/import.test.ts +++ b/server/src/internal-api/import/import.test.ts @@ -40,7 +40,7 @@ t.test("POST /internal-api/import/file", async (t) => { .set("Cookie", cookie) .attach( "scoreData", - GetKTDataBuffer("./csv-eamusement-iidx/small-hv-file.csv"), + GetKTDataBuffer("./csv_eamusement-iidx/small-hv-file.csv"), "my_csv.csv" ) .field("importType", "file/csv:eamusement-iidx") @@ -118,6 +118,70 @@ t.test("POST /internal-api/import/file", async (t) => { t.end(); }); + t.test("file/json:batch-manual", (t) => { + t.test("Empty import", async (t) => { + let res = await mockApi + .post("/internal-api/import/file") + .set("Cookie", cookie) + .attach( + "scoreData", + GetKTDataBuffer("./json_batch-manual/empty-file.json"), + "empty-file.json" + ) + .field("importType", "file/json:batch-manual"); + + t.equal(res.body.success, true, "Should be successful."); + + t.equal(res.body.body.errors.length, 0, "Import Should have 0 failed scores."); + + t.equal(res.body.body.scoreIDs.length, 0, "Should have 0 successful scores."); + + let scoreCount = await db.scores.find({ + scoreID: { $in: res.body.body.scoreIDs }, + }); + + t.equal( + scoreCount.length, + res.body.body.scoreIDs.length, + "All returned scoreIDs should be inserted to the DB." + ); + + t.end(); + }); + + t.test("Single import", async (t) => { + let res = await mockApi + .post("/internal-api/import/file") + .set("Cookie", cookie) + .attach( + "scoreData", + GetKTDataBuffer("./json_batch-manual/small-file.json"), + "small-file.json" + ) + .field("importType", "file/json:batch-manual"); + + t.equal(res.body.success, true, "Should be successful."); + + t.equal(res.body.body.errors.length, 0, "Import Should have 0 failed scores."); + + t.equal(res.body.body.scoreIDs.length, 1, "Should have 1 successful score."); + + let scoreCount = await db.scores.find({ + scoreID: { $in: res.body.body.scoreIDs }, + }); + + t.equal( + scoreCount.length, + res.body.body.scoreIDs.length, + "All returned scoreIDs should be inserted to the DB." + ); + + t.end(); + }); + + t.end(); + }); + t.end(); }); diff --git a/server/src/score-import/import-types/file/json_batch-manual/parser.test.ts b/server/src/score-import/import-types/file/json_batch-manual/parser.test.ts index cf15428a7..ad141e5d6 100644 --- a/server/src/score-import/import-types/file/json_batch-manual/parser.test.ts +++ b/server/src/score-import/import-types/file/json_batch-manual/parser.test.ts @@ -1,6 +1,130 @@ import t from "tap"; +import { CloseMongoConnection } from "../../../../db/db"; +import CreateLogCtx from "../../../../logger"; +import ScoreImportFatalError from "../../../framework/score-importing/score-import-error"; import ParserFn from "./parser"; -t.todo("#ParserFn", (t) => { +const fileify = (obj: any) => + ({ + buffer: Buffer.from(JSON.stringify(obj)), + filename: "fileify.json", + } as Express.Multer.File); + +const logger = CreateLogCtx("parser.test.ts"); + +t.test("#ParserFn", (t) => { + t.test("Non-Object", (t) => { + t.throws( + () => ParserFn(fileify(false), {}, logger), + new ScoreImportFatalError( + 400, + "Invalid BATCH-MANUAL (Not an object, recieved boolean.)" + ), + "Should throw an error." + ); + + t.end(); + }); + + t.test("No Header", (t) => { + t.throws( + () => ParserFn(fileify({ body: [] }), {}, logger), + new ScoreImportFatalError( + 400, + "Could not retrieve head.game - is this valid BATCH-MANUAL?" + ), + "Should throw an error." + ); + + t.end(); + }); + + t.test("No Game", (t) => { + t.throws( + () => ParserFn(fileify({ body: [], head: { service: "foo" } }), {}, logger), + new ScoreImportFatalError( + 400, + "Could not retrieve head.game - is this valid BATCH-MANUAL?" + ), + "Should throw an error." + ); + + t.end(); + }); + + t.test("Invalid Game", (t) => { + t.throws( + () => + ParserFn( + fileify({ body: [], head: { service: "foo", game: "invalid_game" } }), + {}, + logger + ), + new ScoreImportFatalError( + 400, + "Invalid game invalid_game - expected any of iidx, museca, maimai, jubeat, popn, sdvx, ddr, bms, chunithm, gitadora, usc" + ), + "Should throw an error." + ); + + t.throws( + () => ParserFn(fileify({ body: [], head: { service: "foo", game: 123 } }), {}, logger), + new ScoreImportFatalError( + 400, + "Invalid game 123 - expected any of iidx, museca, maimai, jubeat, popn, sdvx, ddr, bms, chunithm, gitadora, usc" + ), + "Should throw an error." + ); + + t.end(); + }); + + t.test("Invalid Service", (t) => { + t.throws( + () => ParserFn(fileify({ body: [], head: { service: "1", game: "iidx" } }), {}, logger), + new ScoreImportFatalError( + 400, + "Invalid BATCH-MANUAL (head.service | Expected a string with length between 3 and 15. | Received 1)" + ), + "Should throw an error." + ); + + t.throws( + () => ParserFn(fileify({ body: [], head: { service: 1, game: "iidx" } }), {}, logger), + new ScoreImportFatalError( + 400, + "Invalid BATCH-MANUAL (head.service | Expected a string with length between 3 and 15. | Received 1)" + ), + "Should throw an error." + ); + + t.end(); + }); + + t.test("Valid Empty BATCH-MANUAL", (t) => { + let res = ParserFn( + fileify({ body: [], head: { service: "foo", game: "iidx" } }), + {}, + logger + ); + + t.hasStrict(res, { + // @ts-expect-error asdf + game: "iidx", + context: { + // @ts-expect-error asdf + service: "foo", + game: "iidx", + version: null, + }, + // @ts-expect-error asdf + iterable: [], + }); + + t.end(); + }); + t.end(); }); + +t.teardown(CloseMongoConnection);