rmm commited on
Commit
a847100
·
1 Parent(s): 99171fa

test: add test for equality of InputObservation objects

Browse files

- this comparison is quite important in the workflow logic since it
guards against overwriting the observations on re-runs

Files changed (1) hide show
  1. tests/test_input_observation.py +65 -0
tests/test_input_observation.py CHANGED
@@ -198,3 +198,68 @@ def test_input_observation_invalid(key, error_type, mock_uploadedFile):
198
  inputs[key] = None
199
  with pytest.raises(error_type):
200
  obs = InputObservation(**inputs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  inputs[key] = None
199
  with pytest.raises(error_type):
200
  obs = InputObservation(**inputs)
201
+
202
+ # we can take a similar approach to test equality.
203
+ # here, construct two dicts, each with valid inputs but all elements different.
204
+ # loop over the keys, and construct two InputObservations that differ on that key only.
205
+ # asser the expected output message.
206
+ # ah, it is the diff func that prints a message. Here we just assert boolean.
207
+
208
+ # we currently expect differences on time to be ignored.
209
+ inequality_keys = [
210
+ ("author_email", False),
211
+ ("uploaded_file", False),
212
+ ("date", False),
213
+ #("time", True),
214
+ pytest.param("time", False, marks=pytest.mark.xfail(reason="Time is currently ignored in __eq__")),
215
+ ("image", False),
216
+ ("image_md5", False),
217
+ ]
218
+ @pytest.mark.parametrize("key, expect_equality", inequality_keys)
219
+ def test_input_observation_equality(key, expect_equality, mock_uploadedFile):
220
+
221
+ # set up the two sets of good inputs
222
+ _date1 = "2023-10-10"
223
+ _time1 = "10:10:10"
224
+ image_datetime_raw1 = _date1 + " " + _time1
225
+ fname1 = "test_image.jpg"
226
+ image1 = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
227
+ dt1 = datetime.datetime.strptime(image_datetime_raw1, "%Y-%m-%d %H:%M:%S")
228
+
229
+ _date2 = "2023-10-11"
230
+ _time2 = "12:13:14"
231
+ image_datetime_raw2 = _date2 + " " + _time2
232
+ fname2 = "test_image.jpg"
233
+ image2 = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
234
+ dt2 = datetime.datetime.strptime(image_datetime_raw2, "%Y-%m-%d %H:%M:%S")
235
+ valid_inputs1 = {
236
+ "author_email": "[email protected]",
237
+ #"image_name": "test_image.jpg",
238
+ "uploaded_file": mock_uploadedFile(name=fname1).get_data(),
239
+ "date": dt1.date(),
240
+ "time": dt1.time(),
241
+ "image": image1,
242
+ "image_md5": 'd1d2515e6f6ac4c5ca6dd739d5143cd4', # 32 hex chars.
243
+ }
244
+
245
+ valid_inputs2 = {
246
+ "author_email": "[email protected]",
247
+ #"image_name": "another.jpg",
248
+ "uploaded_file": mock_uploadedFile(name=fname2).get_data(),
249
+ "date": dt2.date(),
250
+ "time": dt2.time(),
251
+ "image": image2,
252
+ "image_md5": 'cdb235587bdee5915d6ccfa52ca9f3ac', # 32 hex chars.
253
+ }
254
+
255
+ nearly_same_inputs = valid_inputs1.copy()
256
+ nearly_same_inputs[key] = valid_inputs2[key]
257
+ obs1 = InputObservation(**valid_inputs1)
258
+ obs2 = InputObservation(**nearly_same_inputs)
259
+
260
+ if expect_equality is True:
261
+ assert obs1 == obs2
262
+ else:
263
+ assert obs1 != obs2
264
+
265
+