Skip to content

Commit

Permalink
moved manyFromDb and manyFromApi data model methods into aggregators
Browse files Browse the repository at this point in the history
# removed constructor from steam apps aggregate, added static manyFrom methods
# changed steam client to use new aggregator method for instantiation
# used recent mocks to mimic db calls
# removed old small and smallest data set assets files
# adjusted tests to reflect new code
  • Loading branch information
lukatarman committed May 17, 2024
1 parent d3e4a31 commit 32c02e3
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 2,734 deletions.
1,690 changes: 0 additions & 1,690 deletions backend/assets/small.data.set.js

This file was deleted.

871 changes: 0 additions & 871 deletions backend/assets/smallest.data.set.js

This file was deleted.

4 changes: 2 additions & 2 deletions backend/src/adapters/driven/http/steam.client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SteamApp } from "../../../core/models/steam.app.js";
import { SteamAppsAggregate } from "../../../core/models/steam.apps.aggregate.js";

export class SteamClient {
#httpClient;
Expand All @@ -12,7 +12,7 @@ export class SteamClient {
const options = { params: { key: "79E04F52C6B5AD21266624C05CC12E42" } };
const response = await this.#httpClient.get(url, options);

return SteamApp.manyFromSteamApi(response.data.applist.apps);
return SteamAppsAggregate.manyFromSteamApi(response.data.applist.apps);
}

async getCurrentPlayers(game) {
Expand Down
20 changes: 13 additions & 7 deletions backend/src/core/features/game-identifier/game.identifier.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("game.identifier.js", function () {
this.steamClient = createSteamMock([]);
this.steamAppsRepository = createSteamAppsRepositoryMock(
[],
new SteamAppsAggregate([]),
SteamAppsAggregate.manyFromDbEntries([]),
);
this.gamesRepository = createGamesRepositoryMock([]);
this.historyChecksRepository = createHistoryChecksRepositoryMock();
Expand Down Expand Up @@ -62,7 +62,7 @@ describe("game.identifier.js", function () {

describe("Finds two unidentified steam apps in the database, none of them being games", function () {
beforeAll(async function () {
this.steamApps = new SteamAppsAggregate(getXSampleSteamApps(2));
this.steamApps = SteamAppsAggregate.manyFromDbEntries(getXSampleSteamApps(2));

const htmlDetailsPages = [
gta5ageRestrictedHtmlDetailsPage,
Expand Down Expand Up @@ -117,7 +117,7 @@ describe("game.identifier.js", function () {

describe("Finds two unidentified steam apps in the database, one of them being a game", function () {
beforeAll(async function () {
this.steamApps = new SteamAppsAggregate(getXSampleSteamApps(2));
this.steamApps = SteamAppsAggregate.manyFromDbEntries(getXSampleSteamApps(2));

const htmlDetailsPages = [
mortalDarknessGameHtmlDetailsPage,
Expand Down Expand Up @@ -178,7 +178,7 @@ describe("game.identifier.js", function () {
this.steamAppsRepository = createSteamAppsRepositoryMock(
[],
[],
new SteamAppsAggregate([]),
SteamAppsAggregate.manyFromDbEntries([]),
);
this.gamesRepository = createGamesRepositoryMock([]);
this.historyChecksRepository = createHistoryChecksRepositoryMock();
Expand Down Expand Up @@ -219,7 +219,9 @@ describe("game.identifier.js", function () {
beforeAll(async function () {
const gameIds = [468060, 1607890];

this.steamApps = new SteamAppsAggregate(getXSampleSteamApps(2, gameIds));
this.steamApps = SteamAppsAggregate.manyFromDbEntries(
getXSampleSteamApps(2, gameIds),
);

const steamApiApps = [padakVideoSteamApiData, riskOfRainTwoDlcSteamApiData];

Expand Down Expand Up @@ -272,7 +274,9 @@ describe("game.identifier.js", function () {
beforeAll(async function () {
const gameIds = [1245620, 468060];

this.steamApps = new SteamAppsAggregate(getXSampleSteamApps(2, gameIds));
this.steamApps = SteamAppsAggregate.manyFromDbEntries(
getXSampleSteamApps(2, gameIds),
);

const steamApiApps = [eldenRingSteamApiData, padakVideoSteamApiData];

Expand Down Expand Up @@ -362,7 +366,9 @@ describe("game.identifier.js", function () {

this.games = new GamesAggregate(getXGamesWithoutDetails(2, appIds));

this.steamApps = new SteamAppsAggregate(getXSampleSteamApps(2, appIds));
this.steamApps = SteamAppsAggregate.manyFromDbEntries(
getXSampleSteamApps(2, appIds),
);

const steamApiApps = [eldenRingSteamApiData, counterStrikeSteamApiData];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class SteamAppsAggregator {

const steamApps = await this.#steamClient.getAppList();

await this.#steamAppsRepository.insertManySteamApps(steamApps);
await this.#steamAppsRepository.insertManySteamApps(steamApps.content);
await this.#steamAppsUpdateTimestampsRepository.insertOneSteamAppsUpdateTimestamp(
new Date(),
);
Expand All @@ -54,7 +54,7 @@ export class SteamAppsAggregator {
/**
* @TODO https://github.com/lukatarman/steam-game-stats/issues/32
*/
const steamApps = SteamApp.diff(steamAppsApi, steamAppsDb);
const steamApps = SteamApp.diff(steamAppsApi.content, steamAppsDb.content);
if (steamApps.length === 0) {
await this.#steamAppsUpdateTimestampsRepository.insertOneSteamAppsUpdateTimestamp(
new Date(),
Expand Down Expand Up @@ -83,7 +83,7 @@ export class SteamAppsAggregator {
async #collectAndPersist() {
this.#logger.debugc("collecting steam apps");
const steamApps = await this.#steamClient.getAppList();
await this.#steamAppsRepository.insertManyIfNotExist(steamApps);
await this.#steamAppsRepository.insertManyIfNotExist(steamApps.content);
await this.#steamAppsUpdateTimestampsRepository.insertOneSteamAppsUpdateTimestamp(
new Date(),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SteamAppsAggregator } from "./steam.apps.aggregator.js";
import { smallestGamesMock } from "../../../../assets/smallest.data.set.js";
import { gamesMock } from "../../../../assets/small.data.set.js";
import { hoursToMs } from "../../../common/time.utils.js";
import { SteamApp } from "../../models/steam.app.js";
import { createLoggerMock } from "../../../common/logger.mock.js";
import { SteamAppsAggregate } from "../../models/steam.apps.aggregate.js";
import { getXSampleSteamAppsMarkedAsNotGames } from "../../models/steam.app.mocks.js";

describe("SteamAppsAggregator", function () {
const baseTime = new Date(2023, 9, 23);
Expand All @@ -18,9 +18,12 @@ describe("SteamAppsAggregator", function () {

this.updateTimestampsRepo = createSteamAppsUpdateTimestampsRepositoryMock(null);
this.steamAppsRepo = createSteamAppsRepositoryMock(undefined);
this.steamApps = SteamAppsAggregate.manyFromDbEntries(
getXSampleSteamAppsMarkedAsNotGames(2),
);

await new SteamAppsAggregator(
createSteamMock(smallestGamesMock),
createSteamMock(this.steamApps),
this.updateTimestampsRepo,
this.steamAppsRepo,
createLoggerMock(),
Expand All @@ -34,7 +37,7 @@ describe("SteamAppsAggregator", function () {

it("then the recieved steam apps are stored to the database", function () {
expect(this.steamAppsRepo.insertManySteamApps).toHaveBeenCalledOnceWith(
smallestGamesMock,
this.steamApps.content,
);
});

Expand All @@ -55,12 +58,20 @@ describe("SteamAppsAggregator", function () {
jasmine.clock().install();
jasmine.clock().mockDate(baseTime);

this.steamApps = SteamAppsAggregate.manyFromDbEntries(
getXSampleSteamAppsMarkedAsNotGames(2),
);

this.moreSteamApps = SteamAppsAggregate.manyFromDbEntries(
getXSampleSteamAppsMarkedAsNotGames(4),
);

this.updateTimestampsRepo =
createSteamAppsUpdateTimestampsRepositoryMock(updateTimestamp);
this.steamAppsRepo = createSteamAppsRepositoryMock(smallestGamesMock);
this.steamAppsRepo = createSteamAppsRepositoryMock(this.steamApps);

const agg = new SteamAppsAggregator(
createSteamMock(gamesMock),
createSteamMock(this.moreSteamApps),
this.updateTimestampsRepo,
this.steamAppsRepo,
createLoggerMock(),
Expand All @@ -69,7 +80,10 @@ describe("SteamAppsAggregator", function () {
},
);

this.steamAppsDifference = SteamApp.diff(gamesMock, smallestGamesMock);
this.steamAppsDifference = SteamApp.diff(
this.moreSteamApps.content,
this.steamApps.content,
);

await agg.collectSteamApps();
});
Expand Down Expand Up @@ -98,12 +112,16 @@ describe("SteamAppsAggregator", function () {
jasmine.clock().install();
jasmine.clock().mockDate(baseTime);

this.steamApps = SteamAppsAggregate.manyFromDbEntries(
getXSampleSteamAppsMarkedAsNotGames(2),
);

this.updateTimestampsRepo =
createSteamAppsUpdateTimestampsRepositoryMock(updateTimestamp);
this.steamAppsRepo = createSteamAppsRepositoryMock(smallestGamesMock);
this.steamAppsRepo = createSteamAppsRepositoryMock(this.steamApps);

await new SteamAppsAggregator(
createSteamMock(smallestGamesMock),
createSteamMock(this.steamApps),
this.updateTimestampsRepo,
this.steamAppsRepo,
createLoggerMock(),
Expand Down Expand Up @@ -136,10 +154,14 @@ describe("SteamAppsAggregator", function () {
jasmine.clock().install();
jasmine.clock().mockDate(baseTime);

this.steamClient = createSteamMock(smallestGamesMock);
this.steamApps = SteamAppsAggregate.manyFromDbEntries(
getXSampleSteamAppsMarkedAsNotGames(4),
);

this.steamClient = createSteamMock(this.steamApps);
this.updateTimestampsRepo =
createSteamAppsUpdateTimestampsRepositoryMock(updateTimestamp);
this.steamAppsRepo = createSteamAppsRepositoryMock(smallestGamesMock);
this.steamAppsRepo = createSteamAppsRepositoryMock(this.steamApps);

await new SteamAppsAggregator(
this.steamClient,
Expand Down Expand Up @@ -174,12 +196,19 @@ describe("SteamAppsAggregator", function () {
jasmine.clock().install();
jasmine.clock().mockDate(baseTime);

this.steamApps = SteamAppsAggregate.manyFromDbEntries(
getXSampleSteamAppsMarkedAsNotGames(4),
);

this.updateTimestampsRepo =
createSteamAppsUpdateTimestampsRepositoryMock(updateTimestamp);
this.steamAppsRepo = createSteamAppsRepositoryMock(smallestGamesMock);
this.steamAppsRepo = createSteamAppsRepositoryMock(this.steamApps);
this.steamApps = SteamAppsAggregate.manyFromDbEntries(
getXSampleSteamAppsMarkedAsNotGames(3),
);

await new SteamAppsAggregator(
createSteamMock(gamesMock),
createSteamMock(this.steamApps),
this.updateTimestampsRepo,
this.steamAppsRepo,
createLoggerMock(),
Expand All @@ -195,7 +224,7 @@ describe("SteamAppsAggregator", function () {

it("then the new steam apps are stored in the database and the existing steam apps are left unchanged", function () {
expect(this.steamAppsRepo.insertManyIfNotExist).toHaveBeenCalledOnceWith(
gamesMock,
this.steamApps.content,
);
});

Expand Down
21 changes: 9 additions & 12 deletions backend/src/core/models/game.mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,15 @@ export const getOneGameWithDetails = () => {
};

export const getOneGameWithPlayerHistory = () => {
const dbEntry = [
{
id: 239140,
name: "Dying Light",
releaseDate: "21.09.1989",
developers: ["Techland"],
genres: ["Action", "RPG"],
description: "Best game",
playerHistory: getSamplePlayerHistory(),
},
];

const dbEntry = {
id: 239140,
name: "Dying Light",
releaseDate: "21.09.1989",
developers: ["Techland"],
genres: ["Action", "RPG"],
description: "Best game",
playerHistory: getSamplePlayerHistory(),
};
return Game.fromDbEntry(dbEntry);
};

Expand Down
8 changes: 0 additions & 8 deletions backend/src/core/models/steam.app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ export class SteamApp {
return this.type === SteamApp.validTypes.game;
}

static manyFromSteamApi(apps) {
return apps.map((app) => SteamApp.oneFromSteamApi(app));
}

// prettier-ignore
static oneFromSteamApi(data) {
const steamApp = new SteamApp();
Expand All @@ -51,10 +47,6 @@ export class SteamApp {
return steamApp;
}

static manyFromDbEntries(dbEntries) {
return dbEntries.map((dbEntry) => SteamApp.oneFromDbEntry(dbEntry));
}

// prettier-ignore
static oneFromDbEntry(dbEntry) {
const steamApp = new SteamApp();
Expand Down
Loading

0 comments on commit 32c02e3

Please sign in to comment.