def test_dict(self): from normalize.coll import dict_of from testclasses import Person Rolodeck = dict_of(Person) deck = Rolodeck({ "bob": Person(id=123, name="Bob"), "peter": Person(id=124, name="Peter"), "eve": Person(id=125, name="Steve"), }) fs1 = FieldSelector(["bob", "id"]) self.assertEqual(fs1.get(deck), 123) self.assertRaisesRegexp(ValueError, r'id is required', fs1.delete, deck) fs2 = FieldSelector(["cuthbert", "id"]) fs2.post(deck, 923) self.assertEqual(deck['cuthbert'].id, 923) FieldSelector(["ruth"]).post(deck, {"id": 523, "name": "Ruth"}) self.assertEqual(deck['ruth'].name, "Ruth") FieldSelector(["bob"]).delete(deck) self.assertNotIn("bob", deck) FieldSelector([None]).delete(deck) self.assertEqual(deck, {})
def test_dict(self): from normalize.coll import dict_of from testclasses import Person Rolodeck = dict_of(Person) deck = Rolodeck({ "bob": Person(id=123, name="Bob"), "peter": Person(id=124, name="Peter"), "eve": Person(id=125, name="Steve"), }) fs1 = FieldSelector(["bob", "id"]) self.assertEqual(fs1.get(deck), 123) self.assertRaisesRegexp( ValueError, r'id is required', fs1.delete, deck, ) fs2 = FieldSelector(["cuthbert", "id"]) fs2.post(deck, 923) self.assertEqual(deck['cuthbert'].id, 923) FieldSelector(["ruth"]).post(deck, {"id": 523, "name": "Ruth"}) self.assertEqual(deck['ruth'].name, "Ruth") FieldSelector(["bob"]).delete(deck) self.assertNotIn("bob", deck) FieldSelector([None]).delete(deck) self.assertEqual(deck, {})
def test_post(self): record = MockComplexJsonRecord() fs = FieldSelector(["name"]) fs.post(record, "Bobby") self.assertEqual(record.name, "Bobby") fs = FieldSelector(["children", 0, "name"]) fs.post(record, "Johnny") self.assertEqual(record.children[0].name, "Johnny") # Test addition to collection fs = FieldSelector(["children", 1, "name"]) fs.post(record, "Susan") self.assertEqual(record.children[1].name, "Susan") # Test invalid addition to collection fs = FieldSelector(["children", 9999, "name"]) with self.assertRaisesRegexp(ValueError, "FieldSelector set out of order"): fs.post(record, "Joker") # Test create of sub-Record fs = FieldSelector(["nested", "name"]) fs.put(record, "Nested") self.assertEqual(record.nested.name, "Nested")
def test_post(self): record = MockComplexJsonRecord() fs = FieldSelector(["name"]) fs.post(record, "Bobby") self.assertEqual(record.name, "Bobby") fs = FieldSelector(["children", 0, "name"]) fs.post(record, "Johnny") self.assertEqual(record.children[0].name, "Johnny") # Test addition to collection fs = FieldSelector(["children", 1, "name"]) fs.post(record, "Susan") self.assertEqual(record.children[1].name, "Susan") # Test invalid addition to collection fs = FieldSelector(["children", 9999, "name"]) with self.assertRaisesRegexp( ValueError, "FieldSelector set out of order" ): fs.post(record, "Joker") # Test create of sub-Record fs = FieldSelector(["nested", "name"]) fs.put(record, "Nested") self.assertEqual(record.nested.name, "Nested")
def test_post_required(self): class FussyRecord(Record): id = Property(required=True) class FussPot(Record): fuss_list = ListProperty(of=FussyRecord) fuss_map = DictProperty(of=FussyRecord) top_fuss = Property(isa=FussyRecord) fp = FussPot() fs1 = FieldSelector(("top_fuss", "id")) fs2 = FieldSelector(("fuss_map", "phew", "id")) fs3 = FieldSelector(("fuss_list", 0, "id")) fs1.post(fp, 1) fs2.post(fp, 2) fs3.post(fp, 3) expected = FussPot( top_fuss={"id": 1}, fuss_map={"phew": { "id": 2 }}, fuss_list=[{ "id": 3 }], )
def test_post_required(self): class FussyRecord(Record): id = Property(required=True) class FussPot(Record): fuss_list = ListProperty(of=FussyRecord) fuss_map = DictProperty(of=FussyRecord) top_fuss = Property(isa=FussyRecord) fp = FussPot() fs1 = FieldSelector(("top_fuss", "id")) fs2 = FieldSelector(("fuss_map", "phew", "id")) fs3 = FieldSelector(("fuss_list", 0, "id")) fs1.post(fp, 1) fs2.post(fp, 2) fs3.post(fp, 3) expected = FussPot( top_fuss={"id": 1}, fuss_map={"phew": {"id": 2}}, fuss_list=[{"id":3}], )