More tests.

This commit is contained in:
neviyn 2018-10-08 10:05:17 +01:00
parent e6e69b2df9
commit 72e8b095c8
2 changed files with 81 additions and 14 deletions

View File

@ -53,19 +53,21 @@ class Controller {
fun getObservationsForTutor(@PathVariable(value = "id") id: Long): List<SimpleObservation> = fun getObservationsForTutor(@PathVariable(value = "id") id: Long): List<SimpleObservation> =
tutorRepository.findById(id).map { tutor -> tutorRepository.findById(id).map { tutor ->
tutor.observations.map { SimpleObservation(it) } tutor.observations.map { SimpleObservation(it) }
}.get() }.orElse(listOf())
@PostMapping("/observation") @PostMapping("/observation")
fun addObservation(@Valid @RequestBody newObservation: NewObservation): Long { fun addObservation(@Valid @RequestBody newObservation: NewObservation): Long? {
println(newObservation) println(newObservation)
val site = siteRepository.findById(newObservation.site).get() val site = siteRepository.findById(newObservation.site)
val tutors = tutorRepository.findAllById(newObservation.tutors).toSet() val tutors = tutorRepository.findAllById(newObservation.tutors).toSet()
if(!site.isPresent) return null
if(tutors.isEmpty() || tutors.size != newObservation.tutors.size) return null
val overallScores = newObservation.entries.asSequence().groupBy { it.type } val overallScores = newObservation.entries.asSequence().groupBy { it.type }
.map { entry -> entry.key to entry.value.asSequence().mapNotNull { it.rating }.average() } .map { entry -> entry.key to entry.value.asSequence().mapNotNull { it.rating }.average() }
.map { it.first to if (it.second > 0) it.second else null }.toList() .map { it.first to if (it.second > 0) it.second else null }.toList()
.toMap() .toMap()
var observation = Observation( var observation = Observation(
site = site, site = site.get(),
date = LocalDate.now(), date = LocalDate.now(),
type = newObservation.type, type = newObservation.type,
observed = newObservation.observed, observed = newObservation.observed,

View File

@ -1,11 +1,10 @@
package uk.co.neviyn.observationdatabase package uk.co.neviyn.observationdatabase
import junit.framework.TestCase.* import junit.framework.TestCase.*
import org.joda.time.LocalDate
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers import org.mockito.*
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.Mockito.* import org.mockito.Mockito.*
import org.mockito.junit.MockitoJUnitRunner import org.mockito.junit.MockitoJUnitRunner
import java.util.* import java.util.*
@ -82,4 +81,70 @@ class ControllerTest{
assertEquals(NameValue("Foo", 1), result) assertEquals(NameValue("Foo", 1), result)
} }
@Test
fun testGetObservationsForTutor() {
val site = Site(1, "X")
val tutor = Tutor(1, "Foo Bar", site)
val observations = listOf(Observation(
site = site,
date = LocalDate.now(),
type = TrainingType.INITIAL,
observed = "1",
whom = "G1",
monitoring = 5.0,
control = 4.0,
conservatism = 3.0,
teamwork = 2.0,
knowledge = 1.0,
entries = listOf(),
tutors = setOf(tutor)
), Observation(
site = site,
date = LocalDate.now(),
type = TrainingType.INITIAL,
observed = "2",
whom = "G2",
monitoring = 5.0,
control = 4.0,
conservatism = 3.0,
teamwork = 2.0,
knowledge = 1.0,
entries = listOf(),
tutors = setOf(tutor)
))
tutor.observations.addAll(observations)
doReturn(Optional.of(tutor)).`when`(tutorRepository).findById(1)
val result = controller.getObservationsForTutor(1)
for (i in 0..1) {
val expected = observations[i]
val actual = result[i]
assertEquals(expected.date, actual.date)
assertEquals(expected.type, actual.type)
assertEquals(expected.observed, actual.observed)
assertEquals(expected.whom, actual.whom)
assertEquals(expected.entries, actual.entries)
}
}
@Test
fun testGetObservationsForTutor_NoTutor()
{
doReturn(Optional.ofNullable(null)).`when`(tutorRepository).findById(1)
val result = controller.getObservationsForTutor(1)
assertTrue(result.isEmpty())
}
@Test
fun testAddObservation(){
val site = Site(1, "X")
val tutor = Tutor(1, "Foo Bar", site)
doReturn(Optional.of(site)).`when`(siteRepository).findById(1)
doReturn(listOf(tutor)).`when`(tutorRepository).findAllById(listOf(1))
val newData = NewObservation(1, TrainingType.INITIAL, "An Observation", "Group A", listOf(Entry(RatingCategory.MONITORING, 5, "", "")), listOf(1))
val observation = Observation(1, site, LocalDate.now(), TrainingType.INITIAL, "An Observation", "Group A", 5.0, 5.0, 5.0, .05, 5.0, newData.entries, setOf(tutor))
doReturn(observation).`when`(observationRepository).save(ArgumentMatchers.any())
val result = controller.addObservation(newData)
assertEquals(1, result)
}
} }