@@ -190,3 +190,53 @@ func TestParsingDockerImage(t *testing.T) {
190
190
}
191
191
})
192
192
}
193
+
194
+ func TestLatestSnapshot (t * testing.T ) {
195
+ t .Run ("Test selecting the latest snapshot ID" , func (t * testing.T ) {
196
+ dateTime := time .Date (2023 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )
197
+
198
+ testCases := []struct {
199
+ snapshots []resources.Snapshot
200
+ expectedID string
201
+ err error
202
+ }{
203
+ {
204
+ err : errors .New ("no snapshots available" ),
205
+ },
206
+ {
207
+ snapshots : []resources.Snapshot {
208
+ {ID : "test1" , CreatedAt : dateTime },
209
+ {ID : "test2" , CreatedAt : dateTime .Add (time .Hour )},
210
+ {ID : "test3" , CreatedAt : dateTime .Add (- time .Hour )},
211
+ },
212
+ expectedID : "test2" ,
213
+ },
214
+ {
215
+ snapshots : []resources.Snapshot {
216
+ {ID : "test1" , DataStateAt : dateTime },
217
+ {ID : "test2" , DataStateAt : dateTime .Add (time .Hour )},
218
+ {ID : "test3" , DataStateAt : dateTime .Add (2 * time .Hour )},
219
+ },
220
+ expectedID : "test3" ,
221
+ },
222
+ {
223
+ snapshots : []resources.Snapshot {
224
+ {ID : "test1" , CreatedAt : dateTime , DataStateAt : dateTime },
225
+ {ID : "test2" , CreatedAt : dateTime .Add (time .Hour ), DataStateAt : dateTime .Add (time .Hour )},
226
+ {ID : "test3" , CreatedAt : dateTime .Add (- time .Hour ), DataStateAt : dateTime .Add (2 * time .Hour )},
227
+ },
228
+ expectedID : "test3" ,
229
+ },
230
+ }
231
+
232
+ for _ , tc := range testCases {
233
+ latest , err := getLatestSnapshot (tc .snapshots )
234
+ if err != nil {
235
+ assert .EqualError (t , err , tc .err .Error ())
236
+ continue
237
+ }
238
+
239
+ assert .Equal (t , tc .expectedID , latest .ID )
240
+ }
241
+ })
242
+ }
0 commit comments