Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_class_name( cls ):
- return cls.__name__.split('.')[-1]
- def get_property_class_name( cls ):
- name = get_class_name( cls )
- if name.endswith( 'Property' ):
- name = name[:-len('Property')]
- return name
- def pytest_generate_tests(metafunc):
- """Generates tests for test_property_validation & test_mandatory_property.
- """
- if 'property_type_value_iter' in metafunc.funcargnames:
- for property_value in PROPERTY_AND_VALUES.items():
- name = get_property_class_name( property_value[0] )
- metafunc.addcall(funcargs=dict(property_type_value_iter=property_value), id=name)
- else:
- scenario_types_by_arg_name = {
- 'good_validation_scenario_iter': ['good_values'],
- 'bad_validation_scenario_iter': ['bad_values', 'bad_types'],
- 'normalize_validation_scenario_iter': ['normalize_values']
- }
- for arg_name, scenario_types in scenario_types_by_arg_name.items():
- if arg_name in metafunc.funcargnames:
- _generate_property_validation_tests( metafunc, scenario_types, arg_name )
- def _generate_property_validation_tests( metafunc, value_scenarios_types, arg_name ):
- for scenario in VALIDATION_SCENARIOS:
- property = scenario['prop']
- class DBO(dbo.DBObject):
- prop = property
- good_value = scenario['good_values'][0]
- base_name = scenario.get( 'name', get_property_class_name(property.__class__) )
- for check_kind, values in [(k, v) for k, v in scenario.items() if k in value_scenarios_types]:
- for index, value in enumerate(values):
- name = '%s.%s.%d' % (base_name, check_kind, index)
- metafunc.addcall( id=name, funcargs={
- arg_name: check_kind,
- 'dbo_class': DBO,
- 'value': value,
- 'good_value': good_value} )
- def test_mandatory_property( property_type_value_iter ):
- """Iterates over all property types and check if
- mandatory/optional flags are correctly handled.
- """
- property_type, property_data = property_type_value_iter
- property_value = property_data['values'][0]
- mandatory_properties = [property_type( mandatory=True )]
- if not property_data.get('never_pk', False) and not property_data.get('never_mandatory', False):
- mandatory_properties.append( property_type( pk_index=0 ) )
- for mandatory_property in mandatory_properties:
- class DBO(dbo.DBObject):
- mandatory_prop = mandatory_property
- optional_prop = property_type( mandatory=False )
- o = DBO( mandatory_prop=property_value, optional_prop=property_value )
- assert o.mandatory_prop == property_value
- assert o.optional_prop == property_value
- o = DBO( mandatory_prop=property_value, optional_prop=None )
- assert o.mandatory_prop == property_value
- assert o.optional_prop is None
- o = DBO( mandatory_prop=property_value )
- assert o.mandatory_prop == property_value
- assert o.optional_prop is None
- if not property_data.get('never_mandatory', False):
- with py.test.raises(ValueError):
- DBO()
- with py.test.raises(ValueError):
- DBO( mandatory_prop=None, optional_prop=None )
- with py.test.raises(ValueError):
- DBO( mandatory_prop=None )
- with py.test.raises(ValueError):
- DBO( optional_prop=None )
- with py.test.raises(ValueError):
- DBO(optional_prop=property_value)
- def test_property_validation_good_values( good_validation_scenario_iter, dbo_class, value, good_value ):
- """Checks that good values are correctly handled using 'good_values' data."""
- o = dbo_class( prop=value )
- assert o.prop == value
- o = dbo_class()
- o.prop = value
- assert o.prop == value
- def test_property_validation_bad_values_or_types( bad_validation_scenario_iter, dbo_class, value, good_value ):
- """Checks that bad values raise BadPropertyValueError using 'bad_values' and 'bad_types' data."""
- with py.test.raises(dbo.BadPropertyValueError):
- dbo_class( prop=value )
- o = dbo_class( prop=good_value )
- with py.test.raises(dbo.BadPropertyValueError):
- o.prop = value
- def test_property_validation_normalize_value( normalize_validation_scenario_iter, dbo_class, value, good_value ):
- """Checks that good values are correctly normalized when a property is set using 'normalize_values' data."""
- o = dbo_class( prop=value[0] )
- assert o.prop == value[1]
- o = dbo_class()
- o.prop = value[0]
- assert o.prop == value[1]
Advertisement
Add Comment
Please, Sign In to add comment