import pytest from modules.map.species import (HIGH_GRID, LOW_GRID, cell_center, cell_id, decode_cells, decode_months, encode_cells, encode_months, mask_months, month_bit) @pytest.mark.parametrize('grid', [LOW_GRID, HIGH_GRID]) @pytest.mark.parametrize('lat,lon', [ (44.06, -121.31), (-33.86, 151.21), (0.0, 0.0), (-90.0, -180.0), (90.0, 180.0), # The north pole and the antimeridian fall in the last row/column. (89.999, 179.999), ]) def test_cell_center_contains_point(grid, lat, lon): """The center of a point's cell is within half a cell of the point (or on the world's edge, the last cell).""" center_lat, center_lon = cell_center(cell_id(lat, lon, grid), grid) assert abs(center_lat - lat) <= grid / 2 + 1e-9 assert abs(center_lon - lon) <= grid / 2 + 1e-9 def test_cell_id_neighbors(): """Cells are numbered row by row from the south-west corner.""" assert cell_id(-90, -180, LOW_GRID) == 0 assert cell_id(-90, -179.95, LOW_GRID) == 0 assert cell_id(-90, -179.85, LOW_GRID) == 1 assert cell_id(-89.85, -180, LOW_GRID) == 3600 assert cell_id(90, 180, LOW_GRID) == 1800 * 3600 - 1 @pytest.mark.parametrize('cells', [ [], [0], [5, 3, 5, 1], # Unsorted, with a duplicate. [127, 128, 16_383, 16_384, 2 ** 28 - 1], # Varint boundaries and the largest high grid cell. list(range(0, 1_000_000, 37)), ]) def test_encode_decode_cells(cells): assert decode_cells(encode_cells(cells)) == sorted(set(cells)) def test_months(): assert [month_bit(i) for i in (0, 1, 12, 13)] == [0, 1, 2048, 0] assert mask_months(month_bit(1) | month_bit(6) | month_bit(12)) == [1, 6, 12] assert mask_months(0) == [] counts = [0, 0, 3, 10, 0, 0, 0, 0, 0, 0, 1, 0] assert decode_months(encode_months(counts)) == counts assert encode_months([0] * 12) is None assert decode_months(None) == [0] * 12 assert decode_months('1,2') == [0] * 12, 'malformed'