package comics

import (
	"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
	"reflect"
	"testing"

	"gopkg.in/mgo.v2/bson"
)

var comicModelTestTable = []struct {
	name     string
	typeName string
}{
	{"Id", "string"},
	{"Title", "string"},
	{"Publisher", "string"},
	{"CurrentOrder", "bool"},
	{"Completed", "bool"},
	{"Model", "string"},
}

func TestComicModel(t *testing.T) {
	m := Comic{}
	if reflect.TypeOf(m).NumField() != len(comicModelTestTable) {
		t.Fail()
	}
	for index, testData := range comicModelTestTable {
		givenType := reflect.TypeOf(m).Field(index).Type.Kind().String()
		if givenType != testData.typeName {
			t.Fail()
		}
	}
}

func TestListComics(t *testing.T) {
	var (
		comicDao = ComicDAO{Db: dao.TestDb}
	)
	comics, err := comicDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(comics) != 0 {
		t.Fail()
	}
}

func TestInsertComic(t *testing.T) {
	var (
		comicDao = ComicDAO{Db: dao.TestDb}
		comic    = Comic{}
		comics   []Comic
	)
	comic.ID = bson.NewObjectId()
	comic.Title = "Simpsons"
	comicDao.Insert(comic)
	comics, _ = comicDao.FindAll()
	if len(comics) != 1 {
		t.Fail()
	}
}
func TestUpsertComic(t *testing.T) {
	var (
		comicDao = ComicDAO{Db: dao.TestDb}
		comic    = Comic{}
	)
	comic.ID = bson.NewObjectId()
	comic.Title = "1602"
	comicDao.Upsert(comic)
	comics, _ := comicDao.FindAll()
	if len(comics) != 2 {
		t.Fail()
	}
}
func TestDeleteComic(t *testing.T) {
	var (
		comicDao = ComicDAO{Db: dao.TestDb}
	)
	comics, _ := comicDao.FindAll()
	for _, comic := range comics {
		comicDao.Delete(comic)
	}
	comics, _ = comicDao.FindAll()
	if len(comics) != 0 {
		t.Fail()
	}
}
