def test_patch_instance(self): obj = Sample(create=True) obj.save() obj2 = Sample(ident=100000) req = Request.blank("", POST="{}") setattr(req, "objects", {"sample": obj}) resp = Sample._patch_instance_handler(req) assert (resp.status_code == 200) req2 = Request.blank("", POST='{"index":1,"name":"brandon"}') setattr(req2, "objects", {"sample": obj}) resp2 = Sample._patch_instance_handler(req2) assert (resp2.status_code == 200) assert (obj.name == "brandon") expected = json.dumps(obj) assert (resp2.body.decode("utf-8") == expected) req3 = Request.blank("", POST='{}') setattr(req3, "objects", {"sample": obj2}) resp3 = Sample._patch_instance_handler(req3) assert (resp3.status_code == 404) req4 = Request.blank("", POST='{"test":1}') setattr(req4, "objects", {"sample": obj}) resp4 = Sample._patch_instance_handler(req4) assert (resp4.status_code == 200) req5 = Request.blank("", POST='{"money":0}') setattr(req5, "objects", {"sample": obj}) resp5 = Sample._patch_instance_handler(req5) assert (resp5.status_code == 422) errors = simplejson.loads(resp5.body.decode("utf-8")) assert len(errors) == 1
def _put_instance_handler(cls, request): obj = request.objects[cls.get_entity_name()] if (obj._entity != None): errors = [] for prop_name in cls._properties: prop = getattr(cls, prop_name) val = request.json_body.get(prop_name, prop.default) var_errors = prop.validate(val) if var_errors: errors += var_errors else: setattr(obj, prop_name, val) if errors: return ValidationError(json.dumps(errors)) else: obj.save() return OK(json.dumps(obj)) else: return NotFound()
def _post_handler(cls, request): parent = None if (cls._parent_model): parent = request.objects.get(cls._parent_model.get_entity_name()) obj = cls(parent=parent, create=True) errors = [] for (prop_name) in cls._properties: prop = getattr(cls, prop_name) val = request.json_body.get(prop_name, prop.default) var_errors = prop.validate(val) if var_errors: errors += var_errors else: setattr(obj, prop_name, val) if errors: return ValidationError(json.dumps(errors)) else: obj.save() return Created(json.dumps(obj))
def _patch_instance_handler(cls, request): obj = request.objects[cls.get_entity_name()] if (obj._entity != None): errors = [] for (key, val) in request.json.items(): prop = getattr(cls, key, None) if prop == None: continue # extra field skip var_errors = prop.validate(val) if var_errors: errors += var_errors else: setattr(obj, key, val) if errors: return ValidationError(json.dumps(errors)) else: obj.save() return OK(json.dumps(obj)) else: return NotFound()
def test_get_instance(self): temp = Sample(create=True) temp.save() obj = Sample(ident=temp.id) req = Request.blank("") setattr(req, "objects", {"sample": obj}) resp = Sample._get_instance_handler(req) assert (resp.status_code == 200) res = resp.body.decode("utf-8") assert res == json.dumps(obj) req2 = Request.blank("") obj2 = Sample(ident=10000000000000) setattr(req2, "objects", {"sample": obj2}) resp2 = Sample._get_instance_handler(req2) assert (resp2.status_code == 404)
def _get_instance_handler(cls, request): obj = request.objects[cls.get_entity_name()] if obj._entity != None: return OK(json.dumps(obj)) else: return NotFound()
def _get_handler(cls, request): parent = None if (cls._parent_model): parent = request.objects.get(cls._parent_model.get_entity_name()) results = cls.list(parent=parent) return OK(json.dumps(list(results)))