100 lines
1.7 KiB
Go
100 lines
1.7 KiB
Go
package library
|
|
|
|
import (
|
|
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"gopkg.in/mgo.v2/bson"
|
|
)
|
|
|
|
var authorModelTestTable = []struct {
|
|
name string
|
|
typeName string
|
|
}{
|
|
{"Id", "string"},
|
|
{"Name", "string"},
|
|
{"Model", "string"},
|
|
}
|
|
|
|
func TestAuthorModel(t *testing.T) {
|
|
m := Author{}
|
|
if reflect.TypeOf(m).NumField() != len(authorModelTestTable) {
|
|
t.Fail()
|
|
}
|
|
for index, testData := range authorModelTestTable {
|
|
givenType := reflect.TypeOf(m).Field(index).Type.Kind().String()
|
|
if givenType != testData.typeName {
|
|
t.Fail()
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestListAuthors(t *testing.T) {
|
|
var (
|
|
authorDao = AuthorDAO{Db: dao.TestDb}
|
|
)
|
|
authors, err := authorDao.FindAll()
|
|
if err != nil {
|
|
t.Fail()
|
|
}
|
|
if len(authors) != 0 {
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestInsertAuthor(t *testing.T) {
|
|
var (
|
|
authorDao = AuthorDAO{Db: dao.TestDb}
|
|
author = Author{}
|
|
authors []Author
|
|
)
|
|
author.ID = bson.NewObjectId()
|
|
author.Name = "Packt Publishing"
|
|
err := authorDao.Insert(author)
|
|
if err != nil {
|
|
t.Fail()
|
|
}
|
|
authors, err = authorDao.FindAll()
|
|
if len(authors) != 1 {
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestUpsertAuthor(t *testing.T) {
|
|
var (
|
|
authorDao = AuthorDAO{Db: dao.TestDb}
|
|
)
|
|
var author = Author{}
|
|
author.ID = bson.NewObjectId()
|
|
author.Name = "Hansa Verlag"
|
|
authorDao.Upsert(author)
|
|
authors, err := authorDao.FindAll()
|
|
if err != nil {
|
|
t.Fail()
|
|
}
|
|
if len(authors) != 2 {
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestDeleteAuthor(t *testing.T) {
|
|
var (
|
|
authorDao = AuthorDAO{Db: dao.TestDb}
|
|
)
|
|
authors, err := authorDao.FindAll()
|
|
if err != nil {
|
|
t.Fail()
|
|
}
|
|
for _, author := range authors {
|
|
authorDao.Delete(author)
|
|
}
|
|
authors, err = authorDao.FindAll()
|
|
if err != nil {
|
|
t.Fail()
|
|
}
|
|
if len(authors) != 0 {
|
|
t.Fail()
|
|
}
|
|
}
|