package comics

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

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

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

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

func TestListArtists(t *testing.T) {
	var (
		artistDao = ArtistDAO{Db: dao.TestDb}
	)
	artists, err := artistDao.FindAll()
	if err != nil {
		t.Fail()
	}
	if artists != nil {
		t.Fail()
	}
}

func TestInsertArtist(t *testing.T) {
	var (
		artistDao = ArtistDAO{Db: dao.TestDb}
		artist    = Artist{}
		artists   []Artist
	)
	artist.ID = bson.NewObjectId()
	artist.Name = "Turner, Michael"
	err := artistDao.Insert(artist)
	if err != nil {
		t.Fail()
	}
	artists, _ = artistDao.FindAll()
	if len(artists) != 1 {
		t.Fail()
	}
}

func TestUpsertArtist(t *testing.T) {
	var (
		artistDao = ArtistDAO{Db: dao.TestDb}
		artist    = Artist{}
	)
	artist.ID = bson.NewObjectId()
	artist.Name = "Marz, Ron"
	_, err := artistDao.Upsert(artist)
	if err != nil {
		t.Fail()
	}
	artists, _ := artistDao.FindAll()
	if len(artists) != 2 {
		t.Fail()
	}
}

func TestDeleteArtist(t *testing.T) {
	var (
		artistDao = ArtistDAO{Db: dao.TestDb}
	)
	artists, err := artistDao.FindAll()
	if err != nil {
		t.Fail()
	}
	for _, artist := range artists {
		artistDao.Delete(artist)
	}
	artists, _ = artistDao.FindAll()
	if len(artists) != 0 {
		t.Fail()
	}
}
