package comics

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

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

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

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

func TestListPublishers(t *testing.T) {
	var (
		publisherDao = PublisherDAO{Db: dao.TestDb}
	)
	publishers, err := publisherDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if len(publishers) != 0 {
		t.Fail()
	}
}

func TestInsertPublisher(t *testing.T) {
	var (
		publisherDao = PublisherDAO{Db: dao.TestDb}
		publisher    = Publisher{}
		publishers   []Publisher
	)
	publisher.ID = bson.NewObjectId()
	publisher.Name = "CrossGen"
	publisherDao.Insert(publisher)
	publishers, _ = publisherDao.FindAll()
	if len(publishers) != 1 {
		t.Fail()
	}
}

func TestUpsertPublisher(t *testing.T) {
	var (
		publisherDao = PublisherDAO{Db: dao.TestDb}
		publisher    = Publisher{}
	)
	publisher.ID = bson.NewObjectId()
	publisher.Name = "Marvel"
	publisherDao.Upsert(publisher)
	publishers, _ := publisherDao.FindAll()
	if len(publishers) != 2 {
		t.Fail()
	}
}

func TestDeletePublisher(t *testing.T) {
	var (
		publisherDao = PublisherDAO{Db: dao.TestDb}
	)
	publishers, _ := publisherDao.FindAll()
	for _, publisher := range publishers {
		publisherDao.Delete(publisher)
	}
	publishers, _ = publisherDao.FindAll()
	if len(publishers) != 0 {
		t.Fail()
	}
}
