blep

py.test 2.0.3 pytest_generate_tests failure

Apr 29th, 2011
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.24 KB | None | 0 0
  1. #
  2. #
  3. #
  4. from . import dbo, sysoutfix
  5. from .enum import Enum
  6. import py.test
  7. import datetime
  8.  
  9. sysoutfix.install_sysstd_hooks() # better way to do it?
  10.  
  11. TEXT_VALUE1 = 'Some text with\n line return.! \'' * 10
  12. TEXT_VALUE2 = ''.join( chr(i) for i in range(11, 0x8000) )
  13. TEXT_VALUE3 = '%5B.net%5D'
  14. BLOB_VALUE1 = b'binary data'
  15. BLOB_VALUE2 = bytes( i & 255 for i in range(0, 0x8000) )
  16. JSON_VALUE1 = data=[1,2,{'a':3,'b':2}]
  17. DATETIME_VALUE1 = datetime.datetime(year=2000, month=12, day=1, hour=13, minute=12, second=30)
  18. BASE64_VALUE1 = 'ungWv48Bz-pBQUDeXa4iI7ADYaOWF3qctBD_YfIAFa0='
  19. BASE64_VALUE2 = b'\xbax\x16\xbf\x8f\x01\xcf\xeaAA@\xde]\xae"#\xb0\x03a\xa3\x96\x17z\x9c\xb4\x10\xffa\xf2\x00\x15\xad'
  20. BASE64_VALUE3 = 'Eru_wF1OAs896ih6LULtL0ZwAPn3fPiKrlizzPbnuNQ='
  21.  
  22. PROPERTY_AND_VALUES = {
  23.     dbo.IntProperty: {'values': [0]},
  24.     dbo.AutoIdProperty: {'values': [1234], 'never_mandatory': True},
  25.     dbo.StringProperty: {'values': ["abc"]},
  26.     dbo.TextProperty: {'values':[""], 'never_pk': True},
  27.     dbo.ZTextProperty: {'values':[""], 'never_pk': True},
  28.     dbo.BlobProperty: {'values':[b""], 'never_pk': True},
  29.     dbo.ZBlobProperty: {'values':[b""], 'never_pk': True},
  30.     dbo.BoolProperty: {'values':[False]},
  31.     dbo.JsonProperty: {'values':[{}], 'never_pk': True},
  32.     dbo.DateTimeProperty: {'values':[datetime.datetime.now()]},
  33.     dbo.Base64BinaryProperty: {'values': [BASE64_VALUE1]}
  34.     }
  35.  
  36.  
  37. class StatusChoice(Enum):
  38.     planned = 'P'
  39.     available = 'A'
  40.     failed = 'F'
  41.  
  42. VALIDATION_SCENARIOS = [
  43.     {
  44.         'prop': dbo.IntProperty(),
  45.         'good_values': [0, -1, -1000, 1, 1000],
  46.         'bad_types': ['abcd', b'abc', 1.234]
  47.     }, {
  48.         'prop': dbo.IntProperty(min=0),
  49.         'good_values': [0, 1, 1000],
  50.         'bad_values': [-1, -1000]
  51.     }, {
  52.         'prop': dbo.IntProperty(min=0),
  53.         'good_values': [0, 1, 1000],
  54.         'bad_values': [-1, -1000]
  55.     }, {
  56.         'prop': dbo.IntProperty(min=1),
  57.         'good_values': [1, 1000],
  58.         'bad_values': [0, -1, -1000]
  59.     }, {
  60.         'prop': dbo.IntProperty(max=0),
  61.         'good_values': [0, -1, -1000],
  62.         'bad_values': [1, 1000]
  63.     }, {
  64.         'prop': dbo.IntProperty(max=-1),
  65.         'good_values': [-1, -1000],
  66.         'bad_values': [0, 1, 1000]
  67.     }, {
  68.         'prop': dbo.IntProperty(min=1, max=9),
  69.         'good_values': [1, 3, 9],
  70.         'bad_values': [0, 10, 1000, -1]
  71.     }, {
  72.         'prop': dbo.AutoIdProperty(),
  73.         'good_values': [None, 1, 10000],
  74.         'bad_values': [0, -1],
  75.         'bad_types': ['abc', 1.2, b'1234']
  76.     }, {
  77.         'prop': dbo.TextProperty(),
  78.         'good_values': [TEXT_VALUE1, TEXT_VALUE2, TEXT_VALUE3],
  79.         'bad_types': [b'', 0, 1.2, True]
  80.     }, {
  81.         'prop': dbo.ZTextProperty(),
  82.         'good_values': [TEXT_VALUE1, TEXT_VALUE2, TEXT_VALUE3],
  83.         'bad_types': [b'', 0, 1.2, True]
  84.     }, {
  85.         'prop': dbo.BlobProperty(),
  86.         'good_values': [BLOB_VALUE1, BLOB_VALUE2],
  87.         'bad_types': ['', 0, 1.2, True]
  88.     }, {
  89.         'prop': dbo.ZBlobProperty(),
  90.         'good_values': [BLOB_VALUE1, BLOB_VALUE2],
  91.         'bad_types': ['', 0, 1.2, True]
  92.     }, {
  93.         'prop': dbo.BoolProperty(),
  94.         'good_values': [False, True],
  95.         'bad_types': ['', 1.2, b''],
  96.         'normalize_values': [(0, False), (1,True)]
  97.     }, {
  98.         'prop': dbo.JsonProperty(),
  99.         'good_values': [{}, [], JSON_VALUE1],
  100.         'normalize_values': [((), [])],
  101.         'bad_types': ['abc', 0, False, b'ad']
  102.     }, {
  103.         'prop': dbo.DateTimeProperty(),
  104.         'good_values': [DATETIME_VALUE1],
  105.         'bad_types': ['abc', 0, False, b'ad']
  106.     }, {
  107.         'prop': dbo.ChoiceProperty( ('a', 'A', 'b') ),
  108.         'good_values': ['a', 'A', 'b'],
  109.         'bad_values': ['Z', 'planned'],
  110.         'bad_types': [b'a', True, ()]
  111.     }, {
  112.         'prop': dbo.ChoiceProperty( (-1, 12, 100) ),
  113.         'good_values': [-1, 12, 100],
  114.         'bad_values': [0, 1],
  115.         'bad_types': [b'a', True, ()]
  116.     }, {
  117.         'prop': dbo.ChoiceProperty( StatusChoice ),
  118.         'good_values': list(StatusChoice),
  119.         'bad_values': ['Z', 'planned'],
  120.         'bad_types': [b'a', True, ()]
  121.     }, {
  122.         'prop': dbo.Base64BinaryProperty(),
  123.         'good_values': [BASE64_VALUE1],
  124.         'bad_values': ['aa'],
  125.         'bad_types': [1, 12.5, True, (), []]
  126.     }, {
  127.         'prop': dbo.Base64BinaryProperty(exact_byte_length=32),
  128.         'good_values': [BASE64_VALUE1],
  129.         'normalize_values': [(BASE64_VALUE2, BASE64_VALUE1)],
  130.         'bad_values': ['aa'],
  131.         'bad_types': [1, 12.5, True, (), []]
  132.     } ]
  133.  
  134. def pytest_generate_tests(metafunc):
  135.     """Generates tests for test_property_validation & test_mandatory_property.
  136.    """
  137.     if "property_type_value_iter" in metafunc.funcargnames:
  138.         for property_value in PROPERTY_AND_VALUES.items():
  139.             metafunc.addcall(funcargs=dict(property_type_value_iter=property_value))
  140.     if "validation_scenario_iter" in metafunc.funcargnames:
  141.         for scenario in VALIDATION_SCENARIOS:
  142.             property = scenario['prop']
  143.             value_scenarios_types = ('good_values', 'bad_values', 'bad_types', 'normalize_values')
  144.             good_value = scenario['good_values'][0]
  145.             for check_kind, values in [(k, v) for k, v in scenario.items() if k in value_scenarios_types]:
  146.                 for value in values:
  147.                     metafunc.addcall(funcargs=dict(
  148.                         validation_scenario_iter=check_kind,
  149.                         property=property,
  150.                         value=value,
  151.                         good_value=good_value) )
  152.  
  153. def test_mandatory_property( property_type_value_iter ):
  154.     """Iterates over all property types and check if
  155.       mandatory/optional flags are correctly handled.
  156.    """
  157.     property_type, property_data = property_type_value_iter
  158.     property_value = property_data['values'][0]
  159.     mandatory_properties = [property_type( mandatory=True )]
  160.     if not property_data.get('never_pk', False) and not property_data.get('never_mandatory', False):
  161.         mandatory_properties.append( property_type( pk_index=0 ) )
  162.     for mandatory_property in mandatory_properties:
  163.         class DBO(dbo.DBObject):
  164.             mandatory_prop = mandatory_property
  165.             optional_prop = property_type( mandatory=False )
  166.         o = DBO( mandatory_prop=property_value, optional_prop=property_value )
  167.         assert o.mandatory_prop == property_value
  168.         assert o.optional_prop == property_value
  169.         o = DBO( mandatory_prop=property_value, optional_prop=None )
  170.         assert o.mandatory_prop == property_value
  171.         assert o.optional_prop is None
  172.         o = DBO( mandatory_prop=property_value )
  173.         assert o.mandatory_prop == property_value
  174.         assert o.optional_prop is None
  175.         if not property_data.get('never_mandatory', False):
  176.             with py.test.raises(ValueError):
  177.                 DBO()
  178.             with py.test.raises(ValueError):
  179.                 DBO( mandatory_prop=None, optional_prop=None )
  180.             with py.test.raises(ValueError):
  181.                 DBO( mandatory_prop=None )
  182.             with py.test.raises(ValueError):
  183.                 DBO( optional_prop=None )
  184.             with py.test.raises(ValueError):
  185.                 DBO(optional_prop=property_value)
  186.  
  187. def test_property_validation( validation_scenario_iter, property=None, value=None, good_value=None ):
  188.     """Iterates over the validation scenario, and checks validate behavior.
  189.    """
  190.     class DBO(dbo.DBObject):
  191.         prop = property
  192.     if validation_scenario_iter == 'good_values':
  193.         o = DBO( prop=value )
  194.         assert o.prop == value
  195.         o = DBO()
  196.         o.prop = value
  197.         assert o.prop == value
  198.     elif validation_scenario_iter in ('bad_values', 'bad_types'):
  199.         with py.test.raises(dbo.BadPropertyValueError):
  200.             DBO( prop=value )
  201.         o = DBO( prop=good_value )
  202.         with py.test.raises(dbo.BadPropertyValueError):
  203.             o.prop = value
  204.     elif validation_scenario_iter == 'normalize_values':
  205.         o = DBO( prop=value[0] )
  206.         assert o.prop == value[1]
  207.         o = DBO()
  208.         o.prop = value[0]
  209.         assert o.prop == value[1]
  210.     else:
  211.         assert False, 'invalid validation scenario kind: ' + validation_scenario_iter
Advertisement
Add Comment
Please, Sign In to add comment