Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 5 | "reflect" |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 6 | "testing" |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 7 | "time" |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 8 | |
| 9 | th "github.com/rackspace/gophercloud/testhelper" |
| 10 | ) |
| 11 | |
Jon Perritt | 1e17aec | 2014-10-06 14:33:34 -0500 | [diff] [blame] | 12 | func TestMaybeString(t *testing.T) { |
| 13 | testString := "" |
| 14 | var expected *string |
| 15 | actual := MaybeString(testString) |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 16 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | 1e17aec | 2014-10-06 14:33:34 -0500 | [diff] [blame] | 17 | |
| 18 | testString = "carol" |
| 19 | expected = &testString |
| 20 | actual = MaybeString(testString) |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 21 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 22 | } |
| 23 | |
Jon Perritt | 1e17aec | 2014-10-06 14:33:34 -0500 | [diff] [blame] | 24 | func TestMaybeInt(t *testing.T) { |
| 25 | testInt := 0 |
| 26 | var expected *int |
| 27 | actual := MaybeInt(testInt) |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 28 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | 1e17aec | 2014-10-06 14:33:34 -0500 | [diff] [blame] | 29 | |
| 30 | testInt = 4 |
| 31 | expected = &testInt |
| 32 | actual = MaybeInt(testInt) |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 33 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | func 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 Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 49 | actual, err := BuildQueryString(&opts) |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 50 | if err != nil { |
| 51 | t.Errorf("Error building query string: %v", err) |
| 52 | } |
| 53 | |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 54 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 55 | } |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 56 | |
| 57 | func 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 Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 73 | |
| 74 | func TestBuildHeaders(t *testing.T) { |
| 75 | testStruct := struct { |
| 76 | Accept string `h:"Accept"` |
Jon Perritt | dfeb33b | 2014-10-06 16:28:58 -0500 | [diff] [blame^] | 77 | Num int `h:"Number,required"` |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 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) |
Jon Perritt | dfeb33b | 2014-10-06 16:28:58 -0500 | [diff] [blame^] | 88 | |
| 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 Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | func 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 | } |