Skip to content

Commit c955986

Browse files
committed
Added TCK Test for filters
reading graphql, optional cypher params and cypher statement from a markdown file (filter-test.md) then checking them and seeing that the expected number passes
1 parent 46a7026 commit c955986

File tree

3 files changed

+588
-5
lines changed

3 files changed

+588
-5
lines changed

src/main/kotlin/org/neo4j/graphql/Translator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class Translator(val schema: GraphQLSchema) {
7171
val noFilter = all.filterKeys { it != "filter" }
7272
// todo turn it into a Predicate too
7373
val eqExpression = noFilter.map { (k, v) -> "$variable.$k = \$${paramName(variable, k, v)}" }
74-
val expression = (eqExpression + filterExpressions).joinNonEmpty(" AND ")
74+
val expression = (eqExpression + filterExpressions).joinNonEmpty(" AND ") // TODO talk to Will ,"(",")")
7575
return Cypher(" WHERE $expression", filterParams + noFilter.map { (k,v) -> paramName(variable, k, v) to v }.toMap()
7676
)
7777
}

src/test/kotlin/org/neo4j/graphql/FilterTest.kt

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package org.neo4j.graphql
22

3-
import org.antlr.v4.runtime.misc.ParseCancellationException
3+
import org.codehaus.jackson.map.ObjectMapper
44
import org.junit.Assert.assertEquals
55
import org.junit.Assert.assertTrue
6-
import org.junit.Ignore
76
import org.junit.Test
7+
import java.io.File
88

99
class FilterTest {
1010

@@ -36,9 +36,50 @@ type Query {
3636
assertQuery(query, expected, mapOf("filterPersonGender" to "male"))
3737
}
3838

39-
private fun assertQuery(query: String, expected: String, params : Map<String,Any> = emptyMap()) {
39+
private fun assertQuery(query: String, expected: String, params : Map<String,Any?> = emptyMap()) {
4040
val result = Translator(SchemaBuilder.buildSchema(schema)).translate(query).first()
4141
assertEquals(expected, result.query)
42-
assertTrue("${params} IN ${result.params}",result.params.entries.containsAll(params.entries))
42+
assertTrue("${params} IN ${result.params}", result.params.entries.containsAll(params.entries))
43+
}
44+
45+
@Test
46+
fun testTck() {
47+
val pairs = loadQueryPairsFrom("filter-test.md")
48+
val failed = pairs.map { try { assertQuery(it.first,it.second, it.third); null } catch(ae:Throwable) { ae.message } }
49+
.filterNotNull()
50+
failed.forEach(::println)
51+
val EXPECTED_FAILURES = 59
52+
assertEquals("${failed.size} failed of ${pairs.size}", EXPECTED_FAILURES, failed.size)
53+
}
54+
55+
private fun loadQueryPairsFrom(fileName: String): MutableList<Triple<String, String, Map<String, Any?>>> {
56+
val lines = File(javaClass.getResource("/$fileName").toURI())
57+
.readLines()
58+
.filterNot { it.startsWith("#") || it.isBlank() }
59+
var cypher: String? = null
60+
var graphql: String? = null
61+
var params: String? = null
62+
val testData = mutableListOf<Triple<String, String, Map<String,Any?>>>()
63+
// TODO possibly turn stream into type/data pairs adding to the last element in a reduce and add a new element when context changes
64+
for (line in lines) {
65+
when (line) {
66+
"```graphql" -> graphql = ""
67+
"```cypher" -> cypher = ""
68+
"```params" -> params = ""
69+
"```" ->
70+
if (graphql != null && cypher != null) {
71+
testData.add(Triple(graphql.trim(),cypher.trim(),params?.let { ObjectMapper().readValue(params,Map::class.java) as Map<String,Any?> } ?: emptyMap()))
72+
params = null
73+
cypher = null
74+
params = null
75+
}
76+
else ->
77+
if (cypher != null) cypher += " " + line.trim()
78+
else if (params != null) params += line.trim()
79+
else if (graphql != null) graphql += " " + line.trim()
80+
}
81+
// println("line '$line' GQL '$graphql' Cypher '$cypher'")
82+
}
83+
return testData
4384
}
4485
}

0 commit comments

Comments
 (0)