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()
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' }
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}
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')
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
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}
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}
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
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}
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
def test_can_get_by_name(): connect('*****@*****.**', 'token', name='foo') get('foo')
def test_create_connection(): connect('*****@*****.**', 'token')