forked from faisaltheparttimecoder/mock-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema_test.go
50 lines (45 loc) · 1.68 KB
/
schema_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"testing"
)
// Test: MockSchema, check if the data is loaded with schema keyword
func TestMockSchema(t *testing.T) {
setDatabaseConfigForTest()
postgresOrGreenplum()
fakeTable := "fake_table"
cmdOptions.SchemaName = "mock_data_schema"
cmdOptions.Rows = 100
createSchemaStmt := fmt.Sprintf("CREATE SCHEMA %s;", cmdOptions.SchemaName)
createFakeSchemaTableStmt := `
DROP TABLE IF EXISTS %[1]s.%[2]s1;
CREATE TABLE %[1]s.%[2]s1 (id serial, name text, active bool);
DROP TABLE IF EXISTS %[1]s.%[2]s2;
CREATE TABLE %[1]s.%[2]s2 (country varchar, active_date date, last_login timestamp);
DROP TABLE IF EXISTS %[1]s.%[2]s3;
CREATE TABLE %[1]s.%[2]s3 (rating int, price money, balance numeric(4,2));
DROP TABLE IF EXISTS %[1]s.%[2]s4;
CREATE TABLE %[1]s.%[2]s4 (inactive bool, gender char, address text);
DROP TABLE IF EXISTS %[1]s.%[2]s5;
CREATE TABLE %[1]s.%[2]s5 (category varchar, comment text, feedback varchar(500));
`
_, err := ExecuteDB(createSchemaStmt)
if err != nil {
t.Errorf("TestMockSchema, failed creating schema, err: %v", err)
}
_, err = ExecuteDB(fmt.Sprintf(createFakeSchemaTableStmt, cmdOptions.SchemaName, fakeTable))
if err != nil {
t.Errorf("TestMockSchema, failed creating schema tables, err: %v", err)
}
MockSchema()
for i := 0; i < 5; i++ {
tableNumber := i + 1
tabName := fmt.Sprintf("%s%d", fakeTable, tableNumber)
tableNameWithSchema := GenerateTableName(tabName, cmdOptions.SchemaName)
t.Run(fmt.Sprintf("checking_rows_count_of_table_%s", tabName), func(t *testing.T) {
if got := TotalRows(tableNameWithSchema); got != cmdOptions.Rows {
t.Errorf("TestMockSchema = %v, want %v", got, cmdOptions.Rows)
}
})
}
}