def test_init_no_obj(self): fields = ['list', 'of', 'things'] r = resource.Resource(None, fields) self.assertEqual(r.obj, None) self.assertEqual(r.fields, fields)
def test_required_fields(self): fields = [ resource.RestField('name', required=True), resource.RestField('size', required=False), ] data = {'size': 'large'} r = resource.Resource(None, fields) self.assertRaises(resource.RequiredFieldError, r.from_dict, data)
def test_unexpected_fields(self): """ Test To ensure that a dict with keys that are not in fields errors """ data = {'color': 'orange', 'size': 'large'} fields = [] r = resource.Resource(None, fields) self.assertRaises(resource.UnknownFieldError, r.from_dict, data)
def test_base(self): fields = [ resource.RestField('name', required=True), resource.RestField('size', required=False), ] data = {'size': 'large', 'name': 'Bob'} r = resource.Resource(None, fields) result = r.from_dict(data) self.assertDictEqual(result, data)
def test_optional_fields(self): """ Test to ensure optional fields do not error are are not included in resource """ fields = [ resource.RestField('name', required=True), resource.RestField('size', required=False), ] data = {'name': 'Bob'} r = resource.Resource(None, fields) result = r.from_dict(data) self.assertDictEqual(result, data)
def test_output_only_fields(self): """ Test to ensure fields that are output only error if they are in the input. TODO: This should throw a OutputOnlyError - see from_dict """ fields = [ resource.RestField('name', required=True), resource.RestField('size', required=False), resource.RestField('thing', output_only=True), ] data = {'size': 'large', 'thing': 'Yep'} r = resource.Resource(None, fields) self.assertRaises(resource.UnknownFieldError, r.from_dict, data)
def test_base(self): """ """ obj = {'name': 'Bob', 'size': 'large', 'color': 'orange'} fields = [ resource.RestField('name', required=True), resource.RestField('size', required=False), ] r = resource.Resource(obj, fields) result = r.to_dict() # This ensures that other props on the "obj" do not get output to dict self.assertDictEqual(result, { 'name': 'Bob', 'size': 'large', 'resource_type': 'NonDefinedClass' })
def simple_test(self): """ """ fields = [ resource.RestField('sku', output_only=True), resource.RestField('name', required=True), resource.RestField('size') ] # Create A Resource from an object - Base Test obj = {'name': 'Bob', 'size': 'large', 'sku': '1234'} r = resource.Resource(obj, fields) result = r.to_dict() self.assertDictEqual(result, { 'name': 'Bob', 'size': 'large', 'sku': '1234' }) # Create A Resource from a input dict input_data = {'name': 'Bob', 'size': 'large'} result = r.from_dict(input_data) self.assertDictEqual(result, {'name': 'Bob', 'size': 'large'})