package cache_test

import (
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/cache"
	m "github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/redis/tester"
	"github.com/pkg/errors"
	"github.com/stretchr/testify/assert"
)

var someErr = errors.New("some error")

func TestFillCarFilterOnline(t *testing.T) {
	val_false := false
	tests := map[string]struct {
		redisCLI  *tester.MockRedis
		filter    m.CarSearch
		expErr    error
		expFilter m.CarSearch
	}{
		"correct_common": {
			redisCLI: &tester.MockRedis{
				GetSetResults: `["FISKERVIN1","FISKERVIN2"]`,
			},
			filter: m.CarSearch{
				Online: &m.CarOnlineFilter{
					Online: &val_false,
				},
			},
			expFilter: m.CarSearch{
				Online: &m.CarOnlineFilter{
					Online:     &val_false,
					VINsOnline: []string{"FISKERVIN1", "FISKERVIN2"},
				},
			},
		},
		"filter_nil": {
			filter:    m.CarSearch{},
			expFilter: m.CarSearch{},
		},
		"redis_err": {
			redisCLI: &tester.MockRedis{
				Error: someErr,
			},
			filter: m.CarSearch{
				Online: &m.CarOnlineFilter{
					Online: &val_false,
				},
			},
			expErr: someErr,
		},
	}
	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			err := cache.FillCarFilterOnline(tt.redisCLI, &tt.filter)
			if err != nil && tt.expErr != nil {
				assert.Equal(t, tt.expErr.Error(), err.Error())
				return
			}

			assert.Equal(t, tt.expErr, err)
			assert.Equal(t, tt.filter, tt.expFilter)
		})
	}
}
