package tysc

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

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

var teamModelTestTable = []struct {
	name     string
	typeName string
}{
	{"Id", "string"},
	{"Name", "string"},
	{"Shortname", "string"},
	{"Sport", "string"},
	{"Model", "string"},
}

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

func TestListTeams(t *testing.T) {
	var (
		teamDao = TeamDAO{Db: dao.TestDb}
	)
	teams, err := teamDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if teams != nil {
		t.Fail()
	}
}

func TestInsertTeam(t *testing.T) {
	var (
		teamDao = TeamDAO{Db: dao.TestDb}
		team    = Team{}
		teams   []Team
	)
	team.ID = bson.NewObjectId()
	err := teamDao.Insert(team)
	if err != nil {
		t.Fail()
	}
	teams, err = teamDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(teams) != 1 {
		t.Fail()
	}
}

func TestUpsertTeam(t *testing.T) {
	var (
		teamDao = TeamDAO{Db: dao.TestDb}
		team    = Team{}
	)
	team.ID = bson.NewObjectId()
	team.Name = "test2"
	teamDao.Upsert(team)
	teams, err := teamDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(teams) != 2 {
		t.Fail()
	}
}

func TestDeleteTeam(t *testing.T) {
	var (
		teamDao = TeamDAO{Db: dao.TestDb}
	)
	teams, err := teamDao.FindAll()
	if err != nil {
		t.Fail()
	}
	for _, team := range teams {
		teamDao.Delete(team)
	}
	teams, err = teamDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(teams) != 0 {
		t.Fail()
	}
}
