Skip to content

Commit

Permalink
added steamAppsAggregate data model
Browse files Browse the repository at this point in the history
# moved steamAppsIsEmpty functionality to it
# added instantiation to steamApps repo get method
# added tests
  • Loading branch information
lukatarman committed Feb 15, 2024
1 parent 71bd5d8 commit 0fbe661
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 65 deletions.
14 changes: 2 additions & 12 deletions backend/src/core/features/game-identifier/game.identifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,13 @@ export class GameIdentifier {
source,
);

if (this.#steamAppsIsEmpty(steamApps)) return;
if (steamApps.checkIfEmpty(this.#options.globalIterationDelay)) return;

const [games, updatedSteamApps] = await this.#identifyTypes(steamApps, source);
const [games, updatedSteamApps] = await this.#identifyTypes(steamApps.apps, source);

await this.#persistGameCheckUpdates(games, updatedSteamApps);
};

#steamAppsIsEmpty(steamApps) {
if (steamApps.length > 0) return false;

this.#logger.debugc(
`no steam apps in db, retry in: ${this.#options.globalIterationDelay} ms`,
);

return true;
}

async #identifyTypes(steamApps, source) {
const htmlDetailsPages = await this.#getSteamAppsHtmlDetailsPages(steamApps, source);

Expand Down
19 changes: 13 additions & 6 deletions backend/src/core/features/game-identifier/game.identifier.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { gta5ageRestrictedHtmlDetailsPage } from "../../../../assets/steam-detai
import { theSims4dlcHtmlDetailsPage } from "../../../../assets/steam-details-pages/the.sims.4.dlc.html.details.page.js";
import { createHtmlDetailsPages } from "../../../../assets/html.details.pages.mock.js";
import { mortalDarknessGameHtmlDetailsPage } from "../../../../assets/steam-details-pages/mortal.darkness.game.html.details.page.js";
import { SteamAppsAggregate } from "../../models/steam.apps.aggregate.js";

