github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/testing/results_test.go (about) 1 package testing 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/huaweicloud/golangsdk" 8 th "github.com/huaweicloud/golangsdk/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 var dejson interface{} 99 sejson := []byte(singleResponse) 100 err := json.Unmarshal(sejson, &dejson) 101 if err != nil { 102 t.Fatal(err) 103 } 104 105 var singleResult = golangsdk.Result{ 106 Body: dejson, 107 } 108 109 err = singleResult.ExtractIntoStructPtr(&actual, "person") 110 th.AssertNoErr(t, err) 111 112 th.AssertEquals(t, "Bill unmarshalled", actual.Name) 113 th.AssertEquals(t, "Canada unmarshalled", actual.Location) 114 } 115 116 // TestUnmarshalSliceofAnonymousStructs tests if UnmarshalJSON is called on each 117 // of the anonymous structs contained in an overarching struct slice. 118 func TestUnmarshalSliceOfAnonymousStructs(t *testing.T) { 119 var actual []TestPersonWithExtensions 120 121 var dejson interface{} 122 sejson := []byte(multiResponse) 123 err := json.Unmarshal(sejson, &dejson) 124 if err != nil { 125 t.Fatal(err) 126 } 127 128 var multiResult = golangsdk.Result{ 129 Body: dejson, 130 } 131 132 err = multiResult.ExtractIntoSlicePtr(&actual, "people") 133 th.AssertNoErr(t, err) 134 135 th.AssertEquals(t, "Bill unmarshalled", actual[0].Name) 136 th.AssertEquals(t, "Canada unmarshalled", actual[0].Location) 137 th.AssertEquals(t, "Ted unmarshalled", actual[1].Name) 138 th.AssertEquals(t, "Mexico unmarshalled", actual[1].Location) 139 } 140 141 // TestUnmarshalSliceOfStruct tests if extracting results from a "normal" 142 // struct still works correctly. 143 func TestUnmarshalSliceofStruct(t *testing.T) { 144 var actual []TestPerson 145 146 var dejson interface{} 147 sejson := []byte(multiResponse) 148 err := json.Unmarshal(sejson, &dejson) 149 if err != nil { 150 t.Fatal(err) 151 } 152 153 var multiResult = golangsdk.Result{ 154 Body: dejson, 155 } 156 157 err = multiResult.ExtractIntoSlicePtr(&actual, "people") 158 th.AssertNoErr(t, err) 159 160 th.AssertEquals(t, "Bill unmarshalled", actual[0].Name) 161 th.AssertEquals(t, "Ted unmarshalled", actual[1].Name) 162 } 163 164 // TestUnmarshalNamedStruct tests if the result is empty. 165 func TestUnmarshalNamedStructs(t *testing.T) { 166 var actual TestPersonWithExtensionsNamed 167 168 var dejson interface{} 169 sejson := []byte(singleResponse) 170 err := json.Unmarshal(sejson, &dejson) 171 if err != nil { 172 t.Fatal(err) 173 } 174 175 var singleResult = golangsdk.Result{ 176 Body: dejson, 177 } 178 179 err = singleResult.ExtractIntoStructPtr(&actual, "person") 180 th.AssertNoErr(t, err) 181 182 th.AssertEquals(t, "", actual.TestPerson.Name) 183 th.AssertEquals(t, "", actual.TestPersonExt.Location) 184 } 185 186 // TestUnmarshalSliceofNamedStructs tests if the result is empty. 187 func TestUnmarshalSliceOfNamedStructs(t *testing.T) { 188 var actual []TestPersonWithExtensionsNamed 189 190 var dejson interface{} 191 sejson := []byte(multiResponse) 192 err := json.Unmarshal(sejson, &dejson) 193 if err != nil { 194 t.Fatal(err) 195 } 196 197 var multiResult = golangsdk.Result{ 198 Body: dejson, 199 } 200 201 err = multiResult.ExtractIntoSlicePtr(&actual, "people") 202 th.AssertNoErr(t, err) 203 204 th.AssertEquals(t, "", actual[0].TestPerson.Name) 205 th.AssertEquals(t, "", actual[0].TestPersonExt.Location) 206 th.AssertEquals(t, "", actual[1].TestPerson.Name) 207 th.AssertEquals(t, "", actual[1].TestPersonExt.Location) 208 }