def setUp(self): super(BoundFieldTestCase, self).setUp() self.sample = SampleObject( abc=123, # Unused data. hello='world' ) # The metaclasses usually specify name. We'll fake it here. self.abc = BoundField('Basic', name='abc') self.another = BoundField('Missing', name='another', required=False)
class BoundFieldTestCase(unittest.TestCase): def setUp(self): super(BoundFieldTestCase, self).setUp() self.sample = SampleObject( abc=123, # Unused data. hello='world' ) # The metaclasses usually specify name. We'll fake it here. self.abc = BoundField('Basic', name='abc') self.another = BoundField('Missing', name='another', required=False) def test_simple_init(self): base = BoundField('Basic') self.assertEqual(base.name, 'unknown') self.assertEqual(base.is_field, True) self.assertEqual(base.api_name, 'Basic') self.assertEqual(base.snake_name, 'basic') self.assertEqual(base.required, True) def test_complex_init(self): base = BoundField('Complex', required=False) self.assertEqual(base.name, 'unknown') self.assertEqual(base.is_field, True) self.assertEqual(base.api_name, 'Complex') self.assertEqual(base.snake_name, 'complex') self.assertEqual(base.required, False) def test_get_python(self): # Should find data. self.assertEqual(self.abc.get_python(self.sample), 123) # Will miss. with self.assertRaises(KeyError): self.another.get_python(self.sample) def test_set_python(self): self.assertEqual(self.sample._data['abc'], 123) self.abc.set_python(self.sample, 234) self.assertEqual(self.sample._data['abc'], 234) with self.assertRaises(KeyError): self.sample._data['another'] self.another.set_python(self.sample, 'athing') self.assertEqual(self.sample._data['another'], 'athing') def test_get_api(self): # Should find data. self.assertEqual(self.abc.get_api(self.sample), 123) # Will miss. with self.assertRaises(KeyError): self.another.get_api(self.sample) def test_set_api(self): self.assertEqual(self.sample._data['abc'], 123) self.abc.set_api(self.sample, 234) self.assertEqual(self.sample._data['abc'], 234) with self.assertRaises(KeyError): self.sample._data['another'] self.another.set_api(self.sample, 'athing') self.assertEqual(self.sample._data['another'], 'athing') def test_delete(self): self.assertEqual(self.sample._data['abc'], 123) self.abc.delete(self.sample) # Gone now. with self.assertRaises(KeyError): self.sample._data['abc']