Skip to content

Commit bbb0455

Browse files
committed
Added TCK Test class to run markdown file based TCK scripts
1 parent ba02fb3 commit bbb0455

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package demo.org.neo4j.graphql
2+
3+
import org.codehaus.jackson.map.ObjectMapper
4+
import org.junit.Assert
5+
import org.neo4j.graphql.SchemaBuilder
6+
import org.neo4j.graphql.Translator
7+
import java.io.File
8+
9+
class TckTest(val schema:String) {
10+
11+
12+
fun loadQueryPairsFrom(fileName: String): MutableList<Triple<String, String, Map<String, Any?>>> {
13+
val lines = File(javaClass.getResource("/$fileName").toURI())
14+
.readLines()
15+
.filterNot { it.startsWith("#") || it.isBlank() }
16+
var cypher: String? = null
17+
var graphql: String? = null
18+
var params: String? = null
19+
val testData = mutableListOf<Triple<String, String, Map<String,Any?>>>()
20+
// TODO possibly turn stream into type/data pairs adding to the last element in a reduce and add a new element when context changes
21+
for (line in lines) {
22+
when (line) {
23+
"```graphql" -> graphql = ""
24+
"```cypher" -> cypher = ""
25+
"```params" -> params = ""
26+
"```" ->
27+
if (graphql != null && cypher != null) {
28+
testData.add(Triple(graphql.trim(),cypher.trim(),params?.let { ObjectMapper().readValue(params,Map::class.java) as Map<String,Any?> } ?: emptyMap()))
29+
params = null
30+
cypher = null
31+
params = null
32+
}
33+
else ->
34+
if (cypher != null) cypher += " " + line.trim()
35+
else if (params != null) params += line.trim()
36+
else if (graphql != null) graphql += " " + line.trim()
37+
}
38+
// println("line '$line' GQL '$graphql' Cypher '$cypher'")
39+
}
40+
return testData
41+
}
42+
43+
public fun testTck(fileName: String, expectedFailures: Int) {
44+
val pairs = loadQueryPairsFrom(fileName)
45+
val failed = pairs.map {
46+
try {
47+
assertQuery(schema, it.first, it.second, it.third); null
48+
} catch (ae: Throwable) {
49+
ae.message
50+
}
51+
}
52+
.filterNotNull()
53+
failed.forEach(::println)
54+
Assert.assertEquals("${failed.size} failed of ${pairs.size}", expectedFailures, failed.size)
55+
}
56+
57+
companion object {
58+
fun assertQuery(schema:String, query: String, expected: String, params : Map<String,Any?> = emptyMap()) {
59+
val result = Translator(SchemaBuilder.buildSchema(schema)).translate(query).first()
60+
Assert.assertEquals(expected, result.query)
61+
Assert.assertTrue("${params} IN ${result.params}", result.params.entries.containsAll(params.entries))
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)