Skip to content

Commit

Permalink
Add failing test for aggregating data across different areas
Browse files Browse the repository at this point in the history
  • Loading branch information
vimto committed Oct 5, 2023
1 parent 5d105d0 commit 7253bff
Showing 1 changed file with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import org.springframework.jdbc.support.KeyHolder
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
import org.springframework.transaction.annotation.Transactional

data class WaterAllocationUsageRow(
val areaId: String,
Expand Down Expand Up @@ -285,6 +284,51 @@ class WaterAllocationAndUsageViewsTest(@Autowired val jdbcTemplate: JdbcTemplate
result[0].dailyUsage.compareTo(BigDecimal(0)) shouldBe 0
}

@Test
fun `should aggregate allocation data for different areas separately`() {
// GIVEN
createTestAllocation(testAllocation)
val secondAllocationInSameArea =
testAllocation.copy(sourceId = "another-source-same-area", meters = listOf("2", "3"))
createTestAllocation(secondAllocationInSameArea)

val allocationInDifferentArea =
testAllocation.copy(
areaId = "different-area-id",
sourceId = "another-source-different-area",
meters = listOf("4"))
createTestAllocation(allocationInDifferentArea)

val observationDate = LocalDate.now().atStartOfDay().minusDays(10)
createTestObservation(testSiteId, 10, observationDate.toInstant(ZoneOffset.UTC))
createTestObservation(
secondAllocationInSameArea.meters[0].toInt(), 5, observationDate.toInstant(ZoneOffset.UTC))
createTestObservation(
secondAllocationInSameArea.meters[1].toInt(), 5, observationDate.toInstant(ZoneOffset.UTC))
createTestObservation(
allocationInDifferentArea.meters.first().toInt(),
30,
observationDate.toInstant(ZoneOffset.UTC))

// WHEN
val results =
queryAllocationsAndUsage(
"where area_id = '${testAllocation.areaId}' and date = '$observationDate'")

// THEN
results.size shouldBe 1
checkResults(
results,
testAllocation.areaId,
testAllocation.allocation + secondAllocationInSameArea.allocation,
testAllocation.meteredAllocationDaily + secondAllocationInSameArea.meteredAllocationDaily,
testAllocation.meteredAllocationYearly + secondAllocationInSameArea.meteredAllocationYearly)

// TODO: Add an assertion for the aggregated amount in results for testAllocation.areaId
// TODO: Add assertions for separate results matching allocationInDifferentArea.areaId

}

fun queryAllocationsAndUsage(whereClause: String): MutableList<WaterAllocationUsageRow> =
jdbcTemplate.query(
"""select * from water_allocation_and_usage_by_area $whereClause""",
Expand Down Expand Up @@ -359,7 +403,7 @@ class WaterAllocationAndUsageViewsTest(@Autowired val jdbcTemplate: JdbcTemplate
jdbcTemplate.update(
"""
INSERT INTO observation_sites (id, council_id, name)
VALUES ($siteId, $councilId, 'Test site')
VALUES ($siteId, $councilId, 'Test site $siteId')
ON CONFLICT (id) DO NOTHING
""")

Expand Down

0 comments on commit 7253bff

Please sign in to comment.