package tysc

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

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

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

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

func TestListParallelSets(t *testing.T) {
	var (
		parallelsetDao = ParallelSetDAO{Db: dao.TestDb}
	)
	parallelSets, err := parallelsetDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(parallelSets) != 0 {
		t.Fail()
	}
}

func TestInsertParallelSet(t *testing.T) {
	var (
		parallelsetDao = ParallelSetDAO{Db: dao.TestDb}
		parallelSet    = ParallelSet{}
		parallelSets   []ParallelSet
	)
	parallelSet.ID = bson.NewObjectId()
	parallelSet.Name = "test"
	err := parallelsetDao.Insert(parallelSet)
	if err != nil {
		t.Fail()
	}
	parallelSets, err = parallelsetDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(parallelSets) != 1 {
		t.Fail()
	}
}

func TestUpsertParallelSet(t *testing.T) {
	var (
		parallelsetDao = ParallelSetDAO{Db: dao.TestDb}
		parallelSet    = ParallelSet{}
	)
	parallelSet.ID = bson.NewObjectId()
	parallelSet.Name = "test2"
	parallelsetDao.Upsert(parallelSet)
	parallelSets, err := parallelsetDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(parallelSets) != 2 {
		t.Fail()
	}
}

func TestDeleteParallelSet(t *testing.T) {
	var (
		parallelsetDao = ParallelSetDAO{Db: dao.TestDb}
	)
	parallelSets, err := parallelsetDao.FindAll()
	if err != nil {
		t.Fail()
	}
	for _, parallelSet := range parallelSets {
		parallelsetDao.Delete(parallelSet)
	}
	parallelSets, err = parallelsetDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(parallelSets) != 0 {
		t.Fail()
	}
}
