blob: cb3b4d253a916a46b3dd1dcb14c5c0a4f8c2ee3a [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"`
77 Num int `h:"Number"`
78 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)
88}
89
90func TestIsZero(t *testing.T) {
91 var testMap map[string]interface{}
92 testMapValue := reflect.ValueOf(testMap)
93 expected := true
94 actual := isZero(testMapValue)
95 th.CheckEquals(t, expected, actual)
96 testMap = map[string]interface{}{"empty": false}
97 testMapValue = reflect.ValueOf(testMap)
98 expected = false
99 actual = isZero(testMapValue)
100 th.CheckEquals(t, expected, actual)
101
102 var testArray [2]string
103 testArrayValue := reflect.ValueOf(testArray)
104 expected = true
105 actual = isZero(testArrayValue)
106 th.CheckEquals(t, expected, actual)
107 testArray = [2]string{"one", "two"}
108 testArrayValue = reflect.ValueOf(testArray)
109 expected = false
110 actual = isZero(testArrayValue)
111 th.CheckEquals(t, expected, actual)
112
113 var testStruct struct {
114 A string
115 B time.Time
116 }
117 testStructValue := reflect.ValueOf(testStruct)
118 expected = true
119 actual = isZero(testStructValue)
120 th.CheckEquals(t, expected, actual)
121 testStruct = struct {
122 A string
123 B time.Time
124 }{
125 B: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
126 }
127 testStructValue = reflect.ValueOf(testStruct)
128 expected = false
129 actual = isZero(testStructValue)
130 th.CheckEquals(t, expected, actual)
131
132}