From baa2976e80e0b2f5b413534474910aed3c77e114 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Fri, 14 Jun 2019 09:04:46 +0200 Subject: [PATCH] test: getRequestFromFunction --- .../kotlin/fr/postgresjson/connexion/Connection.kt | 8 ++++++-- src/test/kotlin/fr/postgresjson/RequestTest.kt | 10 +++++++++- src/test/resources/sql/function/Test/function_test.sql | 8 ++++++++ .../sql/query/Test/{test.sql => selectOne.sql} | 0 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 src/test/resources/sql/function/Test/function_test.sql rename src/test/resources/sql/query/Test/{test.sql => selectOne.sql} (100%) diff --git a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt index 2c4b228..a1b482a 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt @@ -151,8 +151,12 @@ class Connection( } private fun fetchFunctions(functionsDirectory: File) { - functionsDirectory.walk().filter { it.isDirectory }.forEach { directory -> - directory.walk().filter { it.isFile }.forEach { file -> + functionsDirectory.walk().filter { + it.isDirectory + }.forEach { directory -> + directory.walk().filter { + it.isFile + }.forEach { file -> val fileContent = file.readText() getDefinitions(fileContent).forEach { functions[it.name] = it diff --git a/src/test/kotlin/fr/postgresjson/RequestTest.kt b/src/test/kotlin/fr/postgresjson/RequestTest.kt index 374fb43..68ad941 100644 --- a/src/test/kotlin/fr/postgresjson/RequestTest.kt +++ b/src/test/kotlin/fr/postgresjson/RequestTest.kt @@ -12,7 +12,15 @@ class RequestTest { @Test fun getRequestFromFile() { val resources = File(this::class.java.getResource("/sql/query").toURI()) - val objTest: ObjTest? = Connection(queriesDirectory = resources).getQuery("Test/test").selectOne() + val objTest: ObjTest? = Connection(queriesDirectory = resources).getQuery("Test/selectOne").selectOne() + assertTrue(objTest!!.id == 2) + assertTrue(objTest.name == "test") + } + + @Test + fun getRequestFromFunction() { + val resources = File(this::class.java.getResource("/sql/function").toURI()) + val objTest: ObjTest? = Connection(functionsDirectory = resources).getFunction("test_function").selectOne() assertTrue(objTest!!.id == 2) assertTrue(objTest.name == "test") } diff --git a/src/test/resources/sql/function/Test/function_test.sql b/src/test/resources/sql/function/Test/function_test.sql new file mode 100644 index 0000000..e79bc63 --- /dev/null +++ b/src/test/resources/sql/function/Test/function_test.sql @@ -0,0 +1,8 @@ +CREATE OR REPLACE FUNCTION test_function (name text, IN hi text default 'hello', out result json) +LANGUAGE plpgsql +AS +$$ +BEGIN + result = json_build_object('id', 2, 'name', 'test'); +END; +$$ \ No newline at end of file diff --git a/src/test/resources/sql/query/Test/test.sql b/src/test/resources/sql/query/Test/selectOne.sql similarity index 100% rename from src/test/resources/sql/query/Test/test.sql rename to src/test/resources/sql/query/Test/selectOne.sql