Ejemplo n.º 1
0
func TestDecrement(t *testing.T) {
	for _, cache := range testDrivers() {
		cache.Decrement("key1", 10, 1, 0)
		tests.Compare(t, cache, "key1", 10)

		cache.Decrement("key1", 10, 1, 0)
		tests.Compare(t, cache, "key1", 9)

		cache.Set("string", []byte("value"), 0)
		if err := cache.Decrement("string", 0, 1, 0); err == nil {
			tests.FailMsg(t, cache, "Can't decrement a string value.")
		}

		if err := cache.Decrement("key2", 0, 0, 0); err == nil {
			tests.FailMsg(t, cache, "Can't have an offset of <= 0")
		}

		if err := cache.Decrement("key3", -1, 1, 0); err == nil {
			tests.FailMsg(t, cache, "Can't have an initial value of < 0")
		}

		if err := cache.Decrement("key1", 10, 10, 0); err == nil {
			tests.FailMsg(t, cache, "Can't decrement below 0")
		}

	}
}
Ejemplo n.º 2
0
func TestSetMulti(t *testing.T) {
	for _, cache := range testDrivers() {
		items := map[string][]byte{
			"item1": encoding.Int64Bytes(1),
			"item2": []byte("string"),
		}

		cache.SetMulti(items, 0)

		tests.Compare(t, cache, "item1", 1)
		tests.Compare(t, cache, "item2", "string")
	}
}
Ejemplo n.º 3
0
func TestDeleteMulti(t *testing.T) {
	for _, cache := range testDrivers() {
		items := map[string][]byte{
			"item1": encoding.Int64Bytes(1),
			"item2": []byte("string"),
		}

		cache.SetMulti(items, 0)
		cache.Set("key1", []byte("value1"), 0)

		var keys []string
		for k := range items {
			keys = append(keys, k)
		}

		cache.DeleteMulti(keys)

		if _, _, err := cache.Get("item1"); err == nil {
			tests.FailMsg(t, cache, "`item1` should be deleted from the cache.")
		}

		if _, _, err := cache.Get("item2"); err == nil {
			tests.FailMsg(t, cache, "`item2` should be deleted from the cache.")
		}

		tests.Compare(t, cache, "key1", "value1")
	}
}
Ejemplo n.º 4
0
func TestGet(t *testing.T) {
	for _, cache := range testDrivers() {
		cache.Set("key1", []byte("value1"), 0)
		tests.Compare(t, cache, "key1", "value1")

		if _, _, err := cache.Get("key2"); err == nil {
			tests.FailMsg(t, cache, "Key2 is not present, err should not be nil.")
		}

	}
}
Ejemplo n.º 5
0
func TestDelete(t *testing.T) {
	for _, cache := range testDrivers() {
		cache.Set("key1", []byte("value1"), 0)
		tests.Compare(t, cache, "key1", "value1")

		cache.Delete("key1")

		if _, _, err := cache.Get("key1"); err == nil {
			tests.FailMsg(t, cache, "`key1` should be deleted from the cache.")
		}
	}
}
Ejemplo n.º 6
0
func TestAdd(t *testing.T) {
	for _, cache := range testDrivers() {
		if err := cache.Add("key1", []byte("value1"), 0); err != nil {
			tests.FailMsg(t, cache, "Expecting `key1` to be added to the cache.")
		}

		if err := cache.Add("key1", []byte("value2"), 0); err == nil {
			tests.FailMsg(t, cache, "Expecting `key1` not to be added to the cache.")
		}

		tests.Compare(t, cache, "key1", "value1")
	}
}
Ejemplo n.º 7
0
func TestFlush(t *testing.T) {
	for _, cache := range testDrivers() {
		cache.Set("key1", []byte("value1"), 0)
		tests.Compare(t, cache, "key1", "value1")

		if err := cache.Flush(); err != nil {
			tests.FailMsg(t, cache, "Cache should be able to flush")
		}

		if _, _, err := cache.Get("key1"); err == nil {
			tests.FailMsg(t, cache, "Expecting `key1` to be nil")
		}
	}
}