def test_false_emptiness(self): """Test that Properties with falsy empty values don't throw exceptions""" class SophiesRecord(Record): placeholder = Property() aux_placeholder = Property(empty='') age = Property(empty=0) name = Property(empty=None) sophie = SophiesRecord() with self.assertRaises(AttributeError): sophie.placeholder with self.assertRaises(AttributeError): sophie.aux_placeholder self.assertFalse(sophie.aux_placeholder0) self.assertFalse(sophie.age0) self.assertFalse(sophie.name0) self.assertFalse(sophie.placeholder0) # the properties aren't really set... self.assertEqual(VisitorPattern.visit(sophie), {}) sophie.age = 1 self.assertEqual(VisitorPattern.visit(sophie), {"age": 1}) sophie.age = 0 self.assertEqual(VisitorPattern.visit(sophie), {"age": 0}) del sophie.age self.assertEqual(VisitorPattern.visit(sophie), {})
def test_false_emptiness(self): """Test that Properties with falsy empty values don't throw exceptions""" class SophiesRecord(Record): placeholder = Property() aux_placeholder = Property(default='') age = Property(default=0) name = V1Property(isa=basestring) sophie = SophiesRecord() with self.assertRaises(AttributeError): sophie.placeholder self.assertFalse(sophie.placeholder0) self.assertEqual(sophie.aux_placeholder, '') self.assertFalse(sophie.aux_placeholder0) self.assertEqual(sophie.age, 0) self.assertFalse(sophie.age0) self.assertEqual(sophie.name, None) with self.assertRaises(AttributeError): sophie.name0 sophie.name = "Sophie" self.assertEqual(sophie.name, "Sophie") sophie.name = None self.assertEqual(sophie.name, None) # the properties aren't really set, but VisitorPattern sees them. expected = {"age": 0, "aux_placeholder": ""} self.assertEqual(VisitorPattern.visit(sophie), expected) sophie.age = 1 expected['age'] = 1 self.assertEqual(VisitorPattern.visit(sophie), expected) sophie.age = 0 expected['age'] = 0 self.assertEqual(VisitorPattern.visit(sophie), expected) del sophie.age self.assertEqual(VisitorPattern.visit(sophie), expected)