package tysc

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

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

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

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

func TestListPositions(t *testing.T) {
	var (
		positionDao = PositionDAO{Db: dao.TestDb}
	)
	positions, err := positionDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(positions) != 0 {
		t.Fail()
	}
}

func TestInsertPosition(t *testing.T) {
	var (
		positionDao = PositionDAO{Db: dao.TestDb}
		position    = Position{}
		positions   []Position
	)
	position.ID = bson.NewObjectId()
	position.Name = "test"
	err := positionDao.Insert(position)
	if err != nil {
		t.Fail()
	}
	positions, err = positionDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(positions) != 1 {
		t.Fail()
	}
}

func TestUpsertPosition(t *testing.T) {
	var (
		positionDao = PositionDAO{Db: dao.TestDb}
		position    = Position{}
	)
	position.ID = bson.NewObjectId()
	position.Name = "test2"
	positionDao.Upsert(position)
	positions, err := positionDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(positions) != 2 {
		t.Fail()
	}
}

func TestDeletePosition(t *testing.T) {
	var (
		positionDao = PositionDAO{Db: dao.TestDb}
	)
	positions, err := positionDao.FindAll()
	if err != nil {
		t.Fail()
	}
	for _, position := range positions {
		positionDao.Delete(position)
	}
	positions, err = positionDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(positions) != 0 {
		t.Fail()
	}
}
