This repository has been archived by the owner on Jan 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdocument_test.go
90 lines (85 loc) · 1.77 KB
/
document_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package orient_test
import (
"gopkg.in/istreamdata/orientgo.v2"
"reflect"
"testing"
)
func TestDocumentFromStruct(t *testing.T) {
doc := orient.NewEmptyDocument()
type item struct {
Ind int
Name string
}
a := item{11, "named"}
if err := doc.From(a); err != nil {
t.Fatal(err)
}
var b item
if err := doc.ToStruct(&b); err != nil {
t.Fatal(err)
} else if b != a {
t.Fatal("data differs")
}
}
func TestDocumentFromStructEmbedded(t *testing.T) {
doc := orient.NewEmptyDocument()
type Item struct {
Ind int
Name string
}
type obj struct {
Item Item
Data string
}
a := obj{Item: Item{11, "named"}, Data: "dataz"}
if err := doc.From(a); err != nil {
t.Fatal(err)
} else if doc.GetField("Item").Type != orient.EMBEDDED {
t.Fatal("wrong field type")
}
var b obj
if err := doc.ToStruct(&b); err != nil {
t.Fatal(err)
} else if b != a {
t.Fatal("data differs")
}
}
func TestDocumentFromStructEmbeddedAnon(t *testing.T) {
doc := orient.NewEmptyDocument()
type Item struct {
Ind int
Name string
}
type obj struct {
Item `mapstructure:",squash"`
Data string
}
a := obj{Item: Item{11, "named"}, Data: "dataz"}
if err := doc.From(a); err != nil {
t.Fatal(err)
} else if doc.GetField("Item") != nil {
t.Fatal("default behavior should be squash")
}
var b obj
if err := doc.ToStruct(&b); err != nil {
t.Fatal(err)
} else if b != a {
t.Fatalf("data differs: %+v vs %+v", a, b)
}
}
func TestDocumentFromMap(t *testing.T) {
doc := orient.NewEmptyDocument()
type item struct {
Ind int
Name string
}
a := map[string]interface{}{"Ind": 11, "Name": "named"}
if err := doc.From(a); err != nil {
t.Fatal(err)
}
if b, err := doc.ToMap(); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(a, b) {
t.Fatal("data differs")
}
}