func (s *S) TestQuery(t *C) { esdriver := s.Driver() conn, err := esdriver.GetConnection() if err != nil { t.Fatalf("Error getting connection: %v", err) } k := datastore.NewKey("tweet", "1") tweet := map[string]string{ "user": "******", "state": "NY", "post_date": "2009-11-15T14:12:12", "message": "trying out Elasticsearch", } tweetJSON, err := json.Marshal(tweet) err = conn.Put(k, datastore.NewJSONMessage(tweetJSON, 0)) if err != nil { t.Errorf("%v", err) } k = datastore.NewKey("tweet", "2") tweet = map[string]string{ "user": "******", "state": "NY", "post_date": "2010-11-15T14:12:12", "message": "trying out Elasticsearch again", } tweetJSON, err = json.Marshal(tweet) err = conn.Put(k, datastore.NewJSONMessage(tweetJSON, 0)) if err != nil { t.Errorf("%v", err) } query := search.Query().Search("_exists_:state") testSearch := search.Search("twitter").Type("tweet").Size("10000").Query(query) msgs, err := conn.Query(testSearch) if err != nil { t.Errorf("Unepected error %v", err) } if len(msgs) != 2 { t.Errorf("Expected 2 msgs, got %v", len(msgs)) } //query for non-existant entity query = search.Query().Search("_exists_:blam") testSearch = search.Search("twitter").Type("tweet").Size("10000").Query(query) msgs, err = conn.Query(testSearch) if err != nil { t.Errorf("Unepected error %v", err) } if len(msgs) != 0 { t.Errorf("Expected 0 msgs, got %V", len(msgs)) } }
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()) } }
//func TestPutGetDelete(t *testing.T) { func (s *S) TestPutGetDelete(t *C) { esdriver := s.Driver() // driver, err := getConnection() // if err != nil { // t.Fatalf("Error initializing driver: %v", err) // } conn, err := esdriver.GetConnection() if err != nil { t.Fatalf("Error getting connection: %v", err) } k := datastore.NewKey("tweet", "1") tweet := map[string]string{ "user": "******", "post_date": "2009-11-15T14:12:12", "message": "trying out Elasticsearch", } tweetJSON, err := json.Marshal(tweet) err = conn.Put(k, datastore.NewJSONMessage(tweetJSON, 0)) if err != nil { t.Errorf("%v", err) } //Get tweet raw, err := conn.Get(k) if err != nil { t.Fatalf("Unexpected: %v", err) } glog.Infof("raw is %v", string(raw.Bytes())) var tweetMap map[string]string json.Unmarshal(raw.Bytes(), &tweetMap) glog.Infof("tweet is %v", tweetMap) if tweetMap["user"] != "kimchy" { t.Errorf("Expected kimchy, found %s", tweetMap["user"]) } //Delete tweet err = conn.Delete(k) if err != nil { t.Errorf("Unexpected delete error: %v", err) } //test not found raw, err = conn.Get(k) if raw != nil { t.Errorf("Expected nil return;") } 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) } }
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) } }
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") } }
//HostKey creates a Key suitable for getting, putting and deleting Hosts func HostKey(id string) datastore.Key { id = strings.TrimSpace(id) return datastore.NewKey(kind, id) }
//Key creates a Key suitable for getting, putting and deleting AddressAssignment func Key(id string) datastore.Key { return datastore.NewKey(kind, id) }
//confFileKey creates a Key suitable for getting, putting and deleting svcConfigFile func confFileKey(id string) datastore.Key { return datastore.NewKey(confKind, id) }