Exemplo n.º 1
0
def test_merge_two_attrs_of_type_lists_with_duplicates():
    existing_event = Event.from_dict({"source_id": "5498d53c5f2d60095267a0bb",
                                      "tags": {"author": ["Yusuke", "Sean"]}})
    new_event = Event.from_dict({"id": existing_event.id,
                                 "tags": {"author": ["Yusuke", "Mayur"]}})
    _merge(existing_event, new_event)
    assert existing_event.tags["author"] == ["Yusuke", "Sean", "Mayur"]
Exemplo n.º 2
0
def test_merge_new_attributes():
    existing_event = Event.from_dict({"source_id": "5498d53c5f2d60095267a0bb",
                                      "tags": {"author": ["Saroj"]}})
    new_event = Event.from_dict({"id": existing_event.id,
                                 "tags": {"reviewer": ["Yusuke"]}})
    _merge(existing_event, new_event)
    assert existing_event.tags["reviewer"] == ["Yusuke"]
Exemplo n.º 3
0
def test_merge_two_end_times():
    time = datetime.now()
    existing_event = Event.from_dict({"end_time": time})
    new_event = Event.from_dict(
        {"id": existing_event.id, "end_time": time + timedelta(hours=2)})
    _merge(existing_event, new_event)
    assert existing_event.end_time == new_event.end_time
Exemplo n.º 4
0
def test_same_values_not_added_twice():
    existing_event = Event.from_dict({"source_id": "5498d53c5f2d60095267a0bb",
                                      "tags": {"author": ["Yusuke"]}})
    new_event = Event.from_dict({"id": existing_event.id,
                                 "tags": {"author": ["Yusuke"]}})
    _merge(existing_event, new_event)
    assert existing_event.tags["author"] == ["Yusuke"]
Exemplo n.º 5
0
def test_merge_diff_values_of_reserved_keys_raises_exception():
    existing_event = Event({"source_id": "5498d53c5f2d60095267a0bb"})
    new_event = Event({
        "id": existing_event.id,
        "source_id": "5498d53c5f2d60095267a0bc"
    })
    with pytest.raises(ValueError):
        _merge(existing_event, new_event)
Exemplo n.º 6
0
def test_merge_with_already_existing_keys():
    existing_event = Event.from_dict({"source_id": "5498d53c5f2d60095267a0bb",
                                      "tags": {"author": ["Sean"]}})
    new_event = Event.from_dict({"id": existing_event.id,
                                 "source_id": "5498d53c5f2d60095267a0bb",
                                 "tags": {"author": ["Yusuke"]}})
    _merge(existing_event, new_event)
    assert existing_event.tags["author"] == ["Sean", "Yusuke"]
Exemplo n.º 7
0
def test_merge_two_descriptions():
    existing_event = Event.from_dict({"source_id": "5498d53c5f2d60095267a0bb",
                                      "description":
                                      "This is a Concrete Event."})
    new_event = Event.from_dict({"id": existing_event.id,
                                 "description": "The version is 2.2.3."})
    _merge(existing_event, new_event)
    test_str = "This is a Concrete Event.\nThe version is 2.2.3."
    assert existing_event.description == test_str
Exemplo n.º 8
0
def test_update_existing_values():
    existing_event = Event.from_dict({
        "source_id": "5498d53c5f2d60095267a0bb",
        "tags": {
            "author": ["Saroj"]
        }
    })
    new_event = Event.from_dict({"tags": {"author": ["Yusuke"]}})
    _update(existing_event, new_event)
    assert existing_event.tags["author"] == ["Yusuke"]
Exemplo n.º 9
0
def test_merge_empty_event_with_new_one():
    existing_event = Event()
    new_event = Event.from_dict({"id": existing_event.id,
                                 "source_id": "5498d53c5f2d60095267a0bc",
                                 "end_time": existing_event.end_time + timedelta(hours=2)})
    _merge(existing_event, new_event)
    assert existing_event.id == new_event.id
    assert existing_event.source_id == new_event.source_id
    assert existing_event.end_time == new_event.end_time
    assert len(attr.asdict(existing_event)) == len(attr.asdict(new_event))
Exemplo n.º 10
0
async def test_put_event(db, event, cli):
    resp = await cli.put('/api/v1/event/',
                         headers={"content-type": "application/json"},
                         data=json.dumps({"event": event.to_primitive()}))
    assert resp.status == 200
    retrieved_event_json = (await resp.json())
    retrieved_event = Event.from_dict(retrieved_event_json)
    resp2 = await cli.get('/api/v1/event/{0}'.format(event.id))
    assert resp2.status == 200
    retrieved_event2_json = (await resp2.json())
    retrieved_event2 = Event.from_dict(retrieved_event2_json)
    assert (retrieved_event.to_primitive() == retrieved_event2.to_primitive())
