|
| 1 | +package com.ladybugdb |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 4 | +import org.junit.jupiter.api.Assertions.assertFalse |
| 5 | +import org.junit.jupiter.api.Assertions.assertTrue |
| 6 | +import org.junit.jupiter.api.Test |
| 7 | + |
| 8 | +class KotlinUsageTest : TestBase() { |
| 9 | + @Test |
| 10 | + fun queryResultCanBeReadFromKotlin() { |
| 11 | + val result = TestHelper.getConnection().query("MATCH (p:person) RETURN p.fName ORDER BY p.ID LIMIT 2") |
| 12 | + try { |
| 13 | + assertTrue(result.isSuccess) |
| 14 | + assertEquals(1, result.numColumns) |
| 15 | + assertEquals("p.fName", result.getColumnName(0)) |
| 16 | + |
| 17 | + val names = mutableListOf<String>() |
| 18 | + while (result.hasNext()) { |
| 19 | + val tuple = result.next |
| 20 | + names += tuple.getValue(0).getValue<String>() |
| 21 | + } |
| 22 | + |
| 23 | + assertEquals(listOf("Alice", "Bob"), names) |
| 24 | + } finally { |
| 25 | + result.close() |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + fun preparedStatementCanBeExecutedFromKotlin() { |
| 31 | + val statement = TestHelper.getConnection() |
| 32 | + .prepare("MATCH (p:person) WHERE p.fName = \$name RETURN COUNT(*)") |
| 33 | + try { |
| 34 | + assertTrue(statement.isSuccess) |
| 35 | + assertTrue(statement.isReadOnly) |
| 36 | + |
| 37 | + val params = mapOf("name" to Value("Alice")) |
| 38 | + val result = TestHelper.getConnection().execute(statement, params) |
| 39 | + try { |
| 40 | + assertTrue(result.isSuccess) |
| 41 | + assertTrue(result.hasNext()) |
| 42 | + assertEquals(1L, result.next.getValue(0).getValue<Long>()) |
| 43 | + } finally { |
| 44 | + params.values.forEach { it.close() } |
| 45 | + result.close() |
| 46 | + } |
| 47 | + } finally { |
| 48 | + statement.close() |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun valuesAndDataTypesAreUsableFromKotlin() { |
| 54 | + val value = Value(42L) |
| 55 | + try { |
| 56 | + assertFalse(value.isNull) |
| 57 | + assertEquals(42L, value.getValue<Long>()) |
| 58 | + |
| 59 | + val dataType = value.dataType |
| 60 | + try { |
| 61 | + assertEquals(DataTypeID.INT64, dataType.id) |
| 62 | + } finally { |
| 63 | + dataType.close() |
| 64 | + } |
| 65 | + } finally { |
| 66 | + value.close() |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments