blep

pytest_generate_tests split of validation check

May 1st, 2011
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.67 KB | None | 0 0
  1. def get_class_name( cls ):
  2.     return cls.__name__.split('.')[-1]
  3.  
  4. def get_property_class_name( cls ):
  5.     name = get_class_name( cls )
  6.     if name.endswith( 'Property' ):
  7.         name = name[:-len('Property')]
  8.     return name
  9.  
  10. def pytest_generate_tests(metafunc):
  11.     """Generates tests for test_property_validation & test_mandatory_property.
  12.    """
  13.     if 'property_type_value_iter' in metafunc.funcargnames:
  14.         for property_value in PROPERTY_AND_VALUES.items():
  15.             name = get_property_class_name( property_value[0] )
  16.             metafunc.addcall(funcargs=dict(property_type_value_iter=property_value), id=name)
  17.     else:
  18.         scenario_types_by_arg_name = {
  19.             'good_validation_scenario_iter': ['good_values'],
  20.             'bad_validation_scenario_iter': ['bad_values', 'bad_types'],
  21.             'normalize_validation_scenario_iter': ['normalize_values']
  22.             }
  23.         for arg_name, scenario_types in scenario_types_by_arg_name.items():
  24.             if arg_name in metafunc.funcargnames:
  25.                 _generate_property_validation_tests( metafunc, scenario_types, arg_name )
  26.  
  27. def _generate_property_validation_tests( metafunc, value_scenarios_types, arg_name ):
  28.     for scenario in VALIDATION_SCENARIOS:
  29.         property = scenario['prop']
  30.         class DBO(dbo.DBObject):
  31.             prop = property
  32.         good_value = scenario['good_values'][0]
  33.         base_name = scenario.get( 'name', get_property_class_name(property.__class__) )
  34.         for check_kind, values in [(k, v) for k, v in scenario.items() if k in value_scenarios_types]:
  35.             for index, value in enumerate(values):
  36.                 name = '%s.%s.%d' % (base_name, check_kind, index)
  37.                 metafunc.addcall( id=name, funcargs={
  38.                     arg_name: check_kind,
  39.                     'dbo_class': DBO,
  40.                     'value': value,
  41.                     'good_value': good_value} )
  42.  
  43. def test_mandatory_property( property_type_value_iter ):
  44.     """Iterates over all property types and check if
  45.       mandatory/optional flags are correctly handled.
  46.    """
  47.     property_type, property_data = property_type_value_iter
  48.     property_value = property_data['values'][0]
  49.     mandatory_properties = [property_type( mandatory=True )]
  50.     if not property_data.get('never_pk', False) and not property_data.get('never_mandatory', False):
  51.         mandatory_properties.append( property_type( pk_index=0 ) )
  52.     for mandatory_property in mandatory_properties:
  53.         class DBO(dbo.DBObject):
  54.             mandatory_prop = mandatory_property
  55.             optional_prop = property_type( mandatory=False )
  56.         o = DBO( mandatory_prop=property_value, optional_prop=property_value )
  57.         assert o.mandatory_prop == property_value
  58.         assert o.optional_prop == property_value
  59.         o = DBO( mandatory_prop=property_value, optional_prop=None )
  60.         assert o.mandatory_prop == property_value
  61.         assert o.optional_prop is None
  62.         o = DBO( mandatory_prop=property_value )
  63.         assert o.mandatory_prop == property_value
  64.         assert o.optional_prop is None
  65.         if not property_data.get('never_mandatory', False):
  66.             with py.test.raises(ValueError):
  67.                 DBO()
  68.             with py.test.raises(ValueError):
  69.                 DBO( mandatory_prop=None, optional_prop=None )
  70.             with py.test.raises(ValueError):
  71.                 DBO( mandatory_prop=None )
  72.             with py.test.raises(ValueError):
  73.                 DBO( optional_prop=None )
  74.             with py.test.raises(ValueError):
  75.                 DBO(optional_prop=property_value)
  76.  
  77. def test_property_validation_good_values( good_validation_scenario_iter, dbo_class, value, good_value ):
  78.     """Checks that good values are correctly handled using 'good_values' data."""
  79.     o = dbo_class( prop=value )
  80.     assert o.prop == value
  81.     o = dbo_class()
  82.     o.prop = value
  83.     assert o.prop == value
  84.  
  85. def test_property_validation_bad_values_or_types( bad_validation_scenario_iter, dbo_class, value, good_value ):
  86.     """Checks that bad values raise BadPropertyValueError using 'bad_values' and 'bad_types' data."""
  87.     with py.test.raises(dbo.BadPropertyValueError):
  88.         dbo_class( prop=value )
  89.     o = dbo_class( prop=good_value )
  90.     with py.test.raises(dbo.BadPropertyValueError):
  91.         o.prop = value
  92.  
  93. def test_property_validation_normalize_value( normalize_validation_scenario_iter, dbo_class, value, good_value ):
  94.     """Checks that good values are correctly normalized when a property is set using 'normalize_values' data."""
  95.     o = dbo_class( prop=value[0] )
  96.     assert o.prop == value[1]
  97.     o = dbo_class()
  98.     o.prop = value[0]
  99.     assert o.prop == value[1]
Advertisement
Add Comment
Please, Sign In to add comment