-
Notifications
You must be signed in to change notification settings - Fork 0
/
llorm_test.go
112 lines (101 loc) · 2.72 KB
/
llorm_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
package llorm
import (
"database/sql"
"errors"
"fmt"
"github.com/go-ll/llorm/session"
"log"
"reflect"
"testing"
_ "github.com/mattn/go-sqlite3"
)
func TestLLOrm(t *testing.T) {
db, _ := sql.Open("sqlite3", "ll.db")
defer func() { _ = db.Close() }()
_, _ = db.Exec("DROP TABLE IF EXISTS User;")
_, _ = db.Exec("CREATE TABLE User(Name text);")
result, err := db.Exec("INSERT INTO User(`Name`) values (?), (?)", "Tom", "Sam")
if err == nil {
affected, _ := result.RowsAffected()
log.Println(affected)
}
row := db.QueryRow("SELECT Name FROM User LIMIT 1")
var name string
if err := row.Scan(&name); err == nil {
log.Println(name)
}
}
func TestOne(t *testing.T) {
e, _ := NewEngine("sqlite3", "ll.db")
defer e.Close()
s := e.NewSession()
_, _ = s.Raw("DROP TABLE IF EXISTS User;").Exec()
_, _ = s.Raw("CREATE TABLE User(Name text);").Exec()
result, _ := s.Raw("INSERT INTO User(`Name`) Values(?), (?)", "Cexll", "iMorta").Exec()
count, _ := result.RowsAffected()
fmt.Printf("Exec success, %d affected\n", count)
}
func OpenDB(t *testing.T) *Engine {
t.Helper()
e, err := NewEngine("sqlite3", "ll.db")
if err != nil {
t.Fatal("failed to connect", err)
}
return e
}
type User struct {
Name string `llorm:"PRIMARY KEY"`
Age int
}
func TestEngine_Transaction(t *testing.T) {
t.Run("rollback", func(t *testing.T) {
transactionRollback(t)
})
t.Run("commit", func(t *testing.T) {
transactionCommit(t)
})
}
func transactionRollback(t *testing.T) {
e := OpenDB(t)
defer e.Close()
s := e.NewSession()
_ = s.Model(&User{}).DropTable()
_, err := e.Transaction(func(s *session.Session) (result interface{}, err error) {
_ = s.Model(&User{}).CreateTable()
_, err = s.Insert(&User{"Tom", 18})
return nil, errors.New("Error")
})
if err == nil || s.HasTable() {
t.Fatal("failed to rollback")
}
}
func transactionCommit(t *testing.T) {
e := OpenDB(t)
defer e.Close()
s := e.NewSession()
_ = s.Model(&User{}).DropTable()
_, err := e.Transaction(func(s *session.Session) (result interface{}, err error) {
_ = s.Model(&User{}).CreateTable()
_, err = s.Insert(&User{"Tom", 18})
return
})
u := &User{}
_ = s.First(u)
if err != nil || u.Name != "Tom" {
t.Fatal("failed to commit")
}
}
func TestEngine_Migrate(t *testing.T) {
e := OpenDB(t)
defer e.Close()
s := e.NewSession()
_, _ = s.Raw("DROP TABLE If EXISTS User;").Exec()
_, _ = s.Raw("CREATE TABLE User(Name text PRIMARY KEY, XXX integer);").Exec()
_, _ = s.Raw("INSERT INTO User(`Name`) values (?), (?)", "Tom", "Sam").Exec()
_ = e.Migrate(&User{})
rows, _ := s.Raw("SELECT * FROM User").QueryRows()
columns, _ := rows.Columns()
if reflect.DeepEqual(columns, []string{"Name", "Age"}) {
t.Fatal("Failed to migrate table User, got columns", columns)
}
}