@@ -2,6 +2,7 @@ package template
2
2
3
3
import (
4
4
"testing"
5
+ "time"
5
6
6
7
"github.com/nginx-proxy/docker-gen/internal/context"
7
8
"github.com/stretchr/testify/assert"
@@ -19,28 +20,61 @@ func TestSortStringsDesc(t *testing.T) {
19
20
assert .Equal (t , expected , sortStringsDesc (strings ))
20
21
}
21
22
23
+ func TestGetFieldAsString (t * testing.T ) {
24
+ testStruct := struct {
25
+ String string
26
+ BoolT bool
27
+ BoolF bool
28
+ Int int
29
+ Int32 int32
30
+ Int64 int64
31
+ Time time.Time
32
+ }{
33
+ String : "foo" ,
34
+ BoolT : true ,
35
+ BoolF : false ,
36
+ Int : 42 ,
37
+ Int32 : 43 ,
38
+ Int64 : 44 ,
39
+ Time : time .Date (2023 , 12 , 19 , 0 , 0 , 0 , 0 , time .UTC ),
40
+ }
41
+
42
+ assert .Equal (t , "foo" , getFieldAsString (testStruct , "String" ))
43
+ assert .Equal (t , "true" , getFieldAsString (testStruct , "BoolT" ))
44
+ assert .Equal (t , "false" , getFieldAsString (testStruct , "BoolF" ))
45
+ assert .Equal (t , "42" , getFieldAsString (testStruct , "Int" ))
46
+ assert .Equal (t , "43" , getFieldAsString (testStruct , "Int32" ))
47
+ assert .Equal (t , "44" , getFieldAsString (testStruct , "Int64" ))
48
+ assert .Equal (t , "2023-12-19 00:00:00 +0000 UTC" , getFieldAsString (testStruct , "Time" ))
49
+ assert .Equal (t , "" , getFieldAsString (testStruct , "InvalidField" ))
50
+ }
51
+
22
52
func TestSortObjectsByKeys (t * testing.T ) {
23
53
o0 := & context.RuntimeContainer {
54
+ Created : time .Date (2020 , 1 , 2 , 0 , 0 , 0 , 0 , time .UTC ),
24
55
Env : map [string ]string {
25
56
"VIRTUAL_HOST" : "bar.localhost" ,
26
57
},
27
58
ID : "9" ,
28
59
}
29
60
o1 := & context.RuntimeContainer {
61
+ Created : time .Date (2021 , 1 , 2 , 0 , 0 , 10 , 0 , time .UTC ),
30
62
Env : map [string ]string {
31
63
"VIRTUAL_HOST" : "foo.localhost" ,
32
64
},
33
65
ID : "1" ,
34
66
}
35
67
o2 := & context.RuntimeContainer {
68
+ Created : time .Date (2021 , 1 , 2 , 0 , 0 , 0 , 0 , time .UTC ),
36
69
Env : map [string ]string {
37
70
"VIRTUAL_HOST" : "baz.localhost" ,
38
71
},
39
72
ID : "3" ,
40
73
}
41
74
o3 := & context.RuntimeContainer {
42
- Env : map [string ]string {},
43
- ID : "8" ,
75
+ Created : time .Date (2020 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ),
76
+ Env : map [string ]string {},
77
+ ID : "8" ,
44
78
}
45
79
containers := []* context.RuntimeContainer {o0 , o1 , o2 , o3 }
46
80
@@ -54,6 +88,8 @@ func TestSortObjectsByKeys(t *testing.T) {
54
88
{"Asc complex" , sortObjectsByKeysAsc , "Env.VIRTUAL_HOST" , []interface {}{o3 , o0 , o2 , o1 }},
55
89
{"Desc simple" , sortObjectsByKeysDesc , "ID" , []interface {}{o0 , o3 , o2 , o1 }},
56
90
{"Desc complex" , sortObjectsByKeysDesc , "Env.VIRTUAL_HOST" , []interface {}{o1 , o2 , o0 , o3 }},
91
+ {"Asc time" , sortObjectsByKeysAsc , "Created" , []interface {}{o3 , o0 , o2 , o1 }},
92
+ {"Desc time" , sortObjectsByKeysDesc , "Created" , []interface {}{o1 , o2 , o0 , o3 }},
57
93
} {
58
94
t .Run (tc .desc , func (t * testing.T ) {
59
95
got , err := tc .fn (containers , tc .key )
0 commit comments