Compare commits

...

8 commits

Author SHA1 Message Date
Simon Vieille 11b676446d
add postgres dump for tests
Some checks failed
ci/woodpecker/push/test Pipeline failed
ci/woodpecker/push/build unknown status
2024-03-31 22:20:30 +02:00
Simon Vieille 801b11e33f
add schema for tests 2024-03-31 22:20:19 +02:00
Simon Vieille 40d474b6eb
fix tests for app 2024-03-31 22:19:58 +02:00
Simon Vieille 83682511b3
add IsInterger property in Data struct
add tests
2024-03-31 22:19:32 +02:00
Simon Vieille 60cbaf1530
add IsInterger property in Data struct
add tests
2024-03-31 22:19:15 +02:00
Simon Vieille 5be7927799
fix dsn when when th database is postgresql 2024-03-31 22:18:28 +02:00
Simon Vieille 038d0a2d6b use integer when required in queries 2024-03-31 22:17:07 +02:00
Simon Vieille 16bdb470dc
tests: update mysql password 2024-03-31 22:16:28 +02:00
8 changed files with 79 additions and 20 deletions

View file

@ -45,4 +45,4 @@ services:
image: *mysql_image
environment:
- MYSQL_DATABASE=test
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MARIADB_ROOT_PASSWORD=root

View file

@ -60,8 +60,13 @@ func (a *App) TruncateTable(c config.SchemaConfigAction) error {
pCounter := 1
for _, col := range c.PrimaryKey {
pkeys = append(pkeys, fmt.Sprintf("%s=:p%s", col, strconv.Itoa(pCounter)))
pCounter = pCounter + 1
if row[col].IsInteger {
value, _ := strconv.Atoi(row[col].Value)
pkeys = append(pkeys, fmt.Sprintf("%s=%d", col, value))
} else {
pkeys = append(pkeys, fmt.Sprintf("%s=:p%s", col, strconv.Itoa(pCounter)))
pCounter = pCounter + 1
}
}
sql := fmt.Sprintf(
@ -74,8 +79,10 @@ func (a *App) TruncateTable(c config.SchemaConfigAction) error {
pCounter = 1
for _, col := range c.PrimaryKey {
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(pCounter)), row[col].Value)
pCounter = pCounter + 1
if !row[col].IsInteger {
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(pCounter)), row[col].Value)
pCounter = pCounter + 1
}
}
a.Db.QueryRow(stmt.GetParsedQuery(), (stmt.GetParsedParameters())...).Scan(&scan)
@ -148,19 +155,23 @@ func (a *App) UpdateRows(c config.SchemaConfigAction, globalColumns map[string]s
for _, row := range rows {
updates := []string{}
pkeys := []string{}
values := make(map[int]string)
values := make(map[int][]string)
pCounter := 1
for col, value := range row {
if value.IsUpdated && !value.IsVirtual {
values[pCounter] = value.Value
if value.IsInteger {
values[pCounter] = []string{value.Value, "int"}
} else {
values[pCounter] = []string{value.Value, "string"}
}
updates = append(updates, fmt.Sprintf("%s=:p%s", col, strconv.Itoa(pCounter)))
pCounter = pCounter + 1
}
}
for _, col := range c.PrimaryKey {
values[pCounter] = row[col].Value
values[pCounter] = []string{row[col].Value, "string"}
pkeys = append(pkeys, fmt.Sprintf("%s=:p%s", col, strconv.Itoa(pCounter)))
pCounter = pCounter + 1
}
@ -177,7 +188,12 @@ func (a *App) UpdateRows(c config.SchemaConfigAction, globalColumns map[string]s
pCounter = 1
for i, value := range values {
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(i)), value)
if value[1] == "string" {
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(i)), value[0])
} else {
newValue, _ := strconv.Atoi(value[0])
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(i)), newValue)
}
}
a.Db.QueryRow(stmt.GetParsedQuery(), (stmt.GetParsedParameters())...).Scan(&scan)

View file

