Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def pytest_generate_tests(metafunc):
- """Generates tests for test_property_validation & test_mandatory_property.
- """
- if hasattr( metafunc.function, 'parametrized' ):
- args = metafunc.function.parametrized.args
- kwargs = metafunc.function.parametrized.kwargs
- test_generator, args = args[0], args[1:]
- test_generator( metafunc, *args, **kwargs )
- def _generate_mandatory_property_tests( metafunc, scenarios ):
- for property_value in PROPERTY_AND_VALUES.items():
- name = get_property_class_name( property_value[0] )
- metafunc.addcall(funcargs=dict(property_type_and_data=property_value), id=name)
- @py.test.mark.parametrized( _generate_mandatory_property_tests, PROPERTY_AND_VALUES )
- def test_mandatory_property( property_type_and_data ):
- """Iterates over all property types and check if
- mandatory/optional flags are correctly handled.
- """
- property_type, property_data = property_type_and_data
- 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 _generate_property_validation_tests( metafunc, value_scenarios_types ):
- 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={
- 'dbo_class': DBO,
- 'value': value,
- 'good_value': good_value} )
- @py.test.mark.parametrized( _generate_property_validation_tests, ['good_values'] )
- def test_property_validation_good_values( 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
- @py.test.mark.parametrized( _generate_property_validation_tests, ['bad_values', 'bad_types'] )
- def test_property_validation_bad_values_or_types( 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
- @py.test.mark.parametrized( _generate_property_validation_tests, ['normalize_values'] )
- def test_property_validation_normalize_value( 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