From a5fc2071da0a7ce1f8ee614843ae3943262925de Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 20 Mar 2024 14:10:02 +0100 Subject: [PATCH] [wip] add run tests --- app/app_test.go | 46 +++++++++++++++++++++++++++++++++++++------- tests/mysql_data.sql | 25 ++++++++++++++++++++++++ tests/schema.yml | 4 ++++ 3 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 tests/mysql_data.sql create mode 100644 tests/schema.yml diff --git a/app/app_test.go b/app/app_test.go index 5949cd5..ed58c61 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -1,22 +1,19 @@ package app import ( + "database/sql" _ "github.com/go-sql-driver/mysql" "gitnet.fr/deblan/database-anonymizer/config" "gitnet.fr/deblan/database-anonymizer/faker" "testing" ) -func GetApp() App { - return App{ +func TestAppCreateSelectQuery(t *testing.T) { + c := config.SchemaConfigAction{Table: "foo"} + app := App{ FakeManager: faker.NewFakeManager(), DbConfig: config.DatabaseConfig{Type: "mysql", Dsn: "mysql://foo:bar@tests"}, } -} - -func TestAppCreateSelectQuery(t *testing.T) { - c := config.SchemaConfigAction{Table: "foo"} - app := GetApp() if app.CreateSelectQuery(c) != "SELECT * FROM `foo`" { t.Fatalf("TestAppCreateSelectQuery: empty configured query check failed") @@ -28,3 +25,38 @@ func TestAppCreateSelectQuery(t *testing.T) { t.Fatalf("TestAppCreateSelectQuery: configured query check failed") } } + +func TestAppDoAction(t *testing.T) { + c := config.SchemaConfigAction{Table: "foo"} + app := App{ + FakeManager: faker.NewFakeManager(), + DbConfig: config.DatabaseConfig{Type: "mysql", Dsn: "mysql://foo:bar@tests"}, + } + + if app.CreateSelectQuery(c) != "SELECT * FROM `foo`" { + t.Fatalf("TestAppCreateSelectQuery: empty configured query check failed") + } + + c = config.SchemaConfigAction{Table: "foo", Query: "query"} + + if app.CreateSelectQuery(c) != "query" { + t.Fatalf("TestAppCreateSelectQuery: configured query check failed") + } +} + +func TestAppRun(t *testing.T) { + databaseConfig, _ := config.LoadDatabaseConfig("mysql://tcp(service-mysql)/test") + db, _ := sql.Open(databaseConfig.Type, databaseConfig.Dsn) + schema, _ := config.LoadSchemaConfigFromFile("../tests/schema.yml") + + app := App{} + app.Run(db, schema, faker.NewFakeManager(), databaseConfig) + + var count int + row := db.QueryRow("SELECT COUNT(*) FROM `table_truncate1`") + row.Scan(&count) + + if count != 0 { + t.Fatalf("TestAppRuny: table_truncate1 check failed") + } +} diff --git a/tests/mysql_data.sql b/tests/mysql_data.sql new file mode 100644 index 0000000..8bdb525 --- /dev/null +++ b/tests/mysql_data.sql @@ -0,0 +1,25 @@ +SET NAMES utf8; +SET time_zone = '+00:00'; +SET foreign_key_checks = 0; +SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO'; + +SET NAMES utf8mb4; + +DROP TABLE IF EXISTS `table_truncate1`; +CREATE TABLE `table_truncate1` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + + +DROP TABLE IF EXISTS `table_truncate2`; +CREATE TABLE `table_truncate2` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `delete_me` tinyint(4) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + +INSERT INTO `table_truncate2` (`id`, `delete_me`) VALUES +(1, 1), +(2, 1), +(3, 0); diff --git a/tests/schema.yml b/tests/schema.yml new file mode 100644 index 0000000..e17279b --- /dev/null +++ b/tests/schema.yml @@ -0,0 +1,4 @@ +rules: + actions: + - table: table_truncate1 + truncate: false