-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/add tests for statistic keeper module 406 #569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MikhailDeriabin
merged 8 commits into
dev
from
feature/add-tests-for-statisticKeeper-module-406
May 31, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dfd609c
Add StatisticsKeeper test module.
jkrizsan a87ae38
Implement the PlayerStatisticService.updatePlayerStatistic() test suite
jkrizsan 416f014
fix bug in PlayerStatisticService.trackPlayerMessageCount() handling …
MikhailDeriabin 2b49810
Remove unnecessary code.
jkrizsan 78d3ad1
Refactor PlayerStatisticService.updatePlayerStatistic() tests
jkrizsan 5930f0b
PlayerStatisticService.updatePlayerStatistic() tests: additional refa…
jkrizsan a54a7ff
move `getPlayerService()` back to `StatisticsKeeperModule`
MikhailDeriabin d472288
move player object creation to `beforeEach` hook in PlayerStatisticSe…
MikhailDeriabin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
205 changes: 205 additions & 0 deletions
205
src/__tests__/statisticsKeeper/PlayerStatisticService/updatePlayerStatistic.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| import { ObjectId } from 'mongodb'; | ||
| import { MongooseError } from 'mongoose'; | ||
| import { Message } from '../../../player/message.schema'; | ||
| import { ModelName } from '../../../common/enum/modelName.enum'; | ||
| import PlayerBuilderFactory from '../../player/data/playerBuilderFactory'; | ||
| import { PlayerEvent } from '../../../rewarder/playerRewarder/enum/PlayerEvent.enum'; | ||
| import PlayerModule from '../../player/modules/player.module'; | ||
| import { PlayerService } from '../../../player/player.service'; | ||
| import { PlayerStatisticService } from '../../../statisticsKeeper/playerStatisticKeeper/playerStatisticKeeper.service'; | ||
| import StatisticsKeeperModule from '../modules/statisticsKeeper.module'; | ||
| import { Player } from '../../../player/schemas/player.schema'; | ||
|
|
||
| describe('PlayerStatisticService.updatePlayerStatistic() test suite', () => { | ||
| let playerStatisticService: PlayerStatisticService; | ||
| let playerService: PlayerService; | ||
|
|
||
| const playerModel = PlayerModule.getPlayerModel(); | ||
|
|
||
| const playerBuilder = PlayerBuilderFactory.getBuilder('Player'); | ||
| const gameStatisticsBuilder = | ||
| PlayerBuilderFactory.getBuilder('GameStatistics'); | ||
|
|
||
| const playerName = 'John'; | ||
|
|
||
| const playerId = new ObjectId()._id.toString(); | ||
|
|
||
| const gameStatistics = gameStatisticsBuilder.setWonBattles(0).build(); | ||
|
|
||
| let player: Player; | ||
|
|
||
| beforeEach(async () => { | ||
| playerStatisticService = | ||
| await StatisticsKeeperModule.getPlayerStatisticService(); | ||
|
|
||
| playerService = await StatisticsKeeperModule.getPlayerService(); | ||
|
|
||
| player = playerBuilder | ||
| .setName(playerName) | ||
| .setId(playerId) | ||
| .setGameStatistics(gameStatistics) | ||
| .build(); | ||
| await playerModel.create(player); | ||
| }); | ||
|
|
||
| it('Should increase the players playedBattles if the input is valid | PlayerEvent.BATTLE_PLAYED', async () => { | ||
| const [result, error] = await playerStatisticService.updatePlayerStatistic( | ||
| playerId, | ||
| PlayerEvent.BATTLE_PLAYED, | ||
| ); | ||
|
|
||
| const updatedPlayer = await playerModel.findById(playerId); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(error).toBeNull(); | ||
| expect(updatedPlayer).toBeDefined(); | ||
| expect(updatedPlayer?.gameStatistics.playedBattles).toBe(1); | ||
| expect(updatedPlayer?.name).toBe(playerName); | ||
| }); | ||
|
|
||
| it('Should increase the players wonBattles if the input is valid | PlayerEvent.BATTLE_WON', async () => { | ||
| const [result, error] = await playerStatisticService.updatePlayerStatistic( | ||
| playerId, | ||
| PlayerEvent.BATTLE_WON, | ||
| ); | ||
|
|
||
| const updatedPlayer = await playerModel.findById(playerId); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(error).toBeNull(); | ||
| expect(updatedPlayer).toBeDefined(); | ||
| expect(updatedPlayer?.gameStatistics.wonBattles).toBe(1); | ||
| expect(updatedPlayer?.name).toBe(playerName); | ||
| }); | ||
|
|
||
| it('Should increase the players participatedVotings if the input is valid | PlayerEvent.VOTE_MADE', async () => { | ||
| const [result, error] = await playerStatisticService.updatePlayerStatistic( | ||
| playerId, | ||
| PlayerEvent.VOTE_MADE, | ||
| ); | ||
|
|
||
| const updatedPlayer = await playerModel.findById(playerId); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(error).toBeNull(); | ||
| expect(updatedPlayer).toBeDefined(); | ||
| expect(updatedPlayer?.gameStatistics.participatedVotings).toBe(1); | ||
| expect(updatedPlayer?.name).toBe(playerName); | ||
| }); | ||
|
|
||
| it('Should return with ServiceError if have not read the player from the DB | PlayerEvent.MESSAGE_SENT', async () => { | ||
| await playerModel.findByIdAndDelete(playerId); | ||
|
|
||
| const [result, error] = await playerStatisticService.updatePlayerStatistic( | ||
| playerId, | ||
| PlayerEvent.MESSAGE_SENT, | ||
| ); | ||
|
|
||
| expect(result).toBe(false); | ||
| expect(error).toContainSE_NOT_FOUND(); | ||
|
|
||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('Should return with ServiceError if the players metadata not valid | PlayerEvent.MESSAGE_SENT', async () => { | ||
| jest.spyOn(playerService, 'readOneById').mockImplementation(async () => { | ||
| return { | ||
| data: { [ModelName.BOX]: null }, | ||
| metaData: { | ||
| dataType: 'Player', | ||
| }, | ||
| meta: { dataKey: ModelName.PLAYER }, | ||
| } as any; | ||
| }); | ||
|
|
||
| const [result, error] = await playerStatisticService.updatePlayerStatistic( | ||
| playerId, | ||
| PlayerEvent.MESSAGE_SENT, | ||
| ); | ||
|
|
||
| expect(result).toBe(false); | ||
| expect(error).toContainSE_NOT_FOUND(); | ||
|
|
||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it("Should increase the players message counter if found a today's message | PlayerEvent.MESSAGE_SENT", async () => { | ||
| const player_Id = new ObjectId()._id.toString(); | ||
| const player_Name = 'Jane'; | ||
|
|
||
| const message: Message = { | ||
| date: new Date(), | ||
| count: 1, | ||
| } as unknown as Message; | ||
|
|
||
| const newGameStatistics = gameStatisticsBuilder | ||
| .setWonBattles(1) | ||
| .setMessages([message]) | ||
| .build(); | ||
|
|
||
| const newPlayer = playerBuilder | ||
| .setUniqueIdentifier('unique-id-123') | ||
| .setName(player_Name) | ||
| .setId(player_Id) | ||
| .setGameStatistics(newGameStatistics) | ||
| .build(); | ||
|
|
||
| await playerModel.create(newPlayer); | ||
|
|
||
| const [result, error] = await playerStatisticService.updatePlayerStatistic( | ||
| player_Id, | ||
| PlayerEvent.MESSAGE_SENT, | ||
| ); | ||
|
|
||
| const updatedPlayer = await playerModel.findById(player_Id); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(error).toBeNull(); | ||
| expect(updatedPlayer.name).toBe(player_Name); | ||
| expect(updatedPlayer.gameStatistics.messages[0].count).toBe(2); | ||
| }); | ||
|
|
||
| it("Should add a new message to player with today's date if do not have one yet | PlayerEvent.MESSAGE_SENT", async () => { | ||
| const [result, error] = await playerStatisticService.updatePlayerStatistic( | ||
| playerId, | ||
| PlayerEvent.MESSAGE_SENT, | ||
| ); | ||
|
|
||
| const updatedPlayer = await playerModel.findById(playerId); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(error).toBeNull(); | ||
| expect(updatedPlayer.name).toBe(playerName); | ||
| expect(updatedPlayer.gameStatistics.messages[0].count).toBe(1); | ||
| expect(updatedPlayer.gameStatistics.messages[0].date.toDateString()).toBe( | ||
| new Date().toDateString(), | ||
| ); | ||
| expect(updatedPlayer.gameStatistics.messages.length).toBe(1); | ||
| }); | ||
|
|
||
| it('Should return with MongooseError if have not updated the player in the DB | PlayerEvent.MESSAGE_SENT', async () => { | ||
| jest.spyOn(playerService, 'updateOneById').mockImplementation(async () => { | ||
| return new MongooseError(''); | ||
| }); | ||
|
|
||
| const [result, error] = await playerStatisticService.updatePlayerStatistic( | ||
| playerId, | ||
| PlayerEvent.MESSAGE_SENT, | ||
| ); | ||
|
|
||
| expect(result).toBe(false); | ||
| expect(error).toBeInstanceOf(MongooseError); | ||
|
|
||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('Should return with ServiceError if PlayerEvent type is not supported | PlayerEvent.NotSupported', async () => { | ||
| const [result, error] = await playerStatisticService.updatePlayerStatistic( | ||
| playerId, | ||
| -1 as unknown as PlayerEvent, | ||
| ); | ||
|
|
||
| expect(result).toBe(null); | ||
| expect(error).toContainSE_UNEXPECTED(); | ||
| }); | ||
| }); |
17 changes: 17 additions & 0 deletions
17
src/__tests__/statisticsKeeper/modules/statisticsKeeper.module.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import StatisticsKeeperCommonModule from './statisticsKeeperCommon.module'; | ||
| import { PlayerStatisticService } from '../../../statisticsKeeper/playerStatisticKeeper/playerStatisticKeeper.service'; | ||
| import { PlayerService } from '../../../player/player.service'; | ||
|
|
||
| export default class StatisticsKeeperModule { | ||
| private constructor() {} | ||
|
|
||
| static async getPlayerStatisticService() { | ||
| const module = await StatisticsKeeperCommonModule.getModule(); | ||
| return module.resolve(PlayerStatisticService); | ||
| } | ||
|
|
||
| static async getPlayerService() { | ||
| const module = await StatisticsKeeperCommonModule.getModule(); | ||
| return module.resolve(PlayerService); | ||
| } | ||
| } | ||
35 changes: 35 additions & 0 deletions
35
src/__tests__/statisticsKeeper/modules/statisticsKeeperCommon.module.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { MongooseModule } from '@nestjs/mongoose'; | ||
| import { mongooseOptions, mongoString } from '../../test_utils/const/db'; | ||
| import { ModelName } from '../../../common/enum/modelName.enum'; | ||
| import { PlayerSchema } from '../../../player/schemas/player.schema'; | ||
| import { PlayerStatisticService } from '../../../statisticsKeeper/playerStatisticKeeper/playerStatisticKeeper.service'; | ||
| import { StatisticsKeeperModule } from '../../../statisticsKeeper/statisticsKeeper.module'; | ||
| import { CustomCharacterSchema } from '../../../player/customCharacter/customCharacter.schema'; | ||
| import { RequestHelperModule } from '../../../requestHelper/requestHelper.module'; | ||
| import { PlayerModule } from '../../../player/player.module'; | ||
|
|
||
| export default class StatisticsKeeperCommonModule { | ||
| private constructor() {} | ||
|
|
||
| private static module: TestingModule; | ||
|
|
||
| static async getModule() { | ||
| if (!StatisticsKeeperCommonModule.module) | ||
| StatisticsKeeperCommonModule.module = await Test.createTestingModule({ | ||
| imports: [ | ||
| MongooseModule.forRoot(mongoString, mongooseOptions), | ||
| MongooseModule.forFeature([ | ||
| { name: ModelName.PLAYER, schema: PlayerSchema }, | ||
| { name: ModelName.CUSTOM_CHARACTER, schema: CustomCharacterSchema }, | ||
| ]), | ||
| StatisticsKeeperModule, | ||
| PlayerModule, | ||
| RequestHelperModule, | ||
| ], | ||
| providers: [PlayerStatisticService], | ||
| }).compile(); | ||
|
|
||
| return StatisticsKeeperCommonModule.module; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use PlayerModule testing module to get PlayerService instead of adding a method it here. StatisticsKeeperModule should have only methods for creating own classes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I do that than the mocks will not work. I think because my original solution is getting a reference to the PlayerService instance that is a dependency of the PlayerStatisticService. The purposed solution will get a reference to an another PlayerService.