def test_enum_setting(self): """Validate 'enum' schema setting.""" # Scalar testing # ############### schema = { 'enum_property': {'enum': {'some_value', 99}} } # Create the type my_type = ontic_type.create_ontic_type('EnumCheck', schema) self.assertIsNotNone(my_type) # Create object of type ontic_object = my_type() # Validate an empty object ontic_type.validate_object(ontic_object) # Validate a good setting ontic_object.enum_property = 99 ontic_type.validate_object(ontic_object) # Validate a bad setting ontic_object.enum_property = 'bad, bad, bad' self.assertRaisesRegexp( ValidationException, "The value \"bad, bad, bad\" for \"enum_property\" " "not in enumeration \[99, 'some_value'\].", ontic_type.validate_object, ontic_object)
def test_validation_exception_handling(self): """Ensure that validate_object handles error reporting.""" schema_instance = SchemaType(some_attr={'type': 'int'}) my_type = ontic_type.create_ontic_type('ValidateCheck', schema_instance) ontic_object = my_type() ontic_object.some_attr = 'WRONG' self.assertRaisesRegexp( ValidationException, r"""The value for "some_attr" is not of type "<type 'int'>": WRONG""", ontic_type.validate_object, ontic_object) expected_errors = [ """The value for "some_attr" is not of type "<type \'int\'>": WRONG"""] try: ontic_type.validate_object(ontic_object) self.fail('ValidationException should have been thrown.') except ValidationException as ve: self.assertListEqual(expected_errors, ve.validation_errors) errors = ontic_type.validate_object(ontic_object, raise_validation_exception=False) self.assertListEqual(expected_errors, errors)
def test_collection_enum_setting(self): """Validate 'enum' schema setting on collections.""" schema = {'enum_property': {'type': 'list', 'enum': {'dog', 'cat'}}} # Create the type my_type = ontic_type.create_ontic_type('EnumListCheck', schema) self.assertIsNotNone(ontic_type) # Create object of type ontic_object = my_type() # Validate an empty object, as required not set. ontic_type.validate_object(ontic_object) # Validate a good setting ontic_object.enum_property = ['dog'] ontic_type.validate_object(ontic_object) # Validate a bad setting ontic_object.enum_property = ['fish'] self.assertRaisesRegexp( ValidationException, r'''The value "fish" for "enum_property" not in''' r''' enumeration \['cat', 'dog'\].''', ontic_type.validate_object, ontic_object)
def test_collection_enum_setting(self): """Validate 'enum' schema setting on collections.""" schema = { 'enum_property': {'type': 'list', 'enum': {'dog', 'cat'}} } # Create the type my_type = ontic_type.create_ontic_type('EnumListCheck', schema) self.assertIsNotNone(ontic_type) # Create object of type ontic_object = my_type() # Validate an empty object, as required not set. ontic_type.validate_object(ontic_object) # Validate a good setting ontic_object.enum_property = ['dog'] ontic_type.validate_object(ontic_object) # Validate a bad setting ontic_object.enum_property = ['fish'] self.assertRaisesRegexp( ValidationException, r'''The value "fish" for "enum_property" not in''' r''' enumeration \['cat', 'dog'\].''', ontic_type.validate_object, ontic_object)
def test_enum_setting(self): """Validate 'enum' schema setting.""" # Scalar testing # ############### schema = {'enum_property': {'enum': {'some_value', 99}}} # Create the type my_type = ontic_type.create_ontic_type('EnumCheck', schema) self.assertIsNotNone(my_type) # Create object of type ontic_object = my_type() # Validate an empty object ontic_type.validate_object(ontic_object) # Validate a good setting ontic_object.enum_property = 99 ontic_type.validate_object(ontic_object) # Validate a bad setting ontic_object.enum_property = 'bad, bad, bad' self.assertRaisesRegexp( ValidationException, "The value \"bad, bad, bad\" for \"enum_property\" " "not in enumeration \[99, 'some_value'\].", ontic_type.validate_object, ontic_object)
def test_required_setting(self): """Validate 'required' schema setting.""" schema = { 'some_property': { 'required': True }, 'other_property': { 'required': False } } # Create the type my_type = ontic_type.create_ontic_type('RequireCheck', schema) self.assertIsNotNone(ontic_type) # Create object of type ontic_object = my_type() # Validate an empty object, which should cause ValueError self.assertRaisesRegexp(ValidationException, 'The value for "some_property" is required.', ontic_type.validate_object, ontic_object) # Validate with data ontic_object.some_property = 'Something' ontic_object.other_property = 'Other' ontic_type.validate_object(ontic_object)
def test_validation_exception_handling(self): """Ensure that validate_object handles error reporting.""" schema_instance = SchemaType(some_attr={'type': 'int'}) my_type = ontic_type.create_ontic_type('ValidateCheck', schema_instance) ontic_object = my_type() ontic_object.some_attr = 'WRONG' self.assertRaisesRegexp( ValidationException, r"""The value for "some_attr" is not of type "<type 'int'>": WRONG""", ontic_type.validate_object, ontic_object) expected_errors = [ """The value for "some_attr" is not of type "<type \'int\'>": WRONG""" ] try: ontic_type.validate_object(ontic_object) self.fail('ValidationException should have been thrown.') except ValidationException as ve: self.assertListEqual(expected_errors, ve.validation_errors) errors = ontic_type.validate_object(ontic_object, raise_validation_exception=False) self.assertListEqual(expected_errors, errors)
def test_type_setting(self): """Validate 'type' schema setting.""" schema = { 'bool_property': {'type': 'bool'}, 'dict_property': {'type': 'dict'}, 'float_property': {'type': 'float'}, 'int_property': {'type': 'int'}, 'list_property': {'type': 'list'}, 'set_property': {'type': 'set'}, 'str_property': {'type': 'str'}, 'date_property': {'type': 'date'}, 'time_property': {'type': 'time'}, 'datetime_property': {'type': 'datetime'}, } # Create the type my_type = ontic_type.create_ontic_type('TypeCheck', schema) self.assertIsNotNone(ontic_type) # Create object of type ontic_object = my_type() # Validate an empty object. ontic_type.validate_object(ontic_object) # Validate with known good data. ontic_object.bool_property = True ontic_object.dict_property = {'some_key': 'some_value'} ontic_object.float_property = 3.4 ontic_object.int_property = 5 ontic_object.list_property = [5, 6, 7] ontic_object.set_property = {'dog', 'cat', 'mouse'} ontic_object.str_property = 'some_string' ontic_object.date_property = date(2000, 1, 1) ontic_object.time_property = time(12, 30, 30) ontic_object.datetime_property = datetime(2001, 1, 1, 12, 30, 30) ontic_type.validate_object(ontic_object) # Validate with known bad data. ontic_object.bool_property = 'Dog' self.assertRaisesRegexp( ValidationException, """The value for "bool_property" is not of type "<type 'bool'>": Dog""", ontic_type.validate_object, ontic_object) ontic_object.bool_property = True # Validate a string vs a list type ontic_object.list_property = 'some_string' self.assertRaisesRegexp( ValidationException, """The value for "list_property" is not of type "<type 'list'>": some_string""", ontic_type.validate_object, ontic_object)
def test_regex_setting(self): """Validate 'regex' schema setting.""" schema = { 'b_only_property': {'type': 'str', 'regex': '^b+'} } my_type = ontic_type.create_ontic_type('RegexCheck', schema) self.assertIsNotNone(ontic_type) ontic_object = my_type() # None test, with no required fields ontic_type.validate_object(ontic_object) # Good test ontic_object.b_only_property = '' ontic_type.validate_object(ontic_object) ontic_object.b_only_property = 'b' ontic_type.validate_object(ontic_object) # Bad test ontic_object.b_only_property = 'a' self.assertRaisesRegexp( ValidationException, 'Value \"a\" for b_only_property does not ' 'meet regex: \^b\+', ontic_type.validate_object, ontic_object)
def test_collection_regex_setting(self): """Validate string collection with 'regex' setting.""" schema = { 'set_property': { 'type': set, 'member_type': str, 'regex': 'b+' } } my_type = ontic_type.create_ontic_type('CollectionRegexCheck', schema) self.assertIsNotNone(ontic_type) ontic_object = my_type() # None test, with no required fields. ontic_type.validate_object(ontic_object) # Good test ontic_object.set_property = set() ontic_type.validate_object(ontic_object) ontic_object.set_property.add('bbbbb') ontic_type.validate_object(ontic_object) # Bad test ontic_object.set_property.add('xxxxxx') self.assertRaisesRegexp( ValidationException, r'''Value "xxxxxx" for "set_property" does not meet regex: b+''', ontic_type.validate_object, ontic_object)
def test_item_type_setting(self): """Validate 'member_type' setting.""" schema = { 'list_property': {'type': 'list', 'member_type': 'str'} } my_type = ontic_type.create_ontic_type('ItemTypeCheck', schema) self.assertIsNotNone(ontic_type) ontic_object = my_type() # None test, with no required fields. ontic_type.validate_object(ontic_object) # Good test ontic_object.list_property = [] ontic_type.validate_object(ontic_object) ontic_object.list_property.append('some_item') ontic_type.validate_object(ontic_object) # Bad test ontic_object.list_property.append(99) self.assertRaisesRegexp( ValidationException, r'''The value "99" for "list_property" is not of type ''' r'''"<type 'str'>".''', ontic_type.validate_object, ontic_object)
def test_collection_regex_setting(self): """Validate string collection with 'regex' setting.""" schema = { 'set_property': {'type': set, 'member_type': str, 'regex': 'b+'} } my_type = ontic_type.create_ontic_type( 'CollectionRegexCheck', schema) self.assertIsNotNone(ontic_type) ontic_object = my_type() # None test, with no required fields. ontic_type.validate_object(ontic_object) # Good test ontic_object.set_property = set() ontic_type.validate_object(ontic_object) ontic_object.set_property.add('bbbbb') ontic_type.validate_object(ontic_object) # Bad test ontic_object.set_property.add('xxxxxx') self.assertRaisesRegexp( ValidationException, r'''Value "xxxxxx" for "set_property" does not meet regex: b+''', ontic_type.validate_object, ontic_object)
def register_sink(self, address='*', port=60053, protocol='tcp', type='push'): sink = Sink(id=int(uuid.uuid4()), address=address, port=port, protocol=protocol, type=type) self.log.debug('register_sink: %s', sink) try: ontic_type.validate_object(sink) except ValidationException as ve: raise ValueError(ve.message) self.sink_configs[sink.id] = sink # todo: raulg - add activating the sink if the core is running return deepcopy(sink)
def register_requester(self, address='localhost', port=60053, protocol='tcp', type='request'): requester = Requester(id=int(uuid.uuid4()), address=address, port=port, protocol=protocol, type=type) self.log.info('register_requester: %s', requester) try: ontic_type.validate_object(requester) except ValidationException as ve: raise ValueError(ve.message) self._requester_configs[requester.id] = requester # todo: raulg - add activating the requester if the core is running return deepcopy(requester)
def register_responder(self, address='*', port=60053, protocol='tcp', type='reply'): responder = Responder(id=int(uuid.uuid4()), address=address, port=port, protocol=protocol, type=type) self.log.info('register_responder: %s', responder) try: ontic_type.validate_object(responder) except ValidationException as ve: raise ValueError(ve.message) self._responder_configs[responder.id] = responder # todo: raul - add the actual code return deepcopy(responder)
def register_responder(self, address='*', port=60053, protocol='tcp', type='reply'): responder = Responder( id=int(uuid.uuid4()), address=address, port=port, protocol=protocol, type=type) self.log.info('register_responder: %s', responder) try: ontic_type.validate_object(responder) except ValidationException as ve: raise ValueError(ve.message) self._responder_configs[responder.id] = responder # todo: raul - add the actual code return deepcopy(responder)
def register_requester(self, address='localhost', port=60053, protocol='tcp', type='request'): requester = Requester( id=int(uuid.uuid4()), address=address, port=port, protocol=protocol, type=type) self.log.info('register_requester: %s', requester) try: ontic_type.validate_object(requester) except ValidationException as ve: raise ValueError(ve.message) self._requester_configs[requester.id] = requester # todo: raulg - add activating the requester if the core is running return deepcopy(requester)
def register_sink(self, address='*', port=60053, protocol='tcp', type='push'): sink = Sink( id=int(uuid.uuid4()), address=address, port=port, protocol=protocol, type=type) self.log.debug('register_sink: %s', sink) try: ontic_type.validate_object(sink) except ValidationException as ve: raise ValueError(ve.message) self.sink_configs[sink.id] = sink # todo: raulg - add activating the sink if the core is running return deepcopy(sink)
def handle_msg(self): """ """ msg = self._listener.recv() self.log.debug('handle_msg: "%s"', msg) if not msg: self.log.error('Empty message delivered.') return cmd_load = json.loads(msg) self.log.debug('cmd_load: %s', cmd_load) if not cmd_load: self.log.error('Empty command message delivered.') return # todo: raul - this is where I need a string to model converter cmd_msg = string_to_ontic.transform(CommandMessage, cmd_load) ontic_type.validate_object(cmd_msg) if cmd_msg.cmd == CommandMessage.CMD_KILL: self.log.info('Received kill message.') self.event_core._stopped = True return
def test_required_setting(self): """Validate 'required' schema setting.""" schema = { 'some_property': {'required': True}, 'other_property': {'required': False} } # Create the type my_type = ontic_type.create_ontic_type('RequireCheck', schema) self.assertIsNotNone(ontic_type) # Create object of type ontic_object = my_type() # Validate an empty object, which should cause ValueError self.assertRaisesRegexp( ValidationException, 'The value for "some_property" is required.', ontic_type.validate_object, ontic_object) # Validate with data ontic_object.some_property = 'Something' ontic_object.other_property = 'Other' ontic_type.validate_object(ontic_object)
def test_regex_setting(self): """Validate 'regex' schema setting.""" schema = {'b_only_property': {'type': 'str', 'regex': '^b+'}} my_type = ontic_type.create_ontic_type('RegexCheck', schema) self.assertIsNotNone(ontic_type) ontic_object = my_type() # None test, with no required fields ontic_type.validate_object(ontic_object) # Good test ontic_object.b_only_property = '' ontic_type.validate_object(ontic_object) ontic_object.b_only_property = 'b' ontic_type.validate_object(ontic_object) # Bad test ontic_object.b_only_property = 'a' self.assertRaisesRegexp( ValidationException, 'Value \"a\" for b_only_property does not ' 'meet regex: \^b\+', ontic_type.validate_object, ontic_object)
def test_item_type_setting(self): """Validate 'member_type' setting.""" schema = {'list_property': {'type': 'list', 'member_type': 'str'}} my_type = ontic_type.create_ontic_type('ItemTypeCheck', schema) self.assertIsNotNone(ontic_type) ontic_object = my_type() # None test, with no required fields. ontic_type.validate_object(ontic_object) # Good test ontic_object.list_property = [] ontic_type.validate_object(ontic_object) ontic_object.list_property.append('some_item') ontic_type.validate_object(ontic_object) # Bad test ontic_object.list_property.append(99) self.assertRaisesRegexp( ValidationException, r'''The value "99" for "list_property" is not of type ''' r'''"<type 'str'>".''', ontic_type.validate_object, ontic_object)
def test_perfect_collection_default_copy(self): """Ensure that collection default settings are handled correctly.""" # Configure default collection. default_dict = {'key': 'value'} default_list = ['item'] inner_tuple = (1, 2) outer_tuple = (inner_tuple, 3, 4) default_set = {'entity', outer_tuple} # Configure default collections to test deep copy behavior. ontic_object = ontic_type.OnticType() ontic_object.dict = default_dict default_deep_dict = {'name': default_dict} default_deep_list = [default_dict] default_deep_set = {(inner_tuple, outer_tuple)} schema_def = SchemaType({ 'dict_no_default': { 'type': 'dict', }, 'list_no_default': { 'type': 'list', }, 'set_no_default': { 'type': 'set', }, 'dict_with_default': { 'type': 'dict', 'default': default_dict, }, 'list_with_default': { 'type': 'list', 'default': default_list, }, 'set_with_default': { 'type': 'set', 'default': default_set, }, 'dict_deep_default': { 'type': 'dict', 'default': default_deep_dict, }, 'list_deep_default': { 'type': 'list', 'default': default_deep_list, }, 'set_deep_default': { 'type': 'set', 'default': default_deep_set, }, }) # Execute test subject. my_type = ontic_type.create_ontic_type('CollectionDefaults', schema_def) my_object = my_type() ontic_type.perfect_object(my_object) ontic_type.validate_object(my_object) # Assert the no default state. self.assertIsNone(my_object.dict_no_default) self.assertIsNone(my_object.list_no_default) self.assertIsNone(my_object.set_no_default) # Assert equality and copy of defaults. self.assertDictEqual(default_dict, my_object.dict_with_default) self.assertIsNot(default_dict, my_object.dict_with_default) self.assertListEqual(default_list, my_object.list_with_default) self.assertIsNot(default_list, my_object.list_with_default) self.assertSetEqual(default_set, my_object.set_with_default) self.assertIsNot(default_set, my_object.set_with_default) # Assert equality and copy of deep defaults. self.assertDictEqual(default_dict, my_object.dict_deep_default['name']) self.assertIsNot(default_deep_dict['name'], my_object.dict_deep_default['name']) self.assertDictEqual(default_dict, my_object.list_deep_default[0]) self.assertIsNot(default_deep_list[0], my_object.list_deep_default[0]) self.assertSetEqual(default_deep_set, my_object.set_deep_default) self.assertIsNot(default_deep_set, my_object.set_deep_default)
def test_type_setting(self): """Validate 'type' schema setting.""" schema = { 'bool_property': { 'type': 'bool' }, 'dict_property': { 'type': 'dict' }, 'float_property': { 'type': 'float' }, 'int_property': { 'type': 'int' }, 'list_property': { 'type': 'list' }, 'set_property': { 'type': 'set' }, 'str_property': { 'type': 'str' }, 'date_property': { 'type': 'date' }, 'time_property': { 'type': 'time' }, 'datetime_property': { 'type': 'datetime' }, } # Create the type my_type = ontic_type.create_ontic_type('TypeCheck', schema) self.assertIsNotNone(ontic_type) # Create object of type ontic_object = my_type() # Validate an empty object. ontic_type.validate_object(ontic_object) # Validate with known good data. ontic_object.bool_property = True ontic_object.dict_property = {'some_key': 'some_value'} ontic_object.float_property = 3.4 ontic_object.int_property = 5 ontic_object.list_property = [5, 6, 7] ontic_object.set_property = {'dog', 'cat', 'mouse'} ontic_object.str_property = 'some_string' ontic_object.date_property = date(2000, 1, 1) ontic_object.time_property = time(12, 30, 30) ontic_object.datetime_property = datetime(2001, 1, 1, 12, 30, 30) ontic_type.validate_object(ontic_object) # Validate with known bad data. ontic_object.bool_property = 'Dog' self.assertRaisesRegexp( ValidationException, """The value for "bool_property" is not of type "<type 'bool'>": Dog""", ontic_type.validate_object, ontic_object) ontic_object.bool_property = True # Validate a string vs a list type ontic_object.list_property = 'some_string' self.assertRaisesRegexp( ValidationException, """The value for "list_property" is not of type "<type 'list'>": some_string""", ontic_type.validate_object, ontic_object)
def test_min_setting(self): """Validate 'min' schema setting.""" schema = { 'str_min_property': { 'type': 'str', 'min': 5 }, 'int_min_property': { 'type': 'int', 'min': 10 }, 'float_min_property': { 'type': 'float', 'min': 20 }, 'list_min_property': { 'type': 'list', 'min': 1 }, 'set_min_property': { 'type': 'set', 'min': 1 }, 'dict_min_property': { 'type': 'dict', 'min': 1 }, 'date_min_property': { 'type': 'date', 'min': date(2000, 1, 1) }, 'time_min_property': { 'type': 'time', 'min': time(12, 30, 30) }, 'datetime_min_property': { 'type': 'datetime', 'min': datetime(2000, 1, 1, 12, 30, 30) } } my_type = ontic_type.create_ontic_type('MinCheck', schema) self.assertIsNotNone(ontic_type) ontic_object = my_type() # None test, with no required fields ontic_type.validate_object(ontic_object) # Good test ontic_object.str_min_property = '8 letters' ontic_object.int_min_property = 20 ontic_object.float_min_property = 30.0 ontic_object.list_min_property = ['one item'] ontic_object.set_min_property = {'one item'} ontic_object.dict_min_property = {'some_kee': 'one item'} ontic_object.date_min_property = date(2001, 1, 1) ontic_object.time_min_property = time(13, 30, 30) ontic_object.datetime_min_property = datetime(2001, 1, 1) ontic_type.validate_object(ontic_object) # Str failure ontic_object.str_min_property = '1' self.assertRaisesRegexp( ValidationException, 'The value of "1" for "str_min_property" ' 'fails min of 5.', ontic_type.validate_object, ontic_object) ontic_object.str_min_property = '8 letters' # Int failure ontic_object.int_min_property = 5 self.assertRaisesRegexp( ValidationException, 'The value of "5" for "int_min_property" ' 'fails min of 10.', ontic_type.validate_object, ontic_object) ontic_object.int_min_property = 20 # Float failure ontic_object.float_min_property = 15.0 self.assertRaisesRegexp( ValidationException, 'The value of "15.0" for "float_min_property" ' 'fails min of 20.', ontic_type.validate_object, ontic_object) ontic_object.float_min_property = 30.0 # List failure ontic_object.list_min_property = list() self.assertRaisesRegexp( ValidationException, 'The value of "\[]" for "list_min_property" ' 'fails min of 1.', ontic_type.validate_object, ontic_object) ontic_object.list_min_property = ['one item'] # Set failure ontic_object.set_min_property = set() self.assertRaisesRegexp( ValidationException, 'The value of "set\(\[]\)" for ' '"set_min_property" fails min of 1.', ontic_type.validate_object, ontic_object) ontic_object.set_min_property = {'one item'} # Dict failure ontic_object.dict_min_property = dict() self.assertRaisesRegexp( ValidationException, 'The value of "{}" for "dict_min_property" ' 'fails min of 1.', ontic_type.validate_object, ontic_object) ontic_object.dict_min_property = {'some_key': 'one_item'} # Date failure ontic_object.date_min_property = date(1999, 1, 1) self.assertRaisesRegexp(ValidationException, 'date_min_property" fails min of 2000-01-01.', ontic_type.validate_object, ontic_object) ontic_object.date_min_property = date(2001, 1, 1) # Time failure ontic_object.time_min_property = time(11, 30, 30) self.assertRaisesRegexp( ValidationException, 'The value of "11:30:30" for "time_min_property" ' 'fails min of 12:30:30.', ontic_type.validate_object, ontic_object) ontic_object.time_min_property = time(13, 30, 30) # Datetime failure ontic_object.datetime_min_property = datetime(1999, 1, 1, 11, 30, 30) self.assertRaisesRegexp( ValidationException, 'The value of "1999-01-01 11:30:30" for "datetime_min_property" ' 'fails min of 2000-01-01 12:30:30.', ontic_type.validate_object, ontic_object)
def test_max_setting(self): """Validate 'max' schema setting.""" schema = { 'str_max_property': { 'type': 'str', 'max': 5 }, 'int_max_property': { 'type': 'int', 'max': 10 }, 'float_max_property': { 'type': 'float', 'max': 20 }, 'list_max_property': { 'type': 'list', 'max': 1 }, 'set_max_property': { 'type': 'set', 'max': 1 }, 'dict_max_property': { 'type': 'dict', 'max': 1 }, 'date_max_property': { 'type': 'date', 'max': date(2000, 1, 1) }, 'time_max_property': { 'type': 'time', 'max': time(12, 30, 30) }, 'datetime_max_property': { 'type': 'datetime', 'max': datetime(2000, 1, 1, 12, 30, 30) } } my_type = ontic_type.create_ontic_type('MaxCheck', schema) self.assertIsNotNone(ontic_type) ontic_object = my_type() # None test, with no required fields ontic_type.validate_object(ontic_object) # Good test ontic_object.str_max_property = 'small' ontic_object.int_max_property = 5 ontic_object.float_max_property = 10.0 ontic_object.list_max_property = ['one item'] ontic_object.set_max_property = {'one item'} ontic_object.dict_max_property = {'some_kee': 'one item'} ontic_object.date_max_property = date(1999, 1, 1) ontic_object.time_max_property = time(11, 30, 30) ontic_object.datetime_max_property = datetime(1999, 1, 1) ontic_type.validate_object(ontic_object) # Str failure ontic_object.str_max_property = '8 letters' self.assertRaisesRegexp( ValidationException, 'The value of "8 letters" for ' '"str_max_property" fails max of 5.', ontic_type.validate_object, ontic_object) ontic_object.str_max_property = 'small' # Int failure ontic_object.int_max_property = 20 self.assertRaisesRegexp( ValidationException, 'The value of "20" for "int_max_property" ' 'fails max of 10.', ontic_type.validate_object, ontic_object) ontic_object.int_max_property = 5 # Float failure ontic_object.float_max_property = 30.0 self.assertRaisesRegexp( ValidationException, 'The value of "30.0" for "float_max_property" fails max of 20.', ontic_type.validate_object, ontic_object) ontic_object.float_max_property = 15.0 # List failure ontic_object.list_max_property = ['one item', 'two item'] self.assertRaisesRegexp( ValidationException, 'The value of "\[\'one item\', \'two item\'\]" ' 'for "list_max_property" fails max of 1.', ontic_type.validate_object, ontic_object) ontic_object.list_max_property = ['one item'] # Set failure ontic_object.set_max_property = {'one item', 'two item'} self.assertRaisesRegexp( ValidationException, 'The value of "set\(\[\'one item\', \'two item\'\]\)" ' 'for "set_max_property" fails max of 1.', ontic_type.validate_object, ontic_object) ontic_object.set_max_property = {'one item'} # Dict failure ontic_object.dict_max_property = { 'some_key': 'one_item', 'another_key': 'two_item' } self.assertRaisesRegexp( ValidationException, 'The value of "{\'another_key\': \'two_item\', \'some_key\': ' '\'one_item\'}" for ' '"dict_max_property" fails max of 1.', ontic_type.validate_object, ontic_object) ontic_object.dict_max_property = {'some_key': 'one_item'} # Date failure ontic_object.date_max_property = date(2001, 1, 1) self.assertRaisesRegexp( ValidationException, 'The value of "2001-01-01" for ' '"date_max_property" fails max of 2000-01-01.', ontic_type.validate_object, ontic_object) ontic_object.date_max_property = date(2001, 1, 1) # Time failure ontic_object.time_max_property = time(13, 30, 30) self.assertRaisesRegexp( ValidationException, 'The value of "13:30:30" for "time_max_property" ' 'fails max of 12:30:30.', ontic_type.validate_object, ontic_object) ontic_object.time_max_property = time(13, 30, 30) # Datetime failure ontic_object.datetime_max_property = datetime(2001, 1, 1, 11, 30, 30) self.assertRaisesRegexp( ValidationException, 'The value of "2001-01-01 11:30:30" for "datetime_max_property" ' 'fails max of 2000-01-01 12:30:30.', ontic_type.validate_object, ontic_object)
def test_item_max_setting(self): """Validate 'member_max' setting.""" # Test the item max setting for string items. schema = { 'list_property': { 'type': 'list', 'member_type': 'str', 'member_max': 4} } my_type = ontic_type.create_ontic_type('StrItemMinCheck', schema) self.assertIsNotNone(my_type) ontic_object = my_type() # None test, with no required fields. ontic_type.validate_object(ontic_object) # Good Test ontic_object.list_property = [] ontic_type.validate_object(ontic_object) ontic_object.list_property.append('four') ontic_type.validate_object(ontic_object) # Bad Test ontic_object.list_property.append('seven') self.assertRaisesRegexp( ValidationException, r'''The value of "seven" for "list_property" ''' r'''fails max length of 4.''', ontic_type.validate_object, ontic_object) # Test the item min setting for numeric items. schema = { 'list_property': { 'type': 'list', 'member_type': 'int', 'member_max': 4} } my_type = ontic_type.create_ontic_type('StrItemMinCheck', schema) self.assertIsNotNone(ontic_type) ontic_object = my_type() # None test, with no required fields. ontic_type.validate_object(ontic_object) # Good Test ontic_object.list_property = [] ontic_type.validate_object(ontic_object) ontic_object.list_property.append(4) ontic_type.validate_object(ontic_object) # Bad Test ontic_object.list_property.append(7) self.assertRaisesRegexp( ValidationException, r'''The value of "7" for "list_property" fails max size of 4.''', ontic_type.validate_object, ontic_object)
def test_item_max_setting(self): """Validate 'member_max' setting.""" # Test the item max setting for string items. schema = { 'list_property': { 'type': 'list', 'member_type': 'str', 'member_max': 4 } } my_type = ontic_type.create_ontic_type('StrItemMinCheck', schema) self.assertIsNotNone(my_type) ontic_object = my_type() # None test, with no required fields. ontic_type.validate_object(ontic_object) # Good Test ontic_object.list_property = [] ontic_type.validate_object(ontic_object) ontic_object.list_property.append('four') ontic_type.validate_object(ontic_object) # Bad Test ontic_object.list_property.append('seven') self.assertRaisesRegexp( ValidationException, r'''The value of "seven" for "list_property" ''' r'''fails max length of 4.''', ontic_type.validate_object, ontic_object) # Test the item min setting for numeric items. schema = { 'list_property': { 'type': 'list', 'member_type': 'int', 'member_max': 4 } } my_type = ontic_type.create_ontic_type('StrItemMinCheck', schema) self.assertIsNotNone(ontic_type) ontic_object = my_type() # None test, with no required fields. ontic_type.validate_object(ontic_object) # Good Test ontic_object.list_property = [] ontic_type.validate_object(ontic_object) ontic_object.list_property.append(4) ontic_type.validate_object(ontic_object) # Bad Test ontic_object.list_property.append(7) self.assertRaisesRegexp( ValidationException, r'''The value of "7" for "list_property" fails max size of 4.''', ontic_type.validate_object, ontic_object)
def test_max_setting(self): """Validate 'max' schema setting.""" schema = { 'str_max_property': {'type': 'str', 'max': 5}, 'int_max_property': {'type': 'int', 'max': 10}, 'float_max_property': {'type': 'float', 'max': 20}, 'list_max_property': {'type': 'list', 'max': 1}, 'set_max_property': {'type': 'set', 'max': 1}, 'dict_max_property': {'type': 'dict', 'max': 1}, 'date_max_property': {'type': 'date', 'max': date(2000, 1, 1)}, 'time_max_property': {'type': 'time', 'max': time(12, 30, 30)}, 'datetime_max_property': { 'type': 'datetime', 'max': datetime(2000, 1, 1, 12, 30, 30)} } my_type = ontic_type.create_ontic_type('MaxCheck', schema) self.assertIsNotNone(ontic_type) ontic_object = my_type() # None test, with no required fields ontic_type.validate_object(ontic_object) # Good test ontic_object.str_max_property = 'small' ontic_object.int_max_property = 5 ontic_object.float_max_property = 10.0 ontic_object.list_max_property = ['one item'] ontic_object.set_max_property = {'one item'} ontic_object.dict_max_property = {'some_kee': 'one item'} ontic_object.date_max_property = date(1999, 1, 1) ontic_object.time_max_property = time(11, 30, 30) ontic_object.datetime_max_property = datetime(1999, 1, 1) ontic_type.validate_object(ontic_object) # Str failure ontic_object.str_max_property = '8 letters' self.assertRaisesRegexp( ValidationException, 'The value of "8 letters" for ' '"str_max_property" fails max of 5.', ontic_type.validate_object, ontic_object) ontic_object.str_max_property = 'small' # Int failure ontic_object.int_max_property = 20 self.assertRaisesRegexp( ValidationException, 'The value of "20" for "int_max_property" ' 'fails max of 10.', ontic_type.validate_object, ontic_object) ontic_object.int_max_property = 5 # Float failure ontic_object.float_max_property = 30.0 self.assertRaisesRegexp( ValidationException, 'The value of "30.0" for "float_max_property" fails max of 20.', ontic_type.validate_object, ontic_object) ontic_object.float_max_property = 15.0 # List failure ontic_object.list_max_property = ['one item', 'two item'] self.assertRaisesRegexp( ValidationException, 'The value of "\[\'one item\', \'two item\'\]" ' 'for "list_max_property" fails max of 1.', ontic_type.validate_object, ontic_object) ontic_object.list_max_property = ['one item'] # Set failure ontic_object.set_max_property = {'one item', 'two item'} self.assertRaisesRegexp( ValidationException, 'The value of "set\(\[\'one item\', \'two item\'\]\)" ' 'for "set_max_property" fails max of 1.', ontic_type.validate_object, ontic_object) ontic_object.set_max_property = {'one item'} # Dict failure ontic_object.dict_max_property = {'some_key': 'one_item', 'another_key': 'two_item'} self.assertRaisesRegexp( ValidationException, 'The value of "{\'another_key\': \'two_item\', \'some_key\': ' '\'one_item\'}" for ' '"dict_max_property" fails max of 1.', ontic_type.validate_object, ontic_object) ontic_object.dict_max_property = {'some_key': 'one_item'} # Date failure ontic_object.date_max_property = date(2001, 1, 1) self.assertRaisesRegexp( ValidationException, 'The value of "2001-01-01" for ' '"date_max_property" fails max of 2000-01-01.', ontic_type.validate_object, ontic_object) ontic_object.date_max_property = date(2001, 1, 1) # Time failure ontic_object.time_max_property = time(13, 30, 30) self.assertRaisesRegexp( ValidationException, 'The value of "13:30:30" for "time_max_property" ' 'fails max of 12:30:30.', ontic_type.validate_object, ontic_object) ontic_object.time_max_property = time(13, 30, 30) # Datetime failure ontic_object.datetime_max_property = datetime(2001, 1, 1, 11, 30, 30) self.assertRaisesRegexp( ValidationException, 'The value of "2001-01-01 11:30:30" for "datetime_max_property" ' 'fails max of 2000-01-01 12:30:30.', ontic_type.validate_object, ontic_object)
def test_min_setting(self): """Validate 'min' schema setting.""" schema = { 'str_min_property': {'type': 'str', 'min': 5}, 'int_min_property': {'type': 'int', 'min': 10}, 'float_min_property': {'type': 'float', 'min': 20}, 'list_min_property': {'type': 'list', 'min': 1}, 'set_min_property': {'type': 'set', 'min': 1}, 'dict_min_property': {'type': 'dict', 'min': 1}, 'date_min_property': {'type': 'date', 'min': date(2000, 1, 1)}, 'time_min_property': {'type': 'time', 'min': time(12, 30, 30)}, 'datetime_min_property': { 'type': 'datetime', 'min': datetime(2000, 1, 1, 12, 30, 30)} } my_type = ontic_type.create_ontic_type('MinCheck', schema) self.assertIsNotNone(ontic_type) ontic_object = my_type() # None test, with no required fields ontic_type.validate_object(ontic_object) # Good test ontic_object.str_min_property = '8 letters' ontic_object.int_min_property = 20 ontic_object.float_min_property = 30.0 ontic_object.list_min_property = ['one item'] ontic_object.set_min_property = {'one item'} ontic_object.dict_min_property = {'some_kee': 'one item'} ontic_object.date_min_property = date(2001, 1, 1) ontic_object.time_min_property = time(13, 30, 30) ontic_object.datetime_min_property = datetime(2001, 1, 1) ontic_type.validate_object(ontic_object) # Str failure ontic_object.str_min_property = '1' self.assertRaisesRegexp( ValidationException, 'The value of "1" for "str_min_property" ' 'fails min of 5.', ontic_type.validate_object, ontic_object) ontic_object.str_min_property = '8 letters' # Int failure ontic_object.int_min_property = 5 self.assertRaisesRegexp( ValidationException, 'The value of "5" for "int_min_property" ' 'fails min of 10.', ontic_type.validate_object, ontic_object) ontic_object.int_min_property = 20 # Float failure ontic_object.float_min_property = 15.0 self.assertRaisesRegexp( ValidationException, 'The value of "15.0" for "float_min_property" ' 'fails min of 20.', ontic_type.validate_object, ontic_object) ontic_object.float_min_property = 30.0 # List failure ontic_object.list_min_property = list() self.assertRaisesRegexp( ValidationException, 'The value of "\[]" for "list_min_property" ' 'fails min of 1.', ontic_type.validate_object, ontic_object) ontic_object.list_min_property = ['one item'] # Set failure ontic_object.set_min_property = set() self.assertRaisesRegexp( ValidationException, 'The value of "set\(\[]\)" for ' '"set_min_property" fails min of 1.', ontic_type.validate_object, ontic_object) ontic_object.set_min_property = {'one item'} # Dict failure ontic_object.dict_min_property = dict() self.assertRaisesRegexp( ValidationException, 'The value of "{}" for "dict_min_property" ' 'fails min of 1.', ontic_type.validate_object, ontic_object) ontic_object.dict_min_property = {'some_key': 'one_item'} # Date failure ontic_object.date_min_property = date(1999, 1, 1) self.assertRaisesRegexp( ValidationException, 'date_min_property" fails min of 2000-01-01.', ontic_type.validate_object, ontic_object) ontic_object.date_min_property = date(2001, 1, 1) # Time failure ontic_object.time_min_property = time(11, 30, 30) self.assertRaisesRegexp( ValidationException, 'The value of "11:30:30" for "time_min_property" ' 'fails min of 12:30:30.', ontic_type.validate_object, ontic_object) ontic_object.time_min_property = time(13, 30, 30) # Datetime failure ontic_object.datetime_min_property = datetime(1999, 1, 1, 11, 30, 30) self.assertRaisesRegexp( ValidationException, 'The value of "1999-01-01 11:30:30" for "datetime_min_property" ' 'fails min of 2000-01-01 12:30:30.', ontic_type.validate_object, ontic_object)