예제 #1
0
    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)
예제 #2
0
    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)
예제 #3
0
    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)
예제 #4
0
    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)
예제 #5
0
    def test_base(self):
        """
        """

        input_data = {'name': 'Bob'}
        self.assertEqual('Bob',
                         resource.RestField('name').to_resource(input_data))
예제 #6
0
 def test_against_obj(self):
     """
     """
     obj = TestObj()
     obj.name = 'Bob'
     self.assertEqual('Bob',
                      resource.RestField('name').from_resource(obj, 'name'))
예제 #7
0
    def test_output_only(self):
        """
        """

        input_data = {'size': 'large', 'thing': 'Yep'}

        r = resource.RestField('thing', output_only=True)
        self.assertRaises(resource.OutputOnlyError, r.to_resource, input_data)
예제 #8
0
    def test_required(self):
        """
        """
        input_data = {'name': ''}

        r = resource.RestField('name', required=True)
        self.assertRaises(resource.RequiredFieldError, r.to_resource,
                          input_data)
예제 #9
0
    def test_validation(self):
        """
        Test to ensure that our validation runs if given
        """

        input_data = {'size': 'large', 'thing': 'Yep'}

        r = resource.RestField('size', validator=voluptuous.Coerce(int))
        self.assertRaises(resource.RestValueException, r.to_resource,
                          input_data)
예제 #10
0
    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'
        })
예제 #11
0
    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'})
예제 #12
0
 def test_against_dict(self):
     """
     """
     self.assertEqual(
         'Bob',
         resource.RestField('name').from_resource({'name': 'Bob'}, 'name'))