Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit e635b7d

Browse files
author
Jamie Hannaford
committed
Refactor fixtures
1 parent d3a78ef commit e635b7d

File tree

12 files changed

+210
-425
lines changed

12 files changed

+210
-425
lines changed

rackspace/db/v1/backups/fixtures.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
package backups
22

3-
import (
4-
"testing"
5-
6-
"github.com/rackspace/gophercloud/testhelper/fixture"
7-
)
8-
9-
var singleBackup = `
3+
var getResp = `
104
{
115
"backup": {
126
"created": "2014-02-13T21:47:16",
@@ -28,8 +22,7 @@ var singleBackup = `
2822
}
2923
`
3024

31-
func HandleCreateSuccessfully(t *testing.T) {
32-
requestJSON := `
25+
var createReq = `
3326
{
3427
"backup": {
3528
"description": "My Backup",
@@ -39,11 +32,9 @@ func HandleCreateSuccessfully(t *testing.T) {
3932
}
4033
`
4134

42-
fixture.SetupHandler(t, "/backups", "POST", requestJSON, singleBackup, 202)
43-
}
35+
var createResp = getResp
4436

45-
func HandleListSuccessfully(t *testing.T) {
46-
responseJSON := `
37+
var listResp = `
4738
{
4839
"backups": [
4940
{
@@ -66,14 +57,3 @@ func HandleListSuccessfully(t *testing.T) {
6657
]
6758
}
6859
`
69-
70-
fixture.SetupHandler(t, "/backups", "GET", "", responseJSON, 200)
71-
}
72-
73-
func HandleGetSuccessfully(t *testing.T, backupID string) {
74-
fixture.SetupHandler(t, "/backups/"+backupID, "GET", "", singleBackup, 200)
75-
}
76-
77-
func HandleDeleteSuccessfully(t *testing.T, backupID string) {
78-
fixture.SetupHandler(t, "/backups/"+backupID, "DELETE", "", "", 202)
79-
}

rackspace/db/v1/backups/requests_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@ import (
77
"github.com/rackspace/gophercloud/rackspace/db/v1/datastores"
88
th "github.com/rackspace/gophercloud/testhelper"
99
fake "github.com/rackspace/gophercloud/testhelper/client"
10+
"github.com/rackspace/gophercloud/testhelper/fixture"
1011
)
1112

12-
const backupID = "61f12fef-edb1-4561-8122-e7c00ef26a82"
13+
var (
14+
backupID = "{backupID}"
15+
_rootURL = "/backups"
16+
resURL = _rootURL + "/" + backupID
17+
)
1318

1419
func TestCreate(t *testing.T) {
1520
th.SetupHTTP()
1621
defer th.TeardownHTTP()
17-
18-
HandleCreateSuccessfully(t)
22+
fixture.SetupHandler(t, _rootURL, "POST", createReq, createResp, 202)
1923

2024
opts := CreateOpts{
2125
Name: "snapshot",
@@ -50,13 +54,12 @@ func TestCreate(t *testing.T) {
5054
func TestList(t *testing.T) {
5155
th.SetupHTTP()
5256
defer th.TeardownHTTP()
57+
fixture.SetupHandler(t, _rootURL, "GET", "", listResp, 200)
5358

54-
HandleListSuccessfully(t)
59+
pages := 0
5560

56-
count := 0
57-
58-
List(fake.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
59-
count++
61+
err := List(fake.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
62+
pages++
6063
actual, err := ExtractBackups(page)
6164
th.AssertNoErr(t, err)
6265

@@ -85,16 +88,14 @@ func TestList(t *testing.T) {
8588
return true, nil
8689
})
8790

88-
if count != 1 {
89-
t.Errorf("Expected 1 page, got %d", count)
90-
}
91+
th.AssertNoErr(t, err)
92+
th.AssertEquals(t, 1, pages)
9193
}
9294

9395
func TestGet(t *testing.T) {
9496
th.SetupHTTP()
9597
defer th.TeardownHTTP()
96-
97-
HandleGetSuccessfully(t, backupID)
98+
fixture.SetupHandler(t, resURL, "GET", "", getResp, 200)
9899

99100
instance, err := Get(fake.ServiceClient(), backupID).Extract()
100101
th.AssertNoErr(t, err)
@@ -123,8 +124,7 @@ func TestGet(t *testing.T) {
123124
func TestDelete(t *testing.T) {
124125
th.SetupHTTP()
125126
defer th.TeardownHTTP()
126-
127-
HandleDeleteSuccessfully(t, backupID)
127+
fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
128128

129129
err := Delete(fake.ServiceClient(), backupID).ExtractErr()
130130
th.AssertNoErr(t, err)

rackspace/db/v1/configurations/requests_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ var (
2727
func TestList(t *testing.T) {
2828
th.SetupHTTP()
2929
defer th.TeardownHTTP()
30-
3130
fixture.SetupHandler(t, _baseURL, "GET", "", listConfigsJSON, 200)
3231

3332
count := 0
@@ -49,7 +48,6 @@ func TestList(t *testing.T) {
4948
func TestGet(t *testing.T) {
5049
th.SetupHTTP()
5150
defer th.TeardownHTTP()
52-
5351
fixture.SetupHandler(t, resURL, "GET", "", getConfigJSON, 200)
5452

5553
config, err := Get(fake.ServiceClient(), configID).Extract()
@@ -60,7 +58,6 @@ func TestGet(t *testing.T) {
6058
func TestCreate(t *testing.T) {
6159
th.SetupHTTP()
6260
defer th.TeardownHTTP()
63-
6461
fixture.SetupHandler(t, _baseURL, "POST", createReq, createConfigJSON, 201)
6562

6663
opts := CreateOpts{
@@ -84,7 +81,6 @@ func TestCreate(t *testing.T) {
8481
func TestUpdate(t *testing.T) {
8582
th.SetupHTTP()
8683
defer th.TeardownHTTP()
87-
8884
fixture.SetupHandler(t, resURL, "PATCH", updateReq, "", 200)
8985

9086
opts := UpdateOpts{
@@ -100,7 +96,6 @@ func TestUpdate(t *testing.T) {
10096
func TestReplace(t *testing.T) {
10197
th.SetupHTTP()
10298
defer th.TeardownHTTP()
103-
10499
fixture.SetupHandler(t, resURL, "PUT", updateReq, "", 202)
105100

106101
opts := UpdateOpts{
@@ -116,7 +111,6 @@ func TestReplace(t *testing.T) {
116111
func TestDelete(t *testing.T) {
117112
th.SetupHTTP()
118113
defer th.TeardownHTTP()
119-
120114
fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
121115

122116
err := Delete(fake.ServiceClient(), configID).ExtractErr()
@@ -126,7 +120,6 @@ func TestDelete(t *testing.T) {
126120
func TestListInstances(t *testing.T) {
127121
th.SetupHTTP()
128122
defer th.TeardownHTTP()
129-
130123
fixture.SetupHandler(t, resURL+"/instances", "GET", "", listInstancesJSON, 200)
131124

132125
expectedInstance := instances.Instance{
@@ -155,7 +148,6 @@ func TestListInstances(t *testing.T) {
155148
func TestListDSParams(t *testing.T) {
156149
th.SetupHTTP()
157150
defer th.TeardownHTTP()
158-
159151
fixture.SetupHandler(t, dsParamListURL, "GET", "", listParamsJSON, 200)
160152

161153
pages := 0
@@ -186,7 +178,6 @@ func TestListDSParams(t *testing.T) {
186178
func TestGetDSParam(t *testing.T) {
187179
th.SetupHTTP()
188180
defer th.TeardownHTTP()
189-
190181
fixture.SetupHandler(t, dsParamGetURL, "GET", "", getParamJSON, 200)
191182

192183
param, err := GetDatastoreParam(fake.ServiceClient(), dsID, versionID, paramID).Extract()
@@ -202,7 +193,6 @@ func TestGetDSParam(t *testing.T) {
202193
func TestListGlobalParams(t *testing.T) {
203194
th.SetupHTTP()
204195
defer th.TeardownHTTP()
205-
206196
fixture.SetupHandler(t, globalParamListURL, "GET", "", listParamsJSON, 200)
207197

208198
pages := 0
@@ -233,7 +223,6 @@ func TestListGlobalParams(t *testing.T) {
233223
func TestGetGlobalParam(t *testing.T) {
234224
th.SetupHTTP()
235225
defer th.TeardownHTTP()
236-
237226
fixture.SetupHandler(t, globalParamGetURL, "GET", "", getParamJSON, 200)
238227

239228
param, err := GetGlobalParam(fake.ServiceClient(), versionID, paramID).Extract()

rackspace/db/v1/databases/delegate_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ import (
99
fake "github.com/rackspace/gophercloud/testhelper/client"
1010
)
1111

12-
const instanceID = "{instanceID}"
12+
var (
13+
instanceID = "{instanceID}"
14+
rootURL = "/instances"
15+
resURL = rootURL + "/" + instanceID
16+
uRootURL = resURL + "/root"
17+
aURL = resURL + "/action"
18+
)
1319

1420
func TestCreate(t *testing.T) {
1521
th.SetupHTTP()
1622
defer th.TeardownHTTP()
17-
18-
os.HandleCreateDBSuccessfully(t, instanceID)
23+
os.HandleCreate(t)
1924

2025
opts := os.BatchCreateOpts{
2126
os.CreateOpts{Name: "testingdb", CharSet: "utf8", Collate: "utf8_general_ci"},
@@ -29,8 +34,7 @@ func TestCreate(t *testing.T) {
2934
func TestList(t *testing.T) {
3035
th.SetupHTTP()
3136
defer th.TeardownHTTP()
32-
33-
os.HandleListDBsSuccessfully(t, instanceID)
37+
os.HandleList(t)
3438

3539
expectedDBs := []os.Database{
3640
os.Database{Name: "anotherexampledb"},
@@ -50,22 +54,17 @@ func TestList(t *testing.T) {
5054
}
5155

5256
th.CheckDeepEquals(t, expectedDBs, actual)
53-
5457
return true, nil
5558
})
5659

5760
th.AssertNoErr(t, err)
58-
59-
if pages != 1 {
60-
t.Errorf("Expected 1 page, saw %d", pages)
61-
}
61+
th.AssertEquals(t, 1, pages)
6262
}
6363

6464
func TestDelete(t *testing.T) {
6565
th.SetupHTTP()
6666
defer th.TeardownHTTP()
67-
68-
os.HandleDeleteDBSuccessfully(t, instanceID, "{dbName}")
67+
os.HandleDelete(t)
6968

7069
err := os.Delete(fake.ServiceClient(), instanceID, "{dbName}").ExtractErr()
7170
th.AssertNoErr(t, err)

rackspace/db/v1/datastores/requests_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
func TestList(t *testing.T) {
1313
th.SetupHTTP()
1414
defer th.TeardownHTTP()
15-
1615
fixture.SetupHandler(t, "/datastores", "GET", "", listDSResp, 200)
1716

1817
pages := 0
@@ -37,7 +36,6 @@ func TestList(t *testing.T) {
3736
func TestGet(t *testing.T) {
3837
th.SetupHTTP()
3938
defer th.TeardownHTTP()
40-
4139
fixture.SetupHandler(t, "/datastores/{dsID}", "GET", "", getDSResp, 200)
4240

4341
ds, err := Get(fake.ServiceClient(), "{dsID}").Extract()
@@ -48,7 +46,6 @@ func TestGet(t *testing.T) {
4846
func TestListVersions(t *testing.T) {
4947
th.SetupHTTP()
5048
defer th.TeardownHTTP()
51-
5249
fixture.SetupHandler(t, "/datastores/{dsID}/versions", "GET", "", listVersionsResp, 200)
5350

5451
pages := 0
@@ -73,7 +70,6 @@ func TestListVersions(t *testing.T) {
7370
func TestGetVersion(t *testing.T) {
7471
th.SetupHTTP()
7572
defer th.TeardownHTTP()
76-
7773
fixture.SetupHandler(t, "/datastores/{dsID}/versions/{versionID}", "GET", "", getVersionResp, 200)
7874

7975
ds, err := GetVersion(fake.ServiceClient(), "{dsID}", "{versionID}").Extract()

rackspace/db/v1/flavors/delegate_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import (
1313
func TestListFlavors(t *testing.T) {
1414
th.SetupHTTP()
1515
defer th.TeardownHTTP()
16-
17-
os.HandleListFlavorsSuccessfully(t)
16+
os.HandleList(t)
1817

1918
pages := 0
2019
err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
@@ -78,10 +77,9 @@ func TestListFlavors(t *testing.T) {
7877
func TestGetFlavor(t *testing.T) {
7978
th.SetupHTTP()
8079
defer th.TeardownHTTP()
80+
os.HandleGet(t)
8181

82-
os.HandleGetFlavorSuccessfully(t, "12345")
83-
84-
actual, err := Get(fake.ServiceClient(), "12345").Extract()
82+
actual, err := Get(fake.ServiceClient(), "{flavorID}").Extract()
8583
th.AssertNoErr(t, err)
8684

8785
expected := &os.Flavor{

0 commit comments

Comments
 (0)