def test_map(self): """Test dumping and loading a map (dict/complexType)""" d = {"bar":"biz", "foo":"fiz"} xml = xmlize.dumps(d, "map") d2 = xmlize.loads("<result>%s</result>" % xml) print xml assert d2.map == d
def test_map(self): """Test dumping and loading a map (dict/complexType)""" d = {"bar": "biz", "foo": "fiz"} xml = xmlize.dumps(d, "map") d2 = xmlize.loads("<result>%s</result>" % xml) print xml assert d2.map == d
def find(self, cls, filters=None, sort=None, limit=None): """Find objects by filters @param cls: The class of the object to search for @type cls: class or str @param filters: An Optional array of tuples for the filters like [["description", "!=", "foo"], ['name', '=', ['bar', 'biz', 'fizzle']]] @type filters: list @param sort: Optional param to sort on @type sort: str @param limit: Optional limit to how many results are fetched @type limit: int """ if type(cls) == str: class_name = cls cls = xmlize.get_class(class_name) else: class_name = cls.__name__ url = "/%s%s" % (self.routes[class_name], self._build_query(filters, sort, limit)) resp = self.request("GET", url) return xmlize.loads(resp.read())
def _post(self, request, response, id=None): """Create a new resource, or update a single property on an object""" if id: vals = id.split("/",1) if len(vals) > 1: obj = self.db_class.lookup(vals[0]) return self.update_property(request, response, obj, vals[1]) else: obj = self.db_class.lookup(id) if obj: raise Conflict("Object %s already exists" % id) if request.file_extension == "json" or request.accept.best_match(['application/xml', 'application/json']) == "application/json": new_obj = json.loads(request.body) new_obj['__id__'] = id else: new_obj = xmlize.loads(request.body) new_obj.__id__ = id obj = self.create(new_obj, request.user, request) response.set_status(201) if request.file_extension == "json" or request.accept.best_match(['application/xml', 'application/json']) == "application/json": response.content_type = "application/json" response.app_iter = JSONWrapper(iter([obj]), request.user) else: response.content_type = "text/xml" response.write(xmlize.dumps(obj)) return response
def _put(self, request, response, id=None): """Update an existing resource""" obj = None if id: obj = self.db_class.lookup(id) if not obj: raise NotFound() props = {} if "json" in request.content_type: props = json.loads(request.body) elif request.content_type == "application/x-www-form-urlencoded" and not request.body.startswith("<"): props = request.POST.mixed() else: request.content_type = "text/xml" # By default we assume it's XML new_obj = xmlize.loads(request.body) for prop in new_obj.__dict__: prop_value = getattr(new_obj, prop) if not prop.startswith("_"): props[prop] = prop_value obj = self.update(obj, props, request.user, request) if request.file_extension == "json" or request.accept.best_match(['application/xml', 'application/json']) == "application/json": response.content_type = "application/json" response.app_iter = JSONWrapper(iter([obj]), request.user) else: response.content_type = "text/xml" response.write(xmlize.dumps(obj)) return response
def test_datetime(self): """Try encoding and decoding a datetime""" xmlize.register(ObjectTest) obj = ObjectTest() obj.d = datetime.datetime(year=2009, month=9, day=9, hour=9, minute=9, second=9, tzinfo=pytz.utc) sr = xmlize.dumps(obj) obj2 = xmlize.loads(sr) print obj2.d.tzinfo assert obj2.d == obj.d
def test_loads(self): """Test loading a string that we know should work""" obj = xmlize.loads(TEST_XML) assert obj.test_simple == "Simple String" assert obj.test_string == "Another String" assert obj.test_datetime.year == 2009 assert obj.test_datetime.month == 9 assert obj.test_datetime.day == 9 assert obj.test_datetime.hour == 9 assert obj.test_datetime.minute == 9 assert obj.test_datetime.second == 9 assert obj.test_datetime.tzinfo.utcoffset(obj.test_datetime) == datetime.timedelta(hours = -5)
def test_loads(self): """Test loading a string that we know should work""" obj = xmlize.loads(TEST_XML) assert obj.test_simple == "Simple String" assert obj.test_string == "Another String" assert obj.test_datetime.year == 2009 assert obj.test_datetime.month == 9 assert obj.test_datetime.day == 9 assert obj.test_datetime.hour == 9 assert obj.test_datetime.minute == 9 assert obj.test_datetime.second == 9 assert obj.test_datetime.tzinfo.utcoffset( obj.test_datetime) == datetime.timedelta(hours=-5)
def test_str(self): """Test encoding a string""" xmlize.register(ObjectTest) obj = ObjectTest() obj.foo = "bar" obj.name = "Bizzle" obj.id = "12345" sr = xmlize.dumps(obj) obj2 = xmlize.loads(sr) assert obj2.foo == obj.foo assert obj2.name == obj.name assert obj2.__id__ == obj.id
def test_reference(self): """Test dumping a reference""" xmlize.register(ObjectTestReference) xmlize.register(ObjectTestReferenceSub) obj = ObjectTestReference() obj.name = "Parent Object" obj.id = "1234567890" obj_sub = ObjectTestReferenceSub() obj_sub.parent = obj obj_sub.id = "9378509283" obj_xml = xmlize.dumps(obj_sub) obj_loaded = xmlize.loads(obj_xml) print obj_xml assert obj_loaded.parent.__id__ == obj.id
def get_by_id(self, cls, id): """Get an object by ID @param cls: The class of the object to fetch @type cls: class or str @param id: The ID of the object to fetch @type id: str """ assert len(id) > 0 if type(cls) == str: class_name = cls cls = xmlize.get_class(class_name) else: class_name = cls.__name__ url = "%s/%s" % (self.routes[class_name], id) resp = self.request("GET", url) return xmlize.loads(resp.read())
def test_dump_list(self): """Test dumping a simple list""" l = ['admin', 'test', 'developer'] xml = xmlize.dumps(l, 'auth_groups') l2 = xmlize.loads("<result>%s</result>" % xml) assert l2.auth_groups == l