Beispiel #1
0
func TestPartialDeleteMultiple(t *testing.T) {
	db := testStore(t)
	s := &TestR{
		ID:    uuid.New(),
		Field: "value",
	}

	s1 := &TestR{
		ID:    uuid.New(),
		Field: "value1",
	}

	s2 := &TestR{
		ID:    uuid.New(),
		Field: "value2",
	}

	if err := db.Write(s); err != nil {
		t.Fatal("err", err)
	}

	if err := db.Write(s1); err != nil {
		t.Fatal("err", err)
	}

	toDel := []store.Item{s, s1, s2}
	count, err := db.DeleteMultiple(toDel)
	if err != store.ErrKeyNotFound {
		t.Fatal("expected ErrKeyNotFound, got: ", err)
	}

	if count != 2 {
		t.Fatal("expected 2 deletions, got: ", count)
	}
}
Beispiel #2
0
func TestDeleteMultiple(t *testing.T) {
	s := &TestR{
		ID:    uuid.New(),
		Field: "value",
	}

	s1 := &TestR{
		ID:    uuid.New(),
		Field: "value1",
	}

	db := testStore(t)

	if err := db.Write(s); err != nil {
		t.Fatal("err", err)
	}

	if err := db.Write(s1); err != nil {
		t.Fatal("err", err)
	}

	toDel := []store.Item{s, s1}
	count, err := db.DeleteMultiple(toDel)
	if err != nil {
		t.Fatal("err", err)
	}
	if count != 2 {
		t.Fatal("expected 2 deletions, got: ", count)
	}
}
Beispiel #3
0
// Write writes the item to the store. It constructs the key using the i.Key()
// and prefixes it with the type of struct. When the key is empty, it assigns
// a unique universal id(UUID) using the SetKey method of the Item
func (s *Store) Write(i store.Item) error {
	c := s.pool.Get()
	defer c.Close()

	value := reflect.ValueOf(i).Elem()

	ri := &Item{
		prefix: s.typeName(value),
		data:   make(map[string]interface{}),
	}

	// Use the Items id if set or generate
	// a new UUID
	ri.key = i.Key()
	if len(ri.key) == 0 {
		ri.key = uuid.New()
	}
	i.SetKey(ri.key)

	// convert the item to redis item
	if err := marshall(i, value, ri); err != nil {
		return err
	}

	for key, val := range ri.data {
		if err := c.Send("HSET", ri.Key(), key, val); err != nil {
			return err
		}
		c.Flush()
	}

	return nil
}
Beispiel #4
0
func TestRead(t *testing.T) {
	s := &TestR{
		ID:    uuid.New(),
		Field: "value",
	}
	db := testStore(t)

	if err := db.Write(s); err != nil {
		t.Fatal("err", err)
	}
	got := &TestR{ID: s.Key()}
	if err := db.Read(got); err != nil {
		t.Fatal("err", err)
	}
	if !reflect.DeepEqual(s, got) {
		t.Fatal("expected:", s, " got:", got)
	}
}
Beispiel #5
0
func TestWrite(t *testing.T) {
	s := &TestR{
		ID:         uuid.New(),
		Field:      "value",
		FieldInt:   10,
		FieldFloat: 1.234,
		FieldBool:  true,
		FieldUint:  1,
	}

	db := testStore(t)

	if err := db.Write(s); err != nil {
		t.Fatal("err", err)
	}

	if len(s.Key()) == 0 {
		t.Fatalf("key is emtpy %#v", s)
	}

	cfg, err := NewConfig(testRedisURL)
	if err != nil {
		t.Fatal(err)
	}

	pool := NewPool(cfg)
	c := pool.Get()
	defer c.Close()
	reply, err := driver.Values(c.Do("HGETALL", testNs+":TestR:"+s.Key()))
	if err != nil {
		t.Fatal("err", err)
	}

	got := &TestR{}

	if err := driver.ScanStruct(reply, got); err != nil {
		t.Fatal("err", err)
	}

	if !reflect.DeepEqual(s, got) {
		t.Fatal("expected:", s, " got:", got)
	}
}
Beispiel #6
0
func TestDelete(t *testing.T) {
	s := &TestR{
		ID:    uuid.New(),
		Field: "value",
	}
	db := testStore(t)

	if err := db.Write(s); err != nil {
		t.Fatal("err", err)
	}
	got := &TestR{ID: s.Key()}
	if err := db.Delete(got); err != nil {
		t.Fatal("err", err)
	}

	if err := db.Read(got); err != store.ErrKeyNotFound {
		t.Fatal("expected ErrNotFound, got: ", err)
	}
}
Beispiel #7
0
// Use of this source code is governed by the Apache License, Version 2.0
// that can be found in the LICENSE file.

package redis

import (
	"reflect"
	"testing"

	"github.com/gosuri/go-store/_vendor/src/code.google.com/p/go-uuid/uuid"
	driver "github.com/gosuri/go-store/_vendor/src/github.com/garyburd/redigo/redis"
	"github.com/gosuri/go-store/store"
)

var (
	testNs       = uuid.New()
	testRedisURL = "redis://@127.0.0.1:6379/2"
)

type TestR struct {
	ID         string
	Field      string
	FieldFloat float32
	FieldInt   int
	FieldBool  bool
	FieldUint  uint

	fieldPrivate string
}

type TestRs []TestR