package cache_test

import (
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/cache"
	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/db/queries/mocks"
	"github.com/fiskerinc/cloud-services/pkg/redis"
	"github.com/fiskerinc/cloud-services/pkg/redis/tester"
	"github.com/fiskerinc/cloud-services/pkg/testhelper"

	redigo "github.com/gomodule/redigo/redis"
)

type mockRedisCacheDriverToCars struct {
	redis.Connection
}

func (c *mockRedisCacheDriverToCars) Execute(command ...interface{}) (interface{}, error) {
	return []byte("1"), nil
}

type mockRedisEmptyCacheDriverToCars struct {
	redis.Connection
}

func (c *mockRedisEmptyCacheDriverToCars) Execute(command ...interface{}) (interface{}, error) {
	return nil, redigo.ErrNil
}

func (c *mockRedisEmptyCacheDriverToCars) ExecuteBatch(batch *redis.RedisBatchCommands) (interface{}, error) {
	return nil, nil
}

func TestVerifyCarToDriver(t *testing.T) {
	setupRedisMock()
	mockDB := &mocks.MockCars{
		SelectCarsForDrivers: []common.CarToDriver{{}},
	}

	mockRedis = &mockRedisCacheDriverToCars{}
	redisPool := tester.NewMockClientPool(mockRedis)
	_, err := cache.VerifyCarToDriver(redisPool, mockDB, "VALID_VIN", "VALID_ID")
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestRetrieveAndCacheDriverIDs", nil, err)
	}

	mockRedis = &mockRedisEmptyCacheDriverToCars{}
	redisPool = tester.NewMockClientPool(mockRedis)
	_, err = cache.VerifyCarToDriver(redisPool, mockDB, "VALID_VIN", "VALID_ID")
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestRetrieveAndCacheDriverIDs", nil, err)
	}
}
