Exemple #1
0
def test_missing_required_property():
    testevent = Event(
        validate=False,  # Disable validation on __init__, done further down
        event_time="2019-04-10T12:43:05.200947+00:00",
        uuid="52c76d2c-514f-49ba-b8f7-09065b214e95",
    )
    del testevent["uuid"]

    with pytest.raises(ValidationError):
        testevent.validate()
Exemple #2
0
def test_from_json_invalid_properties(instance):
    # Update with a common base of a valid event
    instance.update({
        "uuid": "52c76d2c-514f-49ba-b8f7-09065b214e95",
        "event_time": "2019-04-10T12:43:05.200947+00:00",
        "schema_title": "Base Event",
        "schema_version": "0.1.0",
    })
    with pytest.raises(ValidationError):
        Event.from_json(instance)
Exemple #3
0
def test_to_json():
    testevent = Event(
        event_time="2019-04-10T12:43:05.200947+00:00",
        uuid="52c76d2c-514f-49ba-b8f7-09065b214e95",
    )
    expected_output = ('{"schema_title": "Event", "schema_version": "0.1.0", '
                       '"uuid": "52c76d2c-514f-49ba-b8f7-09065b214e95", '
                       '"event_time": "2019-04-10T12:43:05.200947+00:00"}')
    expected_repr = f"<Event {expected_output}>".replace('"', "'")

    output_to_json = testevent.to_json()
    output_repr = repr(testevent)

    assert output_to_json == expected_output
    assert output_repr == expected_repr
Exemple #4
0
def test_concrete_event_class():
    testevent = Event()

    assert isinstance(testevent["schema_title"], str)
    assert isinstance(testevent["schema_version"], str)
    assert isinstance(testevent["uuid"], uuid)
    assert isinstance(testevent["event_time"], datetime)
Exemple #5
0
def test_from_json():
    json_str = ('{"uuid": "52c76d2c-514f-49ba-b8f7-09065b214e95", '
                '"event_time": "2019-04-10T12:43:05.200947+00:00", '
                '"schema_title": "Base Event", "schema_version": "0.1.0"}')
    json_obj = {
        "uuid": "52c76d2c-514f-49ba-b8f7-09065b214e95",
        "event_time": "2019-04-10T12:43:05.200947+00:00",
        "schema_title": "Base Event",
        "schema_version": "0.1.0",
    }

    event_from_str = Event.from_json(json_str)
    event_from_obj = Event.from_json(json_obj)

    assert isinstance(event_from_str, Event)
    assert isinstance(event_from_obj, Event)
Exemple #6
0
def test_serializer_dumps():
    event = Event()
    dumped_event = dumps(event)
    reloaded_event = loads(dumped_event)

    assert isinstance(dumped_event, bytes)
    assert isinstance(event, Event)
    assert event == reloaded_event
Exemple #7
0
def test_collection_methods():
    testevent = Event()
    keys = ["uuid", "event_time", "schema_title", "schema_version"]

    # Tests for __iter__
    for key in testevent:
        assert key in keys

    # Tests for __len__
    assert len(testevent) == len(keys)
Exemple #8
0
def fixture_static_yield():
    yield Event()
Exemple #9
0
def test_datetime_validation():
    with pytest.raises(ValidationError):
        Event(event_time="boat", uuid="52c76d2c-514f-49ba-b8f7-09065b214e95")
Exemple #10
0
def test_properties_method():
    testevent = Event()

    assert len(testevent.properties) == 4
    for elem in ("uuid", "schema_title", "event_time", "schema_version"):
        assert elem in testevent.properties
Exemple #11
0
def test_from_json_invalid_object():
    with pytest.raises(JSONDecodeError):
        Event.from_json("invalid json")
Exemple #12
0
def test_from_json_wrong_property_format(instance):
    with pytest.raises(ValidationError):
        Event.from_json(instance)
Exemple #13
0
def test_from_json_wrong_type(instance):
    with pytest.raises(ValueError):
        Event.from_json(instance)
Exemple #14
0
import pytest

from uptimer.events import Event
from uptimer.events.stubs import DummyEvent
from uptimer.exceptions import ValidationError
from uptimer.plugins.writers import WriterPlugin

event = Event(something="something")
dummy = DummyEvent(
    target="this",
    reader="that",
    integer_value=42,
    float_value=13.37,
)


class ConcreteTestClass(WriterPlugin):
    plugin_type = "Concrete Test Implemetation of a Writer Plugin"

    def write(self):
        pass


writer = ConcreteTestClass()


def test_validate_event_type():
    output = writer.validate_event_type(event)

    assert output is event
    with pytest.raises(ValidationError):