Exemplo n.º 11
0
def test_merge_detail_urls():
    existing_event = Event.from_dict({"source_id": "5498d53c5f2d60095267a0bb",
                                      "detail_urls":
                                      {"graphite": "http://graphite",
                                       "concrete": "http://concrete"}})

    new_event = Event.from_dict({"id": existing_event.id,
                                 "detail_urls": {"graphite": "http://graphite",
                                                 "concrete": "http://concrete"}})
    _merge(existing_event, new_event)
    ls = {"graphite": "http://graphite",
          "concrete": "http://concrete"}
    assert existing_event.detail_urls == ls
Exemplo n.º 12
0
async def test_post_event_without_id(event, cli):
    new_event = Event(
        {"description": "Event without id",
         "tags": {"author": ["*****@*****.**"]}
         })
    assert new_event.id != None

    resp = await cli.post('/api/v1/event/',
                          headers={"content-type": "application/json"},
                          data=json.dumps({"operation":
                                           "merge",
                                           "event":
                                           new_event.to_primitive()}))
    assert resp.status == 200
Exemplo n.º 13
0
async def test_post_existing_event_with_upsert_option(upsert, event_in_db,
                                                      cli):
    expected_author = event_in_db.tags["author"]
    new_event = {
        "id": event_in_db.id,
        "start_time": str(event_in_db.start_time - timedelta(seconds=50)),
        "tags": {
            "author": ["*****@*****.**"]
        }
    }
    expected_author.extend(["*****@*****.**"])

    resp = await cli.post('/api/v1/event/',
                          headers={"content-type": "application/json"},
                          data=json.dumps({
                              "operation": "merge",
                              "event": new_event,
                              "insert": upsert,
                          }))
    assert resp.status == 200

    merged_event_json = (await resp.json())
    merged_event = Event.from_dict(merged_event_json)
    assert set(expected_author) == set(merged_event.tags["author"])
    delta = event_in_db.start_time - merged_event.start_time
    assert delta.total_seconds() == 50
Exemplo n.º 14
0
def test_serialize_to_db_time_both_times(patch_update_time, update_time):
    start_time = ignore_microseconds(datetime.utcnow())
    end_time = ignore_microseconds(datetime.utcnow())
    event_db_dict = serialize_to_db_event(
        Event(start_time=start_time, end_time=end_time))
    assert event_db_dict["time"] == [start_time, end_time]
    assert event_db_dict["update_time"] == update_time
Exemplo n.º 15
0
async def test_get_children_events(event_in_db, cli):
    resp = await cli.get('/api/v1/event/{0}/children'.format(
        event_in_db.parent_id))
    retrieved_event_json = (await resp.json())
    retrieved_event = Event.from_dict(retrieved_event_json[0])
    assert (retrieved_event.to_primitive() == event_in_db.to_primitive())
    assert resp.status == 200
Exemplo n.º 16
0
async def test_get_trace_until_source(event_in_db, parent_event_in_db,
                                      source_event_in_db, cli):
    resp = await cli.get('/api/v1/event/{0}/trace'.format(event_in_db.id))
    event_json = (await resp.json())
    result = [Event.from_dict(elem) for elem in event_json]
    ls = [event_in_db, parent_event_in_db, source_event_in_db]
    for i in range(3):
        assert ls[i].to_primitive() == result[i].to_primitive()
Exemplo n.º 17
0
async def test_get_trace_for_source(source_event_in_db,
                                    cli):
    resp = await cli.get('/api/v1/event/{0}/trace'.format(
        source_event_in_db.id
    ))
    event_json = (await resp.json())
    result = [Event.from_dict(elem) for elem in event_json]
    assert source_event_in_db.to_primitive() == result[0].to_primitive()
Exemplo n.º 18
0
async def test_find_one_data_not_exists(app):
    result = await app["db"].event.find_one()
    assert result == Event(
        **{
            "id": result.id,
            "start_time": result.start_time,
            "end_time": result.end_time
        })
Exemplo n.º 19
0
async def test_get_trace_parent_have_two_children(parent_event_in_db,
                                                  source_event_in_db,
                                                  child_event_of_source_in_db,
                                                  cli):
    resp = await cli.get('/api/v1/event/{0}/trace'.format(
        parent_event_in_db.id))
    event_json = (await resp.json())
    result = [Event.from_dict(elem).to_primitive() for elem in event_json]
    assert child_event_of_source_in_db.to_primitive() not in result
Exemplo n.º 20
0
def test_deserialize_db_event_time_exists(eventdb):
    new_eventdb = {"_id": bson.ObjectId(
        "507f1f77bcf86cd799439011"), "time": [
        eventdb.time[0], eventdb.time[1]]}
    new_event = {
        "start_time": eventdb.time[0],
        "end_time": eventdb.time[1],
        "id": "507f1f77bcf86cd799439011"
    }
    assert deserialize_db_event(new_eventdb) == Event(**new_event)
Exemplo n.º 21
0
async def test_merge_description_with_post_existing_event(
        new_description, should_merge, event_in_db, cli):
    new_event = Event.from_dict({
        "id": event_in_db.id,
        "description": new_description
    })

    resp = await cli.post('/api/v1/event/',
                          headers={"content-type": "application/json"},
                          data=json.dumps({
                              "operation": "merge",
                              "event": new_event.to_primitive(),
                              "insert": True
                          }))
    assert resp.status == 200
    merged_event_json = (await resp.json())
    merged_event = Event.from_dict(merged_event_json)
    assert (merged_event.description
            == event_in_db.description) != should_merge
Exemplo n.º 22
0
async def test_post_event_update(event, cli):
    resp = await cli.post('/api/v1/event/',
                          headers={"content-type": "application/json"},
                          data=json.dumps({
                              "operation": "update",
                              "event": event.to_primitive()
                          }))
    retrieved_event_json = (await resp.json())
    retrieved_event = Event.from_dict(retrieved_event_json)
    assert (retrieved_event.to_primitive() == event.to_primitive())
    assert resp.status == 200
Exemplo n.º 23
0
def test_deserialize_db_event_tags_donot_exists(eventdb):
    eventdb_dict = eventdb.asdict()
    del eventdb_dict["tags"]
    event = {
        "id": "5498d53c5f2d60095267a0bb",
        "start_time": eventdb.time[0],
        "end_time": eventdb.time[1],
        "detail_urls": {
            "jira": "http://jira",
            "graphite": "http://graphite"},
        "description": "This is a trigger_deploy event.",
    }
    assert deserialize_db_event(eventdb_dict) == Event(**event)
Exemplo n.º 24
0
async def test_put_event(db, event, cli, log, app):
    with patch("tycho.routes.event.LOG") as mock_log:
        app["config"].log_events = log
        resp = await cli.put(f'/api/v1/event/',
                             headers={"content-type": "application/json"},
                             data=json.dumps({"event":
                                              event.to_primitive()}))
        assert resp.status == 200
        retrieved_event_json = (await resp.json())
        retrieved_event = Event.from_dict(retrieved_event_json)
        resp2 = await cli.get('/api/v1/event/{0}'.format(
            event.id
        ))
        assert resp2.status == 200
        retrieved_event2_json = (await resp2.json())
        retrieved_event2 = Event.from_dict(retrieved_event2_json)
        assert (retrieved_event.to_primitive()
                == retrieved_event2.to_primitive())
        if log:
            mock_log.info.assert_called_with(json.dumps(event.to_primitive()))
        else:
            assert mock_log.assert_not_called
Exemplo n.º 25
0
async def test_post_new_event_with_upsert_option_false(app, event, cli):
    resp = await cli.post('/api/v1/event/',
                          headers={"content-type": "application/json"},
                          data=json.dumps({
                              "operation": "merge",
                              "event": event.to_primitive(),
                              "insert": False
                          }))
    assert resp.status == 200
    merged_event_json = (await resp.json())
    merged_event = Event.from_dict(merged_event_json)

    with pytest.raises(HTTPNotFound):
        await app["db"].event.find_by_id(event.id)
Exemplo n.º 26
0
async def test_post_event_merge(event, cli, app, log):
    with patch("tycho.routes.event.LOG") as mock_log:
        app["config"].log_events = log
        resp = await cli.post('/api/v1/event/',
                              headers={"content-type": "application/json"},
                              data=json.dumps({"operation":
                                               "merge",
                                               "event":
                                               event.to_primitive()}))
        retrieved_event_json = (await resp.json())
        retrieved_event = Event.from_dict(retrieved_event_json)
        assert (retrieved_event.to_primitive()
                == event.to_primitive())
        assert resp.status == 200
        if log:
            mock_log.info.assert_called_with(json.dumps(event.to_primitive()))
        else:
            assert mock_log.assert_not_called
Exemplo n.º 27
0
async def test_post_existing_event_update(event_in_db, cli):
    updated_time = event_in_db.start_time - timedelta(seconds=50)
    new_event = {"id": event_in_db.id,
                 "start_time": str(updated_time),
                 "tags": {"author": ["*****@*****.**"]}
                 }
    resp = await cli.post('/api/v1/event/',
                          headers={"content-type": "application/json"},
                          data=json.dumps({"operation":
                                           "update",
                                           "event":
                                           new_event}))
    assert resp.status == 200
    retrieved_event_json = (await resp.json())
    retrieved_event = Event.from_dict(retrieved_event_json)
    assert event_in_db.id == retrieved_event.id
    assert set(["*****@*****.**"]
               ) == set(retrieved_event.tags["author"])
    assert retrieved_event.start_time == updated_time
Exemplo n.º 28
0
def test_serialize_to_db_detail_urls_donot_exist():
    assert serialize_to_db_event(Event()).get("detail_urls") == {}
Exemplo n.º 29
0
def test_serialize_to_db_detail_urls_exist(patch_update_time, update_time):
    event_db_dict = serialize_to_db_event(Event(detail_urls={"http://url"}))
    assert event_db_dict["detail_urls"] == {"http://url"}
    assert event_db_dict["update_time"] == update_time
Exemplo n.º 30
0
def test_serialize_to_db_time_end_time(patch_update_time, update_time):
    end_time = ignore_microseconds(datetime.utcnow())
    event_db_dict = serialize_to_db_event(Event(end_time=end_time))
    assert end_time in event_db_dict["time"]
    assert event_db_dict["update_time"] == update_time