-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructor_test.go
More file actions
92 lines (78 loc) · 2.57 KB
/
constructor_test.go
File metadata and controls
92 lines (78 loc) · 2.57 KB
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
package constructor_test
import (
"reflect"
"sort"
"strings"
"testing"
"github.com/shved/constructor"
)
type ComplicatedObjectToFilter struct {
Name string
Measure float64 `json:"measurement"`
Exported string `asdf:"asdf"`
unexported string
Field1 int
Field2 ObjectField `constructor:"omit"`
Field3 ObjectField `json:"field_3" constructor:"omit"`
Field4 ObjectField
Field5 []ObjectField
}
type ObjectField struct {
Prop1 string `json:""`
Prop2 string `json:"specialName"`
Prop3 []string
}
// If Options not set explicitly, it will be replaced with defaults.
func TestQueryStringFromStruct_EmptyOptions_ReplacedWithDefault(t *testing.T) {
builder := constructor.NewBuilder(constructor.Options{})
got := builder.QueryStringFromStruct(ComplicatedObjectToFilter{})
paramKey := "filter="
expected := "filter=Field5*specialName,Field5*Prop3,Name,measurement,Exported,Field1,Field4*specialName,Field4*Prop3"
expStart, gotStart := expected[:len(paramKey)-1], got[:len(paramKey)-1]
if expStart != gotStart {
t.Fatalf("expected: %s\n got: %s", expStart, gotStart)
}
expSlice := strings.Split(expected[len(paramKey):], ",")
gotSlice := strings.Split(got[len(paramKey):], ",")
sort.Strings(expSlice)
sort.Strings(gotSlice)
if !reflect.DeepEqual(gotSlice, expSlice) {
t.Fatalf("expected: %s\n got: %s", expSlice, gotSlice)
}
}
func TestQueryStringFromStruct_ExplicitOptions(t *testing.T) {
paramKey := "halleluiah="
builder := constructor.NewBuilder(constructor.Options{
ParamKey: "halleluiah",
Delimiter: ";",
FieldDelimiter: "$",
})
got := builder.QueryStringFromStruct(ComplicatedObjectToFilter{})
expected := "halleluiah=Field5$specialName;Field5$Prop3;Name;measurement;Exported;Field1;Field4$specialName;Field4$Prop3"
expStart, gotStart := expected[:len(paramKey)-1], got[:len(paramKey)-1]
if expStart != gotStart {
t.Fatalf("expected: %s\n got: %s", expStart, gotStart)
}
expSlice := strings.Split(expected[len(paramKey):], ";")
gotSlice := strings.Split(got[len(paramKey):], ";")
sort.Strings(expSlice)
sort.Strings(gotSlice)
if !reflect.DeepEqual(gotSlice, expSlice) {
t.Fatalf("expected: %s\n got: %s", expSlice, gotSlice)
}
}
func TestQueryStringFromStruct_StructWithNoFields(t *testing.T) {
type A struct {
SomeField []string
}
type EmptyResponse struct {
SomeFieldToo []A `json:""`
unexported string
}
expected := ""
builder := constructor.NewBuilder(constructor.Options{})
got := builder.QueryStringFromStruct(EmptyResponse{})
if expected != got {
t.Fatalf("expected: %s\n got: %s", expected, got)
}
}