blob: 9f1d3bdcb01c3a7feed85a0817dc60d14837c441 [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
Jon Perrittb5d13ad2014-10-06 16:39:27 -050036func TestBuildQueryString(t *testing.T) {
37 opts := struct {
Jon Perritt255b6f82014-09-30 16:07:50 -050038 J int `q:"j"`
Jon Perrittb5d13ad2014-10-06 16:39:27 -050039 R string `q:"r,required"`
40 C bool `q:"c"`
41 }{
42 J: 2,
43 R: "red",
44 C: true,
Jon Perritt255b6f82014-09-30 16:07:50 -050045 }
Jon Perrittb5d13ad2014-10-06 16:39:27 -050046 expected := &url.URL{RawQuery: "j=2&r=red&c=true"}
Jon Perrittdb00ad12014-09-30 16:29:50 -050047 actual, err := BuildQueryString(&opts)
Jon Perritt255b6f82014-09-30 16:07:50 -050048 if err != nil {
49 t.Errorf("Error building query string: %v", err)
50 }
Jon Perrittc3e04b62014-10-06 16:17:49 -050051 th.CheckDeepEquals(t, expected, actual)
Jon Perrittdb00ad12014-09-30 16:29:50 -050052
Jon Perrittb5d13ad2014-10-06 16:39:27 -050053 opts = struct {
Jon Perrittdb00ad12014-09-30 16:29:50 -050054 J int `q:"j"`
55 R string `q:"r,required"`
Jon Perrittb5d13ad2014-10-06 16:39:27 -050056 C bool `q:"c"`
57 }{
58 J: 2,
59 C: true,
Jon Perrittdb00ad12014-09-30 16:29:50 -050060 }
Jon Perrittb5d13ad2014-10-06 16:39:27 -050061 _, err = BuildQueryString(&opts)
Jon Perrittdb00ad12014-09-30 16:29:50 -050062 if err == nil {
Jon Perrittb5d13ad2014-10-06 16:39:27 -050063 t.Errorf("Expected error: 'Required field not set'")
Jon Perrittdb00ad12014-09-30 16:29:50 -050064 }
Jon Perrittb5d13ad2014-10-06 16:39:27 -050065 th.CheckDeepEquals(t, expected, actual)
Jon Perrittdb00ad12014-09-30 16:29:50 -050066
Jon Perrittb5d13ad2014-10-06 16:39:27 -050067 _, err = BuildQueryString(map[string]interface{}{"Number": 4})
68 if err == nil {
69 t.Errorf("Expected error: 'Options type is not a struct'")
70 }
Jon Perrittdb00ad12014-09-30 16:29:50 -050071}
Jon Perrittc3e04b62014-10-06 16:17:49 -050072
73func TestBuildHeaders(t *testing.T) {
74 testStruct := struct {
75 Accept string `h:"Accept"`
Jon Perrittdfeb33b2014-10-06 16:28:58 -050076 Num int `h:"Number,required"`
Jon Perrittc3e04b62014-10-06 16:17:49 -050077 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)
Jon Perrittdfeb33b2014-10-06 16:28:58 -050087
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 }
Jon Perrittc3e04b62014-10-06 16:17:49 -050098}
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}