Spaces:
Sleeping
Sleeping
File size: 5,255 Bytes
4bcfb6c d87ef07 4bcfb6c 859e75b 4bcfb6c 6af1379 d87ef07 859e75b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
import pytest
from pathlib import Path
from input_handling import is_valid_email, is_valid_number
from input_handling import get_image_datetime, get_image_latlon
# generate tests for is_valid_email
# - test with valid email
# - basic email with @ and .
# - test with email with multiple .
# - test with empty email
# - test with None email
# - test with non-string email
# - test with invalid email
# - test with email without @
# - test with email without .
# - test with email without domain
# - test with email without username
# - test with email without TLD
# - test with email with multiple @
# - test with email starting with the + sign
def test_is_valid_email_valid():
assert is_valid_email("[email protected]")
assert is_valid_email("[email protected]")
assert is_valid_email("[email protected]")
assert is_valid_email("[email protected]")
assert is_valid_email("[email protected]")
def test_is_valid_email_empty():
assert not is_valid_email("")
def test_is_valid_email_none():
with pytest.raises(TypeError):
is_valid_email(None)
def test_is_valid_email_non_string():
with pytest.raises(TypeError):
is_valid_email(123)
def test_is_valid_email_invalid():
assert not is_valid_email("a.bc")
assert not is_valid_email("a@bc")
assert not is_valid_email("a.b@cc")
assert not is_valid_email("@b.cc")
assert not is_valid_email("[email protected]")
assert not is_valid_email("a@b.")
assert not is_valid_email("a@bb.")
assert not is_valid_email("[email protected].")
assert not is_valid_email("a@[email protected]")
# not sure how xfails come through the CI pipeline yet.
# maybe better to just comment out this stuff until pipeline is setup, then can check /extend
@pytest.mark.xfail(reason="Bug identified, but while setting up CI having failing tests causes more headache")
def test_is_valid_email_invalid_plus():
assert not is_valid_email("[email protected]")
assert not is_valid_email("[email protected]")
def test_is_valid_number_valid():
# with a sign or without, fractional or integer are all valid
assert is_valid_number("123")
assert is_valid_number("123.456")
assert is_valid_number("-123")
assert is_valid_number("-123.456")
assert is_valid_number("+123")
assert is_valid_number("+123.456")
def test_is_valid_number_empty():
assert not is_valid_number("")
def test_is_valid_number_none():
with pytest.raises(TypeError):
is_valid_number(None)
def test_is_valid_number_invalid():
# func should return False for strings that are not numbers
assert not is_valid_number("abc")
assert not is_valid_number("123abc")
assert not is_valid_number("abc123")
assert not is_valid_number("123.456.789")
assert not is_valid_number("123,456")
assert not is_valid_number("123-456")
assert not is_valid_number("123+456")
def test_is_valid_number_valid():
assert is_valid_number("123")
assert is_valid_number("123.456")
assert is_valid_number("-123")
assert is_valid_number("-123.456")
assert is_valid_number("+123")
assert is_valid_number("+123.456")
def test_is_valid_number_empty():
assert not is_valid_number("")
def test_is_valid_number_none():
with pytest.raises(TypeError):
is_valid_number(None)
def test_is_valid_number_invalid():
assert not is_valid_number("abc")
assert not is_valid_number("123abc")
assert not is_valid_number("abc123")
assert not is_valid_number("123.456.789")
assert not is_valid_number("123,456")
assert not is_valid_number("123-456")
assert not is_valid_number("123+456")
# tests for get_image_datetime
# - testing with a valid image with complete, valid metadata
# - testing with a valid image with incomplete metadata (missing datetime info -- that's a legitimate case we should handle)
# - testing with a valid image with incomplete metadata (missing GPS info -- should not affect the datetime extraction)
# - testing with a valid image with no metadata
# - timezones too
test_data_pth = Path('tests/data/')
def test_get_image_datetime():
# this image has lat, lon, and datetime
f1 = test_data_pth / 'cakes.jpg'
assert get_image_datetime(f1) == "2024:10:24 15:59:45"
#"+02:00"
# hmm, the full datetime requires timezone, which is called OffsetTimeOriginal
# missing GPS loc: this should not interfere with the datetime
f2 = test_data_pth / 'cakes_no_exif_gps.jpg'
assert get_image_datetime(f2) == "2024:10:24 15:59:45"
# missng datetime -> expect None
f3 = test_data_pth / 'cakes_no_exif_datetime.jpg'
assert get_image_datetime(f3) == None
def test_get_image_latlon():
# this image has lat, lon, and datetime
f1 = test_data_pth / 'cakes.jpg'
assert get_image_latlon(f1) == (46.51860277777778, 6.562075)
# missing GPS loc
f2 = test_data_pth / 'cakes_no_exif_gps.jpg'
assert get_image_latlon(f2) == None
# missng datetime -> expect gps not affected
f3 = test_data_pth / 'cakes_no_exif_datetime.jpg'
assert get_image_latlon(f3) == (46.51860277777778, 6.562075)
# tests for get_image_latlon with empty file
def test_get_image_latlon_empty():
assert get_image_latlon("") == None
|