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

Commit d3a78ef

Browse files
author
Jamie Hannaford
committed
Refactor to use new handler setup
1 parent a50d135 commit d3a78ef

File tree

8 files changed

+190
-510
lines changed

8 files changed

+190
-510
lines changed

openstack/db/v1/databases/fixtures.go

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,21 @@
11
package databases
22

3-
import (
4-
"fmt"
5-
"net/http"
6-
"testing"
7-
8-
th "github.com/rackspace/gophercloud/testhelper"
9-
fake "github.com/rackspace/gophercloud/testhelper/client"
10-
)
11-
12-
func HandleCreateDBSuccessfully(t *testing.T, instanceID string) {
13-
th.Mux.HandleFunc("/instances/"+instanceID+"/databases", func(w http.ResponseWriter, r *http.Request) {
14-
th.TestMethod(t, r, "POST")
15-
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
16-
th.TestJSONRequest(t, r, `
3+
var createDBsReq = `
174
{
18-
"databases": [
19-
{
20-
"character_set": "utf8",
21-
"collate": "utf8_general_ci",
22-
"name": "testingdb"
23-
},
24-
{
25-
"name": "sampledb"
26-
}
27-
]
28-
}
29-
`)
30-
31-
w.Header().Set("Content-Type", "application/json")
32-
w.WriteHeader(http.StatusAccepted)
33-
})
5+
"databases": [
6+
{
7+
"character_set": "utf8",
8+
"collate": "utf8_general_ci",
9+
"name": "testingdb"
10+
},
11+
{
12+
"name": "sampledb"
13+
}
14+
]
3415
}
16+
`
3517

36-
func HandleListDBsSuccessfully(t *testing.T, instanceID string) {
37-
th.Mux.HandleFunc("/instances/"+instanceID+"/databases", func(w http.ResponseWriter, r *http.Request) {
38-
th.TestMethod(t, r, "GET")
39-
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
40-
41-
w.Header().Set("Content-Type", "application/json")
42-
w.WriteHeader(http.StatusOK)
43-
44-
fmt.Fprintf(w, `
18+
var listDBsResp = `
4519
{
4620
"databases": [
4721
{
@@ -61,16 +35,4 @@ func HandleListDBsSuccessfully(t *testing.T, instanceID string) {
6135
}
6236
]
6337
}
64-
`)
65-
})
66-
}
67-
68-
func HandleDeleteDBSuccessfully(t *testing.T, instanceID, dbName string) {
69-
th.Mux.HandleFunc("/instances/"+instanceID+"/databases/"+dbName, func(w http.ResponseWriter, r *http.Request) {
70-
th.TestMethod(t, r, "DELETE")
71-
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
72-
73-
w.Header().Set("Content-Type", "application/json")
74-
w.WriteHeader(http.StatusAccepted)
75-
})
76-
}
38+
`

openstack/db/v1/databases/requests_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@ import (
66
"github.com/rackspace/gophercloud/pagination"
77
th "github.com/rackspace/gophercloud/testhelper"
88
fake "github.com/rackspace/gophercloud/testhelper/client"
9+
"github.com/rackspace/gophercloud/testhelper/fixture"
910
)
1011

1112
const instanceID = "{instanceID}"
1213

14+
var (
15+
resURL = "/instances/" + instanceID + "/databases"
16+
)
17+
1318
func TestCreate(t *testing.T) {
1419
th.SetupHTTP()
1520
defer th.TeardownHTTP()
1621

17-
HandleCreateDBSuccessfully(t, instanceID)
22+
fixture.SetupHandler(t, resURL, "POST", createDBsReq, "", 202)
1823

1924
opts := BatchCreateOpts{
2025
CreateOpts{Name: "testingdb", CharSet: "utf8", Collate: "utf8_general_ci"},
@@ -29,7 +34,7 @@ func TestList(t *testing.T) {
2934
th.SetupHTTP()
3035
defer th.TeardownHTTP()
3136

32-
HandleListDBsSuccessfully(t, instanceID)
37+
fixture.SetupHandler(t, resURL, "GET", "", listDBsResp, 200)
3338

3439
expectedDBs := []Database{
3540
Database{Name: "anotherexampledb"},
@@ -64,7 +69,7 @@ func TestDelete(t *testing.T) {
6469
th.SetupHTTP()
6570
defer th.TeardownHTTP()
6671

67-
HandleDeleteDBSuccessfully(t, instanceID, "{dbName}")
72+
fixture.SetupHandler(t, resURL+"/{dbName}", "DELETE", "", "", 202)
6873

6974
err := Delete(fake.ServiceClient(), instanceID, "{dbName}").ExtractErr()
7075
th.AssertNoErr(t, err)

openstack/db/v1/flavors/fixtures.go

Lines changed: 24 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,31 @@
11
package flavors
22

3-
import (
4-
"fmt"
5-
"net/http"
6-
"testing"
3+
import "fmt"
74

8-
th "github.com/rackspace/gophercloud/testhelper"
9-
fake "github.com/rackspace/gophercloud/testhelper/client"
10-
)
11-
12-
func HandleListFlavorsSuccessfully(t *testing.T) {
13-
th.Mux.HandleFunc("/flavors", func(w http.ResponseWriter, r *http.Request) {
14-
th.TestMethod(t, r, "GET")
15-
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
16-
17-
w.Header().Set("Content-Type", "application/json")
18-
w.WriteHeader(http.StatusOK)
19-
20-
fmt.Fprintf(w, `
5+
const flavor = `
216
{
22-
"flavors": [
23-
{
24-
"id": 1,
25-
"links": [
26-
{
27-
"href": "https://openstack.example.com/v1.0/1234/flavors/1",
28-
"rel": "self"
29-
},
30-
{
31-
"href": "https://openstack.example.com/flavors/1",
32-
"rel": "bookmark"
33-
}
34-
],
35-
"name": "m1.tiny",
36-
"ram": 512
37-
},
38-
{
39-
"id": 2,
40-
"links": [
41-
{
42-
"href": "https://openstack.example.com/v1.0/1234/flavors/2",
43-
"rel": "self"
44-
},
45-
{
46-
"href": "https://openstack.example.com/flavors/2",
47-
"rel": "bookmark"
48-
}
49-
],
50-
"name": "m1.small",
51-
"ram": 1024
52-
},
53-
{
54-
"id": 3,
55-
"links": [
56-
{
57-
"href": "https://openstack.example.com/v1.0/1234/flavors/3",
58-
"rel": "self"
59-
},
60-
{
61-
"href": "https://openstack.example.com/flavors/3",
62-
"rel": "bookmark"
63-
}
64-
],
65-
"name": "m1.medium",
66-
"ram": 2048
67-
},
68-
{
69-
"id": 4,
70-
"links": [
71-
{
72-
"href": "https://openstack.example.com/v1.0/1234/flavors/4",
73-
"rel": "self"
74-
},
75-
{
76-
"href": "https://openstack.example.com/flavors/4",
77-
"rel": "bookmark"
78-
}
79-
],
80-
"name": "m1.large",
81-
"ram": 4096
82-
}
83-
]
84-
}
85-
`)
86-
})
7+
"id": %d,
8+
"links": [
9+
{
10+
"href": "https://openstack.example.com/v1.0/1234/flavors/%d",
11+
"rel": "self"
12+
},
13+
{
14+
"href": "https://openstack.example.com/flavors/%d",
15+
"rel": "bookmark"
16+
}
17+
],
18+
"name": "%s",
19+
"ram": %d
8720
}
21+
`
8822

89-
func HandleGetFlavorSuccessfully(t *testing.T, flavorID string) {
90-
th.Mux.HandleFunc("/flavors/"+flavorID, func(w http.ResponseWriter, r *http.Request) {
91-
th.TestMethod(t, r, "GET")
92-
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
23+
var (
24+
flavor1 = fmt.Sprintf(flavor, 1, 1, 1, "m1.tiny", 512)
25+
flavor2 = fmt.Sprintf(flavor, 2, 2, 2, "m1.small", 1024)
26+
flavor3 = fmt.Sprintf(flavor, 3, 3, 3, "m1.medium", 2048)
27+
flavor4 = fmt.Sprintf(flavor, 4, 4, 4, "m1.large", 4096)
9328

94-
w.Header().Set("Content-Type", "application/json")
95-
w.WriteHeader(http.StatusOK)
96-
97-
fmt.Fprintf(w, `
98-
{
99-
"flavor": {
100-
"id": 1,
101-
"links": [
102-
{
103-
"href": "https://openstack.example.com/v1.0/1234/flavors/1",
104-
"rel": "self"
105-
}
106-
],
107-
"name": "m1.tiny",
108-
"ram": 512
109-
}
110-
}
111-
`)
112-
})
113-
}
29+
listFlavorsResp = fmt.Sprintf(`{"flavors":[%s, %s, %s, %s]}`, flavor1, flavor2, flavor3, flavor4)
30+
getFlavorResp = fmt.Sprintf(`{"flavor": %s}`, flavor1)
31+
)

openstack/db/v1/flavors/requests_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@ import (
77
"github.com/rackspace/gophercloud/pagination"
88
th "github.com/rackspace/gophercloud/testhelper"
99
fake "github.com/rackspace/gophercloud/testhelper/client"
10+
"github.com/rackspace/gophercloud/testhelper/fixture"
11+
)
12+
13+
var (
14+
flavorID = "{flavorID}"
15+
_baseURL = "/flavors"
16+
resURL = "/flavors/" + flavorID
1017
)
1118

1219
func TestListFlavors(t *testing.T) {
1320
th.SetupHTTP()
1421
defer th.TeardownHTTP()
1522

16-
HandleListFlavorsSuccessfully(t)
23+
fixture.SetupHandler(t, _baseURL, "GET", "", listFlavorsResp, 200)
1724

1825
pages := 0
1926
err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
@@ -78,9 +85,9 @@ func TestGetFlavor(t *testing.T) {
7885
th.SetupHTTP()
7986
defer th.TeardownHTTP()
8087

81-
HandleGetFlavorSuccessfully(t, "12345")
88+
fixture.SetupHandler(t, resURL, "GET", "", getFlavorResp, 200)
8289

83-
actual, err := Get(fake.ServiceClient(), "12345").Extract()
90+
actual, err := Get(fake.ServiceClient(), flavorID).Extract()
8491
th.AssertNoErr(t, err)
8592

8693
expected := &Flavor{

0 commit comments

Comments
 (0)