1
1
mirror of https://github.com/Pygmalion69/OpenTopoMapViewer.git synced 2025-10-06 00:02:42 +02:00

Add unit tests

This commit is contained in:
Pygmalion69
2025-08-30 12:10:40 +02:00
parent 590e930123
commit aff7527f9c
3 changed files with 117 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
package org.nitri.ors
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.junit.Assert.assertEquals
import org.junit.Test
import org.nitri.ors.domain.export.ExportResponse
class ExportResponseStringAsDoubleSerializerTest {
private val json = Json { ignoreUnknownKeys = true }
@Test
fun parses_weight_when_number_or_string() {
val payload = """
{
"nodes": [],
"edges": [
{"fromId":1, "toId":2, "weight": "12.34"},
{"fromId":2, "toId":3, "weight": 56.78}
]
}
""".trimIndent()
val resp = json.decodeFromString<ExportResponse>(payload)
assertEquals(2, resp.edges.size)
assertEquals(12.34, resp.edges[0].weight, 1e-9)
assertEquals(56.78, resp.edges[1].weight, 1e-9)
}
@Test
fun serializes_weight_as_number() {
val payload = """
{
"nodes": [],
"edges": [
{"fromId":1, "toId":2, "weight": "10"}
]
}
""".trimIndent()
val resp = json.decodeFromString<ExportResponse>(payload)
val out = json.encodeToString(resp)
// The serializer writes numbers, so we expect \"weight\":10.0 (or 10) in output
// We assert that there is no quoted weight in the serialized JSON.
assert(!out.contains("\"weight\": \""))
}
}

View File

@@ -0,0 +1,33 @@
package org.nitri.ors
import org.junit.Assert.assertEquals
import org.junit.Assert.assertThrows
import org.junit.Test
import org.nitri.ors.domain.route.RouteRequestBuilderJ
class RouteRequestBuilderJTest {
@Test
fun builds_coordinates_and_language() {
val req = RouteRequestBuilderJ()
.start(8.68, 49.41)
.add(8.69, 49.42)
.end(8.70, 49.43)
.language("de")
.build()
assertEquals(3, req.coordinates.size)
assertEquals(listOf(8.68, 49.41), req.coordinates.first())
assertEquals(listOf(8.70, 49.43), req.coordinates.last())
assertEquals("de", req.language)
}
@Test
fun throws_when_less_than_two_points() {
assertThrows(IllegalArgumentException::class.java) {
RouteRequestBuilderJ()
.start(8.68, 49.41)
.build()
}
}
}

View File

@@ -0,0 +1,35 @@
package org.nitri.ors
import org.junit.Assert.assertEquals
import org.junit.Assert.assertThrows
import org.junit.Test
import org.nitri.ors.domain.snap.SnapRequestBuilderJ
class SnapRequestBuilderTest {
@Test
fun builds_snap_request() {
val req = SnapRequestBuilderJ()
.location(8.0, 48.0)
.radius(25)
.id("jid")
.build()
assertEquals(1, req.locations.size)
assertEquals(25, req.radius)
assertEquals("jid", req.id)
}
@Test
fun requires_location_and_radius() {
assertThrows(IllegalArgumentException::class.java) {
SnapRequestBuilderJ()
.radius(10)
.build()
}
assertThrows(IllegalArgumentException::class.java) {
SnapRequestBuilderJ()
.location(8.0, 48.0)
.build()
}
}
}