def test_render_tile_without_data(tile_generator: TileGenerator, tile_test_cases: Dict[str, dict]): """ test render tiles for tile_test_cases without using ohdm test data Arguments: tile_generator {TileGenerator} -- default TileGenerator """ for test_case in tile_test_cases: print("test: {}".format(test_case)) tile_generator.zoom = tile_test_cases[test_case]["zoom"] tile_generator.x_pixel = tile_test_cases[test_case]["x"] tile_generator.y_pixel = tile_test_cases[test_case]["y"] # generate new tile into a tmp file new_tile: SpooledTemporaryFile = SpooledTemporaryFile() new_tile.write(tile_generator.render_tile()) new_tile.seek(0) # open new tile as image new_tile_image: Image = Image.open(new_tile) # check if the tile is a PNG file assert new_tile_image.format == "PNG" # monochrome & resize images to better compare them reference_tile = Image.open( "/app/compose/local/django/test_tile/{}".format( tile_test_cases[test_case]["tile_png"])) assert ImageChops.difference(reference_tile, new_tile_image).getbbox() is None
def test_render_tile_with_data(tile_generator: TileGenerator, tile_test_cases: Dict[str, dict]): """ test render tiles for tile_test_cases with using ohdm test data Arguments: tile_generator {TileGenerator} -- default TileGenerator """ # cleanup data clear_mapnik_tables() # fill database run_import( file_path="/map.osm", db_cache_size=10000, cache2file=False, ) tile_generator.request_date = timezone.now() for test_case in tile_test_cases: # contine if test case has not test data if not tile_test_cases[test_case]["has_date_data"]: continue print("test: {}".format(test_case)) tile_generator.zoom = tile_test_cases[test_case]["zoom"] tile_generator.x_pixel = tile_test_cases[test_case]["x"] tile_generator.y_pixel = tile_test_cases[test_case]["y"] # generate new tile into a tmp file new_tile: SpooledTemporaryFile = SpooledTemporaryFile() new_tile.write(tile_generator.render_tile()) new_tile.seek(0) # open new tile as image new_tile_image: Image = Image.open(new_tile) new_tile_image.save("/app/bremen.png") # check if the tile is a PNG file assert new_tile_image.format == "PNG" # monochrome & resize images to better compare them reference_tile = Image.open( "/app/compose/local/django/test_tile/{}".format( tile_test_cases[test_case]["tile_png"])) diff: bool = ImageChops.difference(reference_tile, new_tile_image).getbbox() is None assert diff is False # cleanup data clear_mapnik_tables()
def test_success_empty_cache( self, test_tile: Dict[str, dict], tile_generator: TileGenerator ): # clear cache cache.clear() request: WSGIRequest = RequestFactory().get( self.get_path(kwargs=test_tile["data"]) ) response: HttpResponse = generate_tile( request=request, year=test_tile["data"]["year"], month=test_tile["data"]["month"], day=test_tile["data"]["day"], zoom=test_tile["data"]["zoom"], x_pixel=test_tile["data"]["x_pixel"], y_pixel=test_tile["data"]["y_pixel"], ) tile_cache: Optional[dict] = cache.get(test_tile["cache"]["cache_key"]) if tile_cache is None: raise AssertionError tile: Optional[bytes] = cache.get(tile_cache["tile_hash"]) tile_generator.zoom = test_tile["data"]["zoom"] tile_generator.x_pixel = test_tile["data"]["x_pixel"] tile_generator.y_pixel = test_tile["data"]["y_pixel"] # check if the right tile was rendert if response.content != tile_generator.render_tile(): raise AssertionError # check if the cache was setup right if hashlib.md5(response.content).hexdigest() != tile_cache["tile_hash"]: raise AssertionError if response.content != tile: raise AssertionError if not isinstance(response.content, bytes): raise AssertionError if response.status_code != 200: raise AssertionError if response["content-type"] != "image/jpeg": raise AssertionError