-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #185 GradeStore and CategoryStore unit tests
- Loading branch information
1 parent
b5c75dd
commit a891aff
Showing
5 changed files
with
216 additions
and
7 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
commons/src/main/java/pl/sknikod/kodemycommons/util/PrincipalUtil.java
This file contains 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,14 @@ | ||
package pl.sknikod.kodemycommons.util; | ||
|
||
import org.springframework.stereotype.Component; | ||
import pl.sknikod.kodemycommons.security.AuthFacade; | ||
import pl.sknikod.kodemycommons.security.UserPrincipal; | ||
|
||
import java.util.Optional; | ||
|
||
@Component | ||
public class PrincipalUtil { | ||
public Optional<UserPrincipal> getPrincipal(){ | ||
return AuthFacade.getCurrentUserPrincipal(); | ||
} | ||
} |
This file contains 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
This file contains 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
34 changes: 34 additions & 0 deletions
34
kodemy-backend/src/test/groovy/unit/infrastructure/store/CategoryStoreSpec.groovy
This file contains 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,34 @@ | ||
package unit.infrastructure.store | ||
|
||
import org.spockframework.util.Assert | ||
import pl.sknikod.kodemybackend.infrastructure.database.Category | ||
import pl.sknikod.kodemybackend.infrastructure.database.CategoryRepository | ||
import pl.sknikod.kodemybackend.infrastructure.store.CategoryStore | ||
import pl.sknikod.kodemycommons.exception.NotFound404Exception | ||
import spock.lang.Specification | ||
|
||
class CategoryStoreSpec extends Specification{ | ||
def categoryRepository = Mock(CategoryRepository) | ||
|
||
def categoryStore = new CategoryStore(categoryRepository) | ||
|
||
private static final CATEGORY_ID = 1L | ||
|
||
def "shouldFindCategoryById" () { | ||
given: | ||
categoryRepository.findById(CATEGORY_ID) >> Optional.of(new Category()) | ||
when: | ||
def result = categoryStore.findById(CATEGORY_ID) | ||
then: | ||
Assert.that(!result.isEmpty()) | ||
} | ||
|
||
def "shouldThrowNotFoundWhenFindCategoryById" () { | ||
given: | ||
categoryRepository.findById(CATEGORY_ID) >> Optional.empty() | ||
when: | ||
categoryStore.findById(CATEGORY_ID).get() | ||
then: | ||
thrown(NotFound404Exception) | ||
} | ||
} |
157 changes: 157 additions & 0 deletions
157
kodemy-backend/src/test/groovy/unit/infrastructure/store/GradeStoreSpec.groovy
This file contains 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,157 @@ | ||
package unit.infrastructure.store | ||
|
||
import io.vavr.control.Try | ||
import org.spockframework.util.Assert | ||
import org.springframework.data.domain.PageImpl | ||
import org.springframework.data.domain.PageRequest | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority | ||
import pl.sknikod.kodemybackend.infrastructure.database.Grade | ||
import pl.sknikod.kodemybackend.infrastructure.database.GradeRepository | ||
import pl.sknikod.kodemybackend.infrastructure.database.MaterialRepository | ||
import pl.sknikod.kodemybackend.infrastructure.store.GradeStore | ||
import pl.sknikod.kodemybackend.infrastructure.store.UserStore | ||
import pl.sknikod.kodemycommons.exception.NotFound404Exception | ||
import pl.sknikod.kodemycommons.security.UserPrincipal | ||
import pl.sknikod.kodemycommons.util.PrincipalUtil | ||
import spock.lang.Specification | ||
|
||
import java.time.LocalDateTime | ||
import java.time.ZoneId | ||
|
||
class GradeStoreSpec extends Specification { | ||
def gradeRepository = Mock(GradeRepository) | ||
def userStore = Mock(UserStore) | ||
def materialRepository = Mock(MaterialRepository) | ||
def principalUtil = Mock(PrincipalUtil) | ||
|
||
def gradeStore = new GradeStore(gradeRepository, userStore, materialRepository, principalUtil) | ||
|
||
private static final MATERIAL_ID = 1L | ||
private static final GRADE = 3.0 | ||
private static final USER_ID = 1L | ||
|
||
def "shouldFindAvgGradeByMaterialId"() { | ||
given: | ||
gradeRepository.findAvgGradeByMaterialId(MATERIAL_ID) >> GRADE | ||
when: | ||
def result = gradeStore.findAvgGradeByMaterial(MATERIAL_ID).get() | ||
then: | ||
Assert.that(result == 3.0) | ||
} | ||
|
||
def "shouldGetGradeStatsByMaterialId"() { | ||
given: | ||
gradeRepository.countAllByMaterialIdAndValue(MATERIAL_ID, 1.0) | ||
>> 0L | ||
gradeRepository.countAllByMaterialIdAndValue(MATERIAL_ID, 2.0) | ||
>> 0L | ||
gradeRepository.countAllByMaterialIdAndValue(MATERIAL_ID, GRADE) | ||
>> 1L | ||
gradeRepository.countAllByMaterialIdAndValue(MATERIAL_ID, 4.0) | ||
>> 0L | ||
gradeRepository.countAllByMaterialIdAndValue(MATERIAL_ID, 5.0) | ||
>> 0L | ||
when: | ||
def result = gradeStore.getGradeStats(MATERIAL_ID).get() | ||
then: | ||
Assert.that(result == List.of(0L, 0L, 1L, 0L, 0L)) | ||
} | ||
|
||
def "shouldSaveGrade"() { | ||
given: | ||
def grade = new Grade() | ||
gradeRepository.save(_ as Grade) >> grade | ||
when: | ||
def result = gradeStore.save(grade).get() | ||
then: | ||
Assert.that(grade == result) | ||
} | ||
|
||
def "shouldFindGradesByMaterialInDateRange"() { | ||
given: | ||
def grades = List.of( | ||
new Grade(GRADE, USER_ID, MATERIAL_ID) | ||
) | ||
def user = new UserStore.User() | ||
user.id = USER_ID | ||
user.username = "name" | ||
|
||
gradeRepository.findGradesByMaterialInDateRange( | ||
MATERIAL_ID, | ||
_ as LocalDateTime, | ||
_ as LocalDateTime, | ||
_ as PageRequest | ||
) >> new PageImpl<Grade>(grades) | ||
userStore.findUsersById(_ as Set) >> Try.success(new ArrayList<>(List.of(user))) | ||
when: | ||
def result = gradeStore.findGradesByMaterialInDateRange( | ||
MATERIAL_ID, | ||
Date.from(LocalDateTime.now().minusDays(1).atZone(ZoneId.systemDefault()).toInstant()), | ||
Date.from(LocalDateTime.now().plusDays(1).atZone(ZoneId.systemDefault()).toInstant()), | ||
PageRequest.of(1, 1,) | ||
).get() | ||
then: | ||
verifyAll(result) { | ||
result._1() == new PageImpl<Grade>(grades) | ||
result._2() == Set.of(user) | ||
} | ||
} | ||
|
||
def "shouldThrowWhenFindGradesByMaterialInDateRange"() { | ||
given: | ||
def grades = List.of( | ||
new Grade(GRADE, USER_ID, MATERIAL_ID) | ||
) | ||
|
||
gradeRepository.findGradesByMaterialInDateRange( | ||
MATERIAL_ID, | ||
_ as LocalDateTime, | ||
_ as LocalDateTime, | ||
_ as PageRequest | ||
) >> new PageImpl<Grade>(grades) | ||
userStore.findUsersById(_ as Set) >> Try.success(new ArrayList<>()) | ||
when: | ||
gradeStore.findGradesByMaterialInDateRange( | ||
MATERIAL_ID, | ||
Date.from(LocalDateTime.now().minusDays(1).atZone(ZoneId.systemDefault()).toInstant()), | ||
Date.from(LocalDateTime.now().plusDays(1).atZone(ZoneId.systemDefault()).toInstant()), | ||
PageRequest.of(1, 1,) | ||
).get() | ||
then: | ||
thrown(IllegalStateException) | ||
} | ||
|
||
def "shouldAddGrade"() { | ||
given: | ||
def grade = new Grade(GRADE, USER_ID, MATERIAL_ID) | ||
materialRepository.existsById(MATERIAL_ID) >> true | ||
gradeRepository.save(_ as Grade) >> grade | ||
principalUtil.getPrincipal() >> Optional.of(new UserPrincipal( | ||
USER_ID, "", false, false, | ||
false, true, new HashSet<SimpleGrantedAuthority>() | ||
)) | ||
when: | ||
def result = gradeStore.addGrade(MATERIAL_ID, GRADE).get() | ||
then: | ||
Assert.that(grade == result) | ||
} | ||
|
||
def "shouldThrowWhenAddGrade"() { | ||
given: | ||
materialRepository.existsById(MATERIAL_ID) >> false | ||
when: | ||
gradeStore.addGrade(MATERIAL_ID, GRADE).get() | ||
then: | ||
thrown(NotFound404Exception) | ||
} | ||
|
||
def "shouldFindAvgGradeByMaterialsIds"() { | ||
given: | ||
gradeRepository.findAvgGradeByMaterialsIds(_ as List<Long>) | ||
>> new HashSet<Object[]>() | ||
when: | ||
def result = gradeStore.findAvgGradeByMaterialsIds(List.of(1L)).get() | ||
then: | ||
Assert.that(result in List<GradeStore.FindAvgGradeObject>) | ||
} | ||
} |