Skip to content

Commit 83a4933

Browse files
committed
Add test for Get special 404 case
1 parent 52fdb07 commit 83a4933

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

elasticsearch_integration_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,4 +506,42 @@ func TestTypedClient(t *testing.T) {
506506
t.Fatalf("invalid sort serialisation, got: %s", data)
507507
}
508508
})
509+
510+
t.Run("Get document index exists or not", func(t *testing.T) {
511+
es, err := elasticsearch.NewTypedClient(elasticsearch.Config{
512+
Addresses: []string{"http://localhost:9200"},
513+
Logger: &elastictransport.ColorLogger{os.Stdout, true, true},
514+
})
515+
if err != nil {
516+
t.Fatalf("error creating the client: %s", err)
517+
}
518+
519+
var indexName = "test-index-get"
520+
if ok, err := es.Indices.Exists(indexName).IsSuccess(context.Background()); ok && err == nil {
521+
es.Indices.Delete(indexName).Do(context.Background())
522+
}
523+
es.Indices.Create(indexName).Do(context.Background())
524+
es.Index(indexName).Id("test").Request(struct{}{}).Do(context.Background())
525+
526+
res, err := es.Get(indexName, "test").Do(context.Background())
527+
if err != nil {
528+
t.Fatal(err)
529+
}
530+
if !res.Found && res.Id_ != "test" {
531+
t.Fatal(err)
532+
}
533+
534+
res, err = es.Get(indexName, "empty").Do(context.Background())
535+
if err != nil {
536+
t.Fatal(err)
537+
}
538+
if res.Found {
539+
t.Fatal("document shouldn't have been found")
540+
}
541+
542+
res, err = es.Get("foo-index", "empty").Do(context.Background())
543+
if err == nil {
544+
t.Fatalf("error should have been raised : %s", err)
545+
}
546+
})
509547
}

0 commit comments

Comments
 (0)