Skip to content

Commit fa20a49

Browse files
icreatedicreated
authored andcommitted
test array
1 parent 8f542bd commit fa20a49

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/main/kotlin/com/cosmotech/api/utils/StringExtensions.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ fun String.toRedisMetaDataKey() = "${this}MetaData"
4444
fun String.formatQuery(map: Map<String, String>): String {
4545
var newValue = this
4646
map.forEach { (key, value) ->
47-
var nullableValue = if (value.trim() == "") "null" else value
48-
if (nullableValue != "null") {
49-
nullableValue = "\"${nullableValue.replace("\"","\\\"")}\""
50-
}
51-
newValue = newValue.replace("$$key", nullableValue)
47+
var sanitizedValue =
48+
if (value.isNullOrBlank()) {
49+
"null"
50+
} else {
51+
"\"${value.replace("\"","\\\"")}\""
52+
}
53+
newValue = newValue.replace("$$key", sanitizedValue)
5254
}
5355
return newValue
5456
}

src/test/kotlin/com/cosmotech/api/utils/StringExtensionsTests.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,16 @@ class StringExtensionsTests {
4343

4444
@Test
4545
fun `should format Cypher Query with variables`() {
46-
val map = mapOf("name" to "Joe Doe", "id" to "100", "object" to "{id:\"id\"}")
47-
val actual = "CREATE (:Person {name: \$name, id: \$id, object: \$object})"
48-
val expected = "CREATE (:Person {name: \"Joe Doe\", id: \"100\", object: \"{id:\\\"id\\\"}\"})"
46+
val map =
47+
mapOf(
48+
"name" to "Joe Doe",
49+
"id" to "100",
50+
"object" to "{id:\"id\"}",
51+
"array" to "{id:['one', 'two']}")
52+
val actual = "CREATE (:Person {name: \$name, id: \$id, object: \$object, array: \$array)"
53+
val expected =
54+
"CREATE (:Person {name: \"Joe Doe\", id: \"100\", object: \"{id:\\\"id\\\"}\", " +
55+
"array: \"{id:['one', 'two']}\")"
4956
assertEquals(expected, actual.formatQuery(map))
5057
}
5158

0 commit comments

Comments
 (0)