@ -2,7 +2,9 @@ package app
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"gitnet.fr/deblan/database-anonymizer/config"
"gitnet.fr/deblan/database-anonymizer/faker"
"testing"
@ -47,30 +49,30 @@ func TestAppDoAction(t *testing.T) {
func TestAppRun(t *testing.T) {
schema, _ := config.LoadSchemaConfigFromFile("../tests/schema.yml")
dsns := []string{
"mysql://root:root@tcp(service-mysql)/test",
// "postgres://postgres@tcp(service-postgres)/test",
}
dsns := make(map[string]string)
dsns["mysql"] = "mysql://root:root@tcp(service-mysql)/test"
dsns["postgres"] = "postgres://postgres:postgres@service-postgres:5432/test?sslmode=disable"
for _, dsn := range dsns {
var count int
for dbtype, dsn := range dsns {
databaseConfig, _ := config.LoadDatabaseConfig(dsn)
db, _ := sql.Open(databaseConfig.Type, databaseConfig.Dsn)
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")
t.Fatalf(fmt.Sprintf("TestAppRuny: table_truncate1 check failed (%s)", dbtype))
}
row = db.QueryRow("SELECT COUNT(*) FROM table_truncate2")
row.Scan(&count)
if count != 1 {
t.Fatalf("TestAppRuny: table_truncate2 check failed")
t.Fatalf(fmt.Sprintf("TestAppRuny: table_truncate2 check failed (%s)", dbtype))
}
}
}

View file

@ -30,7 +30,12 @@ func LoadDatabaseConfig(dsn string) (DatabaseConfig, error) {
dbType := elements[0]
config.Dsn = strings.Replace(dsn, fmt.Sprintf("%s://", dbType), "", 1)
if dbType == "postgres" {
config.Dsn = dsn
} else {
config.Dsn = strings.Replace(dsn, fmt.Sprintf("%s://", dbType), "", 1)
}
config.Type = elements[0]
return config, nil

View file

@ -16,22 +16,26 @@ type Data struct {
IsVirtual bool
IsPrimaryKey bool
IsUpdated bool
IsInteger bool
}
func (d *Data) FromByte(v []byte) *Data {
d.Value = string(v)
d.IsInteger = false
return d
}
func (d *Data) FromInt64(v int64) *Data {
d.Value = strconv.FormatInt(v, 10)
d.IsInteger = true
return d
}
func (d *Data) FromString(v string) *Data {
d.Value = v
d.IsInteger = false
return d
}

View file

@ -15,6 +15,10 @@ func TestDataFroms(t *testing.T) {
t.Fatalf("TestDataFroms: FromInt64 check failed")
}
if !d.IsInteger {
t.Fatalf("TestDataFroms: FromInt64 + IsInteger check failed")
}
v := []byte{'A', 'B', 'C'}
d.FromByte(v)
@ -22,6 +26,10 @@ func TestDataFroms(t *testing.T) {
if d.Value != "ABC" {
t.Fatalf("TestDataFroms: FromByte check failed")
}
if d.IsInteger {
t.Fatalf("TestDataFroms FromByte + IsInteger check failed")
}
}
func TestDataIsTwigExpression(t *testing.T) {

25
tests/postgres_data.sql Normal file
View file

@ -0,0 +1,25 @@
DROP TABLE IF EXISTS "table_truncate1";
DROP SEQUENCE IF EXISTS table_truncate1_id_seq;
CREATE SEQUENCE table_truncate1_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;
CREATE TABLE "public"."table_truncate1" (
"id" integer DEFAULT nextval('table_truncate1_id_seq') NOT NULL,
CONSTRAINT "table_truncate1_pkey" PRIMARY KEY ("id")
) WITH (oids = false);
INSERT INTO "table_truncate1" ("id") VALUES (1), (2), (3);
DROP TABLE IF EXISTS "table_truncate2";
DROP SEQUENCE IF EXISTS table_truncate2_id_seq;
CREATE SEQUENCE table_truncate2_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;
CREATE TABLE "public"."table_truncate2" (
"id" integer DEFAULT nextval('table_truncate2_id_seq') NOT NULL,
"delete_me" boolean NOT NULL,
CONSTRAINT "table_truncate2_pkey" PRIMARY KEY ("id")
) WITH (oids = false);
INSERT INTO "table_truncate2" ("id", "delete_me") VALUES
(1, 't'),
(2, 't'),
(3, 'f');

View file

@ -2,7 +2,6 @@ rules:
actions:
- table: table_truncate1
truncate: true
- table: table_truncate2
query: 'select * from table_truncate2 where delete_me=1'
query: 'select * from table_truncate2 where delete_me=true'
truncate: true