@@ -8,48 +8,167 @@ import (
8
8
"github.com/leanovate/gopter/gen"
9
9
"github.com/leanovate/gopter/prop"
10
10
"github.com/stretchr/testify/assert"
11
+ "github.com/stretchr/testify/require"
11
12
)
12
13
13
- func TestAnonymisations (t * testing.T ) {
14
- salt := "jump"
15
- conf := & []ActionConfig {
16
- ActionConfig {
17
- Name : "nothing" ,
18
- },
19
- ActionConfig {
20
- Name : "hash" ,
21
- Salt : & salt ,
22
- },
23
- }
24
- // can't test that the functions are equal because of https://github.com/stretchr/testify/issues/182
25
- // and https://github.com/stretchr/testify/issues/159#issuecomment-99557398
26
- // will have to test that the functions return the same
27
- anons := anonymisations (conf )
28
- expectedRes , expectedErr := identity ("a" )
29
- actualRes , actualErr := anons [0 ]("a" )
30
- assert .Equal (t , expectedRes , actualRes )
31
- assert .Equal (t , expectedErr , actualErr )
32
- expectedRes , expectedErr = hash ("jump" )("a" )
33
- actualRes , actualErr = anons [1 ]("a" )
14
+ var salt = "jump"
15
+
16
+ const seed = int64 (1 )
17
+
18
+ //this is the first random salt with the seed above
19
+ const firstSalt = "5577006791947779410"
20
+
21
+ // can't test that the functions are equal because of https://github.com/stretchr/testify/issues/182
22
+ // and https://github.com/stretchr/testify/issues/159#issuecomment-99557398
23
+ // will have to test that the functions return the same
24
+ func assertAnonymisationFunction (t * testing.T , expected Anonymisation , actual Anonymisation , value string ) {
25
+ require .NotNil (t , expected )
26
+ require .NotNil (t , actual )
27
+ expectedRes , expectedErr := expected (value )
28
+ actualRes , actualErr := actual (value )
34
29
assert .Equal (t , expectedRes , actualRes )
35
30
assert .Equal (t , expectedErr , actualErr )
36
31
}
37
32
38
- func TestActionConfig (t * testing.T ) {
39
- t .Run ("saltOrRandom" , func (t * testing.T ) {
40
- t .Run ("if salt is not specified" , func (t * testing.T ) {
33
+ func TestAnonymisations (t * testing.T ) {
34
+ t .Run ("a valid configuration" , func (t * testing.T ) {
35
+ conf := & []ActionConfig {
36
+ ActionConfig {
37
+ Name : "nothing" ,
38
+ },
39
+ ActionConfig {
40
+ Name : "hash" ,
41
+ Salt : & salt ,
42
+ },
43
+ }
44
+ anons , err := anonymisations (conf )
45
+ assert .NoError (t , err )
46
+ assertAnonymisationFunction (t , identity , anons [0 ], "a" )
47
+ assertAnonymisationFunction (t , hash (salt ), anons [1 ], "a" )
48
+ })
49
+ t .Run ("an invalid configuration" , func (t * testing.T ) {
50
+ conf := & []ActionConfig {ActionConfig {Name : "year" , DateConfig : DateConfig {Format : "3333" }}}
51
+ anons , err := anonymisations (conf )
52
+ assert .Error (t , err , "should return an error" )
53
+ assert .Nil (t , anons )
54
+ })
55
+ }
56
+
57
+ func TestActionConfigSaltOrRandom (t * testing.T ) {
58
+ t .Run ("if salt is not specified" , func (t * testing.T ) {
59
+ rand .Seed (seed )
60
+ acNoSalt := ActionConfig {Name : "hash" }
61
+ assert .Equal (t , firstSalt , acNoSalt .saltOrRandom (), "should return a random salt" )
62
+ })
63
+ t .Run ("if salt is specified" , func (t * testing.T ) {
64
+ emptySalt := ""
65
+ acEmptySalt := ActionConfig {Name : "hash" , Salt : & emptySalt }
66
+ assert .Empty (t , acEmptySalt .saltOrRandom (), "should return the empty salt if empty" )
67
+
68
+ acSalt := ActionConfig {Name : "hash" , Salt : & salt }
69
+ assert .Equal (t , "jump" , acSalt .saltOrRandom (), "should return the salt" )
70
+ })
71
+ }
72
+
73
+ func TestActionConfigCreate (t * testing.T ) {
74
+ t .Run ("invalid name" , func (t * testing.T ) {
75
+ ac := ActionConfig {Name : "invalid name" }
76
+ res , err := ac .create ()
77
+ assert .Error (t , err )
78
+ assert .Nil (t , res )
79
+ })
80
+ t .Run ("identity" , func (t * testing.T ) {
81
+ ac := ActionConfig {Name : "nothing" }
82
+ res , err := ac .create ()
83
+ assert .NoError (t , err )
84
+ assertAnonymisationFunction (t , identity , res , "a" )
85
+ })
86
+ t .Run ("outcode" , func (t * testing.T ) {
87
+ ac := ActionConfig {Name : "outcode" }
88
+ res , err := ac .create ()
89
+ assert .NoError (t , err )
90
+ assertAnonymisationFunction (t , outcode , res , "a" )
91
+ })
92
+ t .Run ("hash" , func (t * testing.T ) {
93
+ t .Run ("if salt is not specified uses a random salt" , func (t * testing.T ) {
41
94
rand .Seed (1 )
42
- acNoSalt := ActionConfig {Name : "hash" }
43
- assert .Equal (t , "5577006791947779410" , acNoSalt .saltOrRandom (), "should return a random salt" )
95
+ ac := ActionConfig {Name : "hash" }
96
+ res , err := ac .create ()
97
+ assert .NoError (t , err )
98
+ assertAnonymisationFunction (t , hash (firstSalt ), res , "a" )
99
+ })
100
+ t .Run ("if salt is specified uses it" , func (t * testing.T ) {
101
+ ac := ActionConfig {Name : "hash" , Salt : & salt }
102
+ res , err := ac .create ()
103
+ assert .NoError (t , err )
104
+ assertAnonymisationFunction (t , hash (salt ), res , "a" )
105
+ })
106
+ })
107
+ t .Run ("year" , func (t * testing.T ) {
108
+ t .Run ("with an invalid format" , func (t * testing.T ) {
109
+ ac := ActionConfig {Name : "year" , DateConfig : DateConfig {Format : "11112233" }}
110
+ res , err := ac .create ()
111
+ assert .Error (t , err , "should fail" )
112
+ assert .Nil (t , res )
113
+ })
114
+ t .Run ("with a valid format" , func (t * testing.T ) {
115
+ ac := ActionConfig {Name : "year" , DateConfig : DateConfig {Format : "20060102" }}
116
+ res , err := ac .create ()
117
+ assert .NoError (t , err , "should not fail" )
118
+ y , err := year ("20060102" )
119
+ assert .NoError (t , err )
120
+ assertAnonymisationFunction (t , y , res , "21121212" )
121
+ })
122
+ })
123
+ t .Run ("ranges" , func (t * testing.T ) {
124
+ num := 2.0
125
+ output := "0-100"
126
+ t .Run ("range has at least one of lt, lte, gt, gte" , func (t * testing.T ) {
127
+ ac := ActionConfig {
128
+ Name : "ranges" ,
129
+ RangeConfig : []RangeConfig {RangeConfig {Output : & output }},
130
+ }
131
+ r , err := ac .create ()
132
+ assert .Error (t , err , "if not should return an error" )
133
+ assert .Nil (t , r )
134
+ })
135
+ t .Run ("range contains both lt and lte" , func (t * testing.T ) {
136
+ ac := ActionConfig {
137
+ Name : "ranges" ,
138
+ RangeConfig : []RangeConfig {RangeConfig {Lt : & num , Lte : & num , Output : & output }},
139
+ }
140
+ r , err := ac .create ()
141
+ assert .Error (t , err , "if not should return an error" )
142
+ assert .Nil (t , r )
143
+ })
144
+ t .Run ("range contains both gt and gte" , func (t * testing.T ) {
145
+ ac := ActionConfig {
146
+ Name : "ranges" ,
147
+ RangeConfig : []RangeConfig {RangeConfig {Gt : & num , Gte : & num , Output : & output }},
148
+ }
149
+ r , err := ac .create ()
150
+ assert .Error (t , err , "if not should return an error" )
151
+ assert .Nil (t , r )
152
+ })
153
+ t .Run ("range without output defined" , func (t * testing.T ) {
154
+ ac := ActionConfig {
155
+ Name : "ranges" ,
156
+ RangeConfig : []RangeConfig {RangeConfig {Lt : & num , Gte : & num }},
157
+ }
158
+ r , err := ac .create ()
159
+ assert .Error (t , err , "if not should return an error" )
160
+ assert .Nil (t , r )
44
161
})
45
- t .Run ("if salt is specified" , func (t * testing.T ) {
46
- emptySalt := ""
47
- acEmptySalt := ActionConfig {Name : "hash" , Salt : & emptySalt }
48
- assert .Empty (t , acEmptySalt .saltOrRandom (), "should return the empty salt if empty" )
49
-
50
- salt := "jump"
51
- acSalt := ActionConfig {Name : "hash" , Salt : & salt }
52
- assert .Equal (t , "jump" , acSalt .saltOrRandom (), "should return the salt" )
162
+ t .Run ("valid range" , func (t * testing.T ) {
163
+ rangeConfigs := []RangeConfig {RangeConfig {Lte : & num , Gte : & num , Output : & output }}
164
+ ac := ActionConfig {
165
+ Name : "ranges" ,
166
+ RangeConfig : rangeConfigs ,
167
+ }
168
+ r , err := ac .create ()
169
+ expected , _ := ranges (rangeConfigs )
170
+ assert .NoError (t , err )
171
+ assertAnonymisationFunction (t , expected , r , "2" )
53
172
})
54
173
})
55
174
}
@@ -108,7 +227,7 @@ func TestOutcode(t *testing.T) {
108
227
}
109
228
110
229
func TestYear (t * testing.T ) {
111
- f := year ("20060102" )
230
+ f , _ := year ("20060102" )
112
231
t .Run ("if the date can be parsed" , func (t * testing.T ) {
113
232
res , err := f ("20120102" )
114
233
assert .NoError (t , err , "should return no error" )
@@ -124,7 +243,7 @@ func TestRanges(t *testing.T) {
124
243
min := 0.0
125
244
max := 100.0
126
245
output := "0-100"
127
- f := ranges ([]RangeConfig {RangeConfig {Gt : & min , Lte : & max , Output : & output }})
246
+ f , _ := ranges ([]RangeConfig {RangeConfig {Gt : & min , Lte : & max , Output : & output }})
128
247
t .Run ("if the value is not a float" , func (t * testing.T ) {
129
248
res , err := f ("input" )
130
249
assert .Error (t , err , "should return an error" )
0 commit comments