Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: separate integration tests and do some refactoring #6

Merged
merged 5 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions internal/testing/constants/constants.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package constants

const (
Zero = 0
True = "true"
StatusOK = 200
Zero = 0
True = "true"
StatusOK = 200
TestIndex = "foo-index"
)
103 changes: 80 additions & 23 deletions internal/testing/elasticsearch_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,36 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"

"integration-tests/constants"
"net/http"
"strings"
)

type elasticsearchRepository struct {
client *elasticsearch.Client
}

type ElasticsearchRepository interface {
Search(index, query string) ([]FooDocument, error)
Insert(indexName, docId, document string) error
Delete(indexName, docId string) error
DeleteByQuery(indexName, query string) error
Exists(indexName, docId string) bool
Search(query string) ([]FooDocument, error)
BulkInsert(documents []FooDocument) error
Insert(document FooDocument) error
BulkDelete(docIds []string) error
Delete(docId string) error
DeleteByQuery(query string) error
Exists(docId string) bool
}

func NewElasticsearchRepository(client *elasticsearch.Client) ElasticsearchRepository {
return &elasticsearchRepository{client: client}
}

func (e *elasticsearchRepository) Search(index, query string) ([]FooDocument, error) {
func (e *elasticsearchRepository) Search(query string) ([]FooDocument, error) {
res, err := e.client.Search(
e.client.Search.WithIndex(index),
e.client.Search.WithIndex(constants.TestIndex),
e.client.Search.WithBody(strings.NewReader(query)),
)
defer func() {
Expand Down Expand Up @@ -60,11 +64,41 @@ func (e *elasticsearchRepository) Search(index, query string) ([]FooDocument, er
return result, nil
}

func (e *elasticsearchRepository) Insert(indexName, docId, document string) error {
func (e *elasticsearchRepository) BulkInsert(documents []FooDocument) error {
var bulkRequestBody strings.Builder

for i := range documents {
meta := fmt.Sprintf(`{"index":{"_index":"%s","_id":"%s"}}%s`, constants.TestIndex, documents[i], "\n")
bulkRequestBody.WriteString(meta)

docJson, err := json.Marshal(documents[i])
if err != nil {
return fmt.Errorf("failed to marshal document with Id %s: %w", documents[i].Id, err)
}
bulkRequestBody.WriteString(string(docJson) + "\n")
}
request := esapi.BulkRequest{
Body: strings.NewReader(bulkRequestBody.String()),
Refresh: constants.True,
}

res, err := request.Do(context.Background(), e.client)
if err != nil {
return fmt.Errorf("failed to execute bulk insert request: %w", err)
}
if res.IsError() {
return fmt.Errorf("bulk insert request returned error: %s", res.String())
}
return nil
}

func (e *elasticsearchRepository) Insert(document FooDocument) error {
fooDoc, _ := json.Marshal(document)

request := esapi.IndexRequest{
Index: indexName,
DocumentID: docId,
Body: strings.NewReader(document),
Index: constants.TestIndex,
DocumentID: document.Id,
Body: strings.NewReader(string(fooDoc)),
Refresh: constants.True,
}

Expand All @@ -78,39 +112,62 @@ func (e *elasticsearchRepository) Insert(indexName, docId, document string) erro
return err
}

func (e *elasticsearchRepository) Delete(indexName, docId string) error {
func (e *elasticsearchRepository) Delete(docId string) error {
request := esapi.DeleteRequest{
Index: indexName,
Index: constants.TestIndex,
DocumentID: docId,
}
res, err := request.Do(context.Background(), e.client)
if err != nil {
return fmt.Errorf("failed to execute insert request: %w", err)
return fmt.Errorf("failed to execute delete request: %w", err)
}
if res.IsError() {
return fmt.Errorf("insert request returned error: %s", res.String())
return fmt.Errorf("delete request returned error: %s", res.String())
}
return err
}

func (e *elasticsearchRepository) DeleteByQuery(indexName, query string) error {
func (e *elasticsearchRepository) BulkDelete(docIds []string) error {
var bulkRequestBody strings.Builder

for _, docId := range docIds {
meta := fmt.Sprintf(`{"delete":{"_index":"%s","_id":"%s"}}%s`, constants.TestIndex, docId, "\n")
bulkRequestBody.WriteString(meta)
}

request := esapi.BulkRequest{
Body: strings.NewReader(bulkRequestBody.String()),
Refresh: constants.True,
}

res, err := request.Do(context.Background(), e.client)
if err != nil {
return fmt.Errorf("failed to execute bulk delete request: %w", err)
}
if res.IsError() {
return fmt.Errorf("bulk delete request returned error: %s", res.String())
}
return nil
}

func (e *elasticsearchRepository) DeleteByQuery(query string) error {
request := esapi.DeleteByQueryRequest{
Index: []string{indexName},
Index: []string{constants.TestIndex},
Body: strings.NewReader(query),
}
res, err := request.Do(context.Background(), e.client)
if err != nil {
return fmt.Errorf("failed to execute insert request: %w", err)
return fmt.Errorf("failed to execute delete by query request: %w", err)
}
if res.IsError() {
return fmt.Errorf("insert request returned error: %s", res.String())
return fmt.Errorf("delete by query request returned error: %s", res.String())
}
return err
}

func (e *elasticsearchRepository) Exists(indexName, docId string) bool {
func (e *elasticsearchRepository) Exists(docId string) bool {
request := esapi.ExistsRequest{
Index: indexName,
Index: constants.TestIndex,
DocumentID: docId,
}
res, _ := request.Do(context.Background(), e.client)
Expand Down
130 changes: 0 additions & 130 deletions internal/testing/integration_test.go

This file was deleted.

46 changes: 46 additions & 0 deletions internal/testing/query_string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package testing

import (
"encoding/json"

"github.com/Trendyol/es-query-builder/es"
"github.com/bayraktugrul/go-await"
"github.com/stretchr/testify/assert"
)

func (s *testSuite) Test_it_should_return_documents_that_filtered_by_query_string_with_wildcard_or_operator() {
// Given
foo := FooDocument{
Id: "10",
Foo: "foo",
}
bar := FooDocument{
Id: "20",
Foo: "bar",
}
george := FooDocument{
Id: "30",
Foo: "george orwell",
}
s.ElasticsearchRepository.BulkInsert([]FooDocument{foo, bar, george})
await.New().Await(func() bool { return s.ElasticsearchRepository.Exists(foo.Id) })
await.New().Await(func() bool { return s.ElasticsearchRepository.Exists(bar.Id) })
await.New().Await(func() bool { return s.ElasticsearchRepository.Exists(george.Id) })

query := es.NewQuery(
es.Bool().Must(
es.QueryString[string]("ge* OR bar").AnalyzeWildcard(true)),
)
bodyJSON, _ := json.Marshal(query)

// When
result, err := s.ElasticsearchRepository.Search(string(bodyJSON))

// Then
assert.Nil(s.T(), err)
assert.Equal(s.T(), len(result), 2)
assert.Equal(s.T(), result[0].Foo, "george orwell")
assert.Equal(s.T(), result[1].Foo, "bar")

s.ElasticsearchRepository.BulkDelete([]string{foo.Id, bar.Id, george.Id})
}
15 changes: 9 additions & 6 deletions internal/testing/search_response.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package testing

type SearchResponse struct {
Hits struct {
Hits []Hit `json:"hits"`
} `json:"hits"`
type FooDocument struct {
Id string `json:"id"`
Foo string `json:"foo"`
}

type Hit struct {
Source FooDocument `json:"_source"`
}

type FooDocument struct {
Foo string `json:"foo"`
type Hits struct {
Hits []Hit `json:"hits"`
}

type SearchResponse struct {
Hits Hits `json:"hits"`
}
Loading
Loading