|
9 | 9 | "github.com/cloudquery/plugin-sdk/v4/configtype" |
10 | 10 | "github.com/cloudquery/plugin-sdk/v4/plugin" |
11 | 11 | "github.com/invopop/jsonschema" |
| 12 | + "github.com/mitchellh/hashstructure/v2" |
12 | 13 | "github.com/stretchr/testify/require" |
13 | 14 | ) |
14 | 15 |
|
@@ -151,3 +152,63 @@ func TestTime_JSONSchema(t *testing.T) { |
151 | 152 | }) |
152 | 153 | } |
153 | 154 | } |
| 155 | + |
| 156 | +func TestTime_Hashing(t *testing.T) { |
| 157 | + hnf := func() time.Time { return time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) } |
| 158 | + cases := []struct { |
| 159 | + a configtype.Time |
| 160 | + b configtype.Time |
| 161 | + hashNowFunc func() time.Time |
| 162 | + equal bool |
| 163 | + }{ |
| 164 | + { |
| 165 | + a: func() configtype.Time { ct, _ := configtype.ParseTime("10m"); return ct }(), |
| 166 | + b: func() configtype.Time { ct, _ := configtype.ParseTime("10m"); return ct }(), |
| 167 | + hashNowFunc: hnf, |
| 168 | + equal: true, |
| 169 | + }, |
| 170 | + { |
| 171 | + a: func() configtype.Time { ct, _ := configtype.ParseTime("10m"); return ct }(), |
| 172 | + b: func() configtype.Time { ct, _ := configtype.ParseTime("10m"); return ct }(), |
| 173 | + equal: false, |
| 174 | + }, |
| 175 | + { |
| 176 | + a: func() configtype.Time { ct, _ := configtype.ParseTime("10m"); return ct }(), |
| 177 | + b: func() configtype.Time { ct, _ := configtype.ParseTime("1m"); return ct }(), |
| 178 | + equal: false, |
| 179 | + }, |
| 180 | + { |
| 181 | + a: func() configtype.Time { ct, _ := configtype.ParseTime("2021-09-01T00:00:00Z"); return ct }(), |
| 182 | + b: func() configtype.Time { ct, _ := configtype.ParseTime("2012-01-02T01:02:03Z"); return ct }(), |
| 183 | + hashNowFunc: hnf, |
| 184 | + equal: false, |
| 185 | + }, |
| 186 | + { |
| 187 | + a: func() configtype.Time { ct, _ := configtype.ParseTime("-50m30s"); return ct }(), |
| 188 | + b: func() configtype.Time { ct, _ := configtype.ParseTime("50 minutes 30 seconds ago"); return ct }(), |
| 189 | + hashNowFunc: hnf, |
| 190 | + equal: true, |
| 191 | + }, |
| 192 | + { |
| 193 | + a: func() configtype.Time { ct, _ := configtype.ParseTime("50m30s"); return ct }(), |
| 194 | + b: func() configtype.Time { ct, _ := configtype.ParseTime("50 minutes 30 seconds from now"); return ct }(), |
| 195 | + hashNowFunc: hnf, |
| 196 | + equal: true, |
| 197 | + }, |
| 198 | + } |
| 199 | + for _, tc := range cases { |
| 200 | + tc.a.SetHashNowFunc(tc.hashNowFunc) |
| 201 | + hashA, err := hashstructure.Hash(tc.a, hashstructure.FormatV2, nil) |
| 202 | + require.NoError(t, err) |
| 203 | + time.Sleep(1 * time.Second) |
| 204 | + tc.b.SetHashNowFunc(tc.hashNowFunc) |
| 205 | + hashB, err := hashstructure.Hash(tc.b, hashstructure.FormatV2, nil) |
| 206 | + require.NoError(t, err) |
| 207 | + |
| 208 | + if tc.equal { |
| 209 | + require.Equal(t, hashA, hashB) |
| 210 | + continue |
| 211 | + } |
| 212 | + require.NotEqual(t, hashA, hashB) |
| 213 | + } |
| 214 | +} |
0 commit comments