mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-06-12 16:53:35 +00:00
Keep tests in separated _test package
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
package base
|
package base_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@@ -6,6 +6,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/base"
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base/jsonbased"
|
"github.com/mainnika/mongox-go-driver/v2/mongox/base/jsonbased"
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base/oidbased"
|
"github.com/mainnika/mongox-go-driver/v2/mongox/base/oidbased"
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base/stringbased"
|
"github.com/mainnika/mongox-go-driver/v2/mongox/base/stringbased"
|
||||||
@@ -35,8 +36,8 @@ func TestGetID(t *testing.T) {
|
|||||||
stringbased.Primary `bson:",inline" json:",inline" collection:"3"`
|
stringbased.Primary `bson:",inline" json:",inline" collection:"3"`
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), GetID(&DocWithObjectID{oidbased.Primary{[12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}}}))
|
assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), base.GetID(&DocWithObjectID{Primary: oidbased.Primary{ID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}}}))
|
||||||
assert.Equal(t, primitive.D{{"1", "2"}}, GetID(&DocWithObject{jsonbased.Primary{primitive.D{{"1", "2"}}}}))
|
assert.Equal(t, primitive.D{{"1", "2"}}, base.GetID(&DocWithObject{Primary: jsonbased.Primary{ID: primitive.D{{"1", "2"}}}}))
|
||||||
assert.Equal(t, "foobar", GetID(&DocWithString{stringbased.Primary{"foobar"}}))
|
assert.Equal(t, "foobar", base.GetID(&DocWithString{Primary: stringbased.Primary{ID: "foobar"}}))
|
||||||
assert.Equal(t, 420, GetID(&DocWithCustomInterface{ID: 420}))
|
assert.Equal(t, 420, base.GetID(&DocWithCustomInterface{ID: 420}))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package jsonbased
|
package jsonbased_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -8,15 +8,16 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox-testing/database"
|
"github.com/mainnika/mongox-go-driver/v2/mongox-testing/database"
|
||||||
|
"github.com/mainnika/mongox-go-driver/v2/mongox/base/jsonbased"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_GetID(t *testing.T) {
|
func Test_GetID(t *testing.T) {
|
||||||
|
|
||||||
type DocWithObject struct {
|
type DocWithObject struct {
|
||||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
jsonbased.Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
doc := &DocWithObject{Primary{primitive.D{{"1", "one"}, {"2", "two"}}}}
|
doc := &DocWithObject{Primary: jsonbased.Primary{ID: primitive.D{{"1", "one"}, {"2", "two"}}}}
|
||||||
|
|
||||||
assert.Equal(t, primitive.D{{"1", "one"}, {"2", "two"}}, doc.GetID())
|
assert.Equal(t, primitive.D{{"1", "one"}, {"2", "two"}}, doc.GetID())
|
||||||
}
|
}
|
||||||
@@ -24,10 +25,10 @@ func Test_GetID(t *testing.T) {
|
|||||||
func Test_SetID(t *testing.T) {
|
func Test_SetID(t *testing.T) {
|
||||||
|
|
||||||
type DocWithObject struct {
|
type DocWithObject struct {
|
||||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
jsonbased.Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
doc := &DocWithObject{Primary{primitive.D{{"1", "one"}, {"2", "two"}}}}
|
doc := &DocWithObject{Primary: jsonbased.Primary{ID: primitive.D{{"1", "one"}, {"2", "two"}}}}
|
||||||
|
|
||||||
doc.SetID(primitive.D{{"3", "three"}, {"4", "you"}})
|
doc.SetID(primitive.D{{"3", "three"}, {"4", "you"}})
|
||||||
|
|
||||||
@@ -38,7 +39,7 @@ func Test_SetID(t *testing.T) {
|
|||||||
func Test_SaveLoad(t *testing.T) {
|
func Test_SaveLoad(t *testing.T) {
|
||||||
|
|
||||||
type DocWithObjectID struct {
|
type DocWithObjectID struct {
|
||||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
jsonbased.Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := database.NewEphemeral("")
|
db, err := database.NewEphemeral("")
|
||||||
@@ -48,7 +49,7 @@ func Test_SaveLoad(t *testing.T) {
|
|||||||
|
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
doc1 := &DocWithObjectID{Primary{primitive.D{{"1", "one"}, {"2", "two"}}}}
|
doc1 := &DocWithObjectID{Primary: jsonbased.Primary{ID: primitive.D{{"1", "one"}, {"2", "two"}}}}
|
||||||
doc2 := &DocWithObjectID{}
|
doc2 := &DocWithObjectID{}
|
||||||
|
|
||||||
err = db.SaveOne(doc1)
|
err = db.SaveOne(doc1)
|
||||||
@@ -68,11 +69,10 @@ func Test_SaveLoad(t *testing.T) {
|
|||||||
func Test_Marshal(t *testing.T) {
|
func Test_Marshal(t *testing.T) {
|
||||||
|
|
||||||
type DocWithObjectID struct {
|
type DocWithObjectID struct {
|
||||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
jsonbased.Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
id := primitive.D{{"1", "one"}, {"2", "two"}}
|
doc := &DocWithObjectID{Primary: jsonbased.Primary{ID: primitive.D{{"1", "one"}, {"2", "two"}}}}
|
||||||
doc := &DocWithObjectID{Primary{id}}
|
|
||||||
|
|
||||||
bytes, err := json.Marshal(doc)
|
bytes, err := json.Marshal(doc)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package oidbased
|
package oidbased_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -8,15 +8,16 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox-testing/database"
|
"github.com/mainnika/mongox-go-driver/v2/mongox-testing/database"
|
||||||
|
"github.com/mainnika/mongox-go-driver/v2/mongox/base/oidbased"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_GetID(t *testing.T) {
|
func Test_GetID(t *testing.T) {
|
||||||
|
|
||||||
type DocWithObjectID struct {
|
type DocWithObjectID struct {
|
||||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
oidbased.Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
doc := &DocWithObjectID{Primary{[12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}}}
|
doc := &DocWithObjectID{Primary: oidbased.Primary{ID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}}}
|
||||||
|
|
||||||
assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), doc.GetID())
|
assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), doc.GetID())
|
||||||
}
|
}
|
||||||
@@ -24,7 +25,7 @@ func Test_GetID(t *testing.T) {
|
|||||||
func Test_SetID(t *testing.T) {
|
func Test_SetID(t *testing.T) {
|
||||||
|
|
||||||
type DocWithObjectID struct {
|
type DocWithObjectID struct {
|
||||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
oidbased.Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
doc := &DocWithObjectID{}
|
doc := &DocWithObjectID{}
|
||||||
@@ -38,7 +39,7 @@ func Test_SetID(t *testing.T) {
|
|||||||
func Test_SaveLoad(t *testing.T) {
|
func Test_SaveLoad(t *testing.T) {
|
||||||
|
|
||||||
type DocWithObjectID struct {
|
type DocWithObjectID struct {
|
||||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
oidbased.Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := database.NewEphemeral("")
|
db, err := database.NewEphemeral("")
|
||||||
@@ -68,11 +69,11 @@ func Test_SaveLoad(t *testing.T) {
|
|||||||
func Test_Marshal(t *testing.T) {
|
func Test_Marshal(t *testing.T) {
|
||||||
|
|
||||||
type DocWithObjectID struct {
|
type DocWithObjectID struct {
|
||||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
oidbased.Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
id, _ := primitive.ObjectIDFromHex("feadbeeffeadbeeffeadbeef")
|
id, _ := primitive.ObjectIDFromHex("feadbeeffeadbeeffeadbeef")
|
||||||
doc := &DocWithObjectID{Primary{id}}
|
doc := &DocWithObjectID{Primary: oidbased.Primary{ID: id}}
|
||||||
|
|
||||||
bytes, err := json.Marshal(doc)
|
bytes, err := json.Marshal(doc)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package stringbased
|
package stringbased_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -7,15 +7,16 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox-testing/database"
|
"github.com/mainnika/mongox-go-driver/v2/mongox-testing/database"
|
||||||
|
"github.com/mainnika/mongox-go-driver/v2/mongox/base/stringbased"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_GetID(t *testing.T) {
|
func Test_GetID(t *testing.T) {
|
||||||
|
|
||||||
type DocWithString struct {
|
type DocWithString struct {
|
||||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
stringbased.Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
doc := &DocWithString{Primary{"foobar"}}
|
doc := &DocWithString{Primary: stringbased.Primary{ID: "foobar"}}
|
||||||
|
|
||||||
assert.Equal(t, "foobar", doc.GetID())
|
assert.Equal(t, "foobar", doc.GetID())
|
||||||
}
|
}
|
||||||
@@ -23,10 +24,10 @@ func Test_GetID(t *testing.T) {
|
|||||||
func Test_SetID(t *testing.T) {
|
func Test_SetID(t *testing.T) {
|
||||||
|
|
||||||
type DocWithString struct {
|
type DocWithString struct {
|
||||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
stringbased.Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
doc := &DocWithString{Primary{"foobar"}}
|
doc := &DocWithString{Primary: stringbased.Primary{ID: "foobar"}}
|
||||||
|
|
||||||
doc.SetID("rockrockrock")
|
doc.SetID("rockrockrock")
|
||||||
|
|
||||||
@@ -37,7 +38,7 @@ func Test_SetID(t *testing.T) {
|
|||||||
func Test_SaveLoad(t *testing.T) {
|
func Test_SaveLoad(t *testing.T) {
|
||||||
|
|
||||||
type DocWithObjectID struct {
|
type DocWithObjectID struct {
|
||||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
stringbased.Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := database.NewEphemeral("")
|
db, err := database.NewEphemeral("")
|
||||||
@@ -47,7 +48,7 @@ func Test_SaveLoad(t *testing.T) {
|
|||||||
|
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
doc1 := &DocWithObjectID{Primary{"foobar"}}
|
doc1 := &DocWithObjectID{Primary: stringbased.Primary{ID: "foobar"}}
|
||||||
doc2 := &DocWithObjectID{}
|
doc2 := &DocWithObjectID{}
|
||||||
|
|
||||||
err = db.SaveOne(doc1)
|
err = db.SaveOne(doc1)
|
||||||
@@ -67,10 +68,10 @@ func Test_SaveLoad(t *testing.T) {
|
|||||||
func Test_Marshal(t *testing.T) {
|
func Test_Marshal(t *testing.T) {
|
||||||
|
|
||||||
type DocWithObjectID struct {
|
type DocWithObjectID struct {
|
||||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
stringbased.Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
doc := &DocWithObjectID{Primary{"foobar"}}
|
doc := &DocWithObjectID{Primary: stringbased.Primary{ID: "foobar"}}
|
||||||
|
|
||||||
bytes, err := json.Marshal(doc)
|
bytes, err := json.Marshal(doc)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user