def test_save(self): # Return an object with id for a post(save) request. self.http.respond_to("POST", "/stores.xml", self.xml_headers, util.to_xml(self.general_store)) # Return an object for a put request. self.http.respond_to("PUT", "/stores/1.xml", self.xml_headers, util.to_xml(self.store_update, root="store")) store = self.store(self.store_new) store.save() self.assertEqual(self.general_store, store.attributes) store.manager_id = 3 store.save()
def test_save(self): # Return an object with id for a post(save) request. self.http.respond_to('POST', '/stores.xml', self.xml_headers, util.to_xml(self.general_store)) # Return an object for a put request. self.http.respond_to('PUT', '/stores/1.xml', self.xml_headers, util.to_xml(self.store_update, root='store')) store = self.store(self.store_new) store.save() self.assertEqual(self.general_store, store.attributes) store.manager_id = 3 store.save()
def test_get_with_xml_format(self): person = util.to_xml({'id': 1, 'name': 'Matz'}, root='person') self.http.respond_to( 'GET', 'http://localhost/people/1.xml', {}, person) self.connection.format = formats.XMLFormat response = self.connection.get('/people/1.xml') self.assertEqual(response['name'], 'Matz')
def test_find_with_prefix_options(self): # Paths for prefix_options related requests self.http.respond_to("GET", "/stores/1/people.xml", {}, util.to_xml([self.sam], root="people")) # Prefix options only self.person._site = "http://localhost/stores/$store_id/" sam = self.person.find(store_id=1)[0] self.assertEqual(self.sam, sam.attributes)
def test_find_with_prefix_and_query_options(self): self.http.respond_to('GET', '/stores/1/people.xml?name=Ralph', {}, util.to_xml([], root='people')) # Query & prefix options self.person._site = 'http://localhost/stores/$store_id/' nobody = self.person.find(store_id=1, name='Ralph') self.assertEqual([], nobody)
def test_reload(self): self.http.respond_to( 'GET', '/people/1.xml', {}, util.to_xml(self.arnold, root='person')) arnold = self.person.find(1) arnold.name = 'someone else' arnold.reload() self.assertEqual(self.arnold, arnold.attributes)
def test_find_should_handle_query_params_argument_with_dict_value_to_support_complex_filters(self): query = urllib.urlencode({'count.gt':5, 'vars[key][]': ['val1', 'val2'], 'name':'xpto'}, True) self.http.respond_to( 'GET', '/people.xml?%s' % query, {}, util.to_xml([self.arnold], root='people')) arnold = self.person.find_first(vars={'key': ['val1', 'val2']}, query_params={'name':'xpto', 'count.gt':5}) self.assertEqual(self.arnold, arnold.attributes)
def test_set_prefix_source(self): self.http.respond_to( 'GET', '/stores/1/people.xml?name=Ralph', {}, util.to_xml([], root='people')) self.person.prefix_source = '/stores/${store_id}/' nobody = self.person.find(store_id=1, name='Ralph') self.assertEqual([], nobody)
def test_find_by_id(self): # Return a single person for a find(id=<id>) call self.http.respond_to( 'GET', '/people/1.xml', {}, util.to_xml(self.arnold, root='person')) arnold = self.person.find(1) self.assertEqual(self.arnold, arnold.attributes)
def test_find_should_handle_dictionary_query_args_with_array_value(self): query = urllib.urlencode({'vars[key][]': ['val1', 'val2']}, True) self.http.respond_to( 'GET', '/people.xml?%s' % query, {}, util.to_xml([self.arnold], root='people')) arnold = self.person.find_first(vars={'key': ['val1', 'val2']}) self.assertEqual(self.arnold, arnold.attributes)
def test_find_should_handle_array_query_args(self): query = urllib.parse.urlencode({'vars[]': ['a', 'b', 'c']}, True) self.http.respond_to( 'GET', '/people.xml?%s' % query, {}, util.to_xml([self.arnold], root='people')) arnold = self.person.find_first(vars=['a', 'b', 'c']) self.assertEqual(self.arnold, arnold.attributes)
def test_find_should_handle_array_query_args(self): query = urllib.urlencode({'vars[]': ['a', 'b', 'c']}, True) self.http.respond_to( 'GET', '/people.xml?%s' % query, {}, util.to_xml([self.arnold], root='people')) arnold = self.person.find_first(vars=['a', 'b', 'c']) self.assertEqual(self.arnold, arnold.attributes)
def test_find_should_handle_dictionary_query_args_with_array_value(self): query = urllib.parse.urlencode({'vars[key][]': ['val1', 'val2']}, True) self.http.respond_to( 'GET', '/people.xml?%s' % query, {}, util.to_xml([self.arnold], root='people')) arnold = self.person.find_first(vars={'key': ['val1', 'val2']}) self.assertEqual(self.arnold, arnold.attributes)
def test_get_with_xml_format(self): person = util.to_xml({'id': 1, 'name': 'Matz'}, root='person') self.http.respond_to('GET', 'http://localhost/people/1.xml', {}, person) self.connection.format = formats.XMLFormat response = self.connection.get('/people/1.xml') self.assertEqual(response['name'], 'Matz')
def test_find_with_query_options(self): # Return a single-item people list for a find() call with kwargs self.http.respond_to('GET', '/people.xml?name=Arnold', {}, util.to_xml([self.arnold], root='people')) # Query options only arnold = self.person.find(name='Arnold')[0] self.assertEqual(self.arnold, arnold.attributes)
def test_save_xml_format(self): # Return an object with id for a post(save) request. self.http.respond_to( 'POST', '/stores.xml', self.xml_headers, util.to_xml(self.general_store)) # Return an object for a put request. self.http.respond_to( 'PUT', '/stores/1.xml', self.xml_headers, util.to_xml(self.store_update, root='store')) self.store.format = formats.XMLFormat store = self.store(self.store_new) store.save() self.assertEqual(self.general_store, store.attributes) store.manager_id = 3 store.save()
def setUp(self): '''Create test objects.''' matz = {'id': 1, 'name': 'Matz'} david = {'id': 2, 'name': 'David'} self.matz = util.to_xml(matz, root='person') self.david = util.to_xml(david, root='person') self.people = util.to_xml([matz, david], root='people') self.people_single = util.to_xml([matz], root='people-single-elements') self.people_empty = util.to_xml([], root='people-empty-elements') http_fake.initialize() self.http = http_fake.TestHandler self.http.site = 'http://localhost' self.http.set_response(Error('Bad request')) self.header = {'Key': 'value'} self.connection = connection.Connection(self.http.site)
def test_find_with_query_options(self): # Return a single-item people list for a find() call with kwargs self.http.respond_to( 'GET', '/people.xml?name=Arnold', {}, util.to_xml([self.arnold], root='people')) # Query options only arnold = self.person.find(name='Arnold')[0] self.assertEqual(self.arnold, arnold.attributes)
def test_save_should_clear_errors(self): self.http.respond_to( 'POST', '/stores.xml', self.xml_headers, util.to_xml(self.general_store)) store = self.store(self.store_new) store.errors.add_to_base('bad things!') store.save() self.assertEqual(0, store.errors.size)
def test_find_with_prefix_options(self): # Paths for prefix_options related requests self.http.respond_to('GET', '/stores/1/people.xml', {}, util.to_xml([self.sam], root='people')) # Prefix options only self.person._site = 'http://localhost/stores/$store_id/' sam = self.person.find(store_id=1)[0] self.assertEqual(self.sam, sam.attributes)
def test_find_with_prefix_and_query_options(self): self.http.respond_to( 'GET', '/stores/1/people.xml?name=Ralph', {}, util.to_xml([], root='people')) # Query & prefix options self.person._site = 'http://localhost/stores/$store_id/' nobody = self.person.find(store_id=1, name='Ralph') self.assertEqual([], nobody)
def test_find(self): # Return a list of people for a find method call self.http.respond_to( 'GET', '/people.xml', {}, util.to_xml([self.arnold, self.eb], root='people')) people = self.person.find() self.assertEqual([self.arnold, self.eb], [p.attributes for p in people])
def test_find_one(self): # Return an object for a specific one-off url self.http.respond_to("GET", "/what_kind_of_soup.xml", {}, util.to_xml(self.soup, root="soup")) class Soup(activeresource.ActiveResource): _site = "http://localhost" soup = Soup.find_one(from_="/what_kind_of_soup.xml") self.assertEqual(self.soup, soup.attributes)
def test_find_one(self): # Return an object for a specific one-off url self.http.respond_to( 'GET', '/what_kind_of_soup.xml', {}, util.to_xml(self.soup, root='soup')) class Soup(activeresource.ActiveResource): _site = 'http://localhost' soup = Soup.find_one(from_='/what_kind_of_soup.xml') self.assertEqual(self.soup, soup.attributes)
def setUp(self): """Create test objects.""" matz = {"id": 1, "name": "Matz"} david = {"id": 2, "name": "David"} self.matz = util.to_xml(matz, root="person") self.david = util.to_xml(david, root="person") self.people = util.to_xml([matz, david], root="people") self.people_single = util.to_xml([matz], root="people-single-elements") self.people_empty = util.to_xml([], root="people-empty-elements") http_fake.initialize() self.http = http_fake.TestHandler self.http.site = "http://localhost" self.http.set_response(Error("Bad request")) self.zero_length_content_headers = {"Content-Length": "0", "Content-Type": "application/xml"} self.header = {"Key": "value"} self.connection = connection.Connection(self.http.site)
def setUp(self): '''Create test objects.''' matz = {'id': 1, 'name': 'Matz'} david = {'id': 2, 'name': 'David'} self.matz = util.to_xml(matz, root='person') self.david = util.to_xml(david, root='person') self.people = util.to_xml([matz, david], root='people') self.people_single = util.to_xml( [matz], root='people-single-elements') self.people_empty = util.to_xml([], root='people-empty-elements') http_fake.initialize() self.http = http_fake.TestHandler self.http.site = 'http://localhost' self.http.set_response(Error('Bad request')) self.zero_length_content_headers = {'Content-Length': '0', 'Content-Type': 'application/xml'} self.header = {'Key': 'value'} self.connection = connection.Connection(self.http.site)
def setUp(self): """Create test objects.""" self.arnold = {'id': 1, 'name': 'Arnold Ziffel'} self.eb = {'id': 2, 'name': 'Eb Dawson'} self.sam = {'id': 3, 'name': 'Sam Drucker'} self.soup = {'id': 1, 'name': 'Hot Water Soup'} self.store_new = {'name': 'General Store'} self.general_store = {'id': 1, 'name': 'General Store'} self.store_update = {'manager_id': 3, 'id': 1, 'name': 'General Store'} self.xml_headers = {'Content-type': 'application/xml'} self.matz = util.to_xml({'id': 1, 'name': 'Matz'}, root='person') self.matz_deep = util.to_xml( { 'id': 1, 'name': 'Matz', 'other': 'other' }, root='person') self.matz_array = util.to_xml([{ 'id': 1, 'name': 'Matz' }], root='people') self.ryan = util.to_xml({'name': 'Ryan'}, root='person') self.addy = util.to_xml({ 'id': 1, 'street': '12345 Street' }, root='address') self.addy_deep = util.to_xml( { 'id': 1, 'street': '12345 Street', 'zip': "27519" }, root='address') http_fake.initialize() # Fake all http requests self.http = http_fake.TestHandler self.http.set_response(Error('Bad request')) self.http.site = 'http://localhost' self.zero_length_content_headers = { 'Content-length': '0', 'Content-type': 'application/xml' } class Person(activeresource.ActiveResource): _site = 'http://localhost' self.person = Person class Store(activeresource.ActiveResource): _site = 'http://localhost' self.store = Store class Address(activeresource.ActiveResource): _site = 'http://localhost/people/$person_id/' self.address = Address
def to_xml(self, root=None, header=True, pretty=False): """Convert the object to an xml string. Args: root: The name of the root element for xml output. header: Whether to include the xml header. pretty: Whether to "pretty-print" format the output. Returns: An xml string. """ if not root: root = self._singular return util.to_xml(self.to_dict(), root=root, header=header, pretty=pretty)
def to_xml(self, root=None, header=True, pretty=False, dasherize=True): """Convert the object to an xml string. Args: root: The name of the root element for xml output. header: Whether to include the xml header. pretty: Whether to "pretty-print" format the output. dasherize: Whether to dasherize the xml attribute names. Returns: An xml string. """ if not root: root = self._singular return util.to_xml(self.to_dict(), root=root, header=header, pretty=pretty, dasherize=dasherize)
def setUp(self): """Create test objects.""" self.arnold = {'id': 1, 'name': 'Arnold Ziffel'} self.eb = {'id': 2, 'name': 'Eb Dawson'} self.sam = {'id': 3, 'name': 'Sam Drucker'} self.soup = {'id': 1, 'name': 'Hot Water Soup'} self.store_new = {'name': 'General Store'} self.general_store = {'id': 1, 'name': 'General Store'} self.store_update = {'manager_id': 3, 'id': 1, 'name':'General Store'} self.xml_headers = {'Content-type': 'application/xml'} self.matz = util.to_xml( {'id': 1, 'name': 'Matz'}, root='person') self.matz_deep = util.to_xml( {'id': 1, 'name': 'Matz', 'other': 'other'}, root='person') self.matz_array = util.to_xml( [{'id': 1, 'name': 'Matz'}], root='people') self.ryan = util.to_xml( {'name': 'Ryan'}, root='person') self.addy = util.to_xml( {'id': 1, 'street': '12345 Street'}, root='address') self.addy_deep = util.to_xml( {'id': 1, 'street': '12345 Street', 'zip': "27519" }, root='address') http_fake.initialize() # Fake all http requests self.http = http_fake.TestHandler self.http.set_response(Error('Bad request')) self.http.site = 'http://localhost' self.zero_length_content_headers = {'Content-length': '0', 'Content-type': 'application/xml'} class Person(activeresource.ActiveResource): _site = 'http://localhost' self.person = Person class Store(activeresource.ActiveResource): _site = 'http://localhost' self.store = Store class Address(activeresource.ActiveResource): _site = 'http://localhost/people/$person_id/' self.address = Address
def setUp(self): """Create test objects.""" self.arnold = {"id": 1, "name": "Arnold Ziffel"} self.eb = {"id": 2, "name": "Eb Dawson"} self.sam = {"id": 3, "name": "Sam Drucker"} self.soup = {"id": 1, "name": "Hot Water Soup"} self.store_new = {"name": "General Store"} self.general_store = {"id": 1, "name": "General Store"} self.store_update = {"manager_id": 3, "id": 1, "name": "General Store"} self.xml_headers = {"Content-type": "application/xml"} self.matz = util.to_xml({"id": 1, "name": "Matz"}, root="person") self.matz_deep = util.to_xml({"id": 1, "name": "Matz", "other": "other"}, root="person") self.matz_array = util.to_xml([{"id": 1, "name": "Matz"}], root="people") self.ryan = util.to_xml({"name": "Ryan"}, root="person") self.addy = util.to_xml({"id": 1, "street": "12345 Street"}, root="address") self.addy_deep = util.to_xml({"id": 1, "street": "12345 Street", "zip": "27519"}, root="address") http_fake.initialize() # Fake all http requests self.http = http_fake.TestHandler self.http.set_response(Error("Bad request")) self.http.site = "http://localhost" self.zero_length_content_headers = {"Content-length": "0", "Content-type": "application/xml"} class Person(activeresource.ActiveResource): _site = "http://localhost" self.person = Person class Store(activeresource.ActiveResource): _site = "http://localhost" self.store = Store class Address(activeresource.ActiveResource): _site = "http://localhost/people/$person_id/" self.address = Address
def test_to_json_with_root(self): xml = util.to_xml({'title': 'Test'}, root='product') self.assert_(b'product' in xml)
def test_find_should_handle_long_query_args(self): self.http.respond_to( 'GET', '/people.xml?employee_id=12345', {}, util.to_xml([self.arnold], root='people')) arnold = self.person.find_first(employee_id=12345) self.assertEqual(self.arnold, arnold.attributes)
def test_to_xml_should_allow_disabling_dasherization(self): xml = util.to_xml({'key_name': 'value'}, dasherize=False) self.assert_('<key_name>value</key_name>' in xml)
def test_to_xml_should_allow_unicode(self): xml = util.to_xml({'data': u'\xe9'}) self.assert_('<data>é</data>' in xml)
def test_to_xml_should_allow_utf8_encoded_strings(self): xml = util.to_xml({'data': u'\xe9'.encode('utf-8')}) self.assert_(b'<data>é</data>' in xml)
def test_to_xml_should_encode_bytes_as_base64(self): xml = util.to_xml({'data': b'\xe9'}) self.assert_(b'<data type="base64Binary">6Q==</data>' in xml)
def test_find_should_handle_unicode_query_args(self): self.http.respond_to( 'GET', '/people.xml?name=%C3%83%C3%A9', {}, util.to_xml([self.arnold], root='people')) arnold = self.person.find_first(name=u'\xc3\xe9') self.assertEqual(self.arnold, arnold.attributes)
def test_to_xml_should_honor_dasherize_option_for_children(self): xml = util.to_xml([{'key_name': 'value'}], dasherize=False) self.assert_(b'<key_name>value</key_name>' in xml)
def test_set_prefix_source(self): self.http.respond_to("GET", "/stores/1/people.xml?name=Ralph", {}, util.to_xml([], root="people")) self.person.prefix_source = "/stores/${store_id}/" nobody = self.person.find(store_id=1, name="Ralph") self.assertEqual([], nobody)
def test_find_should_handle_unicode_query_args(self): self.http.respond_to( 'GET', '/people.xml?name=%C3%83%C3%A9', {}, util.to_xml([self.arnold], root='people')) arnold = self.person.find_first(name='\xc3\xe9') self.assertEqual(self.arnold, arnold.attributes)
def test_to_xml_should_allow_unicode(self): xml = util.to_xml({'data': u'\xe9'}) self.assert_(b'<data>é</data>' in xml)
def test_find_should_handle_long_query_args(self): self.http.respond_to( 'GET', '/people.xml?employee_id=12345', {}, util.to_xml([self.arnold], root='people')) arnold = self.person.find_first(employee_id=12345L) self.assertEqual(self.arnold, arnold.attributes)
def test_to_xml_should_allow_disabling_dasherization(self): xml = util.to_xml({'key_name': 'value'}, dasherize=False) self.assert_(b'<key_name>value</key_name>' in xml)