func TestUpdateRedisMapReferenceRedisErrors(t *testing.T) { // should return error if redis does redigomock.Clear() redigomock.Command("GETSET", "map", "map:1").ExpectError(errors.New("redis error")) err := UpdateRedisMapReference(redigomock.NewConn(), Params{}, MapConfig{ Name: "map", HashKey: "map:1", }, ) assert.EqualError(t, err, "redis error") redigomock.Clear() redigomock.Command("GETSET", "map", "map:1").Expect("map:0") redigomock.Command("DEL", "map:0").ExpectError(errors.New("redis error")) err = UpdateRedisMapReference(redigomock.NewConn(), Params{}, MapConfig{ Name: "map", HashKey: "map:1", }, ) assert.EqualError(t, err, "redis error") }
func Test_StringStorage_WalkKeys_CloseDirectly(t *testing.T) { c := redigomock.NewConn() c.Command("SCAN", int64(0), "MATCH", "*", "COUNT", 100).Expect([]interface{}{ []uint8("0"), []interface{}{[]uint8("test-key")}, }) newStorage := testMustNewStorageWithConn(t, c) var count int // Directly close and end walking. closer := make(chan struct{}, 1) closer <- struct{}{} err := newStorage.WalkKeys("*", closer, func(key string) error { count++ return nil }) if err != nil { t.Fatal("expected", nil, "got", err) } if count != 0 { t.Fatal("expected", 0, "got", count) } }
func TestSetRedisHashKeysRedisError(t *testing.T) { redigomock.Clear() redigomock.Command("INCR", "moredis:mapindexcounter").ExpectError(errors.New("redis error")) collectionConfig := CollectionConfig{Maps: []MapConfig{MapConfig{}}} err := SetRedisHashKeys(redigomock.NewConn(), &collectionConfig) assert.EqualError(t, err, "redis error") }
func Test_ScoredSetStorage_WalkScoredSet_CloseAfterCallback(t *testing.T) { c := redigomock.NewConn() c.Command("ZSCAN", "prefix:test-key", int64(0), "COUNT", 100).Expect([]interface{}{ []uint8("0"), []interface{}{[]uint8("test-value-1"), []uint8("0.8"), []uint8("test-value-2"), []uint8("0.8")}, }) newStorage := testMustNewStorageWithConn(t, c) var count int closer := make(chan struct{}, 1) err := newStorage.WalkScoredSet("test-key", closer, func(element string, score float64) error { count++ // Close and end walking. closer <- struct{}{} return nil }) if err != nil { t.Fatal("expected", nil, "got", err) } if count != 1 { t.Fatal("expected", 1, "got", count) } }
func Test_ScoredSetStorage_WalkScoredSet_CloseDirectly(t *testing.T) { c := redigomock.NewConn() c.Command("ZSCAN", "prefix:test-key", int64(0), "COUNT", 100).Expect([]interface{}{ []uint8("0"), []interface{}{[]uint8("test-value-1"), []uint8("0.8"), []uint8("test-value-2"), []uint8("0.8")}, }) newStorage := testMustNewStorageWithConn(t, c) // Directly close and end walking. closer := make(chan struct{}, 1) closer <- struct{}{} var values []interface{} err := newStorage.WalkScoredSet("test-key", closer, func(element string, score float64) error { values = append(values, element, score) return nil }) if err != nil { t.Fatal("expected", nil, "got", err) } if values != nil { t.Fatal("expected", nil, "got", values) } }
func Test_CLG_Input_SetInformationSequenceError(t *testing.T) { newCLG := MustNew() newCtx := context.MustNew() newServiceCollection := testMustNewServiceCollection(t) // Prepare the storage connection to fake a returned error. newInput := "test input" informationIDKey := key.NewNetworkKey("information-sequence:%s:information-id", newInput) // Our test ID factory always returns the same ID. That way we are able to // check for the ID being used during the test. newID, err := newServiceCollection.ID().New() if err != nil { t.Fatal("expected", nil, "got", err) } informationSequenceKey := key.NewNetworkKey("information-id:%s:information-sequence", newID) c := redigomock.NewConn() c.Command("GET", "prefix:"+informationIDKey).ExpectError(redigo.ErrNil) c.Command("SET", "prefix:"+informationIDKey, string(newID)).Expect("OK") c.Command("SET", "prefix:"+informationSequenceKey, newInput).ExpectError(invalidConfigError) newStorageCollection := testMustNewStorageCollectionWithConn(t, c) // Set prepared storage to CLG we want to test. newCLG.(*clg).StorageCollection = newStorageCollection newCLG.(*clg).ServiceCollection = newServiceCollection // Execute CLG. err = newCLG.(*clg).calculate(newCtx, newInput) if !IsInvalidConfig(err) { t.Fatal("expected", true, "got", false) } }
func TestPuts(t *testing.T) { conn := redigomock.NewConn() q := NewQueue(conn) //Low conn.GenericCommand("RPUSH").Expect(int64(1)) l, err := q.PutLow("test1", "val1") if err != nil { t.Error("Should not return error") } if l != 1 { t.Error("Should return 1") } //Normal conn.GenericCommand("RPUSH").Expect(int64(1)) l, err = q.PutNormal("test1", "val1") if err != nil { t.Error("Should not return error") } if l != 1 { t.Error("Should return 1") } //High conn.GenericCommand("RPUSH").Expect(int64(1)) l, err = q.PutHigh("test1", "val1") if err != nil { t.Error("Should not return error") } if l != 1 { t.Error("Should return 1") } }
func Test_ScoredSetStorage_GetHighestScoredElements_Success(t *testing.T) { c := redigomock.NewConn() c.Command("ZREVRANGE", "prefix:foo", 0, 2, "WITHSCORES").Expect([]interface{}{ []uint8("one"), []uint8("0.8"), []uint8("two"), []uint8("0.5"), }) newStorage := testMustNewStorageWithConn(t, c) values, err := newStorage.GetHighestScoredElements("foo", 2) if err != nil { t.Fatal("expected", nil, "got", err) } if len(values) != 4 { t.Fatal("expected", 1, "got", len(values)) } if values[0] != "one" { t.Fatal("expected", "one", "got", values[0]) } if values[1] != "0.8" { t.Fatal("expected", "0.8", "got", values[1]) } if values[2] != "two" { t.Fatal("expected", "two", "got", values[2]) } if values[3] != "0.5" { t.Fatal("expected", "0.5", "got", values[3]) } }
func Test_SetStorage_WalkSet_CloseDirectly(t *testing.T) { c := redigomock.NewConn() c.Command("SSCAN", "prefix:test-key", int64(0), "COUNT", 100).Expect([]interface{}{ []uint8("0"), []interface{}{[]uint8("test-value-1"), []uint8("test-value-2")}, }) newStorage := testMustNewStorageWithConn(t, c) // Directly close and end walking. closer := make(chan struct{}, 1) closer <- struct{}{} var element1 string err := newStorage.WalkSet("test-key", closer, func(element string) error { element1 = element return nil }) if err != nil { t.Fatal("expected", nil, "got", err) } if element1 != "" { t.Fatal("expected", "", "got", element1) } }
func defaultMockDialConfig() mockDialConfig { newConfig := mockDialConfig{ RedisConn: redigomock.NewConn(), } return newConfig }
func Test_StringStorage_WalkKeys_CloseAfterCallback(t *testing.T) { c := redigomock.NewConn() c.Command("SCAN", int64(0), "MATCH", "*", "COUNT", 100).Expect([]interface{}{ []uint8("0"), []interface{}{[]uint8("test-key")}, }) newStorage := testMustNewStorageWithConn(t, c) var count int var element1 string closer := make(chan struct{}, 1) err := newStorage.WalkKeys("*", closer, func(key string) error { count++ element1 = key // Close and end walking. closer <- struct{}{} return nil }) if err != nil { t.Fatal("expected", nil, "got", err) } if count != 1 { t.Fatal("expected", 1, "got", count) } if element1 != "test-key" { t.Fatal("expected", "test-key", "got", element1) } }
func TestNewQueue(t *testing.T) { conn := redigomock.NewConn() q := NewQueue(conn) if q.Prefix != "queue" { t.Error("Wrong prefix") } }
func TestNewQueueWithPrefix(t *testing.T) { conn := redigomock.NewConn() q := NewQueueWithPrefix(conn, "test") if q.Prefix != "test" { t.Error("Wrong prefix") } }
func TestProbabilityOfLabel(t *testing.T) { conn := redigomock.NewConn() conn.Command("MGET", spamCount, totalCount).Expect(pLabelResp) dist := mockRedisProbDist(conn) result, _ := dist.ProbabilityOfLabel(SPAM) expected := .25 if expected != result { t.Error("Expected", expected, "got", result) } }
func TestSetRedisHashKeys(t *testing.T) { redigomock.Clear() redigomock.Command("INCR", "moredis:mapindexcounter").Expect(int64(1)) collectionConfig := CollectionConfig{Maps: []MapConfig{MapConfig{}}} err := SetRedisHashKeys(redigomock.NewConn(), &collectionConfig) assert.Nil(t, err) assert.Equal(t, collectionConfig.Maps[0].HashKey, "moredis:maps:1") }
func TestProbabilityOfTextGivenLabel(t *testing.T) { conn := redigomock.NewConn() conn.Command("MGET", spamWordCount, spamHelloCount).Expect(spamWordCountResp) dist := mockRedisProbDist(conn) result, _ := dist.ProbabilityOfTextGivenLabel(text, SPAM) expected := .25 if expected != result { t.Error("Expected", expected, "got", result) } }
func PrepareRedisTest() (*redigomock.Conn, *database.Redis) { mock := redigomock.NewConn() pool := &RedisPoolMock{ Conn: mock, } conn := database.NewRedisCustomPool(pool) return mock, conn }
func Test_ScoredSetStorage_SetElementByScore_Success(t *testing.T) { c := redigomock.NewConn() c.Command("ZADD", "prefix:key", 0.8, "element").Expect(int64(1)) newStorage := testMustNewStorageWithConn(t, c) err := newStorage.SetElementByScore("key", "element", 0.8) if err != nil { t.Fatal("expected", nil, "got", err) } }
func Test_ScoredSetStorage_SetElementByScore_Error(t *testing.T) { c := redigomock.NewConn() c.Command("ZADD", "prefix:key", 0.8, "element").ExpectError(queryExecutionFailedError) newStorage := testMustNewStorageWithConn(t, c) err := newStorage.SetElementByScore("key", "element", 0.8) if !IsQueryExecutionFailed(err) { t.Fatal("expected", true, "got", false) } }
func Test_SetStorage_Error(t *testing.T) { c := redigomock.NewConn() c.Command("SET", "prefix:foo", "bar").ExpectError(queryExecutionFailedError) newStorage := testMustNewStorageWithConn(t, c) err := newStorage.Set("foo", "bar") if !IsQueryExecutionFailed(err) { t.Fatal("expected", true, "got", false) } }
func Test_ScoredSetStorage_GetHighestScoredElements_Error(t *testing.T) { c := redigomock.NewConn() c.Command("ZREVRANGE", "prefix:foo", 0, 2, "WITHSCORES").ExpectError(queryExecutionFailedError) newStorage := testMustNewStorageWithConn(t, c) _, err := newStorage.GetHighestScoredElements("foo", 2) if !IsQueryExecutionFailed(err) { t.Fatal("expected", true, "got", false) } }
func Test_ScoredSetStorage_WalkScoredSet_QueryError(t *testing.T) { c := redigomock.NewConn() c.Command("ZSCAN").ExpectError(queryExecutionFailedError) newStorage := testMustNewStorageWithConn(t, c) err := newStorage.WalkScoredSet("test-key", nil, nil) if !IsQueryExecutionFailed(err) { t.Fatal("expected", true, "got", false) } }
func Test_ScoredSetStorage_GetElementsByScore_Error(t *testing.T) { c := redigomock.NewConn() c.Command("ZREVRANGEBYSCORE", "prefix:foo", 0.8, 0.8, "LIMIT", 0, 3).ExpectError(queryExecutionFailedError) newStorage := testMustNewStorageWithConn(t, c) _, err := newStorage.GetElementsByScore("foo", 0.8, 3) if !IsQueryExecutionFailed(err) { t.Fatal("expected", true, "got", false) } }
func Test_StringMapStorage_GetStringMap_Error(t *testing.T) { c := redigomock.NewConn() c.Command("HGETALL", "prefix:foo").ExpectError(queryExecutionFailedError) newStorage := testMustNewStorageWithConn(t, c) _, err := newStorage.GetStringMap("foo") if !IsQueryExecutionFailed(err) { t.Fatal("expected", true, "got", false) } }
func Test_StringMapStorage_SetStringMap_Success(t *testing.T) { c := redigomock.NewConn() c.Command("HMSET", "prefix:foo", "k1", "v1").Expect("OK") newStorage := testMustNewStorageWithConn(t, c) err := newStorage.SetStringMap("foo", map[string]string{"k1": "v1"}) if err != nil { t.Fatal("expected", nil, "got", err) } }
func Test_ScoredSetStorage_RemoveScoredElement_Error(t *testing.T) { c := redigomock.NewConn() c.Command("ZREM", "prefix:test-key", "test-element").ExpectError(queryExecutionFailedError) newStorage := testMustNewStorageWithConn(t, c) err := newStorage.RemoveScoredElement("test-key", "test-element") if !IsQueryExecutionFailed(err) { t.Fatal("expected", true, "got", false) } }
func Test_ListStorage_PushToList(t *testing.T) { c := redigomock.NewConn() c.Command("LPUSH", "prefix:test-key", "test-element").Expect(int64(1)) newStorage := testMustNewStorageWithConn(t, c) err := newStorage.PushToList("test-key", "test-element") if err != nil { t.Fatal("expected", nil, "got", err) } }
func Test_StringMapStorage_SetStringMap_Error(t *testing.T) { c := redigomock.NewConn() c.Command("HMSET", "prefix:foo", "k1", "v1").ExpectError(queryExecutionFailedError) newStorage := testMustNewStorageWithConn(t, c) err := newStorage.SetStringMap("foo", map[string]string{"k1": "v1"}) if !IsQueryExecutionFailed(err) { t.Fatal("expected", true, "got", false) } }
func Test_ScoredSetStorage_RemoveScoredElement(t *testing.T) { c := redigomock.NewConn() c.Command("ZREM", "prefix:test-key", "test-element").Expect(int64(1)) newStorage := testMustNewStorageWithConn(t, c) err := newStorage.RemoveScoredElement("test-key", "test-element") if err != nil { t.Fatal("expected", nil, "got", err) } }
func Test_ListStorage_PopFromList_Error(t *testing.T) { c := redigomock.NewConn() c.Command("BRPOP", "prefix:test-key", 0).ExpectError(queryExecutionFailedError) newStorage := testMustNewStorageWithConn(t, c) _, err := newStorage.PopFromList("test-key") if !IsQueryExecutionFailed(err) { t.Fatal("expected", true, "got", false) } }