package tysc

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

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

var playerModelTestTable = []struct {
	name     string
	typeName string
}{
	{"Id", "string"},
	{"Firstname", "string"},
	{"Lastname", "string"},
	{"Model", "string"},
}

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

func TestListPlayers(t *testing.T) {
	var (
		playerDao = PlayerDAO{Db: dao.TestDb}
	)
	players, err := playerDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if players != nil {
		t.Fail()
	}
}

func TestInsertPlayer(t *testing.T) {
	var (
		playerDao = PlayerDAO{Db: dao.TestDb}
		player    = Player{}
		players   []Player
	)
	player.ID = bson.NewObjectId()
	player.Lastname = "test"
	err := playerDao.Insert(player)
	if err != nil {
		t.Fail()
	}
	players, err = playerDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(players) != 1 {
		t.Fail()
	}
}

func TestUpsertPlayer(t *testing.T) {
	var (
		playerDao = PlayerDAO{Db: dao.TestDb}
		player    = Player{}
	)
	player.ID = bson.NewObjectId()
	player.Lastname = "test2"
	playerDao.Upsert(player)
	players, err := playerDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(players) != 2 {
		t.Fail()
	}
}

func TestDeletePlayer(t *testing.T) {
	var (
		playerDao = PlayerDAO{Db: dao.TestDb}
	)
	players, err := playerDao.FindAll()
	if err != nil {
		t.Fail()
	}
	for _, player := range players {
		playerDao.Delete(player)
	}
	players, err = playerDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(players) != 0 {
		t.Fail()
	}
}
