-
Notifications
You must be signed in to change notification settings - Fork 34
/
db_client_test.go
131 lines (106 loc) · 3.19 KB
/
db_client_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package gonymizer
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestCheckIfDbExists(t *testing.T) {
conf := GetTestDbConf(TestDb)
// First connect to postgres db to get connection
conf.DefaultDBName = "postgres"
dbConn, err := OpenDB(conf)
require.Nil(t, err)
require.NotNil(t, dbConn)
// Next check to make sure the database exists
doesExist, err := CheckIfDbExists(dbConn, conf.DefaultDBName)
require.Nil(t, err)
require.True(t, doesExist)
}
func TestGetAllProceduresInSchema(t *testing.T) {
conf := GetTestDbConf(TestDb)
// Check to make sure Public schema has procedures
procs, err := GetAllProceduresInSchema(conf, "public")
if len(procs) < 1 {
t.Fatal("Using 'public' as our schema we received 0 procedures back")
}
require.Nil(t, err)
}
func TestGetAllSchemaColumns(t *testing.T) {
conf := GetTestDbConf(TestDb)
dbConn, err := OpenDB(conf)
require.Nil(t, err)
require.NotNil(t, dbConn)
_, err = GetAllSchemaColumns(dbConn)
require.Nil(t, err)
}
func TestGetAllTablesInSchema(t *testing.T) {
conf := GetTestDbConf(TestDb)
// Check to make sure empty string input for schema works
tables, err := GetAllTablesInSchema(conf, "")
if len(tables) < 1 {
t.Fatal("Using empty string as our schema we received 0 tables back")
}
require.Nil(t, err)
// Check to make sure public schema has tables
tables, err = GetAllTablesInSchema(conf, "public")
if len(tables) < 1 {
t.Fatal("Using empty string '' as our schema we received 0 tables back")
}
require.Nil(t, err)
}
func TestGetSchemasInDatabase(t *testing.T) {
conf := GetTestDbConf(TestDb)
_, err := GetSchemasInDatabase(conf, []string{"public"})
require.Nil(t, err)
_, err = GetSchemasInDatabase(conf, []string{})
require.Nil(t, err)
}
func TestGetSchemaColumnEquals(t *testing.T) {
conf := GetTestDbConf(TestDb)
dbConn, err := OpenDB(conf)
require.Nil(t, err)
// Get column data for "public" schema
_, err = GetSchemaColumnEquals(dbConn, "public")
require.Nil(t, err)
}
func TestGetTableRowCountsInDB(t *testing.T) {
conf := GetTestDbConf(TestDb)
counts, err := GetTableRowCountsInDB(conf, "", nil)
require.Nil(t, err)
total := 0
for _, row := range *counts {
total += *row.Count
}
if total < 1 {
t.Fatal("Unable to get any row counts")
}
}
func TestRenameDatabase(t *testing.T) {
var count int
const tempDbFrom = "anon_rename_db_test"
const tempDbTo = tempDbFrom + "_renamed"
conf := GetTestDbConf(tempDbTo)
// Make sure to remove previous failures
_ = DropDatabase(conf)
conf = GetTestDbConf(tempDbFrom)
_ = DropDatabase(conf)
require.Nil(t, CreateDatabase(conf))
// Rename database
conf.DefaultDBName = "postgres" // Switch to postgres so we are not connected to DBs to be renamed
dbConn, err := OpenDB(conf)
require.Nil(t, err)
require.Nil(t, RenameDatabase(dbConn, tempDbFrom, tempDbTo))
query := `
SELECT COUNT(*)
FROM pg_catalog.pg_database
WHERE datname=$1
`
// Check to see if it exist
result := dbConn.QueryRow(query, tempDbTo)
err = result.Scan(&count)
require.Nil(t, err)
if count < 1 {
t.Fatalf("Renamed database '%s'->'%s', but could not find the latter in pg_catalog", tempDbFrom, tempDbTo)
}
conf.DefaultDBName = tempDbTo
require.Nil(t, DropDatabase(conf))
}