Skip to content

Commit

Permalink
feat(mongo): add "loadFixtures" action (ovh#653)
Browse files Browse the repository at this point in the history
Signed-off-by: Thibaut Rousseau <thibaut@crew.work>
Signed-off-by: Ivan Velasco <ivan.velasco@socotra.com>
  • Loading branch information
Thiht authored and ivan-velasco committed Sep 20, 2023
1 parent 5e380f1 commit e55db7b
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 1 deletion.
27 changes: 27 additions & 0 deletions executors/mongo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@ Example:
}
```

### Load fixtures

```yaml
- type: mongo
uri: mongodb://localhost:27017
database: my-database
actions:
- type: loadFixtures
folder: fixtures/
```

This action will first **drop all the collections in the database**, and then load multiple collections at once from a folder.
The fixtures folder must contain one file per collection, and be named after the collection. For example, `cards.yml` will create a `cards` collection.
The items in the collections are declared as a YAML array. For example:

```yaml
# fixtures/cards.yml
- suit: clubs
value: jack

- suit: clubs
value: queen

- suit: clubs
value: king
```

### Insert documents

```yaml
Expand Down
70 changes: 70 additions & 0 deletions executors/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"io"
"os"
"path"
"strings"

"github.com/mitchellh/mapstructure"
"github.com/ovh/venom"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"gopkg.in/yaml.v3"
)

const Name = "mongo"
Expand Down Expand Up @@ -49,6 +51,69 @@ func (e Executor) Run(ctx context.Context, step venom.TestStep) (any, error) {
for i, action := range e.Actions {
actionType := fmt.Sprintf("%v", action["type"])
switch actionType {
case "loadFixtures":
var loadFixturesAction LoadFixturesAction
if err := mapstructure.Decode(action, &loadFixturesAction); err != nil {
return nil, err
}

if loadFixturesAction.Folder == "" {
return nil, fmt.Errorf("folder is required")
}

// First, drop the existing collections in the database to start clean
collections, err := mongoClient.Database(e.Database).ListCollectionNames(ctx, bson.M{})
if err != nil {
return nil, fmt.Errorf("failed to list collections: %w", err)
}

for _, collection := range collections {
if strings.HasPrefix(collection, "system.") {
continue
}

if err := mongoClient.Database(e.Database).Collection(collection).Drop(ctx); err != nil {
return nil, fmt.Errorf("failed to drop collection %s: %w", collection, err)
}
}

dirEntries, err := os.ReadDir(path.Join(venom.StringVarFromCtx(ctx, "venom.testsuite.workdir"), loadFixturesAction.Folder))
if err != nil {
return nil, err
}

fixtures := make([]string, 0, len(dirEntries))
for _, file := range dirEntries {
if file.IsDir() {
continue
}

extension := path.Ext(file.Name())
if extension != ".yaml" && extension != ".yml" {
continue
}
fixtures = append(fixtures, file.Name())
}

for _, fixture := range fixtures {
collectionName := strings.TrimSuffix(fixture, path.Ext(fixture))
filePath := path.Join(venom.StringVarFromCtx(ctx, "venom.testsuite.workdir"), loadFixturesAction.Folder, fixture)

file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("failed to open file %s: %w", filePath, err)
}

var documents []any
if err := yaml.NewDecoder(file).Decode(&documents); err != nil {
return nil, fmt.Errorf("failed to decode fixture %s: %w", filePath, err)
}

if _, err := mongoClient.Database(e.Database).Collection(collectionName).InsertMany(ctx, documents); err != nil {
return nil, fmt.Errorf("failed to insert documents in collection %s: %w", collectionName, err)
}
}

case "dropCollection":
if err := mongoClient.Database(e.Database).Collection(e.Collection).Drop(ctx); err != nil {
return nil, err
Expand Down Expand Up @@ -290,6 +355,11 @@ type CountAction struct {
}
}

type LoadFixturesAction struct {
Action
Folder string
}

type InsertAction struct {
Action
File string
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ require (
golang.org/x/net v0.8.0
google.golang.org/grpc v1.53.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
modernc.org/sqlite v1.21.0
)

Expand Down Expand Up @@ -101,7 +102,6 @@ require (
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
google.golang.org/protobuf v1.29.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
modernc.org/cc/v3 v3.40.0 // indirect
modernc.org/ccgo/v3 v3.16.13 // indirect
Expand Down
44 changes: 44 additions & 0 deletions tests/mongo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,50 @@ vars:
mongo_collection: cards

testcases:
- name: Load fixtures
steps:
- type: mongo
uri: "{{.mongo_uri}}"
database: "{{.mongo_database}}"
actions:
- type: loadFixtures
folder: mongo/fixturesA

- type: mongo
uri: "{{.mongo_uri}}"
database: "{{.mongo_database}}"
collection: cards
actions:
- type: count
assertions:
- result.actions.actions0.count ShouldEqual 3

- type: mongo
uri: "{{.mongo_uri}}"
database: "{{.mongo_database}}"
actions:
- type: loadFixtures
folder: mongo/fixturesB

# Ensure fixturesA have been dropped
- type: mongo
uri: "{{.mongo_uri}}"
database: "{{.mongo_database}}"
collection: cards
actions:
- type: count
assertions:
- result.actions.actions0.count ShouldEqual 0

- type: mongo
uri: "{{.mongo_uri}}"
database: "{{.mongo_database}}"
collection: pokemons
actions:
- type: count
assertions:
- result.actions.actions0.count ShouldEqual 3

- name: Reset collection
steps:
- type: mongo
Expand Down
8 changes: 8 additions & 0 deletions tests/mongo/fixturesA/cards.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- suit: clubs
value: jack

- suit: clubs
value: queen

- suit: clubs
value: king
12 changes: 12 additions & 0 deletions tests/mongo/fixturesB/pokemons.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
- name: Bulbasaur
types:
- grass
- poison

- name: Charmander
types:
- fire

- name: Squirtle
types:
- water

0 comments on commit e55db7b

Please sign in to comment.