def test_to_obj_invalid_types():
    with pytest.raises(ValueError):
        Payload.to_obj(
            MockData(id=42,
                     value='ok-value',
                     nested=MockNested(ts=datetime.now(
                         tz=timezone.utc))))  # type: ignore

    with pytest.raises(ValueError):
        Payload.to_obj(
            MockData(id='1',
                     value='ok-value',
                     nested=MockNested(ts="NOT A DATETIME")))  # type: ignore
def test_to_obj_dict_dataobject():
    assert Payload.to_obj({
        'item1':
        MockData(
            id='1',
            value='ok-1',
            nested=MockNested(ts=datetime.fromtimestamp(0, tz=timezone.utc))),
        'item2':
        MockData(
            id='2',
            value='ok-2',
            nested=MockNested(ts=datetime.fromtimestamp(0, tz=timezone.utc))),
    }) == {
        "item1": {
            "id": "1",
            "value": "ok-1",
            "nested": {
                "ts": "1970-01-01T00:00:00+00:00"
            }
        },
        "item2": {
            "id": "2",
            "value": "ok-2",
            "nested": {
                "ts": "1970-01-01T00:00:00+00:00"
            }
        }
    }
Beispiel #3
0
def mock_client_app_config():
    return AppConfig(
        app=AppDescriptor(name='mock_client_app', version='test'),
        engine=AppEngineConfig(import_modules=['mock_client_app']),
        app_connections={
            "test_app_connection":
            AppConnection(name="test_app",
                          version=APPS_API_VERSION,
                          client="hopeit.apps_client.AppsClient"),
            "test_app_plugin_connection":
            AppConnection(name="test_app",
                          version=APPS_API_VERSION,
                          client="hopeit.apps_client.AppsClient",
                          plugin_name="test_plugin",
                          plugin_version=APPS_API_VERSION)
        },
        settings={
            "test_app_connection":
            Payload.to_obj(
                AppsClientSettings(
                    connection_str="http://test-host1,http://test-host2")),
            "test_app_plugin_connection":
            Payload.to_obj(
                AppsClientSettings(
                    connection_str="http://test-host1,http://test-host2",
                    auth_strategy=ClientAuthStrategy.FORWARD_CONTEXT))
        },
        events={
            "mock_client_event":
            EventDescriptor(
                type=EventType.GET,
                connections=[
                    EventConnection(app_connection="test_app_connection",
                                    event="test_event_get",
                                    type=EventConnectionType.GET),
                    EventConnection(app_connection="test_app_connection",
                                    event="test_event_post",
                                    type=EventConnectionType.POST),
                    EventConnection(
                        app_connection="test_app_plugin_connection",
                        event="test_event_plugin",
                        type=EventConnectionType.GET)
                ])
        },
        server=ServerConfig(logging=LoggingConfig(
            log_level="DEBUG", log_path="work/logs/test/"))).setup()
def test_to_obj_python_types():
    assert Payload.to_obj('str', key='test') == {"test": "str"}
    assert Payload.to_obj(123, key='test') == {"test": 123}
    assert Payload.to_obj(123.45, key='test') == {"test": 123.45}
    assert Payload.to_obj(True, key='test') == {"test": True}
    assert Payload.to_obj({'test': 'dict'}) == {"test": "dict"}
    assert Payload.to_obj({'test2', 'test1'}) == ["test1", "test2"]
    assert Payload.to_obj(['test2', 'test1']) == ["test2", "test1"]
def test_to_obj_dataobject():
    assert Payload.to_obj(
        MockData(id='test',
                 value='ok',
                 nested=MockNested(
                     ts=datetime.fromtimestamp(0, tz=timezone.utc)))) == {
                         "id": "test",
                         "value": "ok",
                         "nested": {
                             "ts": "1970-01-01T00:00:00+00:00"
                         }
                     }
def test_to_obj_dict_mixed():
    assert Payload.to_obj({
        "item1": {
            'item':
            1,
            'data':
            MockData(id='1',
                     value='ok-1',
                     nested=MockNested(
                         ts=datetime.fromtimestamp(0, tz=timezone.utc)))
        },
        "item2": {
            'item':
            2,
            'data':
            MockData(id='2',
                     value='ok-2',
                     nested=MockNested(
                         ts=datetime.fromtimestamp(0, tz=timezone.utc)))
        }
    }) == {
        'item1': {
            'data': {
                'id': '1',
                'nested': {
                    'ts': '1970-01-01T00:00:00+00:00'
                },
                'value': 'ok-1'
            },
            'item': 1
        },
        'item2': {
            'data': {
                'id': '2',
                'nested': {
                    'ts': '1970-01-01T00:00:00+00:00'
                },
                'value': 'ok-2'
            },
            'item': 2
        }
    }
def test_to_obj_list_dataobject():
    assert Payload.to_obj([
        MockData(
            id='1',
            value='ok-1',
            nested=MockNested(ts=datetime.fromtimestamp(0, tz=timezone.utc))),
        MockData(
            id='2',
            value='ok-2',
            nested=MockNested(ts=datetime.fromtimestamp(0, tz=timezone.utc))),
    ]) == [{
        "id": "1",
        "value": "ok-1",
        "nested": {
            "ts": "1970-01-01T00:00:00+00:00"
        }
    }, {
        "id": "2",
        "value": "ok-2",
        "nested": {
            "ts": "1970-01-01T00:00:00+00:00"
        }
    }]
def test_to_obj_dataobject_do_not_validate():
    data = MockDataDoNotValidate(id='test', value='not-ok')  # type: ignore
    assert Payload.to_obj(data) == {"id": "test", "value": "not-ok"}
def test_to_obj_dataobject_validate():
    data = MockDataValidate(id='test', value='not-ok')  # type: ignore
    with pytest.raises(ValueError):
        Payload.to_obj(data)
Beispiel #10
0
 async def json(self):
     return Payload.to_obj(self.items)
Beispiel #11
0
 async def json(self):
     return Payload.to_obj(self.response)