def test_wms_background_fetch_epsg4326(self): width = 240 height = 200 bbox = [-9072563.021775628, -9043899.136168687, 1492394.0457582686, 1507681.4514153039, "EPSG:3857"] try: image = GenericWMSBackground(thumbnail_width=width, thumbnail_height=height).fetch(bbox) except UnidentifiedImageError as e: logger.error(f"It was not possible to fetch the background: {e}") return expected_image = Image.open(f"{EXPECTED_RESULTS_DIR}background/wms_4326.png") diff = Image.new("RGB", image.size) mismatch = pixelmatch(image, expected_image, diff) if mismatch >= expected_image.size[0] * expected_image.size[1] * 0.01: logger.warn("Mismatch, it was not possible to bump the bg!") # Sometimes this test fails to fetch the OSM background with tempfile.NamedTemporaryFile(dir='/tmp', suffix='.png', delete=False) as tmpfile: logger.error(f"Dumping image to: {tmpfile.name}") image.save(tmpfile) # Let's check that the thumb is valid at least with Image.open(tmpfile) as img: img.verify() with tempfile.NamedTemporaryFile(dir='/tmp', suffix='.png', delete=False) as tmpfile: logger.error(f"Dumping diff to: {tmpfile.name}") diff.save(tmpfile) # Let's check that the thumb is valid at least with Image.open(tmpfile) as img: img.verify() else: self.assertTrue( mismatch < width * height * 0.01, "Expected test and pre-generated backgrounds to differ up to 1%" )
def test_wms_background_retries(self, request_mock): request_mock.return_value = (None, None) width = 240 height = 200 max_retries = 3 retry_delay = 1 bbox = [ -9072563.021775628, -9043899.136168687, 1492394.0457582686, 1507681.4514153039, "EPSG:4326" ] start = datetime.now() with self.assertRaises(UnidentifiedImageError): GenericWMSBackground(thumbnail_width=width, thumbnail_height=height, max_retries=max_retries, retry_delay=retry_delay).fetch(bbox) end = datetime.now() if request_mock.call_count: self.assertEqual( request_mock.call_count, max_retries, f"Expected to {max_retries} number of failing fetches") self.assertGreaterEqual( (end - start).seconds, max_retries * retry_delay - 1, "Expected delay between consecutive failing fetches")