Skip to content

Commit

Permalink
Support getting documents from files
Browse files Browse the repository at this point in the history
  • Loading branch information
nhatthm committed Nov 4, 2021
1 parent d28a5ba commit fa3d424
Show file tree
Hide file tree
Showing 14 changed files with 283 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
uses: golangci/[email protected]
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.42.1
version: v1.43.0

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ jobs:

- name: Stop containers
if: always()
run: docker-compose -f "docker-compose.yml" -p "$GITHUB_SHA" down
run: ci/scripts/docker-down.sh
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ linters:
- paralleltest
- scopelint
- testpackage
- varnamelen
- wrapcheck

issues:
Expand Down
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ Given these docs are stored in index "products":
"""
```

You can also send the docs from a file by using:
- `docs (?:in|from) this file are stored in index "([^"]*)"[:]?$`
- `docs (?:in|from) this file are stored in index "([^"]*)" of es "([^"]*)"[:]?$` (if you want to index in the other instance)

For example:

```gherkin
Given docs in this file are stored in index "products":
"""
../../resources/fixtures/products.json
"""
```

#### Check whether an index exists

- `index "([^"]*)" exists$`
Expand Down Expand Up @@ -213,6 +226,19 @@ Then only these docs are available in index "products":
"""
```

You can also get the expected docs from a file by using:
- `only docs (?:in|from) this file are available in index "([^"]*)"[:]?$`
- `only docs (?:in|from) this file are available in index "([^"]*)" of es "([^"]*)"[:]?$` (if you want to index in the other instance)

For example:

```gherkin
Given only docs in this file are available in index "products":
"""
../../resources/fixtures/result.json
"""
```

#### Query documents

- First step: Setup the query <br/>
Expand Down Expand Up @@ -262,3 +288,16 @@ Then these docs are found in index "products":
]
"""
```

You can also get the expected docs from a file by using:
- `docs (?:in|from) this file are found in index "([^"]*)"[:]?$`
- `docs (?:in|from) this file are found in index "([^"]*)" of es "([^"]*)"[:]?$` (if you want to index in the other instance)

For example:

```gherkin
Given docs in this file are found in index "products":
"""
../../resources/fixtures/result_en_us.json
"""
```
15 changes: 15 additions & 0 deletions ci/assets/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: "3"
services:
elasticsearch:
container_name: elasticsearch
image: docker.elastic.co/elasticsearch/elasticsearch:7.15.1
ports:
- '9200:9200'
environment:
- xpack.security.enabled=false
- discovery.type=single-node
healthcheck:
test: [ "CMD-SHELL", "curl --silent --fail localhost:9200/_cluster/health || exit 1" ]
interval: 30s
timeout: 30s
retries: 3
5 changes: 5 additions & 0 deletions ci/scripts/docker-down.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

DOCKER_COMPOSE="ci/assets/docker-compose.yml"

docker-compose -f "$DOCKER_COMPOSE" -p "$GITHUB_SHA" down
2 changes: 1 addition & 1 deletion ci/scripts/docker-up.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

DOCKER_COMPOSE="docker-compose.yml"
DOCKER_COMPOSE="ci/assets/docker-compose.yml"
RETRY_COUNT=15
RETRY_TIMEOUT=5

Expand Down
43 changes: 39 additions & 4 deletions features/DefaultElasticsearch.feature
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,21 @@ Feature: Test Different ElasticSearch driver
]
"""

Scenario: Indexed documents from a file are available for search
Given index "$DRIVER_default_index_6" is recreated
And docs in this file are stored in index "$DRIVER_default_index_6":
"""
../../resources/fixtures/products_en_us.json
"""

And only docs in this file are available in index "$DRIVER_default_index_6":
"""
../../resources/fixtures/result_en_us.json
"""

Scenario: Search for documents by query
Given there is index "$DRIVER_default_index_6"
And these docs are stored in index "$DRIVER_default_index_6":
Given there is index "$DRIVER_default_index_7"
And these docs are stored in index "$DRIVER_default_index_7":
"""
[
{
Expand Down Expand Up @@ -157,7 +169,7 @@ Feature: Test Different ElasticSearch driver
]
"""

When I search in index "$DRIVER_default_index_6" with query:
When I search in index "$DRIVER_default_index_7" with query:
"""
{
"query": {
Expand All @@ -168,7 +180,7 @@ Feature: Test Different ElasticSearch driver
}
"""

