forked from chiefy/go-statuspage-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent_group_integration_test.go
More file actions
112 lines (90 loc) · 3.28 KB
/
Copy pathcomponent_group_integration_test.go
File metadata and controls
112 lines (90 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// +build integration
package statuspage
import (
"encoding/json"
"os"
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
var c *Client
var testComponentName = "StatusPage API Client Test Component (Group)"
var testComponentDesc = "Component for testing creation of Component Groups"
var testComponentGroupName = "StatusPage API Client Test Component Group"
type Credentials struct {
ApiKey string `json:"apiKey"`
PageID string `json:"pageID"`
}
func setup() {
var creds Credentials
var err error
// load credentials from file and create a client
if f, err := os.Open(".client-testing.json"); err == nil {
json.NewDecoder(f).Decode(&creds)
f.Close()
} else {
panic("Could not find .client-testing.json")
}
c, err = NewClient(creds.ApiKey, creds.PageID)
if err != nil {
panic("Error creating new client!")
}
}
func TestClient_CreateComponentGroup(t *testing.T) {
// Component groups must be created with components in them
// We will create a dummy Component
_, err := c.CreateComponent(testComponentName, testComponentDesc, "", false)
if err != nil {
t.Errorf("Error creating a new Component for Component Group testing: %s\n", err)
}
comp, err := c.GetComponentByName(testComponentName)
if err != nil {
t.Fatalf("Error getting new Component for Component Group testing: %s\n", err)
}
// Now create a new ComponentGroup containing the Component we just created
newCompGroupList := []string{*comp.ID}
_, err = c.CreateComponentGroup(testComponentGroupName, newCompGroupList)
if err != nil {
t.Errorf("Error creating a new Component Group: %s\n", err)
}
compGroup, err := c.GetComponentGroupByName(testComponentGroupName)
if err != nil {
t.Fatalf("Error getting new Component Group: %s\n", err)
}
// Check that the component group contains what we expect
assert.Equal(t, *compGroup.Name, testComponentGroupName)
assert.Equal(t, len(compGroup.Components), len(newCompGroupList))
// Check that the component group contains exactly the same components
sort.Strings(compGroup.Components)
sort.Strings(newCompGroupList)
for i := 0; i < len(compGroup.Components); i++ {
if compGroup.Components[i] != newCompGroupList[i] {
t.Errorf("Component group did not contain the expected components!")
}
}
}
func TestClient_DeleteComponentGroup(t *testing.T) {
// We'll delete the component group we created earlier
compGroup, err := c.GetComponentGroupByName(testComponentGroupName)
if err != nil {
t.Fatalf("Error getting existing Component Group: %s\n", err)
}
err = c.DeleteComponentGroup(compGroup)
if err != nil {
t.Errorf("Error deleting existing Component Group: %s\n", err)
}
// Also delete the component we made for the test
comp, err := c.GetComponentByName(testComponentName)
if err != nil {
t.Errorf("Error getting existing Component (from Component Group tests): %s\n", err)
}
err = c.DeleteComponent(comp)
if err != nil {
t.Fatalf("Error deleting existing Component (from Component Group tests): %s\n", err)
}
}
func TestMain(m *testing.M) {
setup()
r := m.Run()
os.Exit(r)
}