blob: 9f1d3bdcb01c3a7feed85a0817dc60d14837c441 [file] [log] [blame]
Jamie Hannaford8c072a32014-10-16 14:33:32 +02001package gophercloud
2
3import (
4 "net/url"
5 "reflect"
6 "testing"
7 "time"
8
9 th "github.com/rackspace/gophercloud/testhelper"
10)
11
12func TestMaybeString(t *testing.T) {
13 testString := ""
14 var expected *string
15 actual := MaybeString(testString)
16 th.CheckDeepEquals(t, expected, actual)
17
18 testString = "carol"
19 expected = &testString
20 actual = MaybeString(testString)
21 th.CheckDeepEquals(t, expected, actual)
22}
23
24func TestMaybeInt(t *testing.T) {
25 testInt := 0
26 var expected *int
27 actual := MaybeInt(testInt)
28 th.CheckDeepEquals(t, expected, actual)
29
30 testInt = 4
31 expected = &testInt
32 actual = MaybeInt(testInt)
33 th.CheckDeepEquals(t, expected, actual)
34}
35
36func TestBuildQueryString(t *testing.T) {
37 opts := struct {
38 J int `q:"j"`
39 R string `q:"r,required"`
40 C bool `q:"c"`
41 }{
42 J: 2,
43 R: "red",
44 C: true,
45 }
46 expected := &url.URL{RawQuery: "j=2&r=red&c=true"}
47 actual, err := BuildQueryString(&opts)
48 if err != nil {
49 t.Errorf("Error building query string: %v", err)
50 }
51 th.CheckDeepEquals(t, expected, actual)
52
53 opts = struct {
54 J int `q:"j"`
55 R string `q:"r,required"`
56 C bool `q:"c"`
57 }{
58 J: 2,
59 C: true,
60 }
61 _, err = BuildQueryString(&opts)
62 if err == nil {
63 t.Errorf("Expected error: 'Required field not set'")
64 }
65 th.CheckDeepEquals(t, expected, actual)
66
67 _, err = BuildQueryString(map[string]interface{}{"Number": 4})
68 if err == nil {
69 t.Errorf("Expected error: 'Options type is not a struct'")
70 }
71}
72
73func TestBuildHeaders(t *testing.T) {
74 testStruct := struct {
75 Accept string `h:"Accept"`
76 Num int `h:"Number,required"`
77 Style bool `h:"Style"`
78 }{
79 Accept: "application/json",
80 Num: 4,
81 Style: true,
82 }
83 expected := map[string]string{"Accept": "application/json", "Number": "4", "Style": "true"}
84 actual, err := BuildHeaders(&testStruct)
85 th.CheckNoErr(t, err)
86 th.CheckDeepEquals(t, expected, actual)
87
88 testStruct.Num = 0
89 _, err = BuildHeaders(&testStruct)
90 if err == nil {
91 t.Errorf("Expected error: 'Required header not set'")
92 }
93
94 _, err = BuildHeaders(map[string]interface{}{"Number": 4})
95 if err == nil {
96 t.Errorf("Expected error: 'Options type is not a struct'")
97 }
98}
99
100func TestIsZero(t *testing.T) {
101 var testMap map[string]interface{}
102 testMapValue := reflect.ValueOf(testMap)
103 expected := true
104 actual := isZero(testMapValue)
105 th.CheckEquals(t, expected, actual)
106 testMap = map[string]interface{}{"empty": false}
107 testMapValue = reflect.ValueOf(testMap)
108 expected = false
109 actual = isZero(testMapValue)
110 th.CheckEquals(t, expected, actual)
111
112 var testArray [2]string
113 testArrayValue := reflect.ValueOf(testArray)
114 expected = true
115 actual = isZero(testArrayValue)
116 th.CheckEquals(t, expected, actual)
117 testArray = [2]string{"one", "two"}
118 testArrayValue = reflect.ValueOf(testArray)
119 expected = false
120 actual = isZero(testArrayValue)
121 th.CheckEquals(t, expected, actual)
122
123 var testStruct struct {
124 A string
125 B time.Time
126 }
127 testStructValue := reflect.ValueOf(testStruct)
128 expected = true
129 actual = isZero(testStructValue)
130 th.CheckEquals(t, expected, actual)
131 testStruct = struct {
132 A string
133 B time.Time
134 }{
135 B: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
136 }
137 testStructValue = reflect.ValueOf(testStruct)
138 expected = false
139 actual = isZero(testStructValue)
140 th.CheckEquals(t, expected, actual)
141
142}