blob: 11bf9c8205fd8545df8fe91eca6db365eb476068 [file] [log] [blame]
Jamie Hannaford1943b382015-02-12 11:50:02 +01001package databases
2
3import (
4 "testing"
5
6 "github.com/rackspace/gophercloud/pagination"
7 th "github.com/rackspace/gophercloud/testhelper"
8 fake "github.com/rackspace/gophercloud/testhelper/client"
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +01009 "github.com/rackspace/gophercloud/testhelper/fixture"
Jamie Hannaford1943b382015-02-12 11:50:02 +010010)
11
12const instanceID = "{instanceID}"
13
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010014var (
15 resURL = "/instances/" + instanceID + "/databases"
16)
17
Jamie Hannaford1943b382015-02-12 11:50:02 +010018func TestCreate(t *testing.T) {
19 th.SetupHTTP()
20 defer th.TeardownHTTP()
21
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010022 fixture.SetupHandler(t, resURL, "POST", createDBsReq, "", 202)
Jamie Hannaford1943b382015-02-12 11:50:02 +010023
24 opts := BatchCreateOpts{
25 CreateOpts{Name: "testingdb", CharSet: "utf8", Collate: "utf8_general_ci"},
26 CreateOpts{Name: "sampledb"},
27 }
28
29 res := Create(fake.ServiceClient(), instanceID, opts)
30 th.AssertNoErr(t, res.Err)
31}
32
33func TestList(t *testing.T) {
34 th.SetupHTTP()
35 defer th.TeardownHTTP()
36
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010037 fixture.SetupHandler(t, resURL, "GET", "", listDBsResp, 200)
Jamie Hannaford1943b382015-02-12 11:50:02 +010038
39 expectedDBs := []Database{
40 Database{Name: "anotherexampledb"},
41 Database{Name: "exampledb"},
42 Database{Name: "nextround"},
43 Database{Name: "sampledb"},
44 Database{Name: "testingdb"},
45 }
46
47 pages := 0
48 err := List(fake.ServiceClient(), instanceID).EachPage(func(page pagination.Page) (bool, error) {
49 pages++
50
51 actual, err := ExtractDBs(page)
52 if err != nil {
53 return false, err
54 }
55
56 th.CheckDeepEquals(t, expectedDBs, actual)
57
58 return true, nil
59 })
60
61 th.AssertNoErr(t, err)
62
63 if pages != 1 {
64 t.Errorf("Expected 1 page, saw %d", pages)
65 }
66}
67
68func TestDelete(t *testing.T) {
69 th.SetupHTTP()
70 defer th.TeardownHTTP()
71
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010072 fixture.SetupHandler(t, resURL+"/{dbName}", "DELETE", "", "", 202)
Jamie Hannaford1943b382015-02-12 11:50:02 +010073
74 err := Delete(fake.ServiceClient(), instanceID, "{dbName}").ExtractErr()
75 th.AssertNoErr(t, err)
76}