Skip to content

Commit de78365

Browse files
author
Ezequiel Raynaudo
authored
Merge pull request #16 from Zondax/add/MongoConnection
Add/mongo connection
2 parents 05b60c5 + e59c005 commit de78365

8 files changed

Lines changed: 213 additions & 24 deletions

File tree

.circleci/config.yml

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,22 @@ jobs:
2121
- run: make lint
2222

2323
integration:
24-
docker:
25-
- image: golang:1.16
24+
machine:
25+
image: ubuntu-2004:202104-01
2626
steps:
2727
- checkout
2828
- run:
29-
name: "Prebuild"
30-
command: |
31-
echo "Prebuild..."
32-
- run:
33-
name: "Launch background processes"
34-
background: true
35-
command: |
36-
echo "Starting executables we need in background"
37-
- run:
38-
name: "Give time for processes to start"
39-
command: sleep 3
40-
- run:
41-
name: "Run tests"
29+
name: "Run database tests"
4230
command: |
43-
make test_integration
31+
make test-database-services
4432
4533
workflows:
4634
version: 2
4735
build_all:
4836
jobs:
4937
- build
5038
- checks
51-
# - integration:
52-
# requires:
53-
# - build
54-
# - checks
39+
- integration:
40+
requires:
41+
- build
42+
- checks

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,12 @@ lint:
2222

2323
test: build
2424
go test -race ./...
25+
26+
test-database: build
27+
go test ./connections/database/... -v
28+
29+
# Docker
30+
31+
test-database-services:
32+
docker-compose -f tests_docker/test_database.yml up --abort-on-container-exit
33+

connections/database/interface.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package database
22

33
import (
44
"fmt"
5-
"gorm.io/gorm"
65
"net/http"
6+
7+
"gorm.io/gorm"
78
)
89

910
type AuthHeaderTransport struct {
@@ -21,8 +22,8 @@ type DBConnection interface {
2122
}
2223

2324
type IDBQueryClient interface {
24-
Connect() error
2525
GetDB() interface{}
26+
Connect() error
2627
}
2728

2829
type DBQueryClient struct {
@@ -35,6 +36,7 @@ type DBConnectionParams struct {
3536
Name string
3637
Host string
3738
Port string
39+
URI string
3840
}
3941

4042
type GraphqlClientParams struct {

connections/database/mongoDB.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package database
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"go.mongodb.org/mongo-driver/mongo"
9+
"go.mongodb.org/mongo-driver/mongo/options"
10+
"go.mongodb.org/mongo-driver/mongo/readpref"
11+
)
12+
13+
type MongoConnection struct {
14+
db *mongo.Client
15+
}
16+
17+
func NewMongoConnection(params *DBConnectionParams) (*MongoConnection, error) {
18+
uri := params.URI
19+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
20+
defer cancel()
21+
22+
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
23+
if err != nil {
24+
fmt.Printf("Failed to connect to db: %v \n", err)
25+
return nil, err
26+
}
27+
28+
// Ping the primary
29+
if err := client.Ping(ctx, readpref.Primary()); err != nil {
30+
return nil, err
31+
}
32+
fmt.Println("Successfully connected and pinged.")
33+
34+
return &MongoConnection{db: client}, nil
35+
}
36+
37+
func (c *MongoConnection) GetDB() *mongo.Client {
38+
return c.db
39+
}
40+
41+
func (c *DBQueryClient) Connect(params *DBConnectionParams) (*mongo.Client, error) {
42+
conn, err := NewMongoConnection(params)
43+
if err != nil {
44+
return nil, err
45+
}
46+
47+
return conn.GetDB(), nil
48+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package database
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"testing"
8+
9+
"go.mongodb.org/mongo-driver/bson"
10+
"go.mongodb.org/mongo-driver/mongo"
11+
"go.mongodb.org/mongo-driver/mongo/options"
12+
)
13+
14+
func TestBuffer_InsertRead(t *testing.T) {
15+
var c DBQueryClient
16+
params := &DBConnectionParams{URI: os.Getenv("MONGO_URI")}
17+
client, err := c.Connect(params)
18+
if err != nil {
19+
fmt.Println(err)
20+
t.Fail()
21+
}
22+
23+
coll := client.Database("test").Collection("sample")
24+
res, err := coll.InsertOne(context.TODO(), bson.D{{Key: "name", Value: "Alice"}})
25+
if err != nil {
26+
fmt.Println(err)
27+
t.Fail()
28+
}
29+
fmt.Printf("inserted document with ID %v\n", res.InsertedID)
30+
31+
// Find the document for which the _id field matches id.
32+
// Specify the Sort option to sort the documents by age.
33+
// The first document in the sorted order will be returned.
34+
opts := options.FindOne().SetSort(bson.D{{Key: "age", Value: 1}})
35+
var result bson.M
36+
readErr := coll.FindOne(
37+
context.TODO(),
38+
bson.D{{Key: "_id", Value: res.InsertedID}},
39+
opts,
40+
).Decode(&result)
41+
if readErr != nil {
42+
// ErrNoDocuments means that the filter did not match any documents in
43+
// the collection.
44+
if err == mongo.ErrNoDocuments {
45+
fmt.Println("No document found")
46+
}
47+
fmt.Println(err)
48+
t.Fail()
49+
}
50+
51+
fmt.Printf("found document %v", result)
52+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/prometheus/client_golang v1.11.0
1313
github.com/spf13/cobra v1.1.1
1414
github.com/spf13/viper v1.7.1
15+
go.mongodb.org/mongo-driver v1.7.1
1516
go.uber.org/zap v1.16.0
1617
gorm.io/driver/postgres v1.0.6
1718
gorm.io/gorm v1.21.12

go.sum

Lines changed: 63 additions & 2 deletions
Large diffs are not rendered by default.

tests_docker/test_database.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: "3.9"
2+
networks:
3+
dbNetwork:
4+
driver: bridge
5+
6+
services:
7+
mongodb:
8+
image: mongo:latest
9+
logging:
10+
driver: none
11+
networks:
12+
- dbNetwork
13+
environment:
14+
MONGO_INITDB_ROOT_USERNAME: mongo
15+
MONGO_INITDB_ROOT_PASSWORD: password
16+
17+
goapp:
18+
image: golang:latest
19+
depends_on:
20+
- mongodb
21+
networks:
22+
- dbNetwork
23+
volumes:
24+
- ./../:/app
25+
working_dir: /app
26+
environment:
27+
MONGO_URI: mongodb://mongo:password@mongodb:27017/
28+
entrypoint: ["make", "test-database"]

0 commit comments

Comments
 (0)