-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtableio_test.go
More file actions
165 lines (132 loc) · 3.5 KB
/
tableio_test.go
File metadata and controls
165 lines (132 loc) · 3.5 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package tableio
import (
"fmt"
"os"
"testing"
// DB Drivers
_ "github.com/denisenkom/go-mssqldb"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"github.com/katasec/utils/errx"
)
// Create a test struct
type Address struct {
City string
State string
}
type Person struct {
ID int64 `json:"id" tableio:"pk,auto"`
Name string `json:"name" tableio:"unique,required"`
Age int `json:"age"`
Address Address
}
func TestCreateTableMySql(t *testing.T) {
// Get connection string for env
conn := os.Getenv("MYSQL_CONNECTION_STRING")
if conn == "" {
fmt.Println("Error, could not get connectin string from env var MYSQL_CONNECTION_STRING")
os.Exit(1)
}
/*
Create peopleTable struct from struct definition
Provide DB connection string and driver name
*/
peopleTable, err := NewTableIO[Person]("mysql", conn)
errx.PanicOnError(err)
ExecTableOperations(peopleTable)
}
func ExecTableOperations(table *TableIO[Person]) {
// Delete and Recreate Table
table.DeleteTableIfExists(true)
table.CreateTableIfNotExists(true)
defer table.Close()
// Insert data in to table
table.Insert(Person{
Name: "John",
Age: 30,
Address: Address{
City: "New York",
State: "NY",
},
})
table.Insert(Person{
Name: "Ahmed",
Age: 45,
Address: Address{
City: "Cairo",
State: "Cairo",
},
})
// Read Data
data := table.All()
for i, person := range data {
fmt.Printf("%d. ID:%d Name:%s Age:%d City:%s \n", i+1, person.ID, person.Name, person.Age, person.Address.City)
}
// Delete table
//table.DeleteTableIfExists()
}
func TestCreateTablePgSql(t *testing.T) {
// Get connection string for env
conn := os.Getenv("PGSQL_CONNECTION_STRING")
if conn == "" {
fmt.Println("Error, could not get connectin string from env var PGSQL_CONNECTION_STRING")
os.Exit(1)
}
/*
Create peopleTable struct from struct definition
Provide DB connection string and driver name
*/
peopleTable, err := NewTableIO[Person]("postgres", conn)
errx.PanicOnError(err)
ExecTableOperations(peopleTable)
}
// func TestGenSqlForFields(t *testing.T) {
// fields := reflectx.GetDbStructFields[Hello]()
// x := reflectx.GenSqlForFields(fields)
// fmt.Println(x)
// }
// func TestSelectList(t *testing.T) {
// //helloTable, _ := NewTableIO[Entity]("sqlite3", "test.db")
// x := reflectx.GetStructFields[Entity]()
// for i, field := range x {
// fmt.Println(i, "Name:"+field.FieldName, "Type:"+field.FieldType)
// }
// //fmt.Println("Fields: " + helloTable.selectList)
// }
// type AzureCloudspace struct{}
// func TestTableNaming(t *testing.T) {
// fmt.Println(GenTableName[Entity]())
// fmt.Println(GenTableName[AzureCloudspace]())
// }
func TestCreateTableMsSql(t *testing.T) {
// Hardcoded connection string for local MSSQL
conn := "sqlserver://sa:Passw0rd123@localhost:1433?database=master"
/*
Create peopleTable struct from struct definition
Provide DB connection string and driver name
*/
peopleTable, err := NewTableIO[Person]("sqlserver", conn)
errx.PanicOnError(err)
ExecTableOperations(peopleTable)
}
func TestValidateStruct(t *testing.T) {
type TestStruct1 struct {
Age int
}
_, err := NewTableIO[TestStruct1]("", "")
if err != nil {
fmt.Println(err)
}
type TestStruct2 struct {
ID int64 `tableio:"pk,auto"`
Name string `tableio:"unique,required"`
Age int
}
_, err = NewTableIO[TestStruct2]("sqlite3", "test.db")
if err != nil {
fmt.Println(err)
} else {
fmt.Println("TestStruct2 is a valid TableIO struct")
}
}