Skip to content

Commit

Permalink
DBから整数値を取り出してアサートする
Browse files Browse the repository at this point in the history
  • Loading branch information
jnuank committed Nov 21, 2021
1 parent 9b53385 commit 5955d73
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 24 deletions.
2 changes: 1 addition & 1 deletion e2e/specs/db/json_db.spec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

## クエリを指定して、一意の値(整数)を取得する
tags: unimplemented
* "schemaName"スキーマの"tableName"テーブルの、"columnName""whereString"で取得した一意の結果が整数の"test"である
* "schemaName"スキーマの"intTable"テーブルの、"id""4d30aa18-ec51-439c-a98c-e5e2c3223509"で取得した一意の"int_column"が整数の"1"である

___
* テスト用のスキーマ、テーブルを削除する
22 changes: 12 additions & 10 deletions e2e/src/test/kotlin/com/uzabase/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,51 @@ class Database(
value shouldBeEqualTo result
}

@Step("<schemaName>スキーマの<tableName>テーブルの、<columnName>を<whereString>で取得した一意の結果が整数の<vale>である")
@Step("<schemaName>スキーマの<tableName>テーブルの、<columnName>を<whereString>で取得した一意の<valueColumn>が整数の<vale>である")
fun getIntParameterFromDatabase(
schemaName: String,
tableName: String,
columnName: String,
whereString: String,
valueColumn: String,
value: Int
) {
val query = """
select $columnName from $schemaName.$tableName where $whereString;
""".trimIndent()
val result = TODO() //いい感じにSQLを実行する
val result =
Table(url, username, password, schemaName, tableName).where(
columnName,
whereString
).first().column(valueColumn).value
value shouldBeEqualTo result
}

@Step("テスト用のスキーマ、テーブルを用意する")
fun setupTable() {
val test = this.javaClass
val createAndInsertQuery = this.javaClass
.classLoader
.getResourceAsStream("data.sql")
.getResourceAsStream("setup.sql")
?.bufferedReader()
?.use { it.readText() }

println(test)

val conn: Connection =
DriverManager.getConnection(url, username, password)

conn.createStatement().execute(test)
conn.createStatement().execute(createAndInsertQuery)
}

@Step("テスト用のスキーマ、テーブルを削除する")
fun truncateTable() {
val test = this.javaClass
val truncateQuery = this.javaClass
.classLoader
.getResourceAsStream("truncate.sql")
?.bufferedReader()
?.use { it.readText() }

println(test)
val conn: Connection =
DriverManager.getConnection(url, username, password)

conn.createStatement().execute(test)
conn.createStatement().execute(truncateQuery)
}
}
9 changes: 0 additions & 9 deletions e2e/src/test/resources/data.sql

This file was deleted.

17 changes: 17 additions & 0 deletions e2e/src/test/resources/setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE SCHEMA IF NOT EXISTS schemaName;

CREATE TABLE IF NOT EXISTS schemaName.tableName
(
id UUID primary key,
string_column text
);

INSERT INTO schemaName.tableName VALUES ('bf9626ab-6ecd-4f15-be70-ac88fe4ba0f0', 'test');

create table if not exists schemaname.intTable
(
id UUID primary key,
int_column INTEGER
);

INSERT INTO schemaName.intTable VALUES ('4d30aa18-ec51-439c-a98c-e5e2c3223509', 1);
3 changes: 2 additions & 1 deletion e2e/src/test/resources/truncate.sql
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
TRUNCATE TABLE schemaName.tableName
TRUNCATE TABLE schemaName.tableName;
TRUNCATE TABLE schemaName.intTable;
13 changes: 13 additions & 0 deletions playtest-db/src/main/kotlin/com/uzabase/playtest/db/Table.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ class Table(
fun where(column: String, matchValue: String): List<Row> {
val result = Request(source, "select * from $schemaName.$tableName where $column = '$matchValue'")

var rows = mutableListOf<Row>()
for (r in result.rowsList) {
var cols = mutableListOf<Column>()
for (c in r.valuesList) {
cols.add(Column(c.columnName, c.value))
}
rows.add(Row(cols))
}
return rows
}
fun where(column: String, matchValue: Int): List<Row> {
val result = Request(source, "select * from $schemaName.$tableName where $column = $matchValue")

var rows = mutableListOf<Row>()
for (r in result.rowsList) {
var cols = mutableListOf<Column>()
Expand Down
23 changes: 20 additions & 3 deletions playtest-db/src/test/kotlin/com/uzabase/playtest/db/TableTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.uzabase.playtest.db

import org.amshove.kluent.shouldBeEqualTo
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.sql.Connection
Expand All @@ -21,14 +22,22 @@ internal class TableTest {
fun setup() {
conn.createStatement().execute("CREATE SCHEMA IF NOT EXISTS test_schema")
conn.createStatement().execute("""
CREATE TABLE test_schema.test_table (
CREATE TABLE IF NOT EXISTS test_schema.test_table (
id UUID primary key,
string_column text
string_column text,
int_column INTEGER
)
""".trimIndent())

conn.createStatement().execute("""
INSERT INTO test_schema.test_table VALUES ('bf9626ab-6ecd-4f15-be70-ac88fe4ba0f0', 'value1')
INSERT INTO test_schema.test_table VALUES ('bf9626ab-6ecd-4f15-be70-ac88fe4ba0f0', 'value1', 1)
""".trimIndent())
}

@AfterEach
fun teadDown() {
conn.createStatement().execute("""
TRUNCATE TABLE test_schema.test_table
""".trimIndent())
}

Expand All @@ -40,6 +49,14 @@ internal class TableTest {
val actual = table.where("string_column", "value1")
// then
actual.first().column("string_column").value shouldBeEqualTo "value1"
}

@Test
fun 指定した条件で取得したレコードから任意のカラムに入っている整数値を取得する() {
val table = Table("jdbc:h2:mem:test;MODE=PostgreSQL;DB_CLOSE_DELAY=-1", "sa", "", "test_schema", "test_table")

val actual = table.where("int_column", 1)

actual.first().column("int_column").value shouldBeEqualTo 1
}
}

0 comments on commit 5955d73

Please sign in to comment.