Skip to content

Commit

Permalink
feat: #185 MaterialStore tests part2
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinbator committed Dec 8, 2024
1 parent ebb5d91 commit e68fea0
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 27 deletions.
10 changes: 5 additions & 5 deletions kodemy-backend/src/test/groovy/unit/TSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import spock.lang.Specification
class TSpec extends Specification {
def "shouldTrue"() {
given:
def email = ""
def email = ""
when:
email = ""
email = ""
then:
verifyAll {
email == ""
}
verifyAll {
email == ""
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,21 @@ class CategoryStoreSpec extends Specification {
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ class GradeStoreSpec extends Specification {
def "shouldFindAvgGradeByMaterialId"() {
given:
gradeRepository.findAvgGradeByMaterialId(MATERIAL_ID) >> GRADE

when:
def result = gradeStore.findAvgGradeByMaterial(MATERIAL_ID).get()

then:
Assert.that(result == 3.0)
}
Expand All @@ -51,8 +53,10 @@ class GradeStoreSpec extends Specification {
>> 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))
}
Expand All @@ -61,8 +65,10 @@ class GradeStoreSpec extends Specification {
given:
def grade = new Grade()
gradeRepository.save(_ as Grade) >> grade

when:
def result = gradeStore.save(grade).get()

then:
Assert.that(grade == result)
}
Expand All @@ -75,21 +81,22 @@ class GradeStoreSpec extends Specification {
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)
Expand All @@ -102,21 +109,22 @@ class GradeStoreSpec extends Specification {
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)
}
Expand All @@ -130,17 +138,21 @@ class GradeStoreSpec extends Specification {
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)
}
Expand All @@ -149,8 +161,10 @@ class GradeStoreSpec extends Specification {
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>)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ package unit.infrastructure.store
import io.vavr.Tuple
import io.vavr.Tuple2
import io.vavr.control.Try
import jakarta.transaction.Status
import org.apache.catalina.User
import org.hibernate.query.Page
import org.spockframework.util.Assert
import org.springframework.data.domain.PageImpl
import org.springframework.data.domain.PageRequest
import pl.sknikod.kodemybackend.infrastructure.database.Grade
import pl.sknikod.kodemybackend.infrastructure.database.Material
import pl.sknikod.kodemybackend.infrastructure.database.MaterialRepository
import pl.sknikod.kodemybackend.infrastructure.store.GradeStore
Expand All @@ -18,17 +14,19 @@ import pl.sknikod.kodemybackend.infrastructure.store.UserStore
import pl.sknikod.kodemycommons.exception.NotFound404Exception
import spock.lang.Specification

class MaterialStoreSpec extends Specification{
import java.time.LocalDateTime

class MaterialStoreSpec extends Specification {
def materialRepository = Mock(MaterialRepository)
def userStore = Mock(UserStore)
def gradeStore = Mock(GradeStore)

def material = new Material()
def user = new UserStore.User()
def materialStore = new MaterialStore(materialRepository,userStore,gradeStore)
def "shouldFindById"(){

def materialStore = new MaterialStore(materialRepository, userStore, gradeStore)

def "shouldFindById"() {
given:
material.setId(1L)
material.setUserId(1L)
Expand All @@ -37,49 +35,143 @@ class MaterialStoreSpec extends Specification{
materialRepository.findById(1L) >> Optional.of(material)
userStore.findById(1L) >> Try.success(user)
gradeStore.findAvgGradeByMaterial(1L) >> Try.success(4.0D)
gradeStore.getGradeStats(1L) >> Try.success(new ArrayList<>(List.of(1L,2L,3L,4L,5L)))
gradeStore.getGradeStats(1L) >> Try.success(new ArrayList<>(List.of(1L, 2L, 3L, 4L, 5L)))

when:
def result1 = materialStore.findById(1L, false).get()
def result2 = materialStore.findById(1L, true).get()
def result3 = materialStore.findById(1L).get()

then:
Assert.that(result1 instanceof MaterialStore.FindByIdObject)
Assert.that(result2.username()==null)
Assert.that(result2.username() == null)
Assert.that(result3 instanceof MaterialStore.FindByIdObject)
}
def "shouldThrowWhenFindById"(){

def "shouldThrowWhenFindById"() {
given:
materialRepository.findById(1L) >> Optional.empty()

when:
materialStore.findById(1L).get()

then:
thrown(NotFound404Exception)
}
def "shouldSave"(){

def "shouldSave"() {
given:
materialRepository.save(material) >> material

when:
def result = materialStore.save(material).get()

then:
Assert.that(result == material)
}
def "shouldUpdate"(){

def "shouldUpdate"() {
given:
materialRepository.save(material) >> material

when:
def result = materialStore.update(material).get()

then:
Assert.that(result == material)
}

def "shouldChangeStatus"(){

def "shouldFindAllWithUser"() {
given:
materialRepository.searchMaterialsWithAvgGrades(
_ as Long,
_ as String,
_ as List,
_ as Long,
_ as List<Long>,
_ as List<Long>,
_ as Long,
null,
null,
_ as Double,
_ as Double,
_ as PageRequest
) >> new PageImpl<Object[]>(new Object[]{new Object[]{material, 3.0D}} as List<Object[]>)
userStore.findById(1L) >> Try.success(user)

when:
def result = materialStore.findAll(1L, new MaterialStore.FindAllFilters(
"", 1L, new ArrayList<Material.MaterialStatus>(), 1L, List.of(1L),
List.of(1L), 3.0D, 3.0D,
PageRequest.of(1, 1)
)).get()

then:
Assert.that(result instanceof Tuple2<org.springframework.data.domain.Page<MaterialStore.FindAllPageObject>, UserStore.User>)
}

def "shouldFindAll"() {
given:
materialRepository.searchMaterialsWithAvgGrades(
_ as Long,
_ as String,
_ as List,
_ as Long,
_ as List<Long>,
_ as List<Long>,
null,
null,
null,
_ as Double,
_ as Double,
_ as PageRequest
) >> new PageImpl<Object[]>(new Object[]{new Object[]{material, 3.0D}} as List<Object[]>)
userStore.findUsersById(_ as Set) >> Try.success(new ArrayList<>(List.of(user)))

when:
def result = materialStore.findAll(new MaterialStore.FindAllFilters(
"", 1L, new ArrayList<Material.MaterialStatus>(), 1L, List.of(1L),
List.of(1L), 3.0D, 3.0D,
PageRequest.of(1, 1)
)).get()

then:
Assert.that(result instanceof Tuple2<org.springframework.data.domain.Page<MaterialStore.FindAllPageObject>, UserStore.User>)
}

def "shouldFindAllInDataRange"() {
given:
user.username = "name"
user.id = 1L
materialRepository.findMaterialsInDateRangeWithPage(
_ as LocalDateTime,
_ as LocalDateTime,
_ as PageRequest
) >> new PageImpl<Material>(List.of(material))
gradeStore.findAvgGradeByMaterialsIds(_ as Collection)
>> Try.success(new ArrayList<>(List.of(new GradeStore.FindAvgGradeObject(
new Object[]{1L, 3.0D}
))))
userStore.findUsersById(_ as Set) >> Try.success(new ArrayList<>(List.of(user)))

when:
def result = materialStore.findAllInDateRange(
LocalDateTime.now().minusDays(1),
LocalDateTime.now().plusDays(1),
PageRequest.of(1, 1)
).get()

then:
Assert.that(result.content.size() == 1)
}

def "shouldChangeStatus"() {
given:
materialRepository.updateStatus(1L, Material.MaterialStatus.APPROVED) >> _

when:
def result = materialStore.changeStatus(1L, Material.MaterialStatus.APPROVED).get()

then:
Assert.that(result == Tuple.of(1L, Material.MaterialStatus.APPROVED))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ class SectionStoreSpec extends Specification {
def section = new Section()
sectionRepository.findAllWithFetchCategories()
>> new ArrayList<Section>(List.of(section))

when:
def result = sectionStore.findAll().get()

then:
Assert.that(result == List.of(section))
}
Expand Down
Loading

0 comments on commit e68fea0

Please sign in to comment.