package api

import (
	"testing"
	"time"

	"secondbit.org/uuid"
)

var datastores = []Datastore{NewMemstore()}

func compareCollections(expectation, result Collection) (field string, expected, got interface{}) {
	if !expectation.ID.Equal(result.ID) {
		return "ID", expectation.ID, result.ID
	}
	if expectation.Name != result.Name {
		return "name", expectation.Name, result.Name
	}
	if !expectation.Owner.Equal(result.Owner) {
		return "owner", expectation.Owner, result.Owner
	}
	if !expectation.Created.Equal(result.Created) {
		return "created", expectation.Created, result.Created
	}
	return "", nil, nil
}

func TestCreateCollection(t *testing.T) {
	for pos, datastore := range datastores {
		expectation := Collection{
			ID:      uuid.NewID(),
			Name:    "Test Collection",
			Owner:   uuid.NewID(),
			Created: time.Now(),
		}
		err := datastore.CreateCollection(expectation)
		if err != nil {
			t.Errorf("Error creating collection in datastore %d: %s\n", pos, err)
		}
		result, err := datastore.GetCollectionByID(expectation.ID)
		if err != nil {
			t.Errorf("Error retrieving collection in datastore %d: %s\n", pos, err)
		}
		field, exp, got := compareCollections(expectation, result)
		if field != "" {
			t.Errorf("Collection did not meet expectations for datastore %d. Expected %v for %s, got %v.", pos, exp, field, got)
		}
	}
}
