44 "fmt"
55 "strconv"
66 "testing"
7+ "time"
78
89 "github.com/hellofresh/klepto/pkg/config"
910 "github.com/hellofresh/klepto/pkg/database"
@@ -12,6 +13,8 @@ import (
1213 "github.com/stretchr/testify/require"
1314)
1415
16+ const waitTimeout = time .Second
17+
1518func TestReadTable (t * testing.T ) {
1619 t .Parallel ()
1720
@@ -45,6 +48,12 @@ func TestReadTable(t *testing.T) {
4548 opts : reader.ReadTableOpt {},
4649 config : config.Tables {{Name : "test" , Anonymise : map [string ]string {"column_test" : "literal:Hello" }}},
4750 },
51+ {
52+ scenario : "when column anonymiser in invalid" ,
53+ function : testWhenColumnAnonymiserIsInvalid ,
54+ opts : reader.ReadTableOpt {},
55+ config : config.Tables {{Name : "test" , Anonymise : map [string ]string {"column_test" : "Hello" }}},
56+ },
4857 }
4958
5059 for _ , test := range tests {
@@ -83,10 +92,12 @@ func testWhenColumnIsAnonymised(t *testing.T, opts reader.ReadTableOpt, tables c
8392 err := anonymiser .ReadTable ("test" , rowChan , opts )
8493 require .NoError (t , err )
8594
86- for {
87- row := <- rowChan
95+ timeoutChan := time .After (waitTimeout )
96+ select {
97+ case row := <- rowChan :
8898 assert .NotEqual (t , "to_be_anonimised" , row ["column_test" ])
89- break
99+ case <- timeoutChan :
100+ assert .FailNow (t , "Failing due to timeout" )
90101 }
91102}
92103
@@ -99,10 +110,30 @@ func testWhenColumnIsAnonymisedWithLiteral(t *testing.T, opts reader.ReadTableOp
99110 err := anonymiser .ReadTable ("test" , rowChan , opts )
100111 require .NoError (t , err )
101112
102- for {
103- row := <- rowChan
113+ timeoutChan := time .After (waitTimeout )
114+ select {
115+ case row := <- rowChan :
104116 assert .Equal (t , "Hello" , row ["column_test" ])
105- break
117+ case <- timeoutChan :
118+ assert .FailNow (t , "Failing due to timeout" )
119+ }
120+ }
121+
122+ func testWhenColumnAnonymiserIsInvalid (t * testing.T , opts reader.ReadTableOpt , tables config.Tables ) {
123+ anonymiser := NewAnonymiser (& mockReader {}, tables )
124+
125+ rowChan := make (chan database.Row )
126+ defer close (rowChan )
127+
128+ err := anonymiser .ReadTable ("test" , rowChan , opts )
129+ require .NoError (t , err )
130+
131+ timeoutChan := time .After (waitTimeout )
132+ select {
133+ case row := <- rowChan :
134+ assert .Equal (t , "Invalid anonymiser: Hello" , row ["column_test" ])
135+ case <- timeoutChan :
136+ assert .FailNow (t , "Failing due to timeout" )
106137 }
107138}
108139
0 commit comments