-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyvalue_test.go
124 lines (105 loc) · 2.59 KB
/
keyvalue_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package keyvalue_test
import (
"fmt"
"github.com/alfarih31/nb-go-keyvalue"
"log"
"testing"
)
type Account struct {
Name string `json:"name"`
Email string `json:"email"`
PhoneNumber string `json:"phone_number"`
}
func TestFromStruct(t *testing.T) {
p1 := Account{
Name: "John Doe",
Email: "johndoe@mail.com",
PhoneNumber: "+1234567890",
}
p1kv, err := keyvalue.FromStruct(p1)
if err != nil {
log.Fatal(err)
}
fmt.Printf("(current) p1kv:\n\t%v\n", p1kv.JSON())
if p1kv["name"] != "John Doe" {
log.Fatalf(`keyvalue.FromStruct(p1), want convert p1 to map[string]interface{}`)
}
}
func TestKeyValue_AssignTo(t *testing.T) {
p1 := Account{
Name: "John Doe",
Email: "johndoe@mail.com",
PhoneNumber: "+1234567890",
}
p2 := Account{
Name: "Foo Bar",
Email: "foobar@mail.com",
PhoneNumber: "",
}
p1kv, err := keyvalue.FromStruct(p1)
if err != nil {
log.Fatal(err)
}
fmt.Printf("(current) p1kv:\n\t%v\n", p1kv.JSON())
p2kv, err := keyvalue.FromStruct(p2)
if err != nil {
log.Fatal(err)
}
fmt.Printf("(current) p2kv:\n\t%v\n", p2kv.JSON())
// Assigning p1 data to p2
p1kv.AssignTo(p2kv)
if p2kv["phone_number"] != "+1234567890" {
log.Fatalf(`p1kv.AssignTo(p2kv), want match for p2kv["phone_number""]=p1kv["phone_number"]`)
}
fmt.Printf("(now) p2kv:\n\t%v\n", p2kv.JSON())
}
func TestKeyValue_Assign(t *testing.T) {
p1 := Account{
Name: "John Doe",
Email: "johndoe@mail.com",
PhoneNumber: "+1234567890",
}
p2 := Account{
Name: "Foo Bar",
Email: "foobar@mail.com",
PhoneNumber: "",
}
p1kv, err := keyvalue.FromStruct(p1)
if err != nil {
log.Fatal(err)
}
fmt.Printf("(current) p1kv:\n\t%v\n", p1kv.JSON())
p2kv, err := keyvalue.FromStruct(p2)
if err != nil {
log.Fatal(err)
}
fmt.Printf("(current) p2kv:\n\t%v\n", p2kv.JSON())
// Assigning p2 data with p1 data
p2kv.Assign(p1kv)
if p2kv["phone_number"] != "+1234567890" {
log.Fatalf(`p2kv.Assign(p1kv), want match for p1kv["phone_number""]=p2kv["phone_number"]`)
}
fmt.Printf("(now) p2kv:\n\t%v\n", p2kv.JSON())
}
func TestKeyValue_Unmarshal(t *testing.T) {
p1 := Account{
Name: "John Doe",
Email: "johndoe@mail.com",
PhoneNumber: "+1234567890",
}
fmt.Printf("(current) p1:\n\t%v\n", p1)
p1kv, err := keyvalue.FromStruct(p1)
if err != nil {
log.Fatal(err)
}
p1kv["name"] = "Foo Bar"
// Unmarshal p1kv back to p1
err = p1kv.Unmarshal(&p1)
if err != nil {
log.Fatal(err)
}
if p1.Name != "Foo Bar" {
log.Fatalf(`p1kv.Unmarshal(), want p1kv value to p1`)
}
fmt.Printf("(now) p1:\n\t%v\n", p1)
}