Example #1
0
def test_cannot_get_by_name_if_does_not_exist():
    connect('*****@*****.**', 'token', name='foo')
    with assert_raises(MisconfiguredError):
        get('bar')

    with assert_raises(MisconfiguredError):
        get()
Example #2
0
def test_cannot_get_by_name_if_does_not_exist():
    connect('*****@*****.**', 'token', name='foo')
    with assert_raises(MisconfiguredError):
        get('bar')

    with assert_raises(MisconfiguredError):
        get()
Example #3
0
def test_activitytype_listset():
    connect(email='foo', token='bar')
    atls = ActivityTypeListSet()
    resp = Response()
    resp._content = load_fixture_json('activity_types.json').encode('utf-8')
    resp.status_code = codes.ok
    with mock.patch('prospyr.connection.Connection.get') as get:
        get.return_value = resp
        actual = list(atls)

    actual_names = {a.name for a in actual}
    assert actual_names == {
        'Note', 'Phone Call', 'Meeting', 'Property Changed',
        'My Custom Activity Type', 'Pipeline Stage Changed'
    }
Example #4
0
def test_activitytype_listset():
    connect(email='foo', token='bar')
    atls = ActivityTypeListSet()
    resp = Response()
    resp._content = load_fixture_json('activity_types.json').encode('utf-8')
    resp.status_code = codes.ok
    with mock.patch('prospyr.connection.Connection.get') as get:
        get.return_value = resp
        actual = list(atls)

    actual_names = {a.name for a in actual}
    assert actual_names == {
        'Note', 'Phone Call', 'Meeting', 'Property Changed',
        'My Custom Activity Type', 'Pipeline Stage Changed'
    }
Example #5
0
def test_iterable():
    """
    Check that ResultSet iterates over result pages
    """
    # 3 pages of results
    pages = (
        json_to_resp([{
            'id': 1
        }, {
            'id': 2
        }]),
        json_to_resp([{
            'id': 3
        }, {
            'id': 4
        }]),
        json_to_resp([{
            'id': 5
        }, {
            'id': 6
        }]),
    )
    cn = connect(email='foo', token='bar')
    cn.session.post = mock.Mock(side_effect=pages)
    rs = ResultSet(resource_cls=IdResource, page_size=2)
    assert {r.id for r in rs} == {1, 2, 3, 4, 5, 6}
Example #6
0
def test_cannot_clobber_name():
    connect('*****@*****.**', 'token', name='foo')
    with assert_raises(ValueError):
        connect('*****@*****.**', 'token', name='foo')

    # changing email addr shouldn't make a difference
    with assert_raises(ValueError):
        connect('*****@*****.**', 'token', name='foo')

    # nor token
    with assert_raises(ValueError):
        connect('*****@*****.**', 'token2', name='foo')
Example #7
0
def test_cannot_clobber_name():
    connect('*****@*****.**', 'token', name='foo')
    with assert_raises(ValueError):
        connect('*****@*****.**', 'token', name='foo')

    # changing email addr shouldn't make a difference
    with assert_raises(ValueError):
        connect('*****@*****.**', 'token', name='foo')

    # nor token
    with assert_raises(ValueError):
        connect('*****@*****.**', 'token2', name='foo')
Example #8
0
def make_cn_with_resps(url_map, name=None):
    name = name or sha256(str(random()).encode()).hexdigest()
    cn = connect(email='foo', token='bar', name=name)

    def _get(url):
        status, content = url_map[url]
        resp = Response()
        resp._content = content
        resp.status_code = status
        return resp
    cn.get = _get
Example #9
0
def test_iterable():
    """
    Check that ResultSet iterates over result pages
    """
    # 3 pages of results
    pages = (
        json_to_resp([{'id': 1}, {'id': 2}]),
        json_to_resp([{'id': 3}, {'id': 4}]),
        json_to_resp([{'id': 5}, {'id': 6}]),
    )
    cn = connect(email='foo', token='bar')
    cn.session.post = mock.Mock(side_effect=pages)
    rs = ResultSet(resource_cls=IdResource, page_size=2)
    assert {r.id for r in rs} == {1, 2, 3, 4, 5, 6}
Example #10
0
def test_finish_on_small_page():
    """
    ResultSet should detect it is on the last page of results.
    """
    # A page of 2 results, then a second with 1 result. The exception should
    # not be called.
    def pages(*args, **kwargs):
        yield json_to_resp([{'id': 1}, {'id': 2}])
        yield json_to_resp([{'id': 3}])
        raise Exception('ResultSet queried too many pages')

    cn = connect(email='foo', token='bar')
    cn.session.post = mock.Mock(side_effect=pages())
    rs = ResultSet(resource_cls=IdResource, page_size=2)
    assert {r.id for r in rs} == {1, 2, 3}
