@@ -2,6 +2,7 @@ package template
22
33import (
44 "testing"
5+ "time"
56
67 "github.com/nginx-proxy/docker-gen/internal/context"
78 "github.com/stretchr/testify/assert"
@@ -19,28 +20,61 @@ func TestSortStringsDesc(t *testing.T) {
1920 assert .Equal (t , expected , sortStringsDesc (strings ))
2021}
2122
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+
2252func TestSortObjectsByKeys (t * testing.T ) {
2353 o0 := & context.RuntimeContainer {
54+ Created : time .Date (2020 , 1 , 2 , 0 , 0 , 0 , 0 , time .UTC ),
2455 Env : map [string ]string {
2556 "VIRTUAL_HOST" : "bar.localhost" ,
2657 },
2758 ID : "9" ,
2859 }
2960 o1 := & context.RuntimeContainer {
61+ Created : time .Date (2021 , 1 , 2 , 0 , 0 , 10 , 0 , time .UTC ),
3062 Env : map [string ]string {
3163 "VIRTUAL_HOST" : "foo.localhost" ,
3264 },
3365 ID : "1" ,
3466 }
3567 o2 := & context.RuntimeContainer {
68+ Created : time .Date (2021 , 1 , 2 , 0 , 0 , 0 , 0 , time .UTC ),
3669 Env : map [string ]string {
3770 "VIRTUAL_HOST" : "baz.localhost" ,
3871 },
3972 ID : "3" ,
4073 }
4174 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" ,
4478 }
4579 containers := []* context.RuntimeContainer {o0 , o1 , o2 , o3 }
4680
@@ -54,6 +88,8 @@ func TestSortObjectsByKeys(t *testing.T) {
5488 {"Asc complex" , sortObjectsByKeysAsc , "Env.VIRTUAL_HOST" , []interface {}{o3 , o0 , o2 , o1 }},
5589 {"Desc simple" , sortObjectsByKeysDesc , "ID" , []interface {}{o0 , o3 , o2 , o1 }},
5690 {"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 }},
5793 } {
5894 t .Run (tc .desc , func (t * testing.T ) {
5995 got , err := tc .fn (containers , tc .key )
0 commit comments