def testUpdateWithListOfChildren(self): model_json = '{"listofchildren": [{"name": "ishmael"}]}' model = SampleMutableParent.create(model_json) update_obj = model.to_obj() update_obj['listofchildren'][0]['name'] = "moby" update_json = helpers.obj_to_json(update_obj) model.update(update_json) self.assertEqual(model.listofchildren.first().name, 'moby')
def testNullifyListOfChildren(self): model_json = '{"listofchildren": [{"name": "ishmael"}]}' model = SampleMutableParent.create(model_json) update_obj = model.to_obj() update_obj['listofchildren'] = [] update_json = helpers.obj_to_json(update_obj) model.update(update_json) self.assertEqual(model.listofchildren.count(), 0)
def test_create_verify_hash(self): model = SampleImmutableParent.create(self.parent_json) clean_json = helpers.obj_to_json(json.loads(self.parent_json)) expected_hash = hashlib.sha256(clean_json).hexdigest() self.assertEqual(expected_hash, model._id) self.roundTripJson(model) self.roundTripObj(model)
def testNullifyForeignKey(self): model_json = '{"singlechild": {"name": "ishmael"}}' model = SampleMutableParent.create(model_json) update_obj = model.to_obj() update_obj['singlechild'] = None update_json = helpers.obj_to_json(update_obj) model.update(update_json) self.assertEqual(model.singlechild, None)
def testUpdateWithDictChild(self): model_json = '{"singlechild": {"name": "ishmael"}}' model = SampleMutableParent.create(model_json) update_obj = model.to_obj() update_obj['singlechild']['name'] = "moby" update_json = helpers.obj_to_json(update_obj) model.update(update_json) self.assertEqual(model.singlechild.name, 'moby')
def testShortenListOfChildren(self): model_json = '{"listofchildren": [{"name": "ishmael"}, {"name": "moby"}]}' model = SampleMutableParent.create(model_json) self.assertEqual(model.listofchildren.count(), 2) update_obj = model.to_obj() update_obj['listofchildren'].pop() update_json = helpers.obj_to_json(update_obj) model.update(update_json) self.assertEqual(model.listofchildren.count(), 1)
def test_create_verify_hash(self): model = SampleImmutableParent.create(self.parent_json) clean_json = helpers.obj_to_json( json.loads(self.parent_json) ) expected_hash = hashlib.sha256(clean_json).hexdigest() self.assertEqual(expected_hash, model._id) self.roundTripJson(model) self.roundTripObj(model)
def _obj_to_json(cls, data_obj): return helpers.obj_to_json(data_obj)
def setUp(self): child_obj = {'name': 'one'} parent_obj = {'child': child_obj, 'name': 'one'} self.child_json = helpers.obj_to_json(child_obj) self.parent_json = helpers.obj_to_json(parent_obj)