From a0ae8803be4eb44a42672bba878e702739ceb9ba Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Wed, 10 Oct 2018 14:47:01 +0100 Subject: [PATCH] Added tests for controller.getObservations. --- .../observationdatabase/ControllerTest.kt | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/ControllerTest.kt b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/ControllerTest.kt index 8c965f6..906ff6e 100644 --- a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/ControllerTest.kt +++ b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/ControllerTest.kt @@ -99,4 +99,73 @@ class ControllerTest { val result = controller.addObservation(newData) assertEquals(1L, result) } + + @Test + fun testGetObservations_SiteAndTutorNull() { + val request = ObservationsRequest(null, null, "", LocalDate.now(), LocalDate.now()) + val result = controller.getObservations(request) + assertTrue(result.isEmpty()) + } + + @Test + fun testGetObservations_WithSite_TutorNull_NoWhom() { + val request = ObservationsRequest(1, null, "", LocalDate.now(), LocalDate.now()) + val site = Site(1, "Area 51") + val observation = 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()) + doReturn(Optional.of(site)).`when`(siteRepository).findById(1) + doReturn(listOf(observation)) + .`when`(observationRepository).findBySiteAndDateBetween(site, LocalDate.now(), LocalDate.now()) + val result = controller.getObservations(request) + assertEquals(1, result.size) + assertEquals(observation, result[0]) + } + + @Test + fun testGetObservations_WithSite_TutorNull_WithWhom() { + val request = ObservationsRequest(1, null, "Group A", LocalDate.now(), LocalDate.now()) + val site = Site(1, "Area 51") + val observation = Observation(site = site, date = LocalDate.now(), + type = TrainingType.INITIAL, observed = "1", whom = "Group A", monitoring = 5.0, + control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf()) + doReturn(Optional.of(site)).`when`(siteRepository).findById(1) + doReturn(listOf(observation)) + .`when`(observationRepository).findBySiteAndWhomAndDateBetween(site, "Group A", LocalDate.now(), LocalDate.now()) + val result = controller.getObservations(request) + assertEquals(1, result.size) + assertEquals(observation, result[0]) + } + + @Test + fun testGetObservations_WithSite_WithTutor_NoWhom() { + val request = ObservationsRequest(null, 1, "", LocalDate.now(), LocalDate.now()) + val site = Site(1, "Area 51") + val tutor = Tutor(1, "Mr Unknown", site) + val observation = 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)) + doReturn(Optional.of(tutor)).`when`(tutorRepository).findById(1) + doReturn(listOf(observation)) + .`when`(observationRepository).findByTutorsAndDateBetween(tutor, LocalDate.now(), LocalDate.now()) + val result = controller.getObservations(request) + assertEquals(1, result.size) + assertEquals(observation, result[0]) + } + + @Test + fun testGetObservations_WithSite_WithTutor_WithWhom() { + val request = ObservationsRequest(null, 1, "Group A", LocalDate.now(), LocalDate.now()) + val site = Site(1, "Area 51") + val tutor = Tutor(1, "Mr Unknown", site) + val observation = Observation(site = site, date = LocalDate.now(), + type = TrainingType.INITIAL, observed = "1", whom = "Group A", monitoring = 5.0, + control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(tutor)) + doReturn(Optional.of(tutor)).`when`(tutorRepository).findById(1) + doReturn(listOf(observation)) + .`when`(observationRepository).findByTutorsAndWhomAndDateBetween(tutor, "Group A", LocalDate.now(), LocalDate.now()) + val result = controller.getObservations(request) + assertEquals(1, result.size) + assertEquals(observation, result[0]) + } } \ No newline at end of file