PageRenderTime 22ms CodeModel.GetById 2ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/gorm_test.go

http://github.com/sdegutis/GoRM
Go | 196 lines | 154 code | 42 blank | 0 comment | 62 complexity | 341296e55bbe273a1854062d8ce705e2 MD5 | raw file
  1package gorm
  2
  3import (
  4	"testing"
  5	"io/ioutil"
  6	"bytes"
  7	"reflect"
  8)
  9
 10type Person struct {
 11	Id   int
 12	Name string
 13	Age  int
 14}
 15
 16func TestOpenDB(t *testing.T) {
 17	db, err := OpenDB("test.db")
 18	if err != nil || db == nil {
 19		t.Errorf("opening db file should not have failed")
 20	}
 21	err = db.Close()
 22	if err != nil {
 23		t.Errorf("closing db connection should not have failed either")
 24	}
 25}
 26
 27func TestGetSingle(t *testing.T) {
 28	db, _ := OpenDB("test.db")
 29	defer db.Close()
 30
 31	var bob Person
 32	err := db.Get(&bob, "name = ?", "bob")
 33
 34	if err != nil {
 35		t.Error(err)
 36	}
 37
 38	if bob.Name != "bob" || bob.Age != 24 || bob.Id != 2 {
 39		t.Errorf("bob was not filled out properly [%v]", bob)
 40	}
 41}
 42
 43func TestGetSingleById(t *testing.T) {
 44	db, _ := OpenDB("test.db")
 45	defer db.Close()
 46
 47	var bob Person
 48	err := db.Get(&bob, 2)
 49
 50	if err != nil {
 51		t.Error(err)
 52	}
 53
 54	if bob.Name != "bob" || bob.Age != 24 || bob.Id != 2 {
 55		t.Errorf("bob was not filled out properly [%v]", bob)
 56	}
 57}
 58
 59func copyTemp(t *testing.T, path string) string {
 60	data, err := ioutil.ReadFile(path)
 61	if err != nil {
 62		t.Errorf("could not read supposedly 'copyable' file")
 63	}
 64
 65	f, err := ioutil.TempFile("", "gorm-sqlite-prefix")
 66	if err != nil {
 67		t.Errorf("could not create tempfile for writing")
 68	}
 69	f.Write(data)
 70	fname := f.Name()
 71	f.Close()
 72
 73	return fname
 74}
 75
 76func TestCopyTemp(t *testing.T) {
 77	name := "test.db"
 78	tmpName := copyTemp(t, name)
 79	if name == tmpName {
 80		t.Errorf("copyTemp should have given a filename other than %q", name)
 81	}
 82	data1, err1 := ioutil.ReadFile(name)
 83	data2, err2 := ioutil.ReadFile(tmpName)
 84	if err1 != nil || err2 != nil || bytes.Compare(data1, data2) != 0 {
 85		t.Errorf("copyTemp did not copy the file correctly.")
 86	}
 87}
 88
 89func TestInsertViaSave(t *testing.T) {
 90	db, _ := OpenDB(copyTemp(t, "test.db"))
 91	defer db.Close()
 92
 93	var james Person
 94	james.Name = "james"
 95	james.Age = 29
 96
 97	err := db.Save(&james)
 98	if err != nil {
 99		t.Error(err)
100	}
101
102	if james.Id == 0 {
103		t.Error("james should have a new Id, but doesnt. sigh.")
104	}
105
106	var people []Person
107	db.GetAll(&people, "")
108
109	if len(people) != 3 {
110		t.Errorf("the db should contain 3 people now, but it contains %d", len(people))
111	}
112
113	found := false
114
115	for i := 0; i < len(people); i++ {
116		person := people[i]
117		if person.Name == "james" && person.Age == 29 && person.Id == 3 {
118			found = true
119		}
120	}
121
122	if !found {
123		t.Errorf("could not find james in the db. oh no hes lost!! quick call for help!")
124	}
125}
126
127func TestSave(t *testing.T) {
128	db, _ := OpenDB(copyTemp(t, "test.db"))
129	defer db.Close()
130
131	newName := "Fred Jones"
132
133	var bob Person
134	db.Get(&bob, 2)
135
136	bob.Name = newName
137	db.Save(&bob)
138
139	var fred Person
140	err := db.Get(&fred, 2)
141
142	if err != nil {
143		t.Error(err)
144	}
145
146	if fred.Name != newName {
147		t.Errorf("name should have been %q, got %q instead", newName, fred.Name)
148	}
149}
150
151func TestGetMultiple(t *testing.T) {
152	db, _ := OpenDB("test.db")
153	defer db.Close()
154
155	var peoples []Person
156	err := db.GetAll(&peoples, "")
157
158	if err != nil {
159		t.Error(err)
160	}
161
162	if len(peoples) != 2 {
163		t.Errorf("wrong number of people returned, should be 2, but got %d", len(peoples))
164	}
165
166	comparablePeoples := []Person{
167		Person{Name: "john", Id: 1, Age: 42},
168		Person{Name: "bob", Id: 2, Age: 24},
169	}
170
171	if !reflect.DeepEqual(peoples, comparablePeoples) {
172		t.Errorf("peoples was not filled out properly %v", peoples)
173	}
174}
175
176func TestGetMultipleWithoutCondition(t *testing.T) {
177	db, _ := OpenDB("test.db")
178	defer db.Close()
179
180	var people1 []Person
181	db.GetAll(&people1, "id > 0")
182
183	var people2 []Person
184	err := db.GetAll(&people2, "")
185	if err != nil {
186		t.Error(err)
187	}
188
189	if len(people1) != len(people2) {
190		t.Errorf("wrong number of people returned, should be %d, but got %d", len(people1), len(people2))
191	}
192
193	if !reflect.DeepEqual(people1, people2) {
194		t.Errorf("people2 was not filled out properly %v", people2)
195	}
196}