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_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
Exemple #3
0
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_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']
Exemple #5
0
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
Exemple #6
0
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
Exemple #7
0
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_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_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_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(db, testapp):
    ConnectionFactory.create_batch(1)
    db.session.commit()
    res = testapp.get('/connections')
    assert res.status_code == HTTPStatus.OK