def test_construct_from_api_data(): data = json.loads(load_fixture_json('person.json')) jon = Person.from_api_data(data) assert_is_jon(jon) data = {'invalid': 'data'} with assert_raises(exceptions.ValidationError): Person.from_api_data(data)
def test_read(): cn = connect(email='foo', token='bar') cn.session = MockSession(urls={ cn.build_absolute_url('people/1/'): load_fixture_json('person.json') }) jon = Person(id=1) jon.read() assert_is_jon(jon)
def test_str_does_not_raise(): people = ( Person(), Person(id=12), Person(name='Gertrude T. Boondock'), ) for person in people: print(str(person))
def test_resource_validation(): # valid albert albert = Person(name='Albert Cornswaddle', emails=[]) albert.validate() # albert without email albert = Person(name='Albert Cornswaddle') with assert_raises(exceptions.ValidationError): albert.validate()
def test_no_instance_access_to_manager(): # accessing from class is OK Person.objects # but cannot access from class instance person = Person() with assert_raises(AttributeError): person.objects
def test_delete(): cn = make_cn_with_resp(method='delete', status_code=codes.ok, content=[]) person = Person(id=1) person.delete(using=cn.name) expected_url = URLObject(_default_url + 'v1/people/1/') cn.delete.assert_called_with(expected_url) # can't delete without an id with assert_raises(ValueError): no_id_person = Person() no_id_person.delete(using=cn.name)
def test_update(): content = json.loads(load_fixture_json('person.json')) cn = make_cn_with_resp(method='put', status_code=codes.ok, content=content) person = Person(id=1, name='Nantucket Terwilliger') person.update(using=cn.name) expected_url = URLObject(_default_url + 'v1/people/1/') cn.put.assert_called_with( expected_url, json={'name': 'Nantucket Terwilliger'} ) # can't update without an id with assert_raises(ValueError): no_id_person = Person() no_id_person.update(using=cn.name) # validation errors come back. note the `invalid` instance isn't really # invalid; we just simulate the invalid response. invalid = Person(id=1, name='Invalid') cn = make_cn_with_resp( method='put', status_code=codes.unprocessable_entity, content=dict(message='Something wrong') ) try: invalid.update(using=cn.name) except ValueError as ex: assert 'Something wrong' in str(ex) else: raise AssertionError('Exception not thrown')
def test_create(): # can create content = json.loads(load_fixture_json('person.json')) # no support for creation with custom fields del content['custom_fields'] cn = make_cn_with_resp( method='post', status_code=codes.ok, content=content ) person = Person(name='Slithey Tove') person.create(using=cn.name) expected_url = URLObject(_default_url + 'v1/people/') cn.post.assert_called_with(expected_url, json={'name': 'Slithey Tove'}) # can't create with an ID with assert_raises(ValueError): id_person = Person(id=1) id_person.create(using=cn.name) # validation errors come back. note the `invalid` instance isn't really # invalid; we just simulate the invalid response. invalid = Person(name='Invalid') cn = make_cn_with_resp( method='post', status_code=codes.unprocessable_entity, content=dict(message='Something wrong') ) try: invalid.create(using=cn.name) except ValueError as ex: assert 'Something wrong' in str(ex) else: raise AssertionError('Exception not thrown')