def test_invoke_action_with_zinc(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(metadata={ 'id': Ref('123'), 'action': 'doIt' }, columns={ 'key': {}, 'value': {} }) grid.append({'param': 'value'}) request.headers["Content-Type"] = mime_type request.headers["Accept"] = mime_type request.body = shaystack.dump(grid, mode=shaystack.MODE_ZINC) # WHEN response = shaystack.invoke_action(envs, request, "dev") # THEN mock.assert_called_once_with(Ref("123"), "doIt", {}) assert response.status_code == 200 assert response.headers["Content-Type"].startswith(mime_type) assert shaystack.parse(response.body, shaystack.MODE_ZINC) is not None
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_point_write_write_with_zinc(mock) -> None: # GIVEN """ Args: mock: """ envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} mock.return_value = Grid(version=VER_3_0, columns=["level", "levelDis", "val", "who"]) mime_type = shaystack.MODE_ZINC request = HaystackHttpRequest() grid = shaystack.Grid(columns=['id', "level", "val", "who", "duration"]) grid.append({ "id": Ref("1234"), "level": 1, "val": 100.0, "who": "PPR", "duration": Quantity(1, "min") }) request.headers["Content-Type"] = mime_type request.headers["Accept"] = mime_type request.body = shaystack.dump(grid, mode=mime_type) # WHEN response = shaystack.point_write(envs, request, "dev") # THEN mock.assert_called_once_with(Ref("1234"), 1, 100, "PPR", Quantity(1, "min"), 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_his_read_with_range_one_datetime(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', 'range']) datetime_1 = datetime(2020, 1, 1, 0, 0, 0, tzinfo=pytz.UTC) grid.append({"id": Ref("1234"), "range": datetime_1.isoformat()}) request.headers["Content-Type"] = mime_type request.headers["Accept"] = mime_type request.body = shaystack.dump(grid, mode=mime_type) # WHEN response = shaystack.his_read(envs, request, "dev") # THEN cur_datetime = datetime.combine(datetime_1, datetime.min.time()).replace(tzinfo=pytz.UTC) mock.assert_called_once_with(Ref("1234"), (cur_datetime, datetime.max.replace(tzinfo=pytz.UTC) ), 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_watch_poll_with_zinc(mock) -> None: # GIVEN """ Args: mock: """ envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} mock.return_value = ping._PingGrid mime_type = shaystack.MODE_ZINC request = HaystackHttpRequest() grid: Grid = shaystack.Grid( metadata={'watchId': "0123456789ABCDEF", 'refresh': MARKER }, columns=["empty"]) grid.append({}) request.headers["Content-Type"] = mime_type request.headers["Accept"] = mime_type request.body = shaystack.dump(grid, mode=shaystack.MODE_ZINC) # WHEN response = shaystack.watch_poll(envs, request, "dev") # THEN mock.assert_called_once_with("0123456789ABCDEF", True) assert response.status_code == 200 assert response.headers["Content-Type"].startswith(mime_type) assert shaystack.parse(response.body, shaystack.MODE_ZINC) is not None
def test_watch_unsub_with_zinc(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(metadata={"close": True, "watchId": "0123456789ABCDEF"}, columns=['id']) grid.append({"id": Ref("id1")}) grid.append({"id": Ref("id2")}) request.headers["Content-Type"] = mime_type request.headers["Accept"] = mime_type request.body = shaystack.dump(grid, mode=shaystack.MODE_ZINC) # WHEN response = shaystack.watch_unsub(envs, request, "dev") # THEN mock.assert_called_once_with("0123456789ABCDEF", [Ref("id1"), Ref("id2")], True) assert response.status_code == 200 assert response.headers["Content-Type"].startswith(mime_type) assert shaystack.parse(response.body, shaystack.MODE_ZINC) is not None
def test_his_read_with_range_today(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', 'range']) grid.append({"id": Ref("1234"), "range": "today"}) request.headers["Content-Type"] = mime_type request.headers["Accept"] = mime_type request.body = shaystack.dump(grid, mode=mime_type) # WHEN response = shaystack.his_read(envs, request, "dev") # THEN today = datetime.combine(date.today(), datetime.min.time()).replace(tzinfo=get_localzone()) mock.assert_called_once_with(Ref("1234"), (today, today + timedelta(days=1) ), 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_his_write_with_args(mock): # GIVEN """ Args: mock: """ envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} time_serie = [ (datetime(2020, 1, 1, tzinfo=pytz.utc).isoformat() + " UTC", 100), (datetime(2020, 1, 2, tzinfo=pytz.utc).isoformat() + " UTC", 200)] mock.return_value = ping._PingGrid mime_type = DEFAULT_MIME_TYPE request = HaystackHttpRequest() request.args['id'] = str(Ref("1234")) request.args['ts'] = str(time_serie) # WHEN response = shaystack.his_write(envs, request, "dev") # THEN result_ts = Grid(version=VER_3_0, columns=["date", "val"]) result_ts.extend([{"date": parse_hs_datetime_format(d, pytz.UTC), "val": v} for d, v in time_serie]) mock.assert_called_once_with(Ref("1234"), result_ts, 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_his_read_with_zinc(mock, no_cache) -> None: # GIVEN """ Args: mock: no_cache: """ envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} mock.return_value = ping._PingGrid no_cache.return_value = True mime_type = shaystack.MODE_ZINC request = HaystackHttpRequest() grid = shaystack.Grid(columns={'id': {}}) grid.append({"id": Ref("1234")}) request.headers["Content-Type"] = mime_type request.headers["Accept"] = mime_type request.body = shaystack.dump(grid, mode=mime_type) # WHEN response = shaystack.his_read(envs, request, "dev") # THEN mock.assert_called_once_with(Ref("1234"), (datetime.min.replace(tzinfo=pytz.UTC), datetime.max.replace(tzinfo=pytz.UTC)), 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_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_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_watch_sub_with_args(mock): # GIVEN """ Args: mock: """ envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} mock.return_value = Grid(version=VER_3_0, metadata={ "watchId": "0123456789ABCDEF", "lease": 1 }, columns=["empty"]) mock.return_value.append({}) mime_type = shaystack.MODE_ZINC request = HaystackHttpRequest() request.headers["Accept"] = mime_type request.args["watchDis"] = "myWatch" request.args["watchId"] = "myid" request.args["lease"] = "1" ids = ["@id1", "@id2"] request.args["ids"] = str(ids) # WHEN response = shaystack.watch_sub(envs, request, "dev") # THEN mock.assert_called_once_with("myWatch", "myid", [Ref("id1"), Ref("id2")], 1) assert response.status_code == 200 assert response.headers["Content-Type"].startswith(mime_type) assert shaystack.parse(response.body, shaystack.MODE_ZINC) is not None
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_point_write_read_with_arg(mock) -> None: # GIVEN """ Args: mock: """ envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} mock.return_value = Grid(version=VER_3_0, columns=["level", "levelDis", "val", "who"]) mime_type = shaystack.MODE_ZINC request = HaystackHttpRequest() request.headers["Accept"] = mime_type request.args["id"] = str(Ref("1234")) # WHEN response = shaystack.point_write(envs, request, "dev") # THEN mock.assert_called_once_with(Ref("1234"), 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_watch_poll_with_args(mock) -> None: # GIVEN """ Args: mock: """ envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} mock.return_value = ping._PingGrid mime_type = shaystack.MODE_ZINC request = HaystackHttpRequest() request.headers["Accept"] = mime_type request.args["watchId"] = "0123456789ABCDEF" request.args["refresh"] = "true" # WHEN response = shaystack.watch_poll(envs, request, "dev") # THEN mock.assert_called_once_with("0123456789ABCDEF", True) assert response.status_code == 200 assert response.headers["Content-Type"].startswith(mime_type) assert shaystack.parse(response.body, shaystack.MODE_ZINC) is not None
def test_formats_with_zinc(mock): # GIVEN """ Args: mock: """ envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} mock.return_value = ping._PingGrid mime_type = shaystack.MODE_ZINC request = HaystackHttpRequest() request.headers["Content-Type"] = mime_type request.headers["Accept"] = mime_type # apigw_event["body"] = shaystack.dump(grid, mode=shaystack.MODE_ZINC) # WHEN response = shaystack.formats(envs, request, "dev") # THEN mock.assert_called_once_with() 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_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_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_his_read_with_args(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'] = str(Ref("1234")) # WHEN response = shaystack.his_read(envs, request, "dev") # THEN mock.assert_called_once_with(Ref("1234"), (datetime.min.replace(tzinfo=pytz.UTC), datetime.max.replace(tzinfo=pytz.UTC)), 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_nav_with_zinc(mock): # GIVEN """ Args: mock: """ envs = {'HAYSTACK_PROVIDER': 'shaystack.providers.ping'} mock.return_value = ping._PingGrid mime_type = shaystack.MODE_ZINC request = HaystackHttpRequest() grid: Grid = shaystack.Grid(columns=['navId']) grid.append({"navId": "01235456789ABCDEF", "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.nav(envs, request, "dev") # THEN mock.assert_called_once_with("01235456789ABCDEF") assert response.status_code == 200 assert response.headers["Content-Type"].startswith(mime_type) assert shaystack.parse(response.body, shaystack.MODE_ZINC) is not None
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)