class Group(xml_models.Model): name = xml_models.CharField(xpath="/entry/@name") subgroups = xml_models.CollectionField(SubGroup, xpath="/entry/subgroups") collection_node = 'groups' finders = {(): 'http://example.com'}
def test_orders_by_throws_if_unknown(self): xml_string = '<master><sub><name>fred</name></sub><sub><name>jill</name></sub><sub><name>alice</name></sub></master>' xml = objectify.fromstring(xml_string) field = xml_models.CollectionField(CollectionFieldTests.SubModel, xpath='/master/sub', order_by='not_real') with self.assertRaises(AttributeError): field.parse(xml, None)
def test_returns_empty_collection_when_empty(self): xml_string = '<master></master>' xml = objectify.fromstring(xml_string) field = xml_models.CollectionField(CollectionFieldTests.SubModel, xpath='/master/sub', order_by='name') response = field.parse(xml, None) self.assertEqual([], response)
def test_order_by_supplied_attribute_of_user_model_types(self): xml_string = '<master><sub><name>fred</name></sub><sub><name>jill</name></sub><sub><name>alice</name></sub></master>' xml = objectify.fromstring(xml_string) field = xml_models.CollectionField(CollectionFieldTests.SubModel, xpath='/master/sub', order_by='name') response = field.parse(xml, None) self.assertEqual(3, len(response)) self.assertEqual([e.name for e in response], ['alice', 'fred', 'jill'])
def test_returns_expected_number_of_correctly_typed_results(self): xml_string = '<master><sub><name>fred</name></sub><sub><name>jill</name></sub></master>' xml = objectify.fromstring(xml_string) field = xml_models.CollectionField(CollectionFieldTests.SubModel, xpath='/master/sub') response = field.parse(xml, None) self.assertEqual(2, len(response)) for sub in response: self.assertTrue(isinstance(sub, CollectionFieldTests.SubModel))
class Muppet(xml_models.Model): name = xml_models.CharField(xpath='/root/kiddie/value') friends = xml_models.CollectionField(xml_models.CharField, xpath='/root/kiddie/friends/friend') finders = {(name, ): "http://foo.com/muppets/%s"}
class ModelC(xml_models.Model): name = xml_models.CharField(xpath='/root/name') modelb = xml_models.CollectionField(ModelB, xpath='/root/modelbs/modelb')
class CollectionModel(xml_models.Model): names = xml_models.CollectionField(xml_models.CharField, xpath='/root/names/name')