blob: c158f539d6e93a6859b8a37b6f9b01f3569f4f6b [file] [log] [blame]
Jon Perritt255b6f82014-09-30 16:07:50 -05001package gophercloud
2
3import (
4 "net/url"
Jon Perrittc3e04b62014-10-06 16:17:49 -05005 "reflect"
Jon Perritt255b6f82014-09-30 16:07:50 -05006 "testing"
Jon Perrittc3e04b62014-10-06 16:17:49 -05007 "time"
Jon Perritt255b6f82014-09-30 16:07:50 -05008
9 th "github.com/rackspace/gophercloud/testhelper"
10)
11
Jon Perritt1e17aec2014-10-06 14:33:34 -050012func TestMaybeString(t *testing.T) {
13 testString := ""
14 var expected *string
15 actual := MaybeString(testString)
Jon Perrittc3e04b62014-10-06 16:17:49 -050016 th.CheckDeepEquals(t, expected, actual)
Jon Perritt1e17aec2014-10-06 14:33:34 -050017
18 testString = "carol"
19 expected = &testString
20 actual = MaybeString(testString)
Jon Perrittc3e04b62014-10-06 16:17:49 -050021 th.CheckDeepEquals(t, expected, actual)
Jon Perritt255b6f82014-09-30 16:07:50 -050022}
23
Jon Perritt1e17aec2014-10-06 14:33:34 -050024func TestMaybeInt(t *testing.T) {
25 testInt := 0
26 var expected *int
27 actual := MaybeInt(testInt)
Jon Perrittc3e04b62014-10-06 16:17:49 -050028 th.CheckDeepEquals(t, expected, actual)
Jon Perritt1e17aec2014-10-06 14:33:34 -050029
30 testInt = 4
31 expected = &testInt
32 actual = MaybeInt(testInt)
Jon Perrittc3e04b62014-10-06 16:17:49 -050033 th.CheckDeepEquals(t, expected, actual)
Jon Perritt255b6f82014-09-30 16:07:50 -050034}
35
36func TestBuildQueryStringWithPointerToStruct(t *testing.T) {
37 expected := &url.URL{
38 RawQuery: "j=2&r=red",
39 }
40
41 type Opts struct {
42 J int `q:"j"`
43 R string `q:"r"`
44 C bool
45 }
46
47 opts := Opts{J: 2, R: "red"}
48
Jon Perrittdb00ad12014-09-30 16:29:50 -050049 actual, err := BuildQueryString(&opts)
Jon Perritt255b6f82014-09-30 16:07:50 -050050 if err != nil {
51 t.Errorf("Error building query string: %v", err)
52 }
53
Jon Perrittc3e04b62014-10-06 16:17:49 -050054 th.CheckDeepEquals(t, expected, actual)
Jon Perritt255b6f82014-09-30 16:07:50 -050055}
Jon Perrittdb00ad12014-09-30 16:29:50 -050056
57func TestBuildQueryStringWithoutRequiredFieldSet(t *testing.T) {
58 type Opts struct {
59 J int `q:"j"`
60 R string `q:"r,required"`
61 C bool
62 }
63
64 opts := Opts{J: 2, C: true}
65
66 _, err := BuildQueryString(&opts)
67 if err == nil {
68 t.Error("Unexpected result: There should be an error thrown when a required field isn't set.")
69 }
70
71 t.Log(err)
72}
Jon Perrittc3e04b62014-10-06 16:17:49 -050073
74func TestBuildHeaders(t *testing.T) {
75 testStruct := struct {
76 Accept string `h:"Accept"`
Jon Perrittdfeb33b2014-10-06 16:28:58 -050077 Num int `h:"Number,required"`
Jon Perrittc3e04b62014-10-06 16:17:49 -050078 Style bool `h:"Style"`
79 }{
80 Accept: "application/json",
81 Num: 4,
82 Style: true,
83 }
84 expected := map[string]string{"Accept": "application/json", "Number": "4", "Style": "true"}
85 actual, err := BuildHeaders(&testStruct)
86 th.CheckNoErr(t, err)
87 th.CheckDeepEquals(t, expected, actual)
Jon Perrittdfeb33b2014-10-06 16:28:58 -050088
89 testStruct.Num = 0
90 _, err = BuildHeaders(&testStruct)
91 if err == nil {
92 t.Errorf("Expected error: 'Required header not set'")
93 }
94
95 _, err = BuildHeaders(map[string]interface{}{"Number": 4})
96 if err == nil {
97 t.Errorf("Expected error: 'Options type is not a struct'")
98 }
Jon Perrittc3e04b62014-10-06 16:17:49 -050099}
100
101func TestIsZero(t *testing.T) {
102 var testMap map[string]interface{}
103 testMapValue := reflect.ValueOf(testMap)
104 expected := true
105 actual := isZero(testMapValue)
106 th.CheckEquals(t, expected, actual)
107 testMap = map[string]interface{}{"empty": false}
108 testMapValue = reflect.ValueOf(testMap)
109 expected = false
110 actual = isZero(testMapValue)
111 th.CheckEquals(t, expected, actual)
112
113 var testArray [2]string
114 testArrayValue := reflect.ValueOf(testArray)
115 expected = true
116 actual = isZero(testArrayValue)
117 th.CheckEquals(t, expected, actual)
118 testArray = [2]string{"one", "two"}
119 testArrayValue = reflect.ValueOf(testArray)
120 expected = false
121 actual = isZero(testArrayValue)
122 th.CheckEquals(t, expected, actual)
123
124 var testStruct struct {
125 A string
126 B time.Time
127 }
128 testStructValue := reflect.ValueOf(testStruct)
129 expected = true
130 actual = isZero(testStructValue)
131 th.CheckEquals(t, expected, actual)
132 testStruct = struct {
133 A string
134 B time.Time
135 }{
136 B: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
137 }
138 testStructValue = reflect.ValueOf(testStruct)
139 expected = false
140 actual = isZero(testStructValue)
141 th.CheckEquals(t, expected, actual)
142
143}