Example #11
0
def test_storing_invalid_resources():
    remote_data = json_to_resp([{'id': 1}, {'id': 'not-an-integer'}])
    cn = connect(email='foo', token='bar')
    cn.session.post = mock.Mock(side_effect=[remote_data])

    # 'not-an-integer' fails integer validation and raises
    with assert_raises(ValidationError):
        list(ResultSet(resource_cls=IdResource))

    # with store_invalid, no exception is raised
    cn.session.post = mock.Mock(side_effect=[remote_data])
    invalid = []
    valid = list(ResultSet(resource_cls=IdResource).store_invalid(invalid))
    assert len(valid) == 1  # id 1 passed validation
    assert len(invalid) == 1  # id 'not-an-integer' didn't
Example #12
0
def test_storing_invalid_resources():
    remote_data = json_to_resp([{'id': 1}, {'id': 'not-an-integer'}])
    cn = connect(email='foo', token='bar')
    cn.session.post = mock.Mock(side_effect=[remote_data])

    # 'not-an-integer' fails integer validation and raises
    with assert_raises(ValidationError):
        list(ResultSet(resource_cls=IdResource))

    # with store_invalid, no exception is raised
    cn.session.post = mock.Mock(side_effect=[remote_data])
    invalid = []
    valid = list(ResultSet(resource_cls=IdResource).store_invalid(invalid))
    assert len(valid) == 1  # id 1 passed validation
    assert len(invalid) == 1  # id 'not-an-integer' didn't
Example #13
0
def test_finish_on_small_page():
    """
    ResultSet should detect it is on the last page of results.
    """

    # A page of 2 results, then a second with 1 result. The exception should
    # not be called.
    def pages(*args, **kwargs):
        yield json_to_resp([{'id': 1}, {'id': 2}])
        yield json_to_resp([{'id': 3}])
        raise Exception('ResultSet queried too many pages')

    cn = connect(email='foo', token='bar')
    cn.session.post = mock.Mock(side_effect=pages())
    rs = ResultSet(resource_cls=IdResource, page_size=2)
    assert {r.id for r in rs} == {1, 2, 3}
Example #14
0
def test_last_page_is_exactly_page_size():
    """
    Should finish cleanly if last page is full yet there are no more results.
    """
    # A page of 2 results, then a second with 1 result. The exception should
    # not be called.
    def pages(*args, **kwargs):
        yield json_to_resp([{'id': 1}, {'id': 2}])
        yield json_to_resp([{'id': 3}, {'id': 4}])

        # PW returns 200 OK and empty list, not 404, when past last page.
        yield json_to_resp([])

    cn = connect(email='foo', token='bar')
    cn.session.post = mock.Mock(side_effect=pages())
    rs = ResultSet(resource_cls=IdResource, page_size=2)
    assert {r.id for r in rs} == {1, 2, 3, 4}
Example #15
0
def make_cn_with_resp(method, status_code, content, name=None):
    """
    A connection which returns a canned response for one particular verb.

    Content will be JSON-serialised. The verb-method is mocked so assertions
    can be made against it.

    cn = make_cn_with_resp('get', 200, 'Hello World')
    # ... run code under test which calls cn.get(...)
    cn.get.assert_called_with(*expected_args)
    """
    name = name or sha256(str(random()).encode()).hexdigest()
    resp = Response()
    resp._content = json.dumps(content).encode('utf-8')
    resp.status_code = status_code
    cn = connect(email='foo', token='bar', name=name)
    setattr(cn, method, mock.Mock(return_value=resp))
    return cn
Example #16
0
def test_last_page_is_exactly_page_size():
    """
    Should finish cleanly if last page is full yet there are no more results.
    """

    # A page of 2 results, then a second with 1 result. The exception should
    # not be called.
    def pages(*args, **kwargs):
        yield json_to_resp([{'id': 1}, {'id': 2}])
        yield json_to_resp([{'id': 3}, {'id': 4}])

        # PW returns 200 OK and empty list, not 404, when past last page.
        yield json_to_resp([])

    cn = connect(email='foo', token='bar')
    cn.session.post = mock.Mock(side_effect=pages())
    rs = ResultSet(resource_cls=IdResource, page_size=2)
    assert {r.id for r in rs} == {1, 2, 3, 4}
Example #17
0
def test_can_get_by_name():
    connect('*****@*****.**', 'token', name='foo')
    get('foo')
Example #18
0
def test_create_connection():
    connect('*****@*****.**', 'token')
Example #19
0
def test_create_connection():
    connect('*****@*****.**', 'token')
Example #20
0
def test_can_get_by_name():
    connect('*****@*****.**', 'token', name='foo')
    get('foo')