Skip to content

Commit

Permalink
refactor: テストコードのテーブルクリア処理を共通化
Browse files Browse the repository at this point in the history
テストコードの最後でテーブルをクリアする処理を各テストファイルから clear_tables.nim に移動し、共通化しました。
これにより、テストファイル内のテーブルクリア処理が簡潔になり、コードの重複も解消されました。
各データベースエンジン(MySQL, MariaDB, PostgreSQL, SQLite)のテストで clear_tables モジュールをインポートし、テスト終了時に clearTables() を呼び出すように変更しています。
  • Loading branch information
itsumura-h committed Jan 14, 2025
1 parent b460a46 commit a867209
Show file tree
Hide file tree
Showing 21 changed files with 103 additions and 49 deletions.
14 changes: 14 additions & 0 deletions tests/v2/mariadb/clear_tables.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import std/asyncdispatch
import std/json
import std/strformat
import std/strutils
import ../../../src/allographer/query_builder

proc clearTables*(rdb:MariaDBConnections) {.async.} =
let tables = rdb.table("_allographer_migrations").orderBy("id", Desc) .get().await
for table in tables:
let tableName = table["name"].getStr()
if not tableName.startsWith("_"):
rdb.raw(&"DROP TABLE IF EXISTS `{tableName}`").exec().await

rdb.raw("DROP TABLE IF EXISTS `_allographer_migrations`").exec().await
7 changes: 2 additions & 5 deletions tests/v2/mariadb/test_create_schema.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import std/strutils
import std/json
import ../../../src/allographer/schema_builder
import ./connection
import ./clear_tables


let rdb = mariadb
Expand Down Expand Up @@ -97,11 +98,7 @@ suite "Schema output after migration":
check schemaContent.contains("str_relation_id*: string")


rdb.drop(
table("TestSchemaOutput"),
table("IntRelation"),
table("StrRelation")
)
clearTables(rdb).waitFor()
# schema.nim ファイルの削除
if fileExists(schemaFilePath):
removeFile(schemaFilePath)
5 changes: 2 additions & 3 deletions tests/v2/mariadb/test_query.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import std/strformat
import ../../../src/allographer/schema_builder
import ../../../src/allographer/query_builder
import ./connection
import ./clear_tables


proc setup(rdb:MariadbConnections) =
Expand Down Expand Up @@ -510,6 +511,4 @@ suite($rdb & " insert binary"):
res = rdb.table("test").find(id).waitFor().get()
check res["pic"].getStr().len > 0

rdb.raw("DROP TABLE IF EXISTS `test`").exec().waitFor()
rdb.raw("DROP TABLE IF EXISTS `user`").exec().waitFor()
rdb.raw("DROP TABLE IF EXISTS `auth`").exec().waitFor()
clearTables(rdb).waitFor()
5 changes: 5 additions & 0 deletions tests/v2/mariadb/test_schema.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import std/options
import ../../../src/allographer/schema_builder
import ../../../src/allographer/query_builder
import ./connection
import ./clear_tables


let rdb = mariadb

Expand Down Expand Up @@ -291,3 +293,6 @@ suite($rdb & " primary"):
primary = @["index1", "index2"]
)
)


clearTables(rdb).waitFor()
4 changes: 2 additions & 2 deletions tests/v2/mariadb/test_transaction.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import std/asyncdispatch
import ../../../src/allographer/schema_builder
import ../../../src/allographer/query_builder
import ./connection
import ./clear_tables


proc setUp(rdb:MariadbConnections) =
Expand Down Expand Up @@ -121,5 +122,4 @@ suite("transaction"):
)().waitFor()


rdb.raw("DROP TABLE `user`").exec().waitFor()
rdb.raw("DROP TABLE `auth`").exec().waitFor()
clearTables(rdb).waitFor()
14 changes: 14 additions & 0 deletions tests/v2/mysql/clear_tables.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import std/asyncdispatch
import std/json
import std/strformat
import std/strutils
import ../../../src/allographer/query_builder

proc clearTables*(rdb:MySQLConnections) {.async.} =
let tables = rdb.table("_allographer_migrations").orderBy("id", Desc) .get().await
for table in tables:
let tableName = table["name"].getStr()
if not tableName.startsWith("_"):
rdb.raw(&"DROP TABLE IF EXISTS `{tableName}`").exec().await

rdb.raw("DROP TABLE IF EXISTS `_allographer_migrations`").exec().await
8 changes: 3 additions & 5 deletions tests/v2/mysql/test_create_schema.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import std/strutils
import std/json
import ../../../src/allographer/schema_builder
import ./connection
import ./clear_tables


