Skip to content

Commit

Permalink
fix: previous tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
sergioqfeg1 committed Apr 16, 2024
1 parent 8c4bab3 commit c60fc3f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/src/main/java/lab/en2b/quizapi/game/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ private void saveStatistics(Game game){
.correct(game.getCorrectlyAnsweredQuestions())
.wrong(game.getQuestions().size()-game.getCorrectlyAnsweredQuestions())
.total(game.getRounds())
.finishedGames(gameRepository.countFinishedGamesForUser(game.getUser().getId()))
.build();
}
statisticsRepository.save(statistics);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public void newGameWasMeantToBeOverExistingLeaderboard(){
.correct(0L)
.wrong(0L)
.total(0L)
.finishedGames(1L)
.build()));
when(gameRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
defaultGame.setActualRound(10L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lab.en2b.quizapi.commons.user.User;
import lab.en2b.quizapi.commons.user.dtos.UserResponseDto;
import lab.en2b.quizapi.commons.user.UserService;
import lab.en2b.quizapi.game.GameRepository;
import lab.en2b.quizapi.statistics.dtos.StatisticsResponseDto;
import lab.en2b.quizapi.statistics.mappers.StatisticsResponseDtoMapper;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -42,6 +43,9 @@ public class StatisticsServiceTest {
@Mock
private StatisticsResponseDtoMapper statisticsResponseDtoMapper;

@Mock
private GameRepository gameRepository;

private User defaultUser;

private Statistics defaultStatistics1;
Expand All @@ -56,7 +60,7 @@ public class StatisticsServiceTest {

@BeforeEach
public void setUp(){
this.statisticsService = new StatisticsService(statisticsRepository, userService, statisticsResponseDtoMapper);
this.statisticsService = new StatisticsService(statisticsRepository, userService, statisticsResponseDtoMapper, gameRepository);
this.defaultUser = User.builder()
.id(1L)
.email("[email protected]")
Expand All @@ -79,6 +83,7 @@ public void setUp(){
.correct(5L)
.wrong(5L)
.total(10L)
.finishedGames(1L)
.build();

this.defaultStatisticsResponseDto1 = StatisticsResponseDto.builder()
Expand All @@ -88,6 +93,7 @@ public void setUp(){
.total(10L)
.correctRate(50L)
.user(defaultUserResponseDto)
.finishedGames(1L)
.build();

this.defaultStatistics2 = Statistics.builder()
Expand All @@ -96,6 +102,7 @@ public void setUp(){
.correct(7L)
.wrong(3L)
.total(10L)
.finishedGames(1L)
.build();

this.defaultStatisticsResponseDto2 = StatisticsResponseDto.builder()
Expand All @@ -105,6 +112,7 @@ public void setUp(){
.total(10L)
.correctRate(70L)
.user(defaultUserResponseDto)
.finishedGames(1L)
.build();
}

Expand Down Expand Up @@ -135,6 +143,7 @@ public void getTopTenStatisticsTestWhenThereAreNotTenAndAreEqual(){
.correct(5L)
.wrong(5L)
.total(10L)
.finishedGames(1L)
.build();
StatisticsResponseDto defaultStatisticsResponseDto3 = StatisticsResponseDto.builder()
.id(2L)
Expand All @@ -143,6 +152,7 @@ public void getTopTenStatisticsTestWhenThereAreNotTenAndAreEqual(){
.total(10L)
.correctRate(50L)
.user(defaultUserResponseDto)
.finishedGames(1L)
.build();
when(statisticsRepository.findAll()).thenReturn(List.of(defaultStatistics1, defaultStatistics3));
when(statisticsResponseDtoMapper.apply(defaultStatistics1)).thenReturn(defaultStatisticsResponseDto1);
Expand All @@ -159,41 +169,47 @@ public void getTopTenStatisticsWhenThereAreTen(){
.correct(1L)
.wrong(9L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics4 = Statistics.builder()
.id(4L)
.user(defaultUser)
.correct(2L)
.wrong(8L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics5 = Statistics.builder()
.id(5L)
.user(defaultUser)
.correct(3L)
.wrong(7L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics6 = Statistics.builder()
.id(6L)
.user(defaultUser)
.correct(4L)
.wrong(6L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics7 = Statistics.builder()
.id(7L)
.user(defaultUser)
.correct(6L)
.wrong(4L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics8 = Statistics.builder()
.id(8L)
.user(defaultUser)
.correct(8L)
.wrong(2L)
.total(10L)
.finishedGames(1L)
.build();
List<Statistics> statistics = List.of(defaultStatistics8, defaultStatistics2, defaultStatistics7,
defaultStatistics1, defaultStatistics6, defaultStatistics5, defaultStatistics4, defaultStatistics3);
Expand All @@ -206,6 +222,7 @@ public void getTopTenStatisticsWhenThereAreTen(){
.wrong(9L)
.total(10L)
.correctRate(10L)
.finishedGames(1L)
.user(defaultUserResponseDto)
.build());
when(statisticsResponseDtoMapper.apply(defaultStatistics4)).thenReturn(StatisticsResponseDto.builder()
Expand All @@ -214,6 +231,7 @@ public void getTopTenStatisticsWhenThereAreTen(){
.wrong(8L)
.total(10L)
.correctRate(20L)
.finishedGames(1L)
.user(defaultUserResponseDto)
.build());
when(statisticsResponseDtoMapper.apply(defaultStatistics5)).thenReturn(StatisticsResponseDto.builder()
Expand All @@ -222,6 +240,7 @@ public void getTopTenStatisticsWhenThereAreTen(){
.wrong(7L)
.total(10L)
.correctRate(30L)
.finishedGames(1L)
.user(defaultUserResponseDto)
.build());
when(statisticsResponseDtoMapper.apply(defaultStatistics6)).thenReturn(StatisticsResponseDto.builder()
Expand All @@ -230,6 +249,7 @@ public void getTopTenStatisticsWhenThereAreTen(){
.wrong(6L)
.total(10L)
.correctRate(40L)
.finishedGames(1L)
.user(defaultUserResponseDto)
.build());
when(statisticsResponseDtoMapper.apply(defaultStatistics7)).thenReturn(StatisticsResponseDto.builder()
Expand All @@ -238,6 +258,7 @@ public void getTopTenStatisticsWhenThereAreTen(){
.wrong(4L)
.total(10L)
.correctRate(60L)
.finishedGames(1L)
.user(defaultUserResponseDto)
.build());
when(statisticsResponseDtoMapper.apply(defaultStatistics8)).thenReturn(StatisticsResponseDto.builder()
Expand All @@ -246,6 +267,7 @@ public void getTopTenStatisticsWhenThereAreTen(){
.wrong(2L)
.total(10L)
.correctRate(80L)
.finishedGames(1L)
.user(defaultUserResponseDto)
.build());
List<StatisticsResponseDto> result = statistics.stream().map(statisticsResponseDtoMapper::apply).toList();
Expand All @@ -260,48 +282,55 @@ public void getTopTenWhenThereAreMoreThanTen(){
.correct(1L)
.wrong(9L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics4 = Statistics.builder()
.id(4L)
.user(defaultUser)
.correct(2L)
.wrong(8L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics5 = Statistics.builder()
.id(5L)
.user(defaultUser)
.correct(3L)
.wrong(7L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics6 = Statistics.builder()
.id(6L)
.user(defaultUser)
.correct(4L)
.wrong(6L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics7 = Statistics.builder()
.id(7L)
.user(defaultUser)
.correct(6L)
.wrong(4L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics8 = Statistics.builder()
.id(8L)
.user(defaultUser)
.correct(8L)
.wrong(2L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics9 = Statistics.builder()
.id(9L)
.user(defaultUser)
.correct(9L)
.wrong(1L)
.total(10L)
.finishedGames(1L)
.build();
List<Statistics> statistics = List.of(defaultStatistics9, defaultStatistics8, defaultStatistics2,
defaultStatistics7, defaultStatistics1, defaultStatistics6, defaultStatistics5, defaultStatistics4,
Expand All @@ -315,6 +344,7 @@ public void getTopTenWhenThereAreMoreThanTen(){
.wrong(9L)
.total(10L)
.correctRate(10L)
.finishedGames(1L)
.user(defaultUserResponseDto)
.build());
when(statisticsResponseDtoMapper.apply(defaultStatistics4)).thenReturn(StatisticsResponseDto.builder()
Expand All @@ -323,6 +353,7 @@ public void getTopTenWhenThereAreMoreThanTen(){
.wrong(8L)
.total(10L)
.correctRate(20L)
.finishedGames(1L)
.user(defaultUserResponseDto)
.build());
when(statisticsResponseDtoMapper.apply(defaultStatistics5)).thenReturn(StatisticsResponseDto.builder()
Expand All @@ -331,6 +362,7 @@ public void getTopTenWhenThereAreMoreThanTen(){
.wrong(7L)
.total(10L)
.correctRate(30L)
.finishedGames(1L)
.user(defaultUserResponseDto)
.build());
when(statisticsResponseDtoMapper.apply(defaultStatistics6)).thenReturn(StatisticsResponseDto.builder()
Expand All @@ -339,6 +371,7 @@ public void getTopTenWhenThereAreMoreThanTen(){
.wrong(6L)
.total(10L)
.correctRate(40L)
.finishedGames(1L)
.user(defaultUserResponseDto)
.build());
when(statisticsResponseDtoMapper.apply(defaultStatistics7)).thenReturn(StatisticsResponseDto.builder()
Expand All @@ -347,6 +380,7 @@ public void getTopTenWhenThereAreMoreThanTen(){
.wrong(4L)
.total(10L)
.correctRate(60L)
.finishedGames(1L)
.user(defaultUserResponseDto)
.build());
when(statisticsResponseDtoMapper.apply(defaultStatistics8)).thenReturn(StatisticsResponseDto.builder()
Expand All @@ -355,6 +389,7 @@ public void getTopTenWhenThereAreMoreThanTen(){
.wrong(2L)
.total(10L)
.correctRate(80L)
.finishedGames(1L)
.user(defaultUserResponseDto)
.build());
when(statisticsResponseDtoMapper.apply(defaultStatistics9)).thenReturn(StatisticsResponseDto.builder()
Expand All @@ -363,6 +398,7 @@ public void getTopTenWhenThereAreMoreThanTen(){
.wrong(1L)
.total(10L)
.correctRate(90L)
.finishedGames(1L)
.user(defaultUserResponseDto)
.build());
List<StatisticsResponseDto> result = statistics.stream().limit(10).
Expand Down

0 comments on commit c60fc3f

Please sign in to comment.