Spaces:
Sleeping
Sleeping
rmm
commited on
Commit
·
3380012
1
Parent(s):
bb96fca
test: InputObservation with valid and invalid inputs
Browse files- tests/test_input_observation.py +133 -0
tests/test_input_observation.py
CHANGED
|
@@ -65,3 +65,136 @@ def test_mock_uploaded_file(mock_uploadedFile):
|
|
| 65 |
assert mock_file.name == image_name
|
| 66 |
assert mock_file.size == 123456
|
| 67 |
assert mock_file.type == "image/jpeg"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
assert mock_file.name == image_name
|
| 66 |
assert mock_file.size == 123456
|
| 67 |
assert mock_file.type == "image/jpeg"
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# now we move on to test the class InputObservation
|
| 71 |
+
# - with valid input
|
| 72 |
+
# - with invalid input
|
| 73 |
+
# - with missing input
|
| 74 |
+
|
| 75 |
+
def test_input_observation_valid(mock_uploadedFile):
|
| 76 |
+
# image: ndarray
|
| 77 |
+
# lat, lon: float
|
| 78 |
+
# author_email: str
|
| 79 |
+
# date, time: datetime.date, datetime.time
|
| 80 |
+
#uploaded_file: UploadedFile (need to mock this)
|
| 81 |
+
# image_md5: str
|
| 82 |
+
|
| 83 |
+
# setup values for the test (all valid)
|
| 84 |
+
|
| 85 |
+
author_email = "test@example.com"
|
| 86 |
+
image_name = "test_image.jpg"
|
| 87 |
+
mock_file = mock_uploadedFile(name=image_name).get_data()
|
| 88 |
+
|
| 89 |
+
_date="2023-10-10"
|
| 90 |
+
_time="10:10:10"
|
| 91 |
+
image_datetime_raw = _date + " " + _time
|
| 92 |
+
dt = datetime.datetime.strptime(image_datetime_raw, "%Y-%m-%d %H:%M:%S")
|
| 93 |
+
date = dt.date()
|
| 94 |
+
time = dt.time()
|
| 95 |
+
|
| 96 |
+
## make a random image with dtype uint8 using np.random.randint
|
| 97 |
+
image = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
|
| 98 |
+
image_md5 = 'd1d2515e6f6ac4c5ca6dd739d5143cd4' # 32 hex chars.
|
| 99 |
+
|
| 100 |
+
obs = InputObservation(
|
| 101 |
+
image=image,
|
| 102 |
+
latitude=12.34, longitude=56.78, author_email=author_email,
|
| 103 |
+
time=time, date=date,
|
| 104 |
+
uploaded_file=mock_file,
|
| 105 |
+
image_md5=image_md5,
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
assert isinstance(obs.image, np.ndarray)
|
| 109 |
+
assert (obs.image == image).all()
|
| 110 |
+
|
| 111 |
+
assert obs.latitude == 12.34
|
| 112 |
+
assert obs.longitude == 56.78
|
| 113 |
+
assert obs.author_email == author_email
|
| 114 |
+
assert isinstance(obs.date, datetime.date)
|
| 115 |
+
assert isinstance(obs.time, datetime.time)
|
| 116 |
+
assert str(obs.date) == "2023-10-10"
|
| 117 |
+
assert str(obs.time) == "10:10:10"
|
| 118 |
+
|
| 119 |
+
assert obs.uploaded_file.name == image_name
|
| 120 |
+
assert obs.uploaded_file.size == 123456
|
| 121 |
+
assert obs.uploaded_file.type == "image/jpeg"
|
| 122 |
+
|
| 123 |
+
assert isinstance(obs.uploaded_file, BytesIO)
|
| 124 |
+
#assert isinstance(obs.uploaded_file, MockUploadedFile) # is there any point in checking the type of the mock, ?
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
# a list of tuples (strings that are the keys of "valid_inputs", expected error type)
|
| 128 |
+
# loop over the list, and for each tuple, create a dictionary with all valid inputs, and one invalid input
|
| 129 |
+
# assert that the function raises the expected error type
|
| 130 |
+
|
| 131 |
+
invalid_input_scenarios = [
|
| 132 |
+
("author_email", TypeError),
|
| 133 |
+
("image_name", TypeError),
|
| 134 |
+
("uploaded_file", TypeError),
|
| 135 |
+
("date", TypeError),
|
| 136 |
+
("time", TypeError),
|
| 137 |
+
("image", TypeError),
|
| 138 |
+
("image_md5", TypeError),
|
| 139 |
+
]
|
| 140 |
+
|
| 141 |
+
@pytest.mark.parametrize("key, error_type", invalid_input_scenarios)
|
| 142 |
+
def test_input_observation_invalid(key, error_type, mock_uploadedFile):
|
| 143 |
+
# correct datatypes are:
|
| 144 |
+
# - image: ndarray
|
| 145 |
+
# - lat, lon: float
|
| 146 |
+
# - author_email: str
|
| 147 |
+
# - date, time: datetime.date, datetime.time
|
| 148 |
+
# - uploaded_file: UploadedFile (need to mock this)
|
| 149 |
+
# - image_md5: str
|
| 150 |
+
|
| 151 |
+
# the most critical/likely to go wrong would presumably be
|
| 152 |
+
# - date, time (strings not datetime objects)
|
| 153 |
+
# - lat, lon (strings not numbers)
|
| 154 |
+
# - image (not ndarray, maybe accidentally a PIL object or maybe the filename)
|
| 155 |
+
# - uploaded_file (not UploadedFile, maybe a string, or maybe the ndarray)
|
| 156 |
+
|
| 157 |
+
# check it fails when any of the datatypes are wrong,
|
| 158 |
+
# even if the rest are all good want to loop over the inputs, take each one
|
| 159 |
+
# from a bad list, and all others from a good list, and assert fails for
|
| 160 |
+
# each one
|
| 161 |
+
|
| 162 |
+
# set up the good and bad inputs
|
| 163 |
+
_date="2023-10-10"
|
| 164 |
+
_time="10:10:10"
|
| 165 |
+
image_datetime_raw = _date + " " + _time
|
| 166 |
+
fname = "test_image.jpg"
|
| 167 |
+
image = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
|
| 168 |
+
|
| 169 |
+
dt_ok = datetime.datetime.strptime(image_datetime_raw, "%Y-%m-%d %H:%M:%S")
|
| 170 |
+
valid_inputs = {
|
| 171 |
+
"author_email": "test@example.com",
|
| 172 |
+
"image_name": "test_image.jpg",
|
| 173 |
+
"uploaded_file": mock_uploadedFile(name=fname).get_data(),
|
| 174 |
+
"date": dt_ok.date(),
|
| 175 |
+
"time": dt_ok.time(),
|
| 176 |
+
"image": image,
|
| 177 |
+
"image_md5": 'd1d2515e6f6ac4c5ca6dd739d5143cd4', # 32 hex chars.
|
| 178 |
+
}
|
| 179 |
+
invalid_inputs = {
|
| 180 |
+
"author_email": "@example",
|
| 181 |
+
"image_name": 45,
|
| 182 |
+
"uploaded_file": image,
|
| 183 |
+
"date": _date,
|
| 184 |
+
"time": _time,
|
| 185 |
+
"image": fname,
|
| 186 |
+
"image_md5": 45643
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
# test a valid set of inputs, minus the target key, substituted for something invalid
|
| 190 |
+
inputs = valid_inputs.copy()
|
| 191 |
+
inputs[key] = invalid_inputs[key]
|
| 192 |
+
|
| 193 |
+
with pytest.raises(error_type):
|
| 194 |
+
obs = InputObservation(**inputs)
|
| 195 |
+
|
| 196 |
+
# now test the same key set to None
|
| 197 |
+
inputs = valid_inputs.copy()
|
| 198 |
+
inputs[key] = None
|
| 199 |
+
with pytest.raises(error_type):
|
| 200 |
+
obs = InputObservation(**inputs)
|