Esempio n. 1
0
def test_event_processor_list_registerd_events():
    test_cases = [
        {
            "name": "base case",
            "storage":MockSchemaStorage({
                ("a","e1"): "",
                ("a","e2"): "",
                ("b","e1"): "",
            }),
            "query_client":"a",
            "contains": [
                ("a","e1"),
                ("a","e2"),
            ],
            "not contains": [
                ("b","e1"),
            ],
        },
    ]

    for tc in test_cases:
        ep = EventProcessor(MockEventStream(), tc["storage"])
        registered_events = ep.list_registered_events(tc["query_client"])
        for event in tc["contains"]:
            assert event in registered_events, f"{tc['name']}: '{event}' not found"

        for event in tc["not contains"]:
            assert event not in registered_events, f"{tc['name']}: '{event}' should not be present"
Esempio n. 2
0
def register_event(client, event_name):
    ep = EventProcessor(current_app.es, current_app.db)
    schema = request.form.get("event_schema")
    if not schema:
        return {"code":"FAILED", "msg": "'event_schema' key not in data"}, 400

    schema = try_json_decode(schema)
    if not schema:
        return {"code":"FAILED", "msg": "'event_schema' is not a valid json"}, 400

    succ = ep.register_event(client, event_name, schema)
    if not succ:
        return {"code":"FAILED", "msg": "failed to register event"}, 400
    return {"code":"SUCCEEDED", "msg": "event registered"}, 200
Esempio n. 3
0
def send_event(client, event_name):
    ep = EventProcessor(current_app.es, current_app.db)
    data = request.form.get("event")
    if not data:
        return {"code":"FAILED", "msg": "'event' key not in data"}, 400

    data = try_json_decode(data)
    if not data:
        return {"code":"FAILED", "msg": "'event' is not a valid json"}, 400

    succ = ep.receive_event(client, event_name, data)
    if not succ:
        return {"code":"FAILED", "msg": "failed to send event"}, 400
    return {"code":"SUCCEEDED", "msg": "event sent"}, 200
Esempio n. 4
0
def test_event_processor_recieve_event():
    test_cases = [
        {
            "name": "valid event",
            "data": {"color":"blue"},
            "client": 'client1',
            "event": "event_name",
            "storage": MockSchemaStorage({
                ("client1","event_name"): {"type":"object","required":["color"]},
            }),
            "success": True,
        },
        {
            "name": "invalid event",
            "data": {"name":"walter"},
            "client": 'client1',
            "event": "event_name",
            "storage": MockSchemaStorage({
                ("client1","event_name"): {"type":"object","required":["color"]},
            }),
            "success": False,
        },
        {
            "name": "unregistered event",
            "data": {"name":"walter"},
            "client": 'client1',
            "event": "new_event",
            "storage": MockSchemaStorage(),
            "success": False,
        },
    ]

    for tc in test_cases:
        es = MockEventStream()
        ep = EventProcessor(es, tc["storage"])
        success = ep.receive_event(tc['client'], tc['event'], tc['data'])

        assert success == tc["success"], f"{tc['name']}: success did not match"

        if not tc["success"]:
            continue

        events = es.list_events(tc['client'], tc['event'])
        assert tc['data'] in events, "{tc['name']}: event not stored in stream"
Esempio n. 5
0
def test_event_processor_register_event():
    test_cases = [
        {
            "name": "succesfully register",
            "client": "client1",
            "event_name": "new_event",
            "schema": {"type":"object","required":["color"]},
            "storage": MockSchemaStorage(),
            "success":True,
        },
        {
            "name": "already registered",
            "client": "client1",
            "event_name": "old_event",
            "schema": {"type":"object","required":["color"]},
            "storage": MockSchemaStorage({("client1","old_event"): {}}),
            "success":False,
        },
        {
            "name": "invalid schema",
            "client": "client1",
            "event_name": "new_event",
            "schema": {"type":"random_type"},
            "storage": MockSchemaStorage(),
            "success":False,
        },
    ]

    for tc in test_cases:
        ep = EventProcessor(MockEventStream(), tc["storage"])
        got = ep.register_event(tc["client"], tc["event_name"], tc["schema"])
        assert got == tc["success"], f"Error on test_case: {tc['name']}"

        if not tc["success"]:
            continue

        got = ep.get_event(tc["client"], tc["event_name"])
        assert got == tc["schema"], f"Error, stored schema does not match registerd: {tc['name']}"
Esempio n. 6
0
def test_event_processor_valid_event():
    test_cases = [
        {
            "name": "valid event",
            "data": {"fruit":"apple","color":"blue"},
            "schema": {
                "type":"object",
                "required": ["color", "fruit"],
                "properties": {
                    "fruit":{"type":"string", "description":"name of fruit"},
                    "color":{"type":"string", "description":"name of color"},
                },
                "additionalProperties": False,
            },
            "out": True
        },
        {
            "name": "invalid event",
            "data": {"fruit":"apple"},
            "schema": {
                "type":"object",
                "required": ["color", "fruit"],
                "properties": {
                    "fruit":{"type":"string", "description":"name of fruit"},
                    "color":{"type":"string", "description":"name of color"},
                },
                "additionalProperties": False,
            },
            "out": False
        },
    ]


    for tc in test_cases:
        ep = EventProcessor(MockEventStream(), MockSchemaStorage())
        got = ep._valid_event(tc["data"], tc["schema"])
        assert got == tc["out"], f"Error on test_case: {tc['name']}"
Esempio n. 7
0
def list_registered_events(client):
    ep = EventProcessor(current_app.es, current_app.db)
    events = ep.list_registered_events(client)
    return {"code":"SUCCEEDED", "events":events, "msg": "schema recovered"}, 200
Esempio n. 8
0
def get_event_schema(client, event_name):
    ep = EventProcessor(current_app.es, current_app.db)
    schema = ep.get_event(client, event_name)
    if schema is None:
        return {"code":"FAILED", "msg": "event not registered"}, 400
    return {"code":"SUCCEEDED", "schema":schema, "msg": "schema recovered"}, 200