Exemplo n.º 1
0
func (s *S) TestQuery(t *C) {
	ctx := s.ctx

	ds := datastore.New()

	k := datastore.NewKey("tweet", "123")
	tweet := &tweettest{"kimchy", "NY", "2010-11-15T14:12:12", "trying out Elasticsearch", version}

	err := ds.Put(ctx, k, tweet)
	if err != nil {
		t.Errorf("%v", err)
	}

	k = datastore.NewKey("tweet", "234")
	tweet = &tweettest{"kimchy2", "NY", "2010-11-15T14:12:12", "trying out Elasticsearch again", version}
	err = ds.Put(ctx, k, tweet)
	if err != nil {
		t.Errorf("%v", err)
	}

	query := search.Query().Search("_exists_:State")
	testSearch := search.Search("twitter").Type("tweet").Size("10000").Query(query)

	q := datastore.NewQuery(ctx)
	msgs, err := q.Execute(testSearch)

	if err != nil {
		t.Errorf("Unepected error %v", err)
	}
	if msgs.Len() != 2 {
		t.Errorf("Expected 2 msgs, got  %v", msgs.Len())
	}

	//query for non-existant entity
	query = search.Query().Search("_exists_:blam")
	testSearch = search.Search("twitter").Type("tweet").Size("10000").Query(query)

	q = datastore.NewQuery(ctx)
	msgs, err = q.Execute(testSearch)

	if err != nil {
		t.Errorf("Unepected error %v", err)
	}
	if msgs.Len() != 0 {
		t.Errorf("Expected 0 msgs, got  %v", msgs.Len())
	}

}
Exemplo n.º 2
0
func (s *S) TestPutGetDelete(t *C) {
	ctx := s.ctx
	ds := datastore.New()

	key := datastore.NewKey("tweet", "1")
	tweet := tweettest{"kimchy", "", "2009-11-15T14:12:12", "trying out Elasticsearch", version}

	err := ds.Put(ctx, key, &tweet)
	if err != nil {
		t.Errorf("%v", err)
	}

	//Get tweet
	var storedtweet tweettest
	err = ds.Get(ctx, key, &storedtweet)
	if err != nil {
		t.Fatalf("Unexpected: %v", err)
	}
	glog.Infof("tweet is %v", &storedtweet)

	if storedtweet.User != "kimchy" {
		t.Errorf("Expected kimchy, found %s", storedtweet.User)
	}

	//Delete tweet
	err = ds.Delete(ctx, key)
	if err != nil {
		t.Errorf("Unexpected delete error: %v", err)
	}

	//test not found
	err = ds.Get(ctx, key, &storedtweet)
	if err == nil {
		t.Error("Expected error, not nil")
	} else if !datastore.IsErrNoSuchEntity(err) {
		glog.Infof("type is %s", reflect.ValueOf(err))
		t.Fatalf("Unexpected: %v", err)
	}
}
Exemplo n.º 3
0
func (s *S) TestVersionConflict(t *C) {
	ctx := s.ctx
	ds := datastore.New()

	key := datastore.NewKey("tweet", "666")
	tweet := tweettest{"kimchy", "", "2009-11-15T14:12:12", "trying out Elasticsearch", version}

	err := ds.Put(ctx, key, &tweet)
	if err != nil {
		t.Errorf("%v", err)
	}

	//Get tweet
	var storedtweet tweettest
	err = ds.Get(ctx, key, &storedtweet)
	if err != nil {
		t.Fatalf("Unexpected: %v", err)
	}
	if storedtweet.DatabaseVersion != 1 {
		t.Fatalf("Version was not incremented")
	}

	// Update something and send it back with the same version; it should succeed
	storedtweet.Message = "This is a different message"
	err = ds.Put(ctx, key, &storedtweet)
	if err != nil {
		t.Errorf("%v", err)
	}

	// Make a new tweet with a 1 version, which should conflict (since version
	// in the database is now 2)
	tweet.DatabaseVersion = 1
	err = ds.Put(ctx, key, &tweet)
	if err == nil {
		t.Errorf("Did not get a conflict")
	}

}