Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 1 | package databases |
| 2 | |
| 3 | import ( |
| 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 Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 9 | "github.com/rackspace/gophercloud/testhelper/fixture" |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 10 | ) |
| 11 | |
| 12 | const instanceID = "{instanceID}" |
| 13 | |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 14 | var ( |
| 15 | resURL = "/instances/" + instanceID + "/databases" |
| 16 | ) |
| 17 | |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 18 | func TestCreate(t *testing.T) { |
| 19 | th.SetupHTTP() |
| 20 | defer th.TeardownHTTP() |
| 21 | |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 22 | fixture.SetupHandler(t, resURL, "POST", createDBsReq, "", 202) |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 23 | |
| 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 | |
| 33 | func TestList(t *testing.T) { |
| 34 | th.SetupHTTP() |
| 35 | defer th.TeardownHTTP() |
| 36 | |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 37 | fixture.SetupHandler(t, resURL, "GET", "", listDBsResp, 200) |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 38 | |
| 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 | |
| 68 | func TestDelete(t *testing.T) { |
| 69 | th.SetupHTTP() |
| 70 | defer th.TeardownHTTP() |
| 71 | |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 72 | fixture.SetupHandler(t, resURL+"/{dbName}", "DELETE", "", "", 202) |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 73 | |
| 74 | err := Delete(fake.ServiceClient(), instanceID, "{dbName}").ExtractErr() |
| 75 | th.AssertNoErr(t, err) |
| 76 | } |