Keep tests in separated _test package

main
Nikita Tokarchuk 1 year ago
parent 42e484e47f
commit 7c3e50e783
Signed by: mainnika
GPG Key ID: A595FB7E3E56911C
  1. 11
      mongox/base/getid_test.go
  2. 20
      mongox/base/jsonbased/id_test.go
  3. 15
      mongox/base/oidbased/id_test.go
  4. 19
      mongox/base/stringbased/id_test.go

@ -1,4 +1,4 @@
package base
package base_test
import (
"testing"
@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"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/oidbased"
"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"`
}
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.D{{"1", "2"}}, GetID(&DocWithObject{jsonbased.Primary{primitive.D{{"1", "2"}}}}))
assert.Equal(t, "foobar", GetID(&DocWithString{stringbased.Primary{"foobar"}}))
assert.Equal(t, 420, GetID(&DocWithCustomInterface{ID: 420}))
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"}}, base.GetID(&DocWithObject{Primary: jsonbased.Primary{ID: primitive.D{{"1", "2"}}}}))
assert.Equal(t, "foobar", base.GetID(&DocWithString{Primary: stringbased.Primary{ID: "foobar"}}))
assert.Equal(t, 420, base.GetID(&DocWithCustomInterface{ID: 420}))
}

@ -1,4 +1,4 @@
package jsonbased
package jsonbased_test
import (
"encoding/json"
@ -8,15 +8,16 @@ import (
"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/base/jsonbased"
)
func Test_GetID(t *testing.T) {
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())
}
@ -24,10 +25,10 @@ func Test_GetID(t *testing.T) {
func Test_SetID(t *testing.T) {
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"}})
@ -38,7 +39,7 @@ func Test_SetID(t *testing.T) {
func Test_SaveLoad(t *testing.T) {
type DocWithObjectID struct {
Primary `bson:",inline" json:",inline" collection:"1"`
jsonbased.Primary `bson:",inline" json:",inline" collection:"1"`
}
db, err := database.NewEphemeral("")
@ -48,7 +49,7 @@ func Test_SaveLoad(t *testing.T) {
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{}
err = db.SaveOne(doc1)
@ -68,11 +69,10 @@ func Test_SaveLoad(t *testing.T) {
func Test_Marshal(t *testing.T) {
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{id}}
doc := &DocWithObjectID{Primary: jsonbased.Primary{ID: primitive.D{{"1", "one"}, {"2", "two"}}}}
bytes, err := json.Marshal(doc)
assert.NoError(t, err)

@ -1,4 +1,4 @@
package oidbased
package oidbased_test
import (
"encoding/json"
@ -8,15 +8,16 @@ import (
"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/base/oidbased"
)
func Test_GetID(t *testing.T) {
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())
}
@ -24,7 +25,7 @@ func Test_GetID(t *testing.T) {
func Test_SetID(t *testing.T) {
type DocWithObjectID struct {
Primary `bson:",inline" json:",inline" collection:"1"`
oidbased.Primary `bson:",inline" json:",inline" collection:"1"`
}
doc := &DocWithObjectID{}
@ -38,7 +39,7 @@ func Test_SetID(t *testing.T) {
func Test_SaveLoad(t *testing.T) {
type DocWithObjectID struct {
Primary `bson:",inline" json:",inline" collection:"1"`
oidbased.Primary `bson:",inline" json:",inline" collection:"1"`
}
db, err := database.NewEphemeral("")
@ -68,11 +69,11 @@ func Test_SaveLoad(t *testing.T) {
func Test_Marshal(t *testing.T) {
type DocWithObjectID struct {
Primary `bson:",inline" json:",inline" collection:"1"`
oidbased.Primary `bson:",inline" json:",inline" collection:"1"`
}
id, _ := primitive.ObjectIDFromHex("feadbeeffeadbeeffeadbeef")
doc := &DocWithObjectID{Primary{id}}
doc := &DocWithObjectID{Primary: oidbased.Primary{ID: id}}
bytes, err := json.Marshal(doc)
assert.NoError(t, err)

@ -1,4 +1,4 @@
package stringbased
package stringbased_test
import (
"encoding/json"
@ -7,15 +7,16 @@ import (
"github.com/stretchr/testify/assert"
"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) {
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())
}
@ -23,10 +24,10 @@ func Test_GetID(t *testing.T) {
func Test_SetID(t *testing.T) {
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")
@ -37,7 +38,7 @@ func Test_SetID(t *testing.T) {
func Test_SaveLoad(t *testing.T) {
type DocWithObjectID struct {
Primary `bson:",inline" json:",inline" collection:"1"`
stringbased.Primary `bson:",inline" json:",inline" collection:"1"`
}
db, err := database.NewEphemeral("")
@ -47,7 +48,7 @@ func Test_SaveLoad(t *testing.T) {
defer db.Close()
doc1 := &DocWithObjectID{Primary{"foobar"}}
doc1 := &DocWithObjectID{Primary: stringbased.Primary{ID: "foobar"}}
doc2 := &DocWithObjectID{}
err = db.SaveOne(doc1)
@ -67,10 +68,10 @@ func Test_SaveLoad(t *testing.T) {
func Test_Marshal(t *testing.T) {
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)
assert.NoError(t, err)

Loading…
Cancel
Save