blob: 03eaefcdd15938a44054ccc0978cc87e8607aa40 [file] [log] [blame]
Jon Perritt255b6f82014-09-30 16:07:50 -05001package gophercloud
2
3import (
4 "net/url"
5 "testing"
6
7 th "github.com/rackspace/gophercloud/testhelper"
8)
9
10func TestMaybeStringWithNonEmptyString(t *testing.T) {
11 testString := "carol"
12 expected := &testString
13 actual := MaybeString("carol")
14 th.CheckDeepEquals(t, actual, expected)
15}
16
17func TestMaybeStringWithEmptyString(t *testing.T) {
18 var expected *string
19 actual := MaybeString("")
20 th.CheckDeepEquals(t, actual, expected)
21}
22
23func 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 Perrittdb00ad12014-09-30 16:29:50 -050036 actual, err := BuildQueryString(&opts)
Jon Perritt255b6f82014-09-30 16:07:50 -050037 if err != nil {
38 t.Errorf("Error building query string: %v", err)
39 }
40
41 th.CheckDeepEquals(t, actual, expected)
42}
Jon Perrittdb00ad12014-09-30 16:29:50 -050043
44func 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}