mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-06-12 16:53:35 +00:00
Ephemeral database for testing in the testing package
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
package tempdb
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -12,24 +12,24 @@ import (
|
|||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/database"
|
"github.com/mainnika/mongox-go-driver/v2/mongox/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TempDB is a temporary database connection that will be destroyed after close
|
// EphemeralDatabase is a temporary database connection that will be destroyed after close
|
||||||
type TempDB struct {
|
type EphemeralDatabase struct {
|
||||||
mongox.Database
|
mongox.Database
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTempDB creates new mongo connection
|
// NewEphemeral creates new mongo connection
|
||||||
func NewTempDB(URI string) (tempdb *TempDB, err error) {
|
func NewEphemeral(URI string) (db *EphemeralDatabase, err error) {
|
||||||
|
|
||||||
name := strconv.Itoa(rand.Int())
|
name := strconv.Itoa(rand.Int())
|
||||||
opts := options.Client().ApplyURI(URI)
|
opts := options.Client().ApplyURI(URI)
|
||||||
client, err := mongo.Connect(context.Background(), opts)
|
client, err := mongo.Connect(context.Background(), opts)
|
||||||
|
|
||||||
tempdb = &TempDB{Database: database.NewDatabase(client, name)}
|
db = &EphemeralDatabase{Database: database.NewDatabase(client, name)}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close the connection and drop database
|
// Close the connection and drop database
|
||||||
func (tdb *TempDB) Close() {
|
func (e *EphemeralDatabase) Close() error {
|
||||||
_ = tdb.Client().Database(tdb.Name()).Drop(tdb.Context())
|
return e.Client().Database(e.Name()).Drop(e.Context())
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/tempdb"
|
"github.com/mainnika/mongox-go-driver/v2/mongox-testing/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_GetID(t *testing.T) {
|
func Test_GetID(t *testing.T) {
|
||||||
@@ -41,7 +41,7 @@ func Test_SaveLoad(t *testing.T) {
|
|||||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := tempdb.NewTempDB("mongodb://localhost")
|
db, err := database.NewEphemeral("mongodb://localhost")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/tempdb"
|
"github.com/mainnika/mongox-go-driver/v2/mongox-testing/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_GetID(t *testing.T) {
|
func Test_GetID(t *testing.T) {
|
||||||
@@ -41,7 +41,7 @@ func Test_SaveLoad(t *testing.T) {
|
|||||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := tempdb.NewTempDB("mongodb://localhost")
|
db, err := database.NewEphemeral("mongodb://localhost")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/tempdb"
|
"github.com/mainnika/mongox-go-driver/v2/mongox-testing/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_GetID(t *testing.T) {
|
func Test_GetID(t *testing.T) {
|
||||||
@@ -40,7 +40,7 @@ func Test_SaveLoad(t *testing.T) {
|
|||||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := tempdb.NewTempDB("mongodb://localhost")
|
db, err := database.NewEphemeral("mongodb://localhost")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user