|
| 1 | +import os |
| 2 | +import re |
| 3 | +import glob |
| 4 | +import textwrap |
| 5 | +import urllib.request |
| 6 | + |
| 7 | +import imgspy |
| 8 | + |
| 9 | + |
| 10 | +BASEDIR = os.path.dirname(os.path.abspath(__file__)) |
| 11 | +FORMAT = r'sample(?P<width>\d+)x(?P<height>\d+)(?P<comment>[^.]*).(?P<format>\w+)' |
| 12 | + |
| 13 | + |
| 14 | +def test_samples(): |
| 15 | + for filepath in glob.glob(os.path.join(BASEDIR, 'fixtures/sample*')): |
| 16 | + filename = os.path.basename(filepath) |
| 17 | + match = re.match(FORMAT, filename) |
| 18 | + expected = { |
| 19 | + 'type': match.group('format'), |
| 20 | + 'width': int(match.group('width')), |
| 21 | + 'height': int(match.group('height'))} |
| 22 | + |
| 23 | + actual = imgspy.info(open(filepath, 'rb')) |
| 24 | + assert isinstance(actual, dict), filename |
| 25 | + |
| 26 | + actual_subset = {k: v for k, v in actual.items() if k in expected} |
| 27 | + assert actual_subset == expected, filename |
| 28 | + |
| 29 | + |
| 30 | +def test_datastr(): |
| 31 | + data = textwrap.dedent('''data:image/png;base64, |
| 32 | + iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAYAAAD0In+ |
| 33 | + KAAAAD0lEQVR42mNk+M9QzwAEAAmGAYCF+yOnAAAAAElFTkSuQmCC''') |
| 34 | + assert imgspy.info(data) == {'type': 'png', 'width': 2, 'height': 1} |
| 35 | + |
| 36 | + |
| 37 | +def test_url(): |
| 38 | + domain = 'http://via.placeholder.com' |
| 39 | + urls = { |
| 40 | + domain + '/500x500.png': {'type': 'png', 'width': 500, 'height': 500}, |
| 41 | + domain + '/500x500.jpg': {'type': 'jpg', 'width': 500, 'height': 500},} |
| 42 | + for url, expected in urls.items(): |
| 43 | + assert imgspy.info(urllib.request.urlopen(url)) == expected |
0 commit comments