Example #1
0
    def test_decorator_validation(self):
        #
        # Test required values
        #
        obj = IonObject('Deco_Example', {"list1": [1], "list2": ["One element"], "dict1": {"key1": 1}, "dict2": {"key1": 1}, "us_phone_number": "555-555-5555"})
        # Should fail because required value not provided
        self.assertRaises(AttributeError, obj._validate)

        obj.an_important_value = {"key": "value"}

        # Should work now that we have set a value for the required field
        obj._validate

        #
        # Test collection content types validation
        #
        # List
        obj = IonObject('Deco_Example', {"list1": ["Should be a numeric type"], "list2": ["One element"], "dict1": {"key1": 1}, "dict2": {"key1": 1}, "an_important_value": "good value", "us_phone_number": "555-555-5555"})
        self.assertRaises(AttributeError, obj._validate)

        obj.list1 = [1, 2]

        # Should work now that list content has been corrected
        obj._validate
        
        # Dict
        obj = IonObject('Deco_Example', {"list1": [1], "list2": ["One element"], "dict1": {"key1": "Should be a numeric type"}, "dict2": {"key1": 1}, "an_important_value": "good value", "us_phone_number": "555-555-5555"})
        # Should fail because dict value contains non-numeric value
        self.assertRaises(AttributeError, obj._validate)

        obj.dict1 = {"key1": 1}

        # Should work now that dict value content has been corrected
        obj._validate
        
        #
        # Test collection length
        #
        # List
        obj = IonObject('Deco_Example', {"list1": [1,2], "list2": [], "dict1": {"key1": 1}, "dict2": {"key1": 1}, "an_important_value": "good value", "us_phone_number": "555-555-5555"})
        # Should fail since list has zero length
        self.assertRaises(AttributeError, obj._validate)

        obj.list2 = ["Item 1", "Item 2"]

        # Should work new that item length of list has been corrected
        obj._validate
        
        # Dict
        obj = IonObject('Deco_Example', {"list1": [1,2], "list2": [1,2], "dict1": {"key1": 1}, "dict2": {}, "an_important_value": "good value", "us_phone_number": "555-555-5555"})
        # Should fail since dict has zero length
        self.assertRaises(AttributeError, obj._validate)

        obj.dict2 = {"key1": 1}

        # Should work new that item length of dict has been corrected
        obj._validate

        #
        # Test numeric value range
        #
        # int
        obj = IonObject('Deco_Example', {"list1": [1,2], "list2": ["One element"], "dict1": {"key1": 1}, "dict2": {"key1": 1}, "unsigned_short_int": -1, "an_important_value": "good value", "us_phone_number": "555-555-5555"})
        self.assertRaises(AttributeError, obj._validate)

        obj.unsigned_short_int = 256

        # Should work
        obj._validate

        # float
        obj = IonObject('Deco_Example', {"list1": [1,2], "list2": ["One element"], "dict1": {"key1": 1}, "dict2": {"key1": 1}, "a_float": 10.11, "an_important_value": "good value", "us_phone_number": "555-555-5555"})
        self.assertRaises(AttributeError, obj._validate)

        obj.a_float = 1.234

        # Should work
        obj._validate

        #
        # Test string pattern matching
        #
        obj = IonObject('Deco_Example', {"list1": [1,2], "list2": ["One element"], "dict1": {"key1": 1}, "dict2": {"key1": 1}, "an_important_value": "good value", "us_phone_number": "5555555555"})
        self.assertRaises(AttributeError, obj._validate)

        obj.us_phone_number = "555-555-5555"

        # Should work
        obj._validate
Example #2
0
    def test_decorator_validation(self):
        #
        # Test required values
        #
        obj = IonObject(
            'Deco_Example', {
                "list1": [1],
                "list2": ["One element"],
                "dict1": {
                    "key1": 1
                },
                "dict2": {
                    "key1": 1
                },
                "us_phone_number": "555-555-5555"
            })
        # Should fail because required value not provided
        self.assertRaises(AttributeError, obj._validate)

        obj.an_important_value = {"key": "value"}

        # Should work now that we have set a value for the required field
        obj._validate

        #
        # Test collection content types validation
        #
        # List
        obj = IonObject(
            'Deco_Example', {
                "list1": ["Should be a numeric type"],
                "list2": ["One element"],
                "dict1": {
                    "key1": 1
                },
                "dict2": {
                    "key1": 1
                },
                "an_important_value": "good value",
                "us_phone_number": "555-555-5555"
            })
        self.assertRaises(AttributeError, obj._validate)

        obj.list1 = [1, 2]

        # Should work now that list content has been corrected
        obj._validate

        # Dict
        obj = IonObject(
            'Deco_Example', {
                "list1": [1],
                "list2": ["One element"],
                "dict1": {
                    "key1": "Should be a numeric type"
                },
                "dict2": {
                    "key1": 1
                },
                "an_important_value": "good value",
                "us_phone_number": "555-555-5555"
            })
        # Should fail because dict value contains non-numeric value
        self.assertRaises(AttributeError, obj._validate)

        obj.dict1 = {"key1": 1}

        # Should work now that dict value content has been corrected
        obj._validate

        #
        # Test collection length
        #
        # List
        obj = IonObject(
            'Deco_Example', {
                "list1": [1, 2],
                "list2": [],
                "dict1": {
                    "key1": 1
                },
                "dict2": {
                    "key1": 1
                },
                "an_important_value": "good value",
                "us_phone_number": "555-555-5555"
            })
        # Should fail since list has zero length
        self.assertRaises(AttributeError, obj._validate)

        obj.list2 = ["Item 1", "Item 2"]

        # Should work new that item length of list has been corrected
        obj._validate

        # Dict
        obj = IonObject(
            'Deco_Example', {
                "list1": [1, 2],
                "list2": [1, 2],
                "dict1": {
                    "key1": 1
                },
                "dict2": {},
                "an_important_value": "good value",
                "us_phone_number": "555-555-5555"
            })
        # Should fail since dict has zero length
        self.assertRaises(AttributeError, obj._validate)

        obj.dict2 = {"key1": 1}

        # Should work new that item length of dict has been corrected
        obj._validate

        #
        # Test numeric value range
        #
        # int
        obj = IonObject(
            'Deco_Example', {
                "list1": [1, 2],
                "list2": ["One element"],
                "dict1": {
                    "key1": 1
                },
                "dict2": {
                    "key1": 1
                },
                "unsigned_short_int": -1,
                "an_important_value": "good value",
                "us_phone_number": "555-555-5555"
            })
        self.assertRaises(AttributeError, obj._validate)

        obj.unsigned_short_int = 256

        # Should work
        obj._validate

        # float
        obj = IonObject(
            'Deco_Example', {
                "list1": [1, 2],
                "list2": ["One element"],
                "dict1": {
                    "key1": 1
                },
                "dict2": {
                    "key1": 1
                },
                "a_float": 10.11,
                "an_important_value": "good value",
                "us_phone_number": "555-555-5555"
            })
        self.assertRaises(AttributeError, obj._validate)

        obj.a_float = 1.234

        # Should work
        obj._validate

        #
        # Test string pattern matching
        #
        obj = IonObject(
            'Deco_Example', {
                "list1": [1, 2],
                "list2": ["One element"],
                "dict1": {
                    "key1": 1
                },
                "dict2": {
                    "key1": 1
                },
                "an_important_value": "good value",
                "us_phone_number": "5555555555"
            })
        self.assertRaises(AttributeError, obj._validate)

        obj.us_phone_number = "555-555-5555"

        # Should work
        obj._validate