2021-07-30 15:52:01 +02:00
|
|
|
package sqldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2023-06-27 23:19:50 +02:00
|
|
|
func TestPgCreateTable(t *testing.T) {
|
2021-10-28 10:34:10 +02:00
|
|
|
db := Open("postgres", "host=127.0.0.1 port=5432 user=test password=test dbname=test sslmode=disable")
|
|
|
|
defer db.Close()
|
2021-07-30 15:52:01 +02:00
|
|
|
|
2022-05-20 11:00:14 +02:00
|
|
|
jsonFile, err := os.Open("test_table.json")
|
2021-07-30 15:52:01 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
defer jsonFile.Close()
|
|
|
|
|
|
|
|
byteValue, _ := ioutil.ReadAll(jsonFile)
|
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
var jsonSource TableInfo
|
|
|
|
json.Unmarshal([]byte(byteValue), &jsonSource)
|
2021-07-30 15:52:01 +02:00
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
err = db.CreateTable(jsonSource)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2021-07-30 15:52:01 +02:00
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
sch, err := db.Table("test").GetSchema()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
if len(sch.Columns) == 0 {
|
2021-07-30 15:52:01 +02:00
|
|
|
t.Errorf("Create table failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-27 23:19:50 +02:00
|
|
|
func TestPgAddColumn(t *testing.T) {
|
2021-07-30 15:52:01 +02:00
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
db := Open("postgres", "host=127.0.0.1 port=5432 user=test password=test dbname=test sslmode=disable")
|
|
|
|
defer db.Close()
|
2021-07-30 15:52:01 +02:00
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
old, err := db.Table("test").GetSchema()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2021-10-30 22:21:54 +02:00
|
|
|
db.Table("test").AddColumn("addcolumn", "integer", "comment")
|
2021-10-28 10:34:10 +02:00
|
|
|
new, err := db.Table("test").GetSchema()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2021-07-30 15:52:01 +02:00
|
|
|
|
|
|
|
if len(old.Columns) == len(new.Columns) {
|
|
|
|
t.Errorf("Column already exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-27 23:19:50 +02:00
|
|
|
func TestPgInsert(t *testing.T) {
|
2021-07-30 15:52:01 +02:00
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
db := Open("postgres", "host=127.0.0.1 port=5432 user=test password=test dbname=test sslmode=disable")
|
|
|
|
defer db.Close()
|
2021-07-30 15:52:01 +02:00
|
|
|
|
|
|
|
vl := make(AssRow)
|
|
|
|
vl["name"] = "toto"
|
|
|
|
vl["description"] = "tata"
|
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
old, err := db.Table("test").GetAssociativeArray([]string{"*"}, "", []string{}, "")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2021-07-30 15:52:01 +02:00
|
|
|
jsonStringOld, _ := json.Marshal(old)
|
|
|
|
fmt.Println(string(jsonStringOld))
|
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
db.Table("test").UpdateOrInsert(vl)
|
2021-07-30 15:52:01 +02:00
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
new, err := db.Table("test").GetAssociativeArray([]string{"*"}, "", []string{}, "")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2021-07-30 15:52:01 +02:00
|
|
|
jsonStringNew, _ := json.Marshal(new)
|
|
|
|
fmt.Println(string(jsonStringNew))
|
|
|
|
|
|
|
|
if len(jsonStringOld) == len(jsonStringNew) {
|
|
|
|
t.Errorf("Error row not created")
|
|
|
|
}
|
2022-05-20 15:12:04 +02:00
|
|
|
jsonFile, err := os.Open("insert.json")
|
|
|
|
defer jsonFile.Close()
|
|
|
|
var result map[string]interface{}
|
|
|
|
byteValue, _ := ioutil.ReadAll(jsonFile)
|
|
|
|
json.Unmarshal(byteValue, &result)
|
2021-07-30 15:52:01 +02:00
|
|
|
}
|
|
|
|
|
2023-06-27 23:19:50 +02:00
|
|
|
func TestPgUpdate(t *testing.T) {
|
2021-07-30 15:52:01 +02:00
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
db := Open("postgres", "host=127.0.0.1 port=5432 user=test password=test dbname=test sslmode=disable")
|
|
|
|
defer db.Close()
|
2021-07-30 15:52:01 +02:00
|
|
|
|
|
|
|
vl := make(AssRow)
|
|
|
|
vl["id"] = 1
|
|
|
|
vl["name"] = "titi"
|
|
|
|
vl["description"] = "toto"
|
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
old, err := db.Table("test").GetAssociativeArray([]string{"*"}, "", []string{}, "")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2021-07-30 15:52:01 +02:00
|
|
|
jsonStringOld, _ := json.Marshal(old)
|
|
|
|
fmt.Println(string(jsonStringOld))
|
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
db.Table("test").UpdateOrInsert(vl)
|
2021-07-30 15:52:01 +02:00
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
new, err := db.Table("test").GetAssociativeArray([]string{"*"}, "", []string{}, "")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2021-07-30 15:52:01 +02:00
|
|
|
jsonStringNew, _ := json.Marshal(new)
|
|
|
|
fmt.Println(string(jsonStringNew))
|
|
|
|
|
|
|
|
if string(jsonStringOld) == string(jsonStringNew) {
|
|
|
|
t.Errorf("Error row not updated")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-27 23:19:50 +02:00
|
|
|
func TestPgDelete(t *testing.T) {
|
2021-07-30 15:52:01 +02:00
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
db := Open("postgres", "host=127.0.0.1 port=5432 user=test password=test dbname=test sslmode=disable")
|
|
|
|
defer db.Close()
|
2021-07-30 15:52:01 +02:00
|
|
|
|
|
|
|
vl := make(AssRow)
|
|
|
|
vl["id"] = 1
|
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
old, err := db.Table("test").GetAssociativeArray([]string{"*"}, "", []string{}, "")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2021-07-30 15:52:01 +02:00
|
|
|
jsonStringOld, _ := json.Marshal(old)
|
|
|
|
fmt.Println(string(jsonStringOld))
|
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
db.Table("test").Delete(vl)
|
2021-07-30 15:52:01 +02:00
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
new, err := db.Table("test").GetAssociativeArray([]string{"*"}, "", []string{}, "")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2021-07-30 15:52:01 +02:00
|
|
|
jsonStringNew, _ := json.Marshal(new)
|
|
|
|
fmt.Println(string(jsonStringNew))
|
|
|
|
|
|
|
|
if len(jsonStringOld) == len(jsonStringNew) {
|
|
|
|
t.Errorf("Error row not deleted")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-27 23:19:50 +02:00
|
|
|
func TestPgDeleteColumn(t *testing.T) {
|
2021-07-30 15:52:01 +02:00
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
db := Open("postgres", "host=127.0.0.1 port=5432 user=test password=test dbname=test sslmode=disable")
|
|
|
|
defer db.Close()
|
2021-07-30 15:52:01 +02:00
|
|
|
|
2021-10-28 10:34:10 +02:00
|
|
|
old, err := db.Table("test").GetSchema()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
db.Table("test").DeleteColumn("addcolumn")
|
|
|
|
new, err := db.Table("test").GetSchema()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2021-07-30 15:52:01 +02:00
|
|
|
|
|
|
|
if len(old.Columns) == len(new.Columns) {
|
|
|
|
t.Errorf("Error column not deleted")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-27 23:19:50 +02:00
|
|
|
func TestPgDeleteTable(t *testing.T) {
|
2021-10-28 10:34:10 +02:00
|
|
|
db := Open("postgres", "host=127.0.0.1 port=5432 user=test password=test dbname=test sslmode=disable")
|
|
|
|
defer db.Close()
|
|
|
|
db.Table("test").DeleteTable()
|
2021-07-30 15:52:01 +02:00
|
|
|
|
|
|
|
}
|
2021-12-03 11:19:53 +01:00
|
|
|
|
2023-06-27 23:19:50 +02:00
|
|
|
func TestPgImportSchema(t *testing.T) {
|
2021-12-03 11:19:53 +01:00
|
|
|
db := Open("postgres", "host=127.0.0.1 port=5432 user=test password=test dbname=test sslmode=disable")
|
|
|
|
defer db.Close()
|
|
|
|
db.ImportSchema("pfn.json")
|
|
|
|
}
|
|
|
|
|
2023-06-27 23:19:50 +02:00
|
|
|
func TestPgClearImportSchema(t *testing.T) {
|
2021-12-03 11:19:53 +01:00
|
|
|
db := Open("postgres", "host=127.0.0.1 port=5432 user=test password=test dbname=test sslmode=disable")
|
|
|
|
defer db.Close()
|
|
|
|
db.ClearImportSchema("pfn.json")
|
|
|
|
}
|
|
|
|
|
2023-06-27 23:19:50 +02:00
|
|
|
func TestPgGetSchema(t *testing.T) {
|
2021-12-03 11:19:53 +01:00
|
|
|
db := Open("postgres", "host=127.0.0.1 port=5432 user=test password=test dbname=test sslmode=disable")
|
|
|
|
defer db.Close()
|
|
|
|
data, err := db.GetSchema()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
val, _ := json.Marshal(data)
|
|
|
|
fmt.Println(string(val))
|
|
|
|
}
|
|
|
|
|
2023-06-27 23:19:50 +02:00
|
|
|
func TestPgSaveSchema(t *testing.T) {
|
2021-12-03 11:19:53 +01:00
|
|
|
db := Open("postgres", "host=127.0.0.1 port=5432 user=test password=test dbname=test sslmode=disable")
|
|
|
|
defer db.Close()
|
|
|
|
err := db.SaveSchema("schema.json")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
}
|
2023-06-27 23:19:50 +02:00
|
|
|
func TestPgGenerateTemplate(t *testing.T) {
|
2021-12-03 11:19:53 +01:00
|
|
|
db := Open("postgres", "host=127.0.0.1 port=5432 user=test password=test dbname=test sslmode=disable")
|
|
|
|
defer db.Close()
|
|
|
|
err := db.GenerateTemplate("plantuml.tmpl", "schema.puml")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-27 23:19:50 +02:00
|
|
|
func TestPgGenerateTableTemplate(t *testing.T) {
|
2021-12-03 11:19:53 +01:00
|
|
|
db := Open("postgres", "host=127.0.0.1 port=5432 user=test password=test dbname=test sslmode=disable")
|
|
|
|
defer db.Close()
|
|
|
|
err := db.GenerateTableTemplates("table.tmpl", "gen", "html")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
}
|