-
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
Changes from 3 commits
dfd609c
a87ae38
416f014
2b49810
78d3ad1
5930f0b
a54a7ff
d472288
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,266 @@ | ||
| import { ObjectId } from 'mongodb'; | ||
| import { MongooseError } from 'mongoose'; | ||
| import { Message } from '../../../player/message.schema'; | ||
| import ServiceError from '../../../common/service/basicService/ServiceError'; | ||
| import { ModelName } from '../../../common/enum/modelName.enum'; | ||
| import { SEReason } from '../../../common/service/basicService/SEReason'; | ||
| import PlayerBuilderFactory from '../../player/data/playerBuilderFactory'; | ||
| import { PlayerEvent } from '../../../rewarder/playerRewarder/enum/PlayerEvent.enum'; | ||
| import StatisticsKeeperCommonModule from '../modules/statisticsKeeperCommon.module'; | ||
| import PlayerModule from '../../player/modules/player.module'; | ||
| import { PlayerService } from '../../../player/player.service'; | ||
| import { PlayerStatisticService } from '../../../statisticsKeeper/playerStatisticKeeper/playerStatisticKeeper.service'; | ||
|
|
||
| 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(); | ||
|
|
||
| beforeEach(async () => { | ||
| jest.clearAllMocks(); | ||
| playerStatisticService = | ||
| await StatisticsKeeperCommonModule.getPlayerStatisticService(); | ||
|
|
||
| playerService = await StatisticsKeeperCommonModule.getPlayerService(); | ||
| }); | ||
|
|
||
| it('Should increase the players playedBattles if the input is valid | PlayerEvent.BATTLE_PLAYED', async () => { | ||
| const gameStatistics = gameStatisticsBuilder.setWonBattles(0).build(); | ||
|
|
||
| const player = playerBuilder | ||
| .setName(playerName) | ||
| .setId(playerId) | ||
| .setGameStatistics(gameStatistics) | ||
| .build(); | ||
|
|
||
| await playerModel.create(player); | ||
|
|
||
| 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 gameStatistics = gameStatisticsBuilder.setWonBattles(0).build(); | ||
|
|
||
| const player = playerBuilder | ||
| .setName(playerName) | ||
| .setId(playerId) | ||
| .setGameStatistics(gameStatistics) | ||
| .build(); | ||
|
|
||
| await playerModel.create(player); | ||
|
|
||
| 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 gameStatistics = gameStatisticsBuilder.setWonBattles(0).build(); | ||
|
|
||
| const player = playerBuilder | ||
| .setName(playerName) | ||
| .setId(playerId) | ||
| .setGameStatistics(gameStatistics) | ||
| .build(); | ||
|
|
||
| await playerModel.create(player); | ||
|
|
||
| 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 MongooseError if have not read the player from the DB | PlayerEvent.MESSAGE_SENT', async () => { | ||
| 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 () => { | ||
| const gameStatistics = gameStatisticsBuilder.setWonBattles(0).build(); | ||
|
|
||
| const player = playerBuilder | ||
| .setName(playerName) | ||
| .setId(playerId) | ||
| .setGameStatistics(gameStatistics) | ||
| .build(); | ||
|
|
||
| await playerModel.create(player); | ||
|
|
||
| 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[0]).toBeInstanceOf(ServiceError); | ||
|
||
| expect(error[0].reason).toBe(SEReason.NOT_FOUND); | ||
| expect(error[0].message).toBe('Could not read the player'); | ||
|
|
||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('Should increase the players message counter if found a todays message | PlayerEvent.MESSAGE_SENT', async () => { | ||
| const message: Message = { | ||
| date: new Date(), | ||
| count: 1, | ||
| } as unknown as Message; | ||
|
|
||
| const gameStatistics = gameStatisticsBuilder | ||
| .setWonBattles(0) | ||
| .setMessages([message]) | ||
| .build(); | ||
|
|
||
| const player = playerBuilder | ||
| .setName(playerName) | ||
| .setId(playerId) | ||
| .setGameStatistics(gameStatistics) | ||
| .build(); | ||
|
|
||
| await playerModel.create(player); | ||
|
|
||
| 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(2); | ||
| }); | ||
|
|
||
| it('Should add a new message to player with todays date if do not have one yet | PlayerEvent.MESSAGE_SENT', async () => { | ||
| const gameStatistics = gameStatisticsBuilder | ||
| .setWonBattles(0) | ||
| .setMessages([]) | ||
| .build(); | ||
|
|
||
| const player = playerBuilder | ||
| .setName(playerName) | ||
| .setId(playerId) | ||
| .setGameStatistics(gameStatistics) | ||
| .build(); | ||
|
|
||
| await playerModel.create(player); | ||
|
|
||
| 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 () => { | ||
| const gameStatistics = gameStatisticsBuilder.setWonBattles(0).build(); | ||
|
|
||
| const player = playerBuilder | ||
| .setName(playerName) | ||
| .setId(playerId) | ||
| .setGameStatistics(gameStatistics) | ||
| .build(); | ||
|
|
||
| await playerModel.create(player); | ||
|
|
||
| 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 gameStatistics = gameStatisticsBuilder.setWonBattles(0).build(); | ||
|
|
||
| const player = playerBuilder | ||
| .setName(playerName) | ||
| .setId(playerId) | ||
| .setGameStatistics(gameStatistics) | ||
| .build(); | ||
|
|
||
| await playerModel.create(player); | ||
|
|
||
| const [result, error] = await playerStatisticService.updatePlayerStatistic( | ||
| playerId, | ||
| -1 as unknown as PlayerEvent, | ||
| ); | ||
|
|
||
| expect(result).toBe(null); | ||
| expect(error[0]).toBeInstanceOf(ServiceError); | ||
| expect(error[0].message).toBe('Event is not supported'); | ||
| }); | ||
| }); | ||
| 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 await module.resolve(PlayerStatisticService); | ||
| } | ||
|
|
||
| static async getPlayerService() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| const module = await StatisticsKeeperCommonModule.getModule(); | ||
| return await module.resolve(PlayerService); | ||
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| 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 { PlayerService } from '../../../player/player.service'; | ||
| 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 { | ||
| static async getPlayerStatisticService() { | ||
| const module = await StatisticsKeeperCommonModule.getModule(); | ||
| return await module.resolve(PlayerStatisticService); | ||
| } | ||
|
|
||
| static async getPlayerService() { | ||
| const module = await StatisticsKeeperCommonModule.getModule(); | ||
| return await module.resolve(PlayerService); | ||
| } | ||
| 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, | ||
| PlayerService, | ||
| ], | ||
| }).compile(); | ||
|
|
||
| return StatisticsKeeperCommonModule.module; | ||
| } | ||
| } |
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.
Move the creation of player object into beforeEach(), since you are using this object in almost all the cases. Then remove its creation from all of the test cases