Skip to content

Commit 2ef737a

Browse files
committed
tests: sorting template functions
1 parent d50c146 commit 2ef737a

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

internal/template/sort_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package template
2+
3+
import (
4+
"testing"
5+
6+
"github.com/nginx-proxy/docker-gen/internal/context"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestSortStringsAsc(t *testing.T) {
11+
strings := []string{"foo", "bar", "baz", "qux"}
12+
expected := []string{"bar", "baz", "foo", "qux"}
13+
assert.Equal(t, expected, sortStringsAsc(strings))
14+
}
15+
16+
func TestSortStringsDesc(t *testing.T) {
17+
strings := []string{"foo", "bar", "baz", "qux"}
18+
expected := []string{"qux", "foo", "baz", "bar"}
19+
assert.Equal(t, expected, sortStringsDesc(strings))
20+
}
21+
22+
func TestSortObjectsByKeysAsc(t *testing.T) {
23+
containers := []*context.RuntimeContainer{
24+
{
25+
Env: map[string]string{
26+
"VIRTUAL_HOST": "bar.localhost",
27+
},
28+
ID: "9",
29+
},
30+
{
31+
Env: map[string]string{
32+
"VIRTUAL_HOST": "foo.localhost",
33+
},
34+
ID: "1",
35+
},
36+
{
37+
Env: map[string]string{
38+
"VIRTUAL_HOST": "baz.localhost",
39+
},
40+
ID: "3",
41+
},
42+
{
43+
Env: map[string]string{},
44+
ID: "8",
45+
},
46+
}
47+
48+
sorted, err := sortObjectsByKeysAsc(containers, "ID")
49+
50+
assert.NoError(t, err)
51+
assert.Len(t, sorted, 4)
52+
assert.Equal(t, "foo.localhost", sorted[0].(context.RuntimeContainer).Env["VIRTUAL_HOST"])
53+
assert.Equal(t, "9", sorted[3].(context.RuntimeContainer).ID)
54+
55+
sorted, err = sortObjectsByKeysAsc(sorted, "Env.VIRTUAL_HOST")
56+
57+
assert.NoError(t, err)
58+
assert.Len(t, sorted, 4)
59+
assert.Equal(t, "foo.localhost", sorted[3].(context.RuntimeContainer).Env["VIRTUAL_HOST"])
60+
assert.Equal(t, "8", sorted[0].(context.RuntimeContainer).ID)
61+
}
62+
63+
func TestSortObjectsByKeysDesc(t *testing.T) {
64+
containers := []*context.RuntimeContainer{
65+
{
66+
Env: map[string]string{
67+
"VIRTUAL_HOST": "bar.localhost",
68+
},
69+
ID: "9",
70+
},
71+
{
72+
Env: map[string]string{
73+
"VIRTUAL_HOST": "foo.localhost",
74+
},
75+
ID: "1",
76+
},
77+
{
78+
Env: map[string]string{
79+
"VIRTUAL_HOST": "baz.localhost",
80+
},
81+
ID: "3",
82+
},
83+
{
84+
Env: map[string]string{},
85+
ID: "8",
86+
},
87+
}
88+
89+
sorted, err := sortObjectsByKeysDesc(containers, "ID")
90+
91+
assert.NoError(t, err)
92+
assert.Len(t, sorted, 4)
93+
assert.Equal(t, "bar.localhost", sorted[0].(context.RuntimeContainer).Env["VIRTUAL_HOST"])
94+
assert.Equal(t, "1", sorted[3].(context.RuntimeContainer).ID)
95+
96+
sorted, err = sortObjectsByKeysDesc(sorted, "Env.VIRTUAL_HOST")
97+
98+
assert.NoError(t, err)
99+
assert.Len(t, sorted, 4)
100+
assert.Equal(t, "", sorted[3].(context.RuntimeContainer).Env["VIRTUAL_HOST"])
101+
assert.Equal(t, "1", sorted[0].(context.RuntimeContainer).ID)
102+
}

0 commit comments

Comments
 (0)