Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "testing" |
| 6 | |
| 7 | th "github.com/rackspace/gophercloud/testhelper" |
| 8 | ) |
| 9 | |
| 10 | func TestMaybeStringWithNonEmptyString(t *testing.T) { |
| 11 | testString := "carol" |
| 12 | expected := &testString |
| 13 | actual := MaybeString("carol") |
| 14 | th.CheckDeepEquals(t, actual, expected) |
| 15 | } |
| 16 | |
| 17 | func TestMaybeStringWithEmptyString(t *testing.T) { |
| 18 | var expected *string |
| 19 | actual := MaybeString("") |
| 20 | th.CheckDeepEquals(t, actual, expected) |
| 21 | } |
| 22 | |
| 23 | func TestBuildQueryStringWithPointerToStruct(t *testing.T) { |
| 24 | expected := &url.URL{ |
| 25 | RawQuery: "j=2&r=red", |
| 26 | } |
| 27 | |
| 28 | type Opts struct { |
| 29 | J int `q:"j"` |
| 30 | R string `q:"r"` |
| 31 | C bool |
| 32 | } |
| 33 | |
| 34 | opts := Opts{J: 2, R: "red"} |
| 35 | |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame^] | 36 | actual, err := BuildQueryString(&opts) |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 37 | if err != nil { |
| 38 | t.Errorf("Error building query string: %v", err) |
| 39 | } |
| 40 | |
| 41 | th.CheckDeepEquals(t, actual, expected) |
| 42 | } |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame^] | 43 | |
| 44 | func TestBuildQueryStringWithoutRequiredFieldSet(t *testing.T) { |
| 45 | type Opts struct { |
| 46 | J int `q:"j"` |
| 47 | R string `q:"r,required"` |
| 48 | C bool |
| 49 | } |
| 50 | |
| 51 | opts := Opts{J: 2, C: true} |
| 52 | |
| 53 | _, err := BuildQueryString(&opts) |
| 54 | if err == nil { |
| 55 | t.Error("Unexpected result: There should be an error thrown when a required field isn't set.") |
| 56 | } |
| 57 | |
| 58 | t.Log(err) |
| 59 | } |