def test_from_json_malformed(): with pytest.raises(ValueError): Json.from_json( '{"item1": {"id": "1", "value": "ok-1", "nested": {"ts": "1970-01-01T00:00:00+00:00"}', MockData) with pytest.raises(ValueError): Json.from_json('BAD STRING', MockData)
def test_from_json_python_types(): assert Json.from_json('{"value": "str"}', str) == "str" assert Json.from_json('{"value": 123}', int) == int(123) assert Json.from_json('{"value": 123.45}', float) == float(123.45) assert Json.from_json('{"value": true}', bool) is True assert Json.from_json('{"custom": "str"}', str, key='custom') == "str" assert Json.from_json('{"test": "dict"}', dict) == {'test': 'dict'} assert Json.from_json('["test1", "test2"]', set) == {'test2', 'test1'} assert Json.from_json('["test1", "test2"]', list) == ['test1', 'test2']
def _get_runtime_simple_example(url: str): res = RUNTIME_SIMPLE_EXAMPLE res = res.replace("${HOST_NAME}", socket.gethostname()) res = res.replace("${PID}", str(os.getpid())) res = res.replace("${URL}", url) res = res.replace("${ENGINE_VERSION}", ENGINE_VERSION) res = res.replace("${APPS_API_VERSION}", APPS_API_VERSION) res = res.replace("${APPS_ROUTE_VERSION}", APPS_ROUTE_VERSION) return Json.from_json(res, RuntimeApps)
def text_from_json_dataobject(): assert Json.from_json( '{"id": "test", "value": "ok", "nested": {"ts": 0.0}}', MockData ) == MockData( id='test', value='ok', nested=MockNested( ts=datetime.fromtimestamp(0.0) ) )
async def get(self, key: str, *, datatype: Type[DataObject]) -> Optional[DataObject]: """ Retrieves value under specified key, converted to datatype :param key: str :param datatype: dataclass implementing @dataobject (@see DataObject) :return: instance of datatype or None if not found """ assert self._conn payload_str = await self._conn.get(key, encoding='utf-8') if payload_str: return Json.from_json(payload_str, datatype) return None
async def get(self, key: str, *, datatype: Type[DataObject]) -> Optional[DataObject]: """ Retrieves value under specified key, converted to datatype :param key: str :param datatype: dataclass implementing @dataobject (@see DataObject) :return: instance of datatype or None if not found """ payload_str = await self._load_file(path=self.path, file_name=key + SUFFIX) if payload_str: return Json.from_json(payload_str, datatype) return None
async def _request_process_payload( context: EventContext, datatype: Optional[Type[EventPayloadType]], request: web.Request) -> Optional[EventPayloadType]: """ Extract payload from request. Returns payload if parsing succeeded. Raises BadRequest if payload fails to parse """ try: payload_raw = await request.read() if (payload_raw is None) or (payload_raw == b''): return None payload = Json.from_json( payload_raw, datatype) if datatype else payload_raw.decode() return payload # type: ignore except ValueError as e: logger.error(context, e) raise BadRequest(e) from e
def expected_log_entries() -> LogBatch: return Json.from_json( """ { "entries": [ { "ts": "2021-06-02 18:01:44,290", "msg": "START", "app_name": "simple-example", "app_version": "${APPS_API_VERSION}", "event_name": "query_something", "event": "simple_example.${APP_VERSION}.query_something", "extra": { "track.operation_id": "f2659a30-5ac4-4dd4-b1f7-9a00db0bf7d5", "track.request_id": "7ee59fa7-c1e4-4a60-a79b-a25dbbd6cb82", "track.request_ts": "2021-06-02T18:01:44.289394+00:00", "track.caller": "test", "track.session_id": "test" }, "host": "host", "pid": "17031" }, { "ts": "2021-06-02 18:01:44,303", "msg": "DONE", "app_name": "simple-example", "app_version": "${APPS_API_VERSION}", "event_name": "query_something", "event": "simple_example.${APP_VERSION}.query_something", "extra": { "response.status": "404", "metrics.duration": "13.057", "track.operation_id": "f2659a30-5ac4-4dd4-b1f7-9a00db0bf7d5", "track.request_id": "7ee59fa7-c1e4-4a60-a79b-a25dbbd6cb82", "track.request_ts": "2021-06-02T18:01:44.289394+00:00", "track.caller": "test", "track.session_id": "test" }, "host": "host", "pid": "17031" } ] } """.replace("${APPS_API_VERSION}", APPS_API_VERSION).replace("${APP_VERSION}", APP_VERSION), LogBatch)
def test_from_json_missing_fields(): with pytest.raises(ValueError): Json.from_json('{"id": "1", "value": "ok-1", "nested": {}', MockData) with pytest.raises(ValueError): Json.from_json('{"value": 42, "nested": {"ts": "1970-01-01T00:00:00+00:00"}}', MockData)
def test_from_json_invalid_types(): with pytest.raises(ValueError): Json.from_json('{"id": "1", "value": "ok-1", "nested": {"ts": "INVALID DATE"}}', MockData) with pytest.raises(ValueError): Json.from_json('{"id": 42, "value": 42, "nested": {"ts": "1970-01-01T00:00:00+00:00"}}', MockData)
def test_from_json_dataobject_do_not_validate(): data = '{"id": "test", "value": "not-ok"}' assert Json.from_json(data, MockDataDoNotValidate) \ == MockDataDoNotValidate(id='test', value='not-ok') # type: ignore
def test_from_json_dataobject_validate(): data = '{"id": "test", "value": "not-ok"}' with pytest.raises(ValueError): Json.from_json(data, MockDataValidate)
def _deser_json_utf8(data: bytes, datatype: Type[EventPayloadType]) -> EventPayload: return Json.from_json(data.decode('utf-8'), datatype)