示例#1
0
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")

}
示例#2
0
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)
	}
}
示例#3
0
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")
}
示例#4
0
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)
	}
}
示例#5
0
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)
	}
}
示例#6
0
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")
	}

}
示例#8
0
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])
	}
}
示例#9
0
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)
	}
}
示例#10
0
文件: dial.go 项目: xh3b4sd/anna
func defaultMockDialConfig() mockDialConfig {
	newConfig := mockDialConfig{
		RedisConn: redigomock.NewConn(),
	}

	return newConfig
}
示例#11
0
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)
	}
}
示例#12
0
func TestNewQueue(t *testing.T) {
	conn := redigomock.NewConn()
	q := NewQueue(conn)

	if q.Prefix != "queue" {
		t.Error("Wrong prefix")
	}
}
示例#13
0
func TestNewQueueWithPrefix(t *testing.T) {

	conn := redigomock.NewConn()
	q := NewQueueWithPrefix(conn, "test")

	if q.Prefix != "test" {
		t.Error("Wrong prefix")
	}
}
示例#14
0
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)
	}
}
示例#15
0
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")
}
示例#16
0
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)
	}
}
示例#17
0
func PrepareRedisTest() (*redigomock.Conn, *database.Redis) {
	mock := redigomock.NewConn()

	pool := &RedisPoolMock{
		Conn: mock,
	}

	conn := database.NewRedisCustomPool(pool)

	return mock, conn
}
示例#18
0
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)
	}
}
示例#19
0
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)
	}
}
示例#20
0
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)
	}
}
示例#21
0
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)
	}
}
示例#22
0
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)
	}
}
示例#23
0
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)
	}
}
示例#24
0
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)
	}
}
示例#25
0
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)
	}
}
示例#26
0
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)
	}
}
示例#27
0
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)
	}
}
示例#28
0
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)
	}
}
示例#29
0
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)
	}
}
示例#30
0
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)
	}
}