-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Docker Compose file for testing MongoDB, add Makefile for running…
… tests, update GitHub Actions workflow, update dependencies, add test utilities, and add repository test
- Loading branch information
1 parent
40ff68a
commit daaa6c4
Showing
7 changed files
with
115 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.PHONY: test | ||
test: | ||
@echo "Starting MongoDB..." | ||
docker-compose -f ./tests/docker-compose.yml up -d | ||
@echo "Running tests..." | ||
MONGODB_URI="mongodb://username:password@localhost:27017" go test -v -p 1 -count=1 -race -cover ./... | ||
@echo "Stopping MongoDB..." | ||
docker-compose -f ./tests/docker-compose.yml down |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
version: '3.9' | ||
|
||
services: | ||
mongo: | ||
image: mongo | ||
ports: | ||
- "27017:27017" | ||
environment: | ||
MONGO_INITDB_ROOT_USERNAME: username | ||
MONGO_INITDB_ROOT_PASSWORD: password |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tests | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
mongorepository "github.com/dmitrymomot/mongo-repository" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
) | ||
|
||
type User struct { | ||
ID primitive.ObjectID `bson:"_id,omitempty"` | ||
Name string `bson:"name"` | ||
Email string `bson:"email"` | ||
} | ||
|
||
func TestCreateAndFindByID(t *testing.T) { | ||
db := setupMongoDB(t) | ||
repo := mongorepository.NewMongoRepository[User](db, "users") | ||
|
||
user := User{Name: "John Doe", Email: "john@example.com"} | ||
|
||
// Test Create | ||
id, err := repo.Create(context.Background(), user) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, id) | ||
|
||
// Test FindByID | ||
foundUser, err := repo.FindByID(context.Background(), id) | ||
require.NoError(t, err) | ||
assert.Equal(t, user.Name, foundUser.Name) | ||
assert.Equal(t, user.Email, foundUser.Email) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package tests | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
func getMongoDBURI() string { | ||
uri := os.Getenv("MONGODB_URI") | ||
if uri == "" { | ||
return "mongodb://localhost:27017" // default URI | ||
} | ||
return uri | ||
} | ||
|
||
func setupMongoDB(t *testing.T) *mongo.Database { | ||
// Connect to MongoDB | ||
uri := getMongoDBURI() | ||
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri)) | ||
if err != nil { | ||
t.Fatalf("Failed to connect to MongoDB: %v", err) | ||
} | ||
|
||
db := client.Database("test_db") | ||
// Clean up the database before and after the test | ||
t.Cleanup(func() { | ||
if err := db.Drop(context.Background()); err != nil { | ||
t.Errorf("Failed to drop database: %v", err) | ||
} | ||
}) | ||
|
||
return db | ||
} |