package tysc

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

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

var cardsetModelTestTable = []struct {
	name     string
	typeName string
}{
	{"Id", "string"},
	{"Name", "string"},
	{"Manufacturer", "string"},
	{"Model", "string"},
}

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

func TestListCardSets(t *testing.T) {
	var (
		cardsetDao = CardSetDAO{Db: dao.TestDb}
	)
	cardSets, err := cardsetDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(cardSets) != 0 {
		t.Fail()
	}
}

func TestInsertCardSet(t *testing.T) {
	var (
		cardsetDao = CardSetDAO{Db: dao.TestDb}
		cardSet    = CardSet{}
		cardSets   []CardSet
	)
	cardSet.ID = bson.NewObjectId()
	cardSet.Name = "test"
	err := cardsetDao.Insert(cardSet)
	if err != nil {
		t.Fail()
	}
	cardSets, err = cardsetDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(cardSets) != 1 {
		t.Fail()
	}
}

func TestUpsertCardSet(t *testing.T) {
	var (
		cardsetDao = CardSetDAO{Db: dao.TestDb}
		cardSet    = CardSet{}
	)
	cardSet.ID = bson.NewObjectId()
	cardSet.Name = "test2"
	cardsetDao.Upsert(cardSet)
	cardSets, err := cardsetDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(cardSets) != 2 {
		t.Fail()
	}
}

func TestDeleteCardSet(t *testing.T) {
	var (
		cardsetDao = CardSetDAO{Db: dao.TestDb}
	)
	cardSets, err := cardsetDao.FindAll()
	if err != nil {
		t.Fail()
	}
	for _, cardSet := range cardSets {
		cardsetDao.Delete(cardSet)
	}
	cardSets, err = cardsetDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(cardSets) != 0 {
		t.Fail()
	}
}
