def test_can_get_connections(db, testapp): instance = PersonFactory(first_name='Wolverine') target = PersonFactory(first_name='Sabertooth') db.session.commit() ConnectionFactory(from_person_id=instance.id, to_person_id=target.id, connection_type='coworker') ConnectionFactory(from_person_id=target.id, to_person_id=instance.id, connection_type='coworker') db.session.commit() res = testapp.get('/connections') assert res.status_code == HTTPStatus.OK # assert res.json == None assert len(res.json) == 2 for item in res.json[0]['connection']: for field in EXPECTED_FIELDS: assert field in res.json[0]['connection'] for item in res.json[1]['connection']: for field in EXPECTED_FIELDS: assert field in res.json[1]['connection']
def test_can_get_connection(db, testapp): ConnectionFactory.create_batch(10) db.session.commit() res = testapp.get('/connections') assert res.status_code == HTTPStatus.OK assert len(res.json) == 10 for connection in res.json: for field in EXPECTED_FIELDS: assert field in connection
def test_mutual_friends(db): instance = PersonFactory() target = PersonFactory() # some decoy connections (not mutual) ConnectionFactory.create_batch(5, to_person=instance) ConnectionFactory.create_batch(5, to_person=target) mutual_friends = PersonFactory.create_batch(3) for f in mutual_friends: ConnectionFactory(from_person=instance, to_person=f, connection_type='friend') ConnectionFactory(from_person=target, to_person=f, connection_type='friend') # mutual connections, but not friends decoy = PersonFactory() ConnectionFactory(from_person=instance, to_person=decoy, connection_type='coworker') ConnectionFactory(from_person=target, to_person=decoy, connection_type='coworker') db.session.commit() expected_mutual_friend_ids = [f.id for f in mutual_friends] results = instance.mutual_friends(target) assert len(results) == 3 for f in results: assert f.id in expected_mutual_friend_ids
def test_can_get_connections(db, testapp): ConnectionFactory.create_batch(5, from_person_id=PersonFactory(), to_person_id=PersonFactory()) db.session.commit() res = testapp.get('/connections') assert res.status_code == HTTPStatus.OK assert len(res.json) == 5 pprint(res.json) for connection in res.json: for field in EXPECTED_FIELDS: assert field in connection
def test_update_connection_status(db, testapp): person_from = PersonFactory(first_name='Diana') person_to = PersonFactory(first_name='Harry') db.session.commit() connection = ConnectionFactory( from_person=person_from, to_person=person_to, ) db.session.commit() connection_type = 'mother' expected_connection = { 'id': connection.id, 'from_person_id': person_from.id, 'to_person_id': person_to.id, 'connection_type': connection_type, } payload = { 'connection_type': connection_type, } res = testapp.put(f'/connections/{connection.id}', json=payload) assert res.status_code == HTTPStatus.OK result_connection = res.json assert expected_connection['from_person_id'] == result_connection['from_person_id'] assert expected_connection['to_person_id'] == result_connection['to_person_id'] assert expected_connection['connection_type'] == result_connection['connection_type']
def test_get_connections(db, testapp): ConnectionFactory.create_batch(3) db.session.commit() res = testapp.get('/connections') assert res.status_code == HTTPStatus.OK assert len(res.json) == 3 for connection in res.json: for field in EXPECTED_FIELDS: assert field in connection for field in EXPECTED_PERSON_FIELDS: assert field in connection['from_person'] for field in EXPECTED_PERSON_FIELDS: assert field in connection['to_person']
def test_get_connections(db, testapp): person_from = PersonFactory(first_name='Diana') person_to = PersonFactory(first_name='Harry') db.session.commit() ConnectionFactory( from_person=person_from, to_person=person_to, ) db.session.commit() expected_connection = { 'from_person_id': person_from.id, 'to_person_id': person_to.id, 'connection_type': 'friend', } res = testapp.get('/connections') assert res.status_code == HTTPStatus.OK assert len(res.json) == 1 result_connection = res.json[0] assert expected_connection['from_person_id'] == result_connection[ 'from_person_id'] assert expected_connection['to_person_id'] == result_connection[ 'to_person_id'] assert expected_connection['connection_type'] == result_connection[ 'connection_type']
def test_can_delete_connection(db, testapp): new_connection = ConnectionFactory() db.session.commit() res = testapp.delete('connections/{}'.format(new_connection.id)) assert res.status_code == HTTPStatus.NO_CONTENT assert db.session.query(Connection).get(new_connection.id) is None
def test_can_get_connection_delete_by_id(db, testapp): connection = ConnectionFactory() db.session.commit() check_entry = testapp.get('/connections/' + str(connection.id)) assert check_entry.status_code == HTTPStatus.OK res = testapp.delete('/connections/' + str(connection.id)) assert res.status_code == HTTPStatus.OK
def test_create_connection_validate(db, testapp, connection_payload, field, value, error_message): connection_payload[field] = value connection = ConnectionFactory() res = testapp.patch(f'/connections/{connection.id}', json=connection_payload) assert res.status_code == HTTPStatus.BAD_REQUEST assert res.json['description'] == 'Input failed validation.' errors = res.json['errors'] assert error_message in errors[field][0]
def test_can_update_connection_type(db, testapp, connection_payload): connection = ConnectionFactory.create( connection_type=ConnectionType.father.value) db.session.commit() res = testapp.patch('/connections/' + str(connection.id), json=connection_payload) assert res.status_code == HTTPStatus.OK assert res.json['connection_type'] == connection_payload['connection_type']
def test_can_get_connections(db, testapp): personA = PersonFactory() personB = PersonFactory() ConnectionFactory.create_batch(10, from_person=personA, to_person=personB) db.session.commit() res = testapp.get('/connections') assert res.status_code == HTTPStatus.OK assert len(res.json) == 10 for connection in res.json: for field in EXPECTED_FIELDS: assert field in connection for p_field in PEOPLE_EXPECTED_FIELDS: assert p_field in connection['from_person'] assert p_field in connection['to_person']
def test_get_mutual_friends(db, testapp): instance = PersonFactory() target = PersonFactory() # some decoy connections (not mutual) ConnectionFactory.create_batch(5, to_person=instance) ConnectionFactory.create_batch(5, to_person=target) mutual_friends = PersonFactory.create_batch(3) for f in mutual_friends: ConnectionFactory(from_person=instance, to_person=f, connection_type='friend') ConnectionFactory(from_person=target, to_person=f, connection_type='friend') # mutual connections, but not friends decoy = PersonFactory() ConnectionFactory(from_person=instance, to_person=decoy, connection_type='coworker') ConnectionFactory(from_person=target, to_person=decoy, connection_type='coworker') db.session.commit() expected_mutual_friend_ids = [f.id for f in mutual_friends] # Trigger webservice call to get mutual friends res = testapp.get('/people/' + str(instance.id) + '/mutual_friends?target_id=' + str(target.id)) assert res.status_code == HTTPStatus.OK assert len(res.json) == 3 for mutual_friend in res.json: assert mutual_friend.id in expected_mutual_friend_ids # Switch target/instance ids and make sure the resulting friends are the same res2 = testapp.get('/people/' + str(target.id) + '/mutual_friends?target_id=' + str(instance.id)) assert res2.status_code == HTTPStatus.OK assert len(res2.json) == 3 for mutual_friend in res2.json: assert mutual_friend.id in expected_mutual_friend_ids
def test_update_connection_type_validations(db, testapp, connection_payload, field, value, error_message): connection = ConnectionFactory.create( connection_type=ConnectionType.father) db.session.commit() connection_payload[field] = value res = testapp.patch('/connections/' + str(connection.id), json=connection_payload) assert res.status_code == HTTPStatus.BAD_REQUEST assert res.json['description'] == 'Input failed validation.' errors = res.json['errors'] assert error_message in errors[field]
def test_can_patch_connection(db, testapp, connection_payload): test_connection = ConnectionFactory() db.session.commit() res = testapp.patch('/connections/{}'.format(test_connection.id), json=connection_payload) assert res.status_code == HTTPStatus.OK assert res.json['connection_type'] == connection_payload['connection_type'] mod_connect = Connection.query.get(res.json['id']) assert mod_connect is not None assert getattr( mod_connect, 'connection_type').value == connection_payload['connection_type']
def test_can_patch_connection(db, testapp, connection_payload): connection = ConnectionFactory() db.session.commit() connection_id = connection.id res = testapp.patch(f'connections/{connection_id}', json=connection_payload) assert res.status_code == HTTPStatus.OK assert 'id' in res.json connection = Connection.query.get(res.json['id']) assert connection is not None assert connection.connection_type.value == connection_payload['connection_type']
def test_can_get_mutual_friends(db, testapp): instance = PersonFactory() target = PersonFactory() ConnectionFactory.create_batch(5, to_person=instance) ConnectionFactory.create_batch(5, to_person=target) mutual_friends = PersonFactory.create_batch(3) for f in mutual_friends: ConnectionFactory(from_person=instance, to_person=f, connection_type='friend') ConnectionFactory(from_person=target, to_person=f, connection_type='friend') decoy = PersonFactory() ConnectionFactory(from_person=instance, to_person=decoy, connection_type='coworker') ConnectionFactory(from_person=target, to_person=decoy, connection_type='coworker') db.session.commit() expected_mutual_friend_ids = [f.id for f in mutual_friends] res = (testapp.get('/people/{}/mutual_friends?target_id={}'.format( instance.id, target.id))) res_reverse = (testapp.get('/people/{}/mutual_friends?target_id={}'.format( target.id, instance.id))) assert res.status_code == HTTPStatus.OK assert res_reverse.status_code == HTTPStatus.OK assert len(res.json) == 3 for f in res.json: assert f['id'] in expected_mutual_friend_ids assert res.json == res_reverse.json for person in res.json: for field in EXPECTED_FIELDS: assert field in person
def test_can_get_connection_by_id(db, testapp): instance = PersonFactory(first_name='Thanos') target = PersonFactory(first_name='Inevitable') db.session.commit() conn = ConnectionFactory(from_person_id=instance.id, to_person_id=target.id, connection_type='friend') db.session.commit() res = testapp.get('/connections/' + str(conn.id)) assert res.status_code == HTTPStatus.OK assert res.json['connection_type'] == conn.connection_type.value assert res.json['from_person_id'] == conn.from_person_id assert res.json['to_person_id'] == conn.to_person_id
def test_can_update_connection(db, testapp): person = ConnectionFactory(from_person=PersonFactory(), to_person_id=PersonFactory(), connection_type="friend") db.session.commit() payload = {'connection_type': 'mother'} res = testapp.patch('/connections/' + str(person.id), json=payload) assert res.status_code == HTTPStatus.OK assert 'id' in res.json assert res.json["connection_type"] == 'mother' bad_id = testapp.patch('/connections/99999444', json=payload) assert bad_id.status_code == HTTPStatus.BAD_REQUEST assert bad_id.json['description'] == 'Connection ID does not exist.'
def test_can_get_mutual_friends(db, testapp): instance = PersonFactory() target = PersonFactory() # some decoy connections (not mutual) ConnectionFactory.create_batch(5, to_person=instance) ConnectionFactory.create_batch(5, to_person=target) mutual_friends = PersonFactory.create_batch(3) for f in mutual_friends: ConnectionFactory(from_person=instance, to_person=f, connection_type='friend') ConnectionFactory(from_person=target, to_person=f, connection_type='friend') # mutual connections, but not friends decoy = PersonFactory() ConnectionFactory(from_person=instance, to_person=decoy, connection_type='coworker') ConnectionFactory(from_person=target, to_person=decoy, connection_type='coworker') db.session.commit() res = testapp.get( f'/people/{instance.id}/mutual_friends?target_id={target.id}') assert res.status_code == HTTPStatus.OK for person in res.json: for field in EXPECTED_FIELDS: assert field in person assert len(res.json) == 3 # This might make sense as a separate test, probably ideally in the unit tests, not here switched_res = testapp.get( f'/people/{target.id}/mutual_friends?target_id={instance.id}') assert set([p['id'] for p in res.json ]) == set([p['id'] for p in switched_res.json])
def test_patch_validations(db, testapp, connection_payload, field, value, error_message): connection_payload[field] = value test_connection = ConnectionFactory() db.session.commit() res = testapp.patch('connections/{}'.format(test_connection.id), json=connection_payload) assert res.status_code == HTTPStatus.BAD_REQUEST assert res.json['description'] == 'Input failed validation.' errors = res.json['errors'] if (connection_payload[field] is None): assert error_message in errors[field] else: assert error_message in errors[field][0]
def test_get_mutual_friends_correct_data(db, testapp): instance = PersonFactory() target = PersonFactory() # some decoy connections (not mutual) ConnectionFactory.create_batch(5, to_person=instance) ConnectionFactory.create_batch(5, to_person=target) mutual_friends = PersonFactory.create_batch(3) for f in mutual_friends: ConnectionFactory(from_person=instance, to_person=f, connection_type='friend') ConnectionFactory(from_person=target, to_person=f, connection_type='friend') # mutual connections, but not friends decoy = PersonFactory() ConnectionFactory(from_person=instance, to_person=decoy, connection_type='coworker') ConnectionFactory(from_person=target, to_person=decoy, connection_type='coworker') db.session.commit() # call mutual_friends endpoints res_a = testapp.get('/people/' + str(instance.id) + '/mutual_friends?target_id=' + str(target.id)) res_b = testapp.get('/people/' + str(target.id) + '/mutual_friends?target_id=' + str(instance.id)) assert res_a.status_code == HTTPStatus.OK assert res_b.status_code == HTTPStatus.OK # confirm that both calls return the same values instance_f_ids = [f['id'] for f in res_a.json] target_f_ids = [f['id'] for f in res_b.json] assert set(instance_f_ids) == set(target_f_ids)
def test_can_get_connection_patch_by_id(db, testapp): instance = PersonFactory(first_name='Tony') target = PersonFactory(first_name='IronMan') db.session.commit() pre_patch_conn = ConnectionFactory(from_person_id=instance.id, to_person_id=target.id, connection_type='friend') db.session.commit() pre = testapp.get('/connections/' + str(pre_patch_conn.id)) assert pre.json['connection_type'] == 'friend' res = testapp.patch('/connections/' + str(pre_patch_conn.id) + '?connection_type=mother') assert res.status_code == HTTPStatus.OK assert res.json['id'] == pre.json['id'] assert res.json['connection_type'] == 'mother'
def test_get_mutual_friends(db, testapp): instance = PersonFactory() target = PersonFactory() # some decoy connections (not mutual) ConnectionFactory.create_batch(5, to_person=instance) ConnectionFactory.create_batch(5, to_person=target) mutual_friends = PersonFactory.create_batch(3) for f in mutual_friends: ConnectionFactory(from_person=instance, to_person=f, connection_type='friend') ConnectionFactory(from_person=target, to_person=f, connection_type='friend') # mutual connections, but not friends decoy = PersonFactory() ConnectionFactory(from_person=instance, to_person=decoy, connection_type='coworker') ConnectionFactory(from_person=target, to_person=decoy, connection_type='coworker') db.session.commit() res = testapp.get('/people/' + str(instance.id) + '/mutual_friends?target_id=' + str(target.id)) assert res.status_code == HTTPStatus.OK for person in res.json: for field in EXPECTED_FIELDS: assert field in person
def test_can_get_connection_people(db, testapp): from_person = PersonFactory() to_person = PersonFactory() ConnectionFactory(from_person=from_person, to_person=to_person, connection_type=ConnectionType.friend) db.session.commit() res = testapp.get('/connections') assert res.status_code == HTTPStatus.OK # Because we're not currently tearing down between tests, this is 11 assert len(res.json) == 11 connection = res.json[10] assert connection['from_person']['id'] == from_person.id assert connection['to_person']['id'] == to_person.id assert connection['connection_type'] == ConnectionType.friend.value for field in EXPECTED_PERSON_FIELDS: assert field in connection['to_person'] assert field in connection['from_person']
def test_can_get_connection(db, testapp): ConnectionFactory.create_batch(1) db.session.commit() res = testapp.get('/connections') assert res.status_code == HTTPStatus.OK