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_get_bbox_test_cases(tile_generator: TileGenerator, tile_test_cases: Dict[str, dict]): """ test get_bbox for cases and all tiles between zoomlevel 0 to 10 Arguments: tile_generator {TileGenerator} -- default TileGenerator tile_test_cases {Dict[str, dict]} -- tile test cases """ # test special cases for test_case in tile_test_cases: 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"] box2d: Box2d = tile_generator.get_bbox() if not isinstance(box2d, Box2d): raise AssertionError if "{}".format(box2d.maxx) != tile_test_cases[test_case]["maxx"]: raise AssertionError if "{}".format(box2d.maxy) != tile_test_cases[test_case]["maxy"]: raise AssertionError if "{}".format(box2d.minx) != tile_test_cases[test_case]["minx"]: raise AssertionError if "{}".format(box2d.miny) != tile_test_cases[test_case]["miny"]: raise AssertionError
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_get_bbox_zoom_0_10(tile_generator: TileGenerator): """ test get_bbox for all tiles between zoomlevel 0 to 10 Arguments: tile_generator {TileGenerator} -- default TileGenerator """ # test all tiles for zoom level 0 to 10 tile_generator.get_bbox() for zoom in range(10): tile_generator.zoom = zoom for x in range(int(pow(2, zoom))): for y in range(int(pow(2, zoom))): tile_generator.x_pixel = x tile_generator.y_pixel = y assert isinstance(tile_generator.get_bbox(), Box2d)
def test_get_bbox_random_zoom_10_20(tile_generator: TileGenerator): """ test get_bbox for random 1000 tiles each zoom level Arguments: tile_generator {TileGenerator} -- default TileGenerator """ for zoom in range(10, 20): tile_generator.zoom = zoom coordinate_limit: int = int(pow(2, zoom)) for _ in range(100): x: int = randrange(coordinate_limit) for _ in range(100): y: int = randrange(coordinate_limit) tile_generator.x_pixel = x tile_generator.y_pixel = y assert isinstance(tile_generator.get_bbox(), Box2d)
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