describe("game.identifier.js", function () {
describe(".tryIfGameViaSource.", function () {
Expand All @@ -27,7 +28,10 @@ describe("game.identifier.js", function () {
beforeAll(async function () {
this.source = ValidDataSources.validDataSources.steamWeb;
this.steamClient = createSteamMock([]);
this.steamAppsRepository = createSteamAppsRepositoryMock([], []);
this.steamAppsRepository = createSteamAppsRepositoryMock(
[],
SteamAppsAggregate.manyFromDbEntries([], createLoggerMock()),
);
this.gamesRepository = createGamesRepositoryMock([]);
this.historyChecksRepository = createHistoryChecksRepositoryMock();

Expand Down Expand Up @@ -102,7 +106,7 @@ describe("game.identifier.js", function () {
this.steamClient = createSteamMock(this.htmlDetailsPages);
this.steamAppsRepository = createSteamAppsRepositoryMock(
undefined,
this.steamApps,
SteamAppsAggregate.manyFromDbEntries(this.steamApps, createLoggerMock()),
);
this.gamesRepository = createGamesRepositoryMock();
this.historyChecksRepository = createHistoryChecksRepositoryMock();
Expand Down Expand Up @@ -195,7 +199,7 @@ describe("game.identifier.js", function () {
this.steamClient = createSteamMock(this.htmlDetailsPages);
this.steamAppsRepository = createSteamAppsRepositoryMock(
undefined,
this.steamApps,
SteamAppsAggregate.manyFromDbEntries(this.steamApps, createLoggerMock()),
);
this.gamesRepository = createGamesRepositoryMock();
this.historyChecksRepository = createHistoryChecksRepositoryMock();
Expand Down Expand Up @@ -276,7 +280,10 @@ describe("game.identifier.js", function () {
beforeAll(async function () {
this.source = ValidDataSources.validDataSources.steamcharts;
this.steamClient = createSteamMock([]);
this.steamAppsRepository = createSteamAppsRepositoryMock([], []);
this.steamAppsRepository = createSteamAppsRepositoryMock(
[],
SteamAppsAggregate.manyFromDbEntries([], createLoggerMock()),
);
this.gamesRepository = createGamesRepositoryMock([]);
this.historyChecksRepository = createHistoryChecksRepositoryMock();

Expand Down Expand Up @@ -348,7 +355,7 @@ describe("game.identifier.js", function () {
this.steamClient = createSteamMock(this.htmlDetailsPages);
this.steamAppsRepository = createSteamAppsRepositoryMock(
undefined,
this.steamApps,
SteamAppsAggregate.manyFromDbEntries(this.steamApps, createLoggerMock()),
);
this.gamesRepository = createGamesRepositoryMock();
this.historyChecksRepository = createHistoryChecksRepositoryMock();
Expand Down Expand Up @@ -438,7 +445,7 @@ describe("game.identifier.js", function () {
this.steamClient = createSteamMock(this.htmlDetailsPages);
this.steamAppsRepository = createSteamAppsRepositoryMock(
undefined,
this.steamApps,
SteamAppsAggregate.manyFromDbEntries(this.steamApps, createLoggerMock()),
);
this.gamesRepository = createGamesRepositoryMock();
this.historyChecksRepository = createHistoryChecksRepositoryMock();
Expand Down
22 changes: 22 additions & 0 deletions backend/src/core/models/steam.apps.aggregate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { SteamApp } from "./steam.app.js";

export class SteamAppsAggregate {
apps;
logger;

static manyFromDbEntries(apps, logger) {
const steamAppsAggregate = new SteamAppsAggregate();
steamAppsAggregate.apps = apps.map((app) => SteamApp.oneFromDbEntry(app));
steamAppsAggregate.logger = logger;

return steamAppsAggregate;
}

checkIfEmpty(delay) {
if (this.apps.length > 0) return false;

this.logger.debugc(`no steam apps in db, retry in: ${delay} ms`);

return true;
}
}
5 changes: 3 additions & 2 deletions backend/src/core/repositories/steam.apps.repository.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { SteamApp } from "../models/steam.app.js";
import { ValidDataSources } from "../models/valid.data.sources.js";
import { SteamAppsAggregate } from "../models/steam.apps.aggregate.js";

export class SteamAppsRepository {
#dbClient;
#logger;
#steamAppsCollection;

constructor(dbClient) {
Expand Down Expand Up @@ -57,7 +58,7 @@ export class SteamAppsRepository {
.limit(amount)
.toArray();

return SteamApp.manyFromDbEntries(response);
return SteamAppsAggregate.manyFromDbEntries(response, this.#logger);
}

async getSteamAppsById(ids) {
Expand Down
107 changes: 62 additions & 45 deletions backend/src/core/repositories/steam.apps.repository.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import { ValidDataSources } from "../models/valid.data.sources.js";
import { initiateInMemoryDatabase } from "../../adapters/driven/db/in.memory.database.client.js";
import { SteamAppsRepository } from "./steam.apps.repository.js";
import { SteamAppsAggregate } from "../models/steam.apps.aggregate.js";

describe("SteamAppsRepository", function () {
describe(".insertManyIfNotExist inserts all the non existing steam apps from a given array", function () {
Expand Down Expand Up @@ -210,7 +211,7 @@ describe("SteamAppsRepository", function () {
});
});

describe(".getSourceUntriedFilteredSteamApps.", function () {
fdescribe(".getSourceUntriedFilteredSteamApps.", function () {
describe("via SteamWeb source", function () {
describe("If three steam apps out of eigth match the filters,", function () {
describe("and the amount of 2 is provided", function () {
Expand All @@ -236,28 +237,32 @@ describe("SteamAppsRepository", function () {
this.databaseClient.disconnect();
});

it("the result is an instance of SteamAppsAggregate", function () {
expect(this.result).toBeInstanceOf(SteamAppsAggregate);
});

it("the result has two games", function () {
expect(this.result.length).toBe(2);
expect(this.result.apps.length).toBe(2);
});

it("the first result is an instance of SteamApp", function () {
expect(this.result[0]).toBeInstanceOf(SteamApp);
expect(this.result.apps[0]).toBeInstanceOf(SteamApp);
});

it("the first array has the correct values", function () {
expect(this.result[0].appid).toBe(2);
expect(this.result[0].type).toBe("unknown");
expect(this.result[0].triedVia).toEqual([]);
expect(this.result.apps[0].appid).toBe(2);
expect(this.result.apps[0].type).toBe("unknown");
expect(this.result.apps[0].triedVia).toEqual([]);
});

it("the second result is an instance of SteamApp", function () {
expect(this.result[1]).toBeInstanceOf(SteamApp);
expect(this.result.apps[1]).toBeInstanceOf(SteamApp);
});

it("the second array has the correct values", function () {
expect(this.result[1].appid).toBe(4);
expect(this.result[1].type).toBe("unknown");
expect(this.result[1].triedVia).toEqual([]);
expect(this.result.apps[1].appid).toBe(4);
expect(this.result.apps[1].type).toBe("unknown");
expect(this.result.apps[1].triedVia).toEqual([]);
});
});

Expand All @@ -284,38 +289,42 @@ describe("SteamAppsRepository", function () {
this.databaseClient.disconnect();
});

it("the result is an instance of SteamAppsAggregate", function () {
expect(this.result).toBeInstanceOf(SteamAppsAggregate);
});

it("the resulting array has a length of 3", function () {
expect(this.result.length).toBe(3);
expect(this.result.apps.length).toBe(3);
});

it("the first result is an instance of SteamApp", function () {
expect(this.result[0]).toBeInstanceOf(SteamApp);
expect(this.result.apps[0]).toBeInstanceOf(SteamApp);
});

it("the first array has the correct values", function () {
expect(this.result[0].appid).toBe(2);
expect(this.result[0].type).toBe("unknown");
expect(this.result[0].triedVia).toEqual([]);
expect(this.result.apps[0].appid).toBe(2);
expect(this.result.apps[0].type).toBe("unknown");
expect(this.result.apps[0].triedVia).toEqual([]);
});

it("the second result is an instance of SteamApp", function () {
expect(this.result[1]).toBeInstanceOf(SteamApp);
expect(this.result.apps[1]).toBeInstanceOf(SteamApp);
});

it("the second array has the correct values", function () {
expect(this.result[1].appid).toBe(4);
expect(this.result[1].type).toBe("unknown");
expect(this.result[1].triedVia).toEqual([]);
expect(this.result.apps[1].appid).toBe(4);
expect(this.result.apps[1].type).toBe("unknown");
expect(this.result.apps[1].triedVia).toEqual([]);
});

it("the third result is an instance of SteamApp", function () {
expect(this.result[2]).toBeInstanceOf(SteamApp);
expect(this.result.apps[2]).toBeInstanceOf(SteamApp);
});

it("the third array has the correct values", function () {
expect(this.result[2].appid).toBe(7);
expect(this.result[2].type).toBe("unknown");
expect(this.result[2].triedVia).toEqual([]);
expect(this.result.apps[2].appid).toBe(7);
expect(this.result.apps[2].type).toBe("unknown");
expect(this.result.apps[2].triedVia).toEqual([]);
});
});
});
Expand Down Expand Up @@ -346,28 +355,32 @@ describe("SteamAppsRepository", function () {
this.databaseClient.disconnect();
});

it("the result is an instance of SteamAppsAggregate", function () {
expect(this.result).toBeInstanceOf(SteamAppsAggregate);
});

it("the result has two games", function () {
expect(this.result.length).toBe(2);
expect(this.result.apps.length).toBe(2);
});

it("the first result is an instance of SteamApp", function () {
expect(this.result[0]).toBeInstanceOf(SteamApp);
expect(this.result.apps[0]).toBeInstanceOf(SteamApp);
});

it("the first array has the correct values", function () {
expect(this.result[0].appid).toBe(2);
expect(this.result[0].type).toBe("unknown");
expect(this.result[0].triedVia).toEqual([]);
expect(this.result.apps[0].appid).toBe(2);
expect(this.result.apps[0].type).toBe("unknown");
expect(this.result.apps[0].triedVia).toEqual([]);
});

it("the second result is an instance of SteamApp", function () {
expect(this.result[1]).toBeInstanceOf(SteamApp);
expect(this.result.apps[1]).toBeInstanceOf(SteamApp);
});

it("the second array has the correct values", function () {
expect(this.result[1].appid).toBe(4);
expect(this.result[1].type).toBe("unknown");
expect(this.result[1].triedVia).toEqual([]);
expect(this.result.apps[1].appid).toBe(4);
expect(this.result.apps[1].type).toBe("unknown");
expect(this.result.apps[1].triedVia).toEqual([]);
});
});

Expand All @@ -394,38 +407,42 @@ describe("SteamAppsRepository", function () {
this.databaseClient.disconnect();
});

it("the result is an instance of SteamAppsAggregate", function () {
expect(this.result).toBeInstanceOf(SteamAppsAggregate);
});

it("the resulting array has a length of 3", function () {
expect(this.result.length).toBe(3);
expect(this.result.apps.length).toBe(3);
});

it("the first result is an instance of SteamApp", function () {
expect(this.result[0]).toBeInstanceOf(SteamApp);
expect(this.result.apps[0]).toBeInstanceOf(SteamApp);
});

it("the first array has the correct values", function () {
expect(this.result[0].appid).toBe(2);
expect(this.result[0].type).toBe("unknown");
expect(this.result[0].triedVia).toEqual([]);
expect(this.result.apps[0].appid).toBe(2);
expect(this.result.apps[0].type).toBe("unknown");
expect(this.result.apps[0].triedVia).toEqual([]);
});

it("the second result is an instance of SteamApp", function () {
expect(this.result[1]).toBeInstanceOf(SteamApp);
expect(this.result.apps[1]).toBeInstanceOf(SteamApp);
});

it("the second array has the correct values", function () {
expect(this.result[1].appid).toBe(4);
expect(this.result[1].type).toBe("unknown");
expect(this.result[1].triedVia).toEqual([]);
expect(this.result.apps[1].appid).toBe(4);
expect(this.result.apps[1].type).toBe("unknown");
expect(this.result.apps[1].triedVia).toEqual([]);
});

it("the third result is an instance of SteamApp", function () {
expect(this.result[2]).toBeInstanceOf(SteamApp);
expect(this.result.apps[2]).toBeInstanceOf(SteamApp);
});

it("the third array has the correct values", function () {
expect(this.result[2].appid).toBe(7);
expect(this.result[2].type).toBe("unknown");
expect(this.result[2].triedVia).toEqual([]);
expect(this.result.apps[2].appid).toBe(7);
expect(this.result.apps[2].type).toBe("unknown");
expect(this.result.apps[2].triedVia).toEqual([]);
});
});
});
Expand Down

0 comments on commit 0fbe661

Please sign in to comment.