package tysc

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

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

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

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

func TestListSports(t *testing.T) {
	var (
		sportDao = SportDAO{Db: dao.TestDb}
	)
	sports, err := sportDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(sports) != 0 {
		t.Fail()
	}
}

func TestInsertSport(t *testing.T) {
	var (
		sportDao = SportDAO{Db: dao.TestDb}
		sport    = Sport{}
		sports   []Sport
	)
	sport.ID = bson.NewObjectId()
	sport.Name = "test"
	err := sportDao.Insert(sport)
	if err != nil {
		t.Fail()
	}
	sports, err = sportDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(sports) != 1 {
		t.Fail()
	}
}

func TestUpsertSport(t *testing.T) {
	var (
		sportDao = SportDAO{Db: dao.TestDb}
		sport    = Sport{}
	)
	sport.ID = bson.NewObjectId()
	sport.Name = "test2"
	sportDao.Upsert(sport)
	sports, err := sportDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(sports) != 2 {
		t.Fail()
	}
}

func TestDeleteSport(t *testing.T) {
	var (
		sportDao = SportDAO{Db: dao.TestDb}
	)
	sports, err := sportDao.FindAll()
	if err != nil {
		t.Fail()
	}
	for _, sport := range sports {
		sportDao.Delete(sport)
	}
	sports, err = sportDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(sports) != 0 {
		t.Fail()
	}
}
