def test_read_with_zinc_and_id(mock) -> None: # GIVEN """ Args: mock: """ envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} mock.return_value = ping._PingGrid mime_type = shaystack.MODE_ZINC request = HaystackHttpRequest() grid = shaystack.Grid(columns=['id']) grid.append({"id": Ref("me")}) request.headers["Content-Type"] = mime_type request.headers["Accept"] = mime_type request.body = shaystack.dump(grid, mode=mime_type) # WHEN response = shaystack.read(envs, request, "dev") # THEN ids = [Ref("me")] mock.assert_called_once_with(0, None, ids, '', None) assert response.status_code == 200 assert response.headers["Content-Type"].startswith(mime_type) assert shaystack.parse(response.body, mime_type) is not None
def test_negociation_without_accept() -> None: # GIVEN envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} mime_type = shaystack.MODE_CSV request = HaystackHttpRequest() grid = Grid(columns={'filter': {}, 'limit': {}}) grid.append({'filter': '', 'limit': -1}) del request.headers["Accept"] request.headers["Content-Type"] = mime_type request.body = shaystack.dump(grid, mode=mime_type) # WHEN try: shaystack.read(envs, request, "dev") assert False, "Must generate exception" except HttpError as ex: assert ex.error == 406
def test_negociation_json_without_content_type() -> None: # GIVEN envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} mime_type = shaystack.MODE_JSON request = HaystackHttpRequest() grid: Grid = Grid(columns={'id': {}}) request.headers["Accept"] = mime_type del request.headers["Content-Type"] request.body = shaystack.dump(grid, mode=mime_type) # WHEN response = shaystack.read(envs, request, "dev") # THEN assert response.status_code == 200 assert response.headers["Content-Type"].startswith(mime_type) shaystack.parse(response.body, mime_type)
def test_negociation_with_complex_accept() -> None: # GIVEN envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} mime_type = shaystack.MODE_ZINC request = HaystackHttpRequest() grid = Grid(columns={'filter': {}, 'limit': {}}) grid.append({'filter': '', 'limit': -1}) request.headers["Accept"] = "text/json;q=0.9,text/zinc;q=1" request.headers["Content-Type"] = mime_type request.body = shaystack.dump(grid, mode=mime_type) # WHEN response = shaystack.read(envs, request, "dev") # THEN assert response.status_code == 200 assert response.headers["Content-Type"].startswith(mime_type)
def test_negociation_json_with_unknown_content_type() -> None: # GIVEN envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} mime_type = shaystack.MODE_ZINC request = HaystackHttpRequest() grid: Grid = Grid(columns={'id': {}}) request.headers["Accept"] = mime_type request.headers["Content-Type"] = "text/html" request.body = shaystack.dump(grid, mode=mime_type) # WHEN response = shaystack.read(envs, request, "dev") # THEN assert response.status_code == 406 assert response.headers["Content-Type"].startswith(mime_type) error_grid: Grid = shaystack.parse(response.body, mime_type) assert "err" in error_grid.metadata
def test_read_with_arg_and_id(mock) -> None: # GIVEN """ Args: mock: """ envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} mock.return_value = ping._PingGrid mime_type = DEFAULT_MIME_TYPE request = HaystackHttpRequest() request.args["id"] = Ref("me").name # WHEN response = shaystack.read(envs, request, "dev") # THEN ids = [Ref("me")] mock.assert_called_once_with(0, None, ids, '', None) assert response.status_code == 200 assert response.headers["Content-Type"].startswith(mime_type) assert shaystack.parse(response.body, mime_type) is not None
def test_read_with_arg_and_filter(mock) -> None: # GIVEN """ Args: mock: """ envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} mock.return_value = ping._PingGrid mime_type = DEFAULT_MIME_TYPE request = HaystackHttpRequest() request.args["filter"] = "id==@me" request.args["limit"] = "1" # WHEN response = shaystack.read(envs, request, "dev") # THEN mock.assert_called_once_with(1, None, None, 'id==@me', None) assert response.status_code == 200 assert response.headers["Content-Type"].startswith(mime_type) read_grid: Grid = shaystack.parse(response.body, mime_type) assert not read_grid
def test_negociation_with_zinc(no_cache) -> None: # GIVEN """ Args: no_cache: """ envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} no_cache.return_value = True mime_type = shaystack.MODE_ZINC request = HaystackHttpRequest() grid = Grid(columns={'filter': {}, "limit": {}}) grid.append({"filter": "id==@me", "limit": 1}) request.headers["Content-Type"] = mime_type request.headers["Accept"] = mime_type request.body = shaystack.dump(grid, mode=mime_type) # WHEN response = shaystack.read(envs, request, "dev") # THEN assert response.status_code == 200 assert response.headers["Content-Type"].startswith(mime_type) shaystack.parse(response.body, shaystack.MODE_ZINC)
def test_negociation_with_navigator_accept() -> None: # GIVEN envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} mime_type = shaystack.MODE_CSV request = HaystackHttpRequest() grid = Grid(columns={'filter': {}, 'limit': {}}) grid.append({'filter': '', 'limit': -1}) request.headers[ "Accept"] = "Accept:text/html,application/xhtml+xml," \ "application/xml;q=0.9," \ "image/webp,image/apng," \ "*/*;q=0.8," \ "application/signed-exchange;v=b3;q=0.9" request.headers["Content-Type"] = mime_type request.body = shaystack.dump(grid, mode=mime_type) # WHEN response = shaystack.read(envs, request, "dev") # THEN assert response.status_code == 200 assert response.headers["Content-Type"].startswith(mime_type) shaystack.parse(response.body, mime_type)
def test_read_with_zinc_and_filter(mock) -> None: # GIVEN """ Args: mock: """ envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} mock.return_value = ping._PingGrid mime_type = shaystack.MODE_ZINC request = HaystackHttpRequest() grid = shaystack.Grid(columns={'filter': {}, "limit": {}}) grid.append({"filter": "id==@me", "limit": 1}) request.headers["Content-Type"] = mime_type request.headers["Accept"] = mime_type request.body = shaystack.dump(grid, mode=shaystack.MODE_ZINC) # WHEN response = shaystack.read(envs, request, "dev") # THEN mock.assert_called_once_with(1, None, None, 'id==@me', None) assert response.status_code == 200 assert response.headers["Content-Type"].startswith(mime_type) assert not shaystack.parse(response.body, mime_type)