91 lines
1.9 KiB
Go
91 lines
1.9 KiB
Go
package mongoext
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.blackforestbytes.com/BlackForestBytes/goext/tst"
|
|
)
|
|
|
|
func TestProjectionFromStructSimple(t *testing.T) {
|
|
type model struct {
|
|
ID string `bson:"_id"`
|
|
Name string `bson:"name"`
|
|
Age int `bson:"age"`
|
|
}
|
|
|
|
res := ProjectionFromStruct(model{})
|
|
|
|
tst.AssertEqual(t, len(res), 3)
|
|
tst.AssertEqual(t, res["_id"], 1)
|
|
tst.AssertEqual(t, res["name"], 1)
|
|
tst.AssertEqual(t, res["age"], 1)
|
|
}
|
|
|
|
func TestProjectionFromStructIgnoresUntagged(t *testing.T) {
|
|
type model struct {
|
|
Tagged string `bson:"tagged"`
|
|
Untagged string
|
|
Other int `json:"other"`
|
|
}
|
|
|
|
res := ProjectionFromStruct(model{})
|
|
|
|
tst.AssertEqual(t, len(res), 1)
|
|
tst.AssertEqual(t, res["tagged"], 1)
|
|
|
|
if _, ok := res["Untagged"]; ok {
|
|
t.Errorf("untagged field should not be in projection")
|
|
}
|
|
if _, ok := res["Other"]; ok {
|
|
t.Errorf("non-bson-tagged field should not be in projection")
|
|
}
|
|
}
|
|
|
|
func TestProjectionFromStructWithOptions(t *testing.T) {
|
|
type model struct {
|
|
ID string `bson:"_id,omitempty"`
|
|
Name string `bson:"name,omitempty"`
|
|
Slug string `bson:"slug,inline"`
|
|
}
|
|
|
|
res := ProjectionFromStruct(model{})
|
|
|
|
tst.AssertEqual(t, len(res), 3)
|
|
tst.AssertEqual(t, res["_id"], 1)
|
|
tst.AssertEqual(t, res["name"], 1)
|
|
tst.AssertEqual(t, res["slug"], 1)
|
|
}
|
|
|
|
func TestProjectionFromStructEmpty(t *testing.T) {
|
|
type empty struct{}
|
|
|
|
res := ProjectionFromStruct(empty{})
|
|
|
|
tst.AssertEqual(t, len(res), 0)
|
|
}
|
|
|
|
func TestProjectionFromStructPointerValues(t *testing.T) {
|
|
type model struct {
|
|
Name *string `bson:"name"`
|
|
Tags []int `bson:"tags"`
|
|
}
|
|
|
|
res := ProjectionFromStruct(model{})
|
|
|
|
tst.AssertEqual(t, len(res), 2)
|
|
tst.AssertEqual(t, res["name"], 1)
|
|
tst.AssertEqual(t, res["tags"], 1)
|
|
}
|
|
|
|
func TestProjectionFromStructAllSkipped(t *testing.T) {
|
|
type model struct {
|
|
A string
|
|
B int
|
|
C bool
|
|
}
|
|
|
|
res := ProjectionFromStruct(model{})
|
|
|
|
tst.AssertEqual(t, len(res), 0)
|
|
}
|