Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- #
- #
- from . import dbo, sysoutfix
- from .enum import Enum
- import py.test
- import datetime
- sysoutfix.install_sysstd_hooks() # better way to do it?
- TEXT_VALUE1 = 'Some text with\n line return.! \'' * 10
- TEXT_VALUE2 = ''.join( chr(i) for i in range(11, 0x8000) )
- TEXT_VALUE3 = '%5B.net%5D'
- BLOB_VALUE1 = b'binary data'
- BLOB_VALUE2 = bytes( i & 255 for i in range(0, 0x8000) )
- JSON_VALUE1 = data=[1,2,{'a':3,'b':2}]
- DATETIME_VALUE1 = datetime.datetime(year=2000, month=12, day=1, hour=13, minute=12, second=30)
- BASE64_VALUE1 = 'ungWv48Bz-pBQUDeXa4iI7ADYaOWF3qctBD_YfIAFa0='
- 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'
- BASE64_VALUE3 = 'Eru_wF1OAs896ih6LULtL0ZwAPn3fPiKrlizzPbnuNQ='
- PROPERTY_AND_VALUES = {
- dbo.IntProperty: {'values': [0]},
- dbo.AutoIdProperty: {'values': [1234], 'never_mandatory': True},
- dbo.StringProperty: {'values': ["abc"]},
- dbo.TextProperty: {'values':[""], 'never_pk': True},
- dbo.ZTextProperty: {'values':[""], 'never_pk': True},
- dbo.BlobProperty: {'values':[b""], 'never_pk': True},
- dbo.ZBlobProperty: {'values':[b""], 'never_pk': True},
- dbo.BoolProperty: {'values':[False]},
- dbo.JsonProperty: {'values':[{}], 'never_pk': True},
- dbo.DateTimeProperty: {'values':[datetime.datetime.now()]},
- dbo.Base64BinaryProperty: {'values': [BASE64_VALUE1]}
- }
- class StatusChoice(Enum):
- planned = 'P'
- available = 'A'
- failed = 'F'
- VALIDATION_SCENARIOS = [
- {
- 'prop': dbo.IntProperty(),
- 'good_values': [0, -1, -1000, 1, 1000],
- 'bad_types': ['abcd', b'abc', 1.234]
- }, {
- 'prop': dbo.IntProperty(min=0),
- 'good_values': [0, 1, 1000],
- 'bad_values': [-1, -1000]
- }, {
- 'prop': dbo.IntProperty(min=0),
- 'good_values': [0, 1, 1000],
- 'bad_values': [-1, -1000]
- }, {
- 'prop': dbo.IntProperty(min=1),
- 'good_values': [1, 1000],
- 'bad_values': [0, -1, -1000]
- }, {
- 'prop': dbo.IntProperty(max=0),
- 'good_values': [0, -1, -1000],
- 'bad_values': [1, 1000]
- }, {
- 'prop': dbo.IntProperty(max=-1),
- 'good_values': [-1, -1000],
- 'bad_values': [0, 1, 1000]
- }, {
- 'prop': dbo.IntProperty(min=1, max=9),
- 'good_values': [1, 3, 9],
- 'bad_values': [0, 10, 1000, -1]
- }, {
- 'prop': dbo.AutoIdProperty(),
- 'good_values': [None, 1, 10000],
- 'bad_values': [0, -1],
- 'bad_types': ['abc', 1.2, b'1234']
- }, {
- 'prop': dbo.TextProperty(),
- 'good_values': [TEXT_VALUE1, TEXT_VALUE2, TEXT_VALUE3],
- 'bad_types': [b'', 0, 1.2, True]
- }, {
- 'prop': dbo.ZTextProperty(),
- 'good_values': [TEXT_VALUE1, TEXT_VALUE2, TEXT_VALUE3],
- 'bad_types': [b'', 0, 1.2, True]
- }, {
- 'prop': dbo.BlobProperty(),
- 'good_values': [BLOB_VALUE1, BLOB_VALUE2],
- 'bad_types': ['', 0, 1.2, True]
- }, {
- 'prop': dbo.ZBlobProperty(),
- 'good_values': [BLOB_VALUE1, BLOB_VALUE2],
- 'bad_types': ['', 0, 1.2, True]
- }, {
- 'prop': dbo.BoolProperty(),
- 'good_values': [False, True],
- 'bad_types': ['', 1.2, b''],
- 'normalize_values': [(0, False), (1,True)]
- }, {
- 'prop': dbo.JsonProperty(),
- 'good_values': [{}, [], JSON_VALUE1],
- 'normalize_values': [((), [])],
- 'bad_types': ['abc', 0, False, b'ad']
- }, {
- 'prop': dbo.DateTimeProperty(),
- 'good_values': [DATETIME_VALUE1],
- 'bad_types': ['abc', 0, False, b'ad']
- }, {
- 'prop': dbo.ChoiceProperty( ('a', 'A', 'b') ),
- 'good_values': ['a', 'A', 'b'],
- 'bad_values': ['Z', 'planned'],
- 'bad_types': [b'a', True, ()]
- }, {
- 'prop': dbo.ChoiceProperty( (-1, 12, 100) ),
- 'good_values': [-1, 12, 100],
- 'bad_values': [0, 1],
- 'bad_types': [b'a', True, ()]
- }, {
- 'prop': dbo.ChoiceProperty( StatusChoice ),
- 'good_values': list(StatusChoice),
- 'bad_values': ['Z', 'planned'],
- 'bad_types': [b'a', True, ()]
- }, {
- 'prop': dbo.Base64BinaryProperty(),
- 'good_values': [BASE64_VALUE1],
- 'bad_values': ['aa'],
- 'bad_types': [1, 12.5, True, (), []]
- }, {
- 'prop': dbo.Base64BinaryProperty(exact_byte_length=32),
- 'good_values': [BASE64_VALUE1],
- 'normalize_values': [(BASE64_VALUE2, BASE64_VALUE1)],
- 'bad_values': ['aa'],
- 'bad_types': [1, 12.5, True, (), []]
- } ]
- 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():
- metafunc.addcall(funcargs=dict(property_type_value_iter=property_value))
- if "validation_scenario_iter" in metafunc.funcargnames:
- for scenario in VALIDATION_SCENARIOS:
- property = scenario['prop']
- value_scenarios_types = ('good_values', 'bad_values', 'bad_types', 'normalize_values')
- good_value = scenario['good_values'][0]
- for check_kind, values in [(k, v) for k, v in scenario.items() if k in value_scenarios_types]:
- for value in values:
- metafunc.addcall(funcargs=dict(
- validation_scenario_iter=check_kind,
- property=property,
- 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( validation_scenario_iter, property=None, value=None, good_value=None ):
- """Iterates over the validation scenario, and checks validate behavior.
- """
- class DBO(dbo.DBObject):
- prop = property
- if validation_scenario_iter == 'good_values':
- o = DBO( prop=value )
- assert o.prop == value
- o = DBO()
- o.prop = value
- assert o.prop == value
- elif validation_scenario_iter in ('bad_values', 'bad_types'):
- with py.test.raises(dbo.BadPropertyValueError):
- DBO( prop=value )
- o = DBO( prop=good_value )
- with py.test.raises(dbo.BadPropertyValueError):
- o.prop = value
- elif validation_scenario_iter == 'normalize_values':
- o = DBO( prop=value[0] )
- assert o.prop == value[1]
- o = DBO()
- o.prop = value[0]
- assert o.prop == value[1]
- else:
- assert False, 'invalid validation scenario kind: ' + validation_scenario_iter
Advertisement
Add Comment
Please, Sign In to add comment