Then these docs are found in index "$DRIVER_default_index_6":
Then these docs are found in index "$DRIVER_default_index_7":
"""
[
{
Expand All @@ -193,3 +205,26 @@ Feature: Test Different ElasticSearch driver
}
]
"""

Scenario: Search for documents from a file by query
Given there is index "$DRIVER_default_index_8"
And docs in this file are stored in index "$DRIVER_default_index_8":
"""
../../resources/fixtures/products_mixed.json
"""

When I search in index "$DRIVER_default_index_8" with query:
"""
{
"query": {
"match": {
"locale": "en_US"
}
}
}
"""

Then docs in this file are found in index "$DRIVER_default_index_8":
"""
../../resources/fixtures/result_en_us.json
"""
43 changes: 39 additions & 4 deletions features/ExtraDefaultElasticsearch.feature
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,21 @@ Feature: Test Different ElasticSearch driver
]
"""

Scenario: Indexed documents from a file are available for search
Given index "$DRIVER_extra_index_6" is recreated in es "extra"
And docs in this file are stored in index "$DRIVER_extra_index_6" of es "extra":
"""
../../resources/fixtures/products_en_us.json
"""

And only docs in this file are available in index "$DRIVER_extra_index_6" of es "extra":
"""
../../resources/fixtures/result_en_us.json
"""

Scenario: Search for documents by query
Given there is index "$DRIVER_extra_index_6" in es "extra"
And these docs are stored in index "$DRIVER_extra_index_6" of es "extra":
Given there is index "$DRIVER_extra_index_7" in es "extra"
And these docs are stored in index "$DRIVER_extra_index_7" of es "extra":
"""
[
{
Expand Down Expand Up @@ -157,7 +169,7 @@ Feature: Test Different ElasticSearch driver
]
"""

When I search in index "$DRIVER_extra_index_6" of es "extra" with query:
When I search in index "$DRIVER_extra_index_7" of es "extra" with query:
"""
{
"query": {
Expand All @@ -168,7 +180,7 @@ Feature: Test Different ElasticSearch driver
}
"""

Then these docs are found in index "$DRIVER_extra_index_6" of es "extra":
Then these docs are found in index "$DRIVER_extra_index_7" of es "extra":
"""
[
{
Expand All @@ -193,3 +205,26 @@ Feature: Test Different ElasticSearch driver
}
]
"""

Scenario: Search for documents from a file by query
Given there is index "$DRIVER_extra_index_8" in es "extra"
And docs in this file are stored in index "$DRIVER_extra_index_8" of es "extra":
"""
../../resources/fixtures/products_mixed.json
"""

When I search in index "$DRIVER_extra_index_8" of es "extra" with query:
"""
{
"query": {
"match": {
"locale": "en_US"
}
}
}
"""

Then docs in this file are found in index "$DRIVER_extra_index_8" of es "extra":
"""
../../resources/fixtures/result_en_us.json
"""
43 changes: 43 additions & 0 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"os"

"github.com/cucumber/godog"
"github.com/swaggest/assertjson"
Expand Down Expand Up @@ -53,6 +54,11 @@ func (m *Manager) registerPrerequisites(sc *godog.ScenarioContext) {
sc.Step(`these docs are stored in index "([^"]*)"[:]?$`, func(index string, docs *godog.DocString) error {
return m.indexDocs(index, defaultInstance, docs)
})

sc.Step(`docs (?:in|from) this file are stored in index "([^"]*)" of es "([^"]*)"[:]?$`, m.indexDocsFromFile)
sc.Step(`docs (?:in|from) this file are stored in index "([^"]*)"[:]?$`, func(index string, body *godog.DocString) error {
return m.indexDocsFromFile(index, defaultInstance, body)
})
}

func (m *Manager) registerActions(sc *godog.ScenarioContext) {
Expand Down Expand Up @@ -83,10 +89,20 @@ func (m *Manager) registerAssertions(sc *godog.ScenarioContext) {
return m.assertAllDocs(index, defaultInstance, docs)
})

sc.Step(`only docs (?:in|from) this file are available in index "([^"]*)" of es "([^"]*)"[:]?$`, m.assertAllDocsFromFile)
sc.Step(`only docs (?:in|from) this file are available in index "([^"]*)"[:]?$`, func(index string, body *godog.DocString) error {
return m.assertAllDocsFromFile(index, defaultInstance, body)
})

sc.Step(`these docs are found in index "([^"]*)" of es "([^"]*)"[:]?$`, m.assertFoundDocs)
sc.Step(`these docs are found in index "([^"]*)"[:]?$`, func(index string, docs *godog.DocString) error {
return m.assertFoundDocs(index, defaultInstance, docs)
})

sc.Step(`docs (?:in|from) this file are found in index "([^"]*)" of es "([^"]*)"[:]?$`, m.assertFoundDocsFromFile)
sc.Step(`docs (?:in|from) this file are found in index "([^"]*)"[:]?$`, func(index string, body *godog.DocString) error {
return m.assertFoundDocsFromFile(index, defaultInstance, body)
})
}

// RegisterContext registers the manager to the test suite.
Expand Down Expand Up @@ -128,6 +144,15 @@ func (m *Manager) indexDocs(index, instance string, body *godog.DocString) error
return m.client(instance).IndexDocuments(context.Background(), index, docs...)
}

func (m *Manager) indexDocsFromFile(index, instance string, body *godog.DocString) error {
content, err := os.ReadFile(body.Content)
if err != nil {
return fmt.Errorf("could not read docs from file %q: %w", body.Content, err)
}

return m.indexDocs(index, instance, &godog.DocString{Content: string(content)})
}

func (m *Manager) findDocuments(index, instance string, query *godog.DocString) error {
if _, ok := m.queries[instance]; !ok {
m.queries[instance] = make(map[string]*string)
Expand Down Expand Up @@ -185,6 +210,15 @@ func (m *Manager) assertAllDocs(index, instance string, body *godog.DocString) e
return nil
}

func (m *Manager) assertAllDocsFromFile(index, instance string, body *godog.DocString) error {
content, err := os.ReadFile(body.Content)
if err != nil {
return fmt.Errorf("could not read docs from file %q: %w", body.Content, err)
}

return m.assertAllDocs(index, instance, &godog.DocString{Content: string(content)})
}

func (m *Manager) assertFoundDocs(index, instance string, body *godog.DocString) error {
var query *string

Expand All @@ -211,6 +245,15 @@ func (m *Manager) assertFoundDocs(index, instance string, body *godog.DocString)
return nil
}

func (m *Manager) assertFoundDocsFromFile(index, instance string, body *godog.DocString) error {
content, err := os.ReadFile(body.Content)
if err != nil {
return fmt.Errorf("could not read docs from file %q: %w", body.Content, err)
}

return m.assertFoundDocs(index, instance, &godog.DocString{Content: string(content)})
}

// ManagerOption sets up the manager.
type ManagerOption func(m *Manager)

Expand Down
33 changes: 33 additions & 0 deletions manager_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,17 @@ func TestManager_indexDocs(t *testing.T) {
}
}

func TestManager_indexDocsFromFile_NotFound(t *testing.T) {
t.Parallel()

m := mockManager()(t)
err := m.indexDocsFromFile(index, instance, &godog.DocString{Content: "unknown"})

expected := `could not read docs from file "unknown": open unknown: no such file or directory`

assert.EqualError(t, err, expected)
}

func TestManager_assertIndexExists(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -440,6 +451,17 @@ func TestManager_assertAllDocs(t *testing.T) {
}
}

func TestManager_assertAllDocsFromFile_NotFound(t *testing.T) {
t.Parallel()

m := mockManager()(t)
err := m.assertAllDocsFromFile(index, instance, &godog.DocString{Content: "unknown"})

expected := `could not read docs from file "unknown": open unknown: no such file or directory`

assert.EqualError(t, err, expected)
}

func TestManager_assertFoundDocs(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -537,6 +559,17 @@ func TestManager_assertFoundDocs_WithQuery(t *testing.T) {
assert.NoError(t, err)
}

func TestManager_assertFoundDocsFromFile_NotFound(t *testing.T) {
t.Parallel()

m := mockManager()(t)
err := m.assertFoundDocsFromFile(index, instance, &godog.DocString{Content: "unknown"})

expected := `could not read docs from file "unknown": open unknown: no such file or directory`

assert.EqualError(t, err, expected)
}

func mockManager(mocks ...func(c *client)) func(t *testing.T) *Manager {
return func(t *testing.T) *Manager {
t.Helper()
Expand Down
Loading

0 comments on commit fa3d424

Please sign in to comment.