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
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"]
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"]
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"]
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"]
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
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"]
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())
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
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
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
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()
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()
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
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
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))
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
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
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)
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
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
def test_update_non_existing_values(): existing_event = Event.from_dict({"source_id": "5498d53c5f2d60095267a0bb"}) new_event = Event.from_dict({"parent_id": "5498d53c5f2d60095267a0bb"}) _update(existing_event, new_event) assert existing_event.parent_id == "5498d53c5f2d60095267a0bb"