github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/docs/contributor-tutorial/.template/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/service/vN/resources"
     8  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     9  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    10  	"github.com/vnpaycloud-console/gophercloud/v2/testhelper/client"
    11  )
    12  
    13  func TestListResources(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  	HandleListResourcesSuccessfully(t)
    17  
    18  	count := 0
    19  	err := resources.List(client.ServiceClient(), nil).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
    20  		count++
    21  
    22  		actual, err := resources.ExtractResources(page)
    23  		th.AssertNoErr(t, err)
    24  
    25  		th.AssertDeepEquals(t, ExpectedResourcesSlice, actual)
    26  
    27  		return true, nil
    28  	})
    29  	th.AssertNoErr(t, err)
    30  	th.AssertEquals(t, count, 1)
    31  }
    32  
    33  func TestListResourcesAllPages(t *testing.T) {
    34  	th.SetupHTTP()
    35  	defer th.TeardownHTTP()
    36  	HandleListResourcesSuccessfully(t)
    37  
    38  	allPages, err := resources.List(client.ServiceClient(), nil).AllPages(context.TODO())
    39  	th.AssertNoErr(t, err)
    40  	actual, err := resources.ExtractResources(allPages)
    41  	th.AssertNoErr(t, err)
    42  	th.AssertDeepEquals(t, ExpectedResourcesSlice, actual)
    43  }
    44  
    45  func TestGetResource(t *testing.T) {
    46  	th.SetupHTTP()
    47  	defer th.TeardownHTTP()
    48  	HandleGetResourceSuccessfully(t)
    49  
    50  	actual, err := resources.Get(context.TODO(), client.ServiceClient(), "9fe1d3").Extract()
    51  	th.AssertNoErr(t, err)
    52  	th.AssertDeepEquals(t, SecondResource, *actual)
    53  }
    54  
    55  func TestCreateResource(t *testing.T) {
    56  	th.SetupHTTP()
    57  	defer th.TeardownHTTP()
    58  	HandleCreateResourceSuccessfully(t)
    59  
    60  	createOpts := resources.CreateOpts{
    61  		Name: "resource two",
    62  	}
    63  
    64  	actual, err := resources.Create(context.TODO(), client.ServiceClient(), createOpts).Extract()
    65  	th.AssertNoErr(t, err)
    66  	th.AssertDeepEquals(t, SecondResource, *actual)
    67  }
    68  
    69  func TestDeleteResource(t *testing.T) {
    70  	th.SetupHTTP()
    71  	defer th.TeardownHTTP()
    72  	HandleDeleteResourceSuccessfully(t)
    73  
    74  	res := resources.Delete(context.TODO(), client.ServiceClient(), "9fe1d3")
    75  	th.AssertNoErr(t, res.Err)
    76  }
    77  
    78  func TestUpdateResource(t *testing.T) {
    79  	th.SetupHTTP()
    80  	defer th.TeardownHTTP()
    81  	HandleUpdateResourceSuccessfully(t)
    82  
    83  	updateOpts := resources.UpdateOpts{
    84  		Description: "Staging Resource",
    85  	}
    86  
    87  	actual, err := resources.Update(context.TODO(), client.ServiceClient(), "9fe1d3", updateOpts).Extract()
    88  	th.AssertNoErr(t, err)
    89  	th.AssertDeepEquals(t, SecondResourceUpdated, *actual)
    90  }