let rdb = mysql
let schemaFilePath = getCurrentDir() / "schema.nim"
Expand Down Expand Up @@ -96,11 +98,7 @@ suite "Schema output after migration":
check schemaContent.contains("str_relation_id*: string")


rdb.drop(
table("TestSchemaOutput"),
table("IntRelation"),
table("StrRelation")
)
clearTables(rdb).waitFor()
# schema.nim ファイルの削除
if fileExists(schemaFilePath):
removeFile(schemaFilePath)
6 changes: 3 additions & 3 deletions tests/v2/mysql/test_query.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import std/strformat
import ../../../src/allographer/schema_builder
import ../../../src/allographer/query_builder
import ./connection
import ./clear_tables


proc setup(rdb:MysqlConnections) =
Expand Down Expand Up @@ -513,6 +514,5 @@ suite($rdb & " insert binary"):
res = rdb.table("test").find(id).waitFor().get()
check res["pic"].getStr().len > 0

rdb.raw("DROP TABLE IF EXISTS `test`").exec().waitFor()
rdb.raw("DROP TABLE IF EXISTS `user`").exec().waitFor()
rdb.raw("DROP TABLE IF EXISTS `auth`").exec().waitFor()

clearTables(rdb).waitFor()
5 changes: 5 additions & 0 deletions tests/v2/mysql/test_schema.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import std/options
import ../../../src/allographer/schema_builder
import ../../../src/allographer/query_builder
import ./connection
import ./clear_tables


let rdb = mysql

Expand Down Expand Up @@ -600,3 +602,6 @@ suite($rdb & " primary"):
primary = @["index1", "index2"]
)
)


clearTables(rdb).waitFor()
4 changes: 2 additions & 2 deletions tests/v2/mysql/test_transaction.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import std/asyncdispatch
import ../../../src/allographer/schema_builder
import ../../../src/allographer/query_builder
import ./connection
import ./clear_tables


proc setUp(rdb:MysqlConnections) =
Expand Down Expand Up @@ -121,5 +122,4 @@ suite("transaction"):
)().waitFor()


rdb.raw("DROP TABLE `user`").exec().waitFor()
rdb.raw("DROP TABLE `auth`").exec().waitFor()
clearTables(rdb).waitFor()
14 changes: 14 additions & 0 deletions tests/v2/postgres/clear_tables.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import std/asyncdispatch
import std/json
import std/strformat
import std/strutils
import ../../../src/allographer/query_builder

proc clearTables*(rdb:PostgresConnections) {.async.} =
let tables = rdb.table("_allographer_migrations").orderBy("id", Desc) .get().await
for table in tables:
let tableName = table["name"].getStr()
if not tableName.startsWith("_"):
rdb.raw(&"DROP TABLE IF EXISTS \"{tableName}\"").exec().await

rdb.raw("DROP TABLE IF EXISTS \"_allographer_migrations\"").exec().await
8 changes: 2 additions & 6 deletions tests/v2/postgres/test_create_schema.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import std/strutils
import std/json
import ../../../src/allographer/schema_builder
import ./connection

import ./clear_tables
let rdb = postgres
let schemaFilePath = getCurrentDir() / "schema.nim"

Expand Down Expand Up @@ -96,11 +96,7 @@ suite "Schema output after migration":
check schemaContent.contains("str_relation_id*: string")


rdb.drop(
table("TestSchemaOutput"),
table("IntRelation"),
table("StrRelation")
)
clearTables(rdb).waitFor()
# schema.nim ファイルの削除
if fileExists(schemaFilePath):
removeFile(schemaFilePath)
5 changes: 2 additions & 3 deletions tests/v2/postgres/test_query.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import std/strformat
import ../../../src/allographer/schema_builder
import ../../../src/allographer/query_builder
import ./connection
import ./clear_tables


# =============================================================================
Expand Down Expand Up @@ -521,6 +522,4 @@ suite($rdb & " binary data"):
check res["pic"].getStr().len > 0


rdb.raw("DROP TABLE IF EXISTS \"test\"").exec().waitFor()
rdb.raw("DROP TABLE IF EXISTS \"user\"").exec().waitFor()
rdb.raw("DROP TABLE IF EXISTS \"auth\"").exec().waitFor()
clearTables(rdb).waitFor()
11 changes: 3 additions & 8 deletions tests/v2/postgres/test_schema.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import std/options
import ../../../src/allographer/schema_builder
import ../../../src/allographer/query_builder
import ./connection
import ./clear_tables

let rdb = postgres

Expand Down Expand Up @@ -292,11 +293,5 @@ suite($rdb & " primary"):
)
)


rdb.raw("DROP TABLE IF EXISTS \"TypeIndex\"").exec().waitFor()
rdb.raw("DROP TABLE IF EXISTS \"TypeUnique\"").exec().waitFor()
rdb.raw("DROP TABLE IF EXISTS \"StrRelation\"").exec().waitFor()
rdb.raw("DROP TABLE IF EXISTS \"IntRelation\"").exec().waitFor()
rdb.raw("DROP TABLE IF EXISTS \"TypeIndex_renamed\"").exec().waitFor()
rdb.raw("DROP TABLE IF EXISTS \"relation\"").exec().waitFor()


clearTables(rdb).waitFor()
4 changes: 2 additions & 2 deletions tests/v2/postgres/test_transaction.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import std/asyncdispatch
import ../../../src/allographer/schema_builder
import ../../../src/allographer/query_builder
import ./connection
import ./clear_tables


proc setUp(rdb:PostgresConnections) =
Expand Down Expand Up @@ -121,5 +122,4 @@ suite("transaction"):
)().waitFor()


rdb.raw("DROP TABLE \"user\"").exec().waitFor()
rdb.raw("DROP TABLE \"auth\"").exec().waitFor()
clearTables(rdb).waitFor()
14 changes: 14 additions & 0 deletions tests/v2/sqlite/clear_tables.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import std/asyncdispatch
import std/json
import std/strformat
import std/strutils
import ../../../src/allographer/query_builder

proc clearTables*(rdb:SqliteConnections) {.async.} =
let tables = rdb.table("_allographer_migrations").orderBy("id", Desc) .get().await
for table in tables:
let tableName = table["name"].getStr()
if not tableName.startsWith("_"):
rdb.raw(&"DROP TABLE IF EXISTS \"{tableName}\"").exec().await

rdb.raw("DROP TABLE IF EXISTS \"_allographer_migrations\"").exec().await
7 changes: 2 additions & 5 deletions tests/v2/sqlite/test_create_schema.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import std/strutils
import std/json
import ../../../src/allographer/schema_builder
import ./connection
import ./clear_tables

let rdb = sqlite
let schemaFilePath = getCurrentDir() / "schema.nim"
Expand Down Expand Up @@ -96,11 +97,7 @@ suite "Schema output after migration":
check schemaContent.contains("str_relation_id*: string")


rdb.drop(
table("TestSchemaOutput"),
table("IntRelation"),
table("StrRelation")
)
clearTables(rdb).waitFor()
# schema.nim ファイルの削除
if fileExists(schemaFilePath):
removeFile(schemaFilePath)
5 changes: 2 additions & 3 deletions tests/v2/sqlite/test_query.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import std/strformat
import ../../../src/allographer/schema_builder
import ../../../src/allographer/query_builder
import ./connection
import ./clear_tables


# =============================================================================
Expand Down Expand Up @@ -515,6 +516,4 @@ suite($rdb & " insert binary"):
check res["pic"].getStr().len > 0


rdb.raw("DROP TABLE IF EXISTS \"test\"").exec().waitFor()
rdb.raw("DROP TABLE IF EXISTS \"user\"").exec().waitFor()
rdb.raw("DROP TABLE IF EXISTS \"auth\"").exec().waitFor()
clearTables(rdb).waitFor()
4 changes: 4 additions & 0 deletions tests/v2/sqlite/test_schema.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import std/options
import ../../../src/allographer/schema_builder
import ../../../src/allographer/query_builder
import ./connection
import ./clear_tables

let rdb = sqlite

Expand Down Expand Up @@ -291,3 +292,6 @@ suite($rdb & " primary"):
primary = @["index1", "index2"]
)
)


clearTables(rdb).waitFor()
4 changes: 2 additions & 2 deletions tests/v2/sqlite/test_transaction.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import std/asyncdispatch
import ../../../src/allographer/schema_builder
import ../../../src/allographer/query_builder
import ./connection
import ./clear_tables


proc setUp(rdb:SqliteConnections) =
Expand Down Expand Up @@ -124,5 +125,4 @@ suite("transaction"):
)().waitFor()


rdb.raw("DROP TABLE \"user\"").exec().waitFor()
rdb.raw("DROP TABLE \"auth\"").exec().waitFor()
clearTables(rdb).waitFor()
4 changes: 4 additions & 0 deletions tests/v2/surrealdb/clear_tables.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import std/strformat
import std/strutils
import ../../../src/allographer/query_builder


proc clearTables*(rdb:SurrealConnections) {.async.} =
let dbInfo = rdb.raw("INFO FOR DB").info().await
echo "=".repeat(30)
echo "dbInfo: ", dbInfo

let tables = dbInfo[0]["result"]["tb"]
for (table, _) in tables.pairs:
if not table.startsWith("_"):
Expand Down

0 comments on commit a867209

Please sign in to comment.