Skip to content

Commit ab69ef4

Browse files
nathansamsonJoshua-Anderson
authored andcommitted
feat(*): Define composite types that are sortable (#55)
1 parent e29e1a5 commit ab69ef4

18 files changed

Lines changed: 184 additions & 8 deletions

api/apps.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ type App struct {
1010
UUID string `json:"uuid"`
1111
}
1212

13+
type Apps []App
14+
15+
func (a Apps) Len() int { return len(a) }
16+
func (a Apps) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
17+
func (a Apps) Less(i, j int) bool { return a[i].ID < a[j].ID }
18+
1319
// AppCreateRequest is the definition of POST /v2/apps/.
1420
type AppCreateRequest struct {
1521
ID string `json:"id,omitempty"`

api/apps_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package api
2+
3+
import (
4+
"sort"
5+
"testing"
6+
)
7+
8+
func TestAppsSorted(t *testing.T) {
9+
apps := Apps{
10+
{"2014-01-01T00:00:00UTC", "Zulu", "John", "2016-01-02", "zulu.example.com", "d57be2ba-7ae2-4825-9ace-7c86cb893046"},
11+
{"2014-01-01T00:00:00UTC", "Alpha", "John", "2016-01-02", "alpha.example.com", "3d501190-1b8e-41ef-94c5-dd9a0bb707bb"},
12+
{"2014-01-01T00:00:00UTC", "Gamma", "John", "2016-01-02", "gamma.example.com", "41d95133-fd4d-4f4c-92a2-e454857371cc"},
13+
{"2014-01-01T00:00:00UTC", "Beta", "John", "2016-01-02", "beta.example.com", "222ed1aa-e985-4bec-9966-a88215300661"},
14+
}
15+
16+
sort.Sort(apps)
17+
expectedAppNames := []string{"Alpha", "Beta", "Gamma", "Zulu"}
18+
19+
for i, app := range apps {
20+
if expectedAppNames[i] != app.ID {
21+
t.Errorf("Expected apps to be sorted %v, Got %v at index %v", expectedAppNames[i], app.ID, i)
22+
}
23+
}
24+
}

api/domains.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ type Domain struct {
99
Updated string `json:"updated"`
1010
}
1111

12+
type Domains []Domain
13+
14+
func (d Domains) Len() int { return len(d) }
15+
func (d Domains) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
16+
func (d Domains) Less(i, j int) bool { return d[i].Domain < d[j].Domain }
17+
1218
// DomainCreateRequest is the structure of POST /v2/app/<app id>/domains/.
1319
type DomainCreateRequest struct {
1420
Domain string `json:"domain"`

api/domains_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package api
2+
3+
import (
4+
"sort"
5+
"testing"
6+
)
7+
8+
func TestDomainsSorted(t *testing.T) {
9+
domains := Domains{
10+
{"Alpha", "", "gamma.example.com", "", ""},
11+
{"Alpha", "", "alpha1.example.com", "", ""},
12+
{"Alpha", "", "zulu.example.com", "", ""},
13+
{"Alpha", "", "delta.example.com", "", ""},
14+
}
15+
16+
sort.Sort(domains)
17+
expectedDomains := []string{"alpha1.example.com", "delta.example.com", "gamma.example.com", "zulu.example.com"}
18+
19+
for i, domain := range domains {
20+
if expectedDomains[i] != domain.Domain {
21+
t.Errorf("Expected domains to be sorted %v, Got %v at index %v", expectedDomains[i], domain.Domain, i)
22+
}
23+
}
24+
}

api/keys.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ type Key struct {
1010
UUID string `json:"uuid"`
1111
}
1212

13+
type Keys []Key
14+
15+
func (k Keys) Len() int { return len(k) }
16+
func (k Keys) Swap(i, j int) { k[i], k[j] = k[j], k[i] }
17+
func (k Keys) Less(i, j int) bool { return k[i].ID < k[j].ID }
18+
1319
// KeyCreateRequest is the definition of POST /v2/keys/.
1420
type KeyCreateRequest struct {
1521
ID string `json:"id"`

api/keys_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package api
2+
3+
import (
4+
"sort"
5+
"testing"
6+
)
7+
8+
func TestKeysSorted(t *testing.T) {
9+
keys := Keys{
10+
{"", "Delta", "", "", "", ""},
11+
{"", "Alpha", "", "", "", ""},
12+
{"", "Gamma", "", "", "", ""},
13+
{"", "Zeta", "", "", "", ""},
14+
}
15+
16+
sort.Sort(keys)
17+
expectedKeys := []string{"Alpha", "Delta", "Gamma", "Zeta"}
18+
19+
for i, key := range keys {
20+
if expectedKeys[i] != key.ID {
21+
t.Errorf("Expected domains to be sorted %v, Got %v at index %v", expectedKeys[i], key.ID, i)
22+
}
23+
}
24+
}

api/ps.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,20 @@ type Pods struct {
1010
State string `json:"state"`
1111
Started time.Time `json:"started"`
1212
}
13+
14+
type PodsList []Pods
15+
16+
func (p PodsList) Len() int { return len(p) }
17+
func (p PodsList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
18+
func (p PodsList) Less(i, j int) bool { return p[i].Name < p[j].Name }
19+
20+
type PodType struct {
21+
Type string
22+
PodsList PodsList
23+
}
24+
25+
type PodTypes []PodType
26+
27+
func (p PodTypes) Len() int { return len(p) }
28+
func (p PodTypes) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
29+
func (p PodTypes) Less(i, j int) bool { return p[i].Type < p[j].Type }

api/ps_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package api
2+
3+
import (
4+
"sort"
5+
"testing"
6+
7+
"github.com/deis/controller-sdk-go/pkg/time"
8+
)
9+
10+
func TestPodsListSorted(t *testing.T) {
11+
pods := PodsList{
12+
{"", "web", "web.fsdfgh4", "up", time.Time{}},
13+
{"", "web", "web.asdfgh1", "up", time.Time{}},
14+
{"", "web", "web.csdfgh3", "up", time.Time{}},
15+
{"", "web", "web.bsdfgh2", "up", time.Time{}},
16+
}
17+
18+
sort.Sort(pods)
19+
20+
expectedPodNames := []string{"web.asdfgh1", "web.bsdfgh2", "web.csdfgh3", "web.fsdfgh4"}
21+
22+
for i, pod := range pods {
23+
if expectedPodNames[i] != pod.Name {
24+
t.Errorf("Expected pods to be sorted %v, Got %v", expectedPodNames[i], pod.Name)
25+
}
26+
}
27+
}
28+
29+
func TestPodTypesSorted(t *testing.T) {
30+
podTypes := PodTypes{
31+
{"worker", PodsList{}},
32+
{"web", PodsList{}},
33+
{"clock", PodsList{}},
34+
}
35+
36+
sort.Sort(podTypes)
37+
expectedPodTypes := []string{"clock", "web", "worker"}
38+
39+
for i, podType := range podTypes {
40+
if expectedPodTypes[i] != podType.Type {
41+
t.Errorf("Expected pod types to be sorted %v, Got %v at index %v", expectedPodTypes[i], podType.Type, i)
42+
}
43+
}
44+
}

api/users.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ Date Joined: %s`
3333
return fmt.Sprintf(tpl, u.ID, u.Username, u.Email, u.FirstName, u.LastName, u.LastLogin,
3434
u.IsSuperuser, u.IsStaff, u.IsActive, u.DateJoined)
3535
}
36+
37+
type Users []User
38+
39+
func (u Users) Len() int { return len(u) }
40+
func (u Users) Swap(i, j int) { u[i], u[j] = u[j], u[i] }
41+
func (u Users) Less(i, j int) bool { return u[i].Username < u[j].Username }

api/users_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"sort"
45
"testing"
56
)
67

@@ -33,3 +34,21 @@ Date Joined: Yesterday`
3334
t.Errorf("Got:\n\n%s\n\nExpected:\n\n%s", user.String(), expected)
3435
}
3536
}
37+
38+
func TestUsersSorted(t *testing.T) {
39+
users := Users{
40+
{1, "", false, "Zulu", "", "", "", false, false, ""},
41+
{2, "", false, "Beta", "", "", "", false, false, ""},
42+
{3, "", false, "Gamma", "", "", "", false, false, ""},
43+
{4, "", false, "Alpha", "", "", "", false, false, ""},
44+
}
45+
46+
sort.Sort(users)
47+
expectedUsernames := []string{"Alpha", "Beta", "Gamma", "Zulu"}
48+
49+
for i, user := range users {
50+
if expectedUsernames[i] != user.Username {
51+
t.Errorf("Expected users to be sorted %v, Got %v at index %v", expectedUsernames[i], user.Username, i)
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)