github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/testing/results_test.go (about) 1 package testing 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 golangsdk "github.com/opentelekomcloud/gophertelekomcloud" 8 th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" 9 ) 10 11 var singleResponse = ` 12 { 13 "person": { 14 "name": "Bill", 15 "email": "bill@example.com", 16 "location": "Canada" 17 } 18 } 19 ` 20 21 var multiResponse = ` 22 { 23 "people": [ 24 { 25 "name": "Bill", 26 "email": "bill@example.com", 27 "location": "Canada" 28 }, 29 { 30 "name": "Ted", 31 "email": "ted@example.com", 32 "location": "Mexico" 33 } 34 ] 35 } 36 ` 37 38 type TestPerson struct { 39 Name string `json:"-"` 40 Email string `json:"email"` 41 } 42 43 func (r *TestPerson) UnmarshalJSON(b []byte) error { 44 type tmp TestPerson 45 var s struct { 46 tmp 47 Name string `json:"name"` 48 } 49 50 err := json.Unmarshal(b, &s) 51 if err != nil { 52 return err 53 } 54 55 *r = TestPerson(s.tmp) 56 r.Name = s.Name + " unmarshalled" 57 58 return nil 59 } 60 61 type TestPersonExt struct { 62 Location string `json:"-"` 63 } 64 65 func (r *TestPersonExt) UnmarshalJSON(b []byte) error { 66 type tmp TestPersonExt 67 var s struct { 68 tmp 69 Location string `json:"location"` 70 } 71 72 err := json.Unmarshal(b, &s) 73 if err != nil { 74 return err 75 } 76 77 *r = TestPersonExt(s.tmp) 78 r.Location = s.Location + " unmarshalled" 79 80 return nil 81 } 82 83 type TestPersonWithExtensions struct { 84 TestPerson 85 TestPersonExt 86 } 87 88 type TestPersonWithExtensionsNamed struct { 89 TestPerson TestPerson 90 TestPersonExt TestPersonExt 91 } 92 93 // TestUnmarshalAnonymousStruct tests if UnmarshalJSON is called on each 94 // of the anonymous structs contained in an overarching struct. 95 func TestUnmarshalAnonymousStructs(t *testing.T) { 96 var actual TestPersonWithExtensions 97 98 sejson := []byte(singleResponse) 99 var singleResult = golangsdk.Result{ 100 Body: sejson, 101 } 102 103 err := singleResult.ExtractIntoStructPtr(&actual, "person") 104 th.AssertNoErr(t, err) 105 106 th.AssertEquals(t, "Bill unmarshalled", actual.Name) 107 th.AssertEquals(t, "Canada unmarshalled", actual.Location) 108 } 109 110 // TestUnmarshalSliceofAnonymousStructs tests if UnmarshalJSON is called on each 111 // of the anonymous structs contained in an overarching struct slice. 112 func TestUnmarshalSliceOfAnonymousStructs(t *testing.T) { 113 var actual []TestPersonWithExtensions 114 115 sejson := []byte(multiResponse) 116 117 var multiResult = golangsdk.Result{ 118 Body: sejson, 119 } 120 121 err := multiResult.ExtractIntoSlicePtr(&actual, "people") 122 th.AssertNoErr(t, err) 123 124 th.AssertEquals(t, "Bill unmarshalled", actual[0].Name) 125 th.AssertEquals(t, "Canada unmarshalled", actual[0].Location) 126 th.AssertEquals(t, "Ted unmarshalled", actual[1].Name) 127 th.AssertEquals(t, "Mexico unmarshalled", actual[1].Location) 128 } 129 130 // TestUnmarshalSliceOfStruct tests if extracting results from a "normal" 131 // struct still works correctly. 132 func TestUnmarshalSliceofStruct(t *testing.T) { 133 var actual []TestPerson 134 135 sejson := []byte(multiResponse) 136 var multiResult = golangsdk.Result{ 137 Body: sejson, 138 } 139 140 err := multiResult.ExtractIntoSlicePtr(&actual, "people") 141 th.AssertNoErr(t, err) 142 143 th.AssertEquals(t, "Bill unmarshalled", actual[0].Name) 144 th.AssertEquals(t, "Ted unmarshalled", actual[1].Name) 145 } 146 147 // TestUnmarshalNamedStruct tests if the result is empty. 148 func TestUnmarshalNamedStructs(t *testing.T) { 149 var actual TestPersonWithExtensionsNamed 150 151 sejson := []byte(singleResponse) 152 153 var singleResult = golangsdk.Result{ 154 Body: sejson, 155 } 156 157 err := singleResult.ExtractIntoStructPtr(&actual, "person") 158 th.AssertNoErr(t, err) 159 160 th.AssertEquals(t, "", actual.TestPerson.Name) 161 th.AssertEquals(t, "", actual.TestPersonExt.Location) 162 } 163 164 // TestUnmarshalSliceofNamedStructs tests if the result is empty. 165 func TestUnmarshalSliceOfNamedStructs(t *testing.T) { 166 var actual []TestPersonWithExtensionsNamed 167 168 sejson := []byte(multiResponse) 169 170 var multiResult = golangsdk.Result{ 171 Body: sejson, 172 } 173 174 err := multiResult.ExtractIntoSlicePtr(&actual, "people") 175 th.AssertNoErr(t, err) 176 177 th.AssertEquals(t, "", actual[0].TestPerson.Name) 178 th.AssertEquals(t, "", actual[0].TestPersonExt.Location) 179 th.AssertEquals(t, "", actual[1].TestPerson.Name) 180 th.AssertEquals(t, "", actual[1].TestPersonExt.Location) 181 }