github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/testing/results_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/vnpaycloud-console/gophercloud/v2"
     8  	th "github.com/vnpaycloud-console/gophercloud/v2/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 any
    99  	sejson := []byte(singleResponse)
   100  	err := json.Unmarshal(sejson, &dejson)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  
   105  	var singleResult = gophercloud.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  func TestUnmarshalNilStruct(t *testing.T) {
   117  	var x *TestPerson
   118  	var y TestPerson
   119  
   120  	err1 := gophercloud.Result{}.ExtractIntoStructPtr(&x, "")
   121  	err2 := gophercloud.Result{}.ExtractIntoStructPtr(nil, "")
   122  	err3 := gophercloud.Result{}.ExtractIntoStructPtr(y, "")
   123  	err4 := gophercloud.Result{}.ExtractIntoStructPtr(&y, "")
   124  	err5 := gophercloud.Result{}.ExtractIntoStructPtr(x, "")
   125  
   126  	th.AssertErr(t, err1)
   127  	th.AssertErr(t, err2)
   128  	th.AssertErr(t, err3)
   129  	th.AssertNoErr(t, err4)
   130  	th.AssertErr(t, err5)
   131  }
   132  
   133  func TestUnmarshalNilSlice(t *testing.T) {
   134  	var x *[]TestPerson
   135  	var y []TestPerson
   136  
   137  	err1 := gophercloud.Result{}.ExtractIntoSlicePtr(&x, "")
   138  	err2 := gophercloud.Result{}.ExtractIntoSlicePtr(nil, "")
   139  	err3 := gophercloud.Result{}.ExtractIntoSlicePtr(y, "")
   140  	err4 := gophercloud.Result{}.ExtractIntoSlicePtr(&y, "")
   141  	err5 := gophercloud.Result{}.ExtractIntoSlicePtr(x, "")
   142  
   143  	th.AssertErr(t, err1)
   144  	th.AssertErr(t, err2)
   145  	th.AssertErr(t, err3)
   146  	th.AssertNoErr(t, err4)
   147  	th.AssertErr(t, err5)
   148  }
   149  
   150  // TestUnmarshalSliceofAnonymousStructs tests if UnmarshalJSON is called on each
   151  // of the anonymous structs contained in an overarching struct slice.
   152  func TestUnmarshalSliceOfAnonymousStructs(t *testing.T) {
   153  	var actual []TestPersonWithExtensions
   154  
   155  	var dejson any
   156  	sejson := []byte(multiResponse)
   157  	err := json.Unmarshal(sejson, &dejson)
   158  	if err != nil {
   159  		t.Fatal(err)
   160  	}
   161  
   162  	var multiResult = gophercloud.Result{
   163  		Body: dejson,
   164  	}
   165  
   166  	err = multiResult.ExtractIntoSlicePtr(&actual, "people")
   167  	th.AssertNoErr(t, err)
   168  
   169  	th.AssertEquals(t, "Bill unmarshalled", actual[0].Name)
   170  	th.AssertEquals(t, "Canada unmarshalled", actual[0].Location)
   171  	th.AssertEquals(t, "Ted unmarshalled", actual[1].Name)
   172  	th.AssertEquals(t, "Mexico unmarshalled", actual[1].Location)
   173  }
   174  
   175  // TestUnmarshalSliceOfStruct tests if extracting results from a "normal"
   176  // struct still works correctly.
   177  func TestUnmarshalSliceofStruct(t *testing.T) {
   178  	var actual []TestPerson
   179  
   180  	var dejson any
   181  	sejson := []byte(multiResponse)
   182  	err := json.Unmarshal(sejson, &dejson)
   183  	if err != nil {
   184  		t.Fatal(err)
   185  	}
   186  
   187  	var multiResult = gophercloud.Result{
   188  		Body: dejson,
   189  	}
   190  
   191  	err = multiResult.ExtractIntoSlicePtr(&actual, "people")
   192  	th.AssertNoErr(t, err)
   193  
   194  	th.AssertEquals(t, "Bill unmarshalled", actual[0].Name)
   195  	th.AssertEquals(t, "Ted unmarshalled", actual[1].Name)
   196  }
   197  
   198  // TestUnmarshalNamedStruct tests if the result is empty.
   199  func TestUnmarshalNamedStructs(t *testing.T) {
   200  	var actual TestPersonWithExtensionsNamed
   201  
   202  	var dejson any
   203  	sejson := []byte(singleResponse)
   204  	err := json.Unmarshal(sejson, &dejson)
   205  	if err != nil {
   206  		t.Fatal(err)
   207  	}
   208  
   209  	var singleResult = gophercloud.Result{
   210  		Body: dejson,
   211  	}
   212  
   213  	err = singleResult.ExtractIntoStructPtr(&actual, "person")
   214  	th.AssertNoErr(t, err)
   215  
   216  	th.AssertEquals(t, "", actual.TestPerson.Name)
   217  	th.AssertEquals(t, "", actual.TestPersonExt.Location)
   218  }
   219  
   220  // TestUnmarshalSliceofNamedStructs tests if the result is empty.
   221  func TestUnmarshalSliceOfNamedStructs(t *testing.T) {
   222  	var actual []TestPersonWithExtensionsNamed
   223  
   224  	var dejson any
   225  	sejson := []byte(multiResponse)
   226  	err := json.Unmarshal(sejson, &dejson)
   227  	if err != nil {
   228  		t.Fatal(err)
   229  	}
   230  
   231  	var multiResult = gophercloud.Result{
   232  		Body: dejson,
   233  	}
   234  
   235  	err = multiResult.ExtractIntoSlicePtr(&actual, "people")
   236  	th.AssertNoErr(t, err)
   237  
   238  	th.AssertEquals(t, "", actual[0].TestPerson.Name)
   239  	th.AssertEquals(t, "", actual[0].TestPersonExt.Location)
   240  	th.AssertEquals(t, "", actual[1].TestPerson.Name)
   241  	th.AssertEquals(t, "", actual[1].TestPersonExt.Location)
   242  }