package tysc

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

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

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

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

func TestListInsertSets(t *testing.T) {
	var (
		insertsetDao = InsertSetDAO{Db: dao.TestDb}
	)
	insertSets, err := insertsetDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(insertSets) != 0 {
		t.Fail()
	}
}

func TestInsertInsertSet(t *testing.T) {
	var (
		insertsetDao = InsertSetDAO{Db: dao.TestDb}
		insertSet    = InsertSet{}
		insertSets   []InsertSet
	)
	insertSet.ID = bson.NewObjectId()
	insertSet.Name = "test"
	err := insertsetDao.Insert(insertSet)
	if err != nil {
		t.Fail()
	}
	insertSets, err = insertsetDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(insertSets) != 1 {
		t.Fail()
	}
}

func TestUpsertInsertSet(t *testing.T) {
	var (
		insertsetDao = InsertSetDAO{Db: dao.TestDb}
		insertSet    = InsertSet{}
	)
	insertSet.ID = bson.NewObjectId()
	insertSet.Name = "test2"
	insertsetDao.Upsert(insertSet)
	insertSets, err := insertsetDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(insertSets) != 2 {
		t.Fail()
	}
}

func TestDeleteInsertSet(t *testing.T) {
	var (
		insertsetDao = InsertSetDAO{Db: dao.TestDb}
	)
	insertSets, err := insertsetDao.FindAll()
	if err != nil {
		t.Fail()
	}
	for _, insertSet := range insertSets {
		insertsetDao.Delete(insertSet)
	}
	insertSets, err = insertsetDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(insertSets) != 0 {
		t.Fail()
	}
}
