package tysc

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

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

var cardModelTestTable = []struct {
	name     string
	typeName string
}{
	{"Id", "string"},
	{"Player", "string"},
	{"Team", "string"},
	{"Manufacturer", "string"},
	{"CardSet", "string"},
	{"ParallelSet", "string"},
	{"InsertSet", "string"},
	{"Rookie", "bool"},
	{"Year", "int"},
	{"Number", "int"},
	{"Model", "string"},
}

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

func TestListCards(t *testing.T) {
	var (
		cardDao = CardDAO{Db: dao.TestDb}
	)
	cards, err := cardDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(cards) != 0 {
		t.Fail()
	}
}

func TestInsertCard(t *testing.T) {
	var (
		cardDao = CardDAO{Db: dao.TestDb}
		card    = Card{}
		cards   []Card
	)
	card.ID = bson.NewObjectId()
	card.Number = 1
	err := cardDao.Insert(card)
	if err != nil {
		t.Fail()
	}
	cards, err = cardDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(cards) != 1 {
		t.Fail()
	}
}

func TestUpsertCard(t *testing.T) {
	var (
		cardDao = CardDAO{Db: dao.TestDb}
		card    = Card{}
	)
	card.ID = bson.NewObjectId()
	card.Number = 2
	cardDao.Upsert(card)
	cards, err := cardDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(cards) != 2 {
		t.Fail()
	}
}

func TestDeleteCard(t *testing.T) {
	var (
		cardDao = CardDAO{Db: dao.TestDb}
	)
	cards, err := cardDao.FindAll()
	if err != nil {
		t.Fail()
	}
	for _, card := range cards {
		cardDao.Delete(card)
	}
	cards, err = cardDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(cards) != 0 {
		t.Fail()
	}
}
