Best Python code snippet using assertpy_python
validation.py
Source:validation.py
...11 'uri', 'name', 'genre', 'date', 'comment', 'musicbrainz_id'}12DISTINCT_FIELDS = {13 'track', 'artist', 'albumartist', 'album', 'composer', 'performer', 'date',14 'genre'}15# TODO: _check_iterable(check, msg, **kwargs) + [check(a) for a in arg]?16def _check_iterable(arg, msg, **kwargs):17 """Ensure we have an iterable which is not a string or an iterator"""18 if isinstance(arg, compat.string_types):19 raise exceptions.ValidationError(msg.format(arg=arg, **kwargs))20 elif not isinstance(arg, collections.Iterable):21 raise exceptions.ValidationError(msg.format(arg=arg, **kwargs))22 elif iter(arg) is iter(arg):23 raise exceptions.ValidationError(msg.format(arg=arg, **kwargs))24def check_choice(arg, choices, msg='Expected one of {choices}, not {arg!r}'):25 if arg not in choices:26 raise exceptions.ValidationError(msg.format(27 arg=arg, choices=tuple(choices)))28def check_boolean(arg, msg='Expected a boolean, not {arg!r}'):29 check_instance(arg, bool, msg=msg)30def check_instance(arg, cls, msg='Expected a {name} instance, not {arg!r}'):31 if not isinstance(arg, cls):32 raise exceptions.ValidationError(33 msg.format(arg=arg, name=cls.__name__))34def check_instances(arg, cls, msg='Expected a list of {name}, not {arg!r}'):35 _check_iterable(arg, msg, name=cls.__name__)36 if not all(isinstance(instance, cls) for instance in arg):37 raise exceptions.ValidationError(38 msg.format(arg=arg, name=cls.__name__))39def check_integer(arg, min=None, max=None):40 if not isinstance(arg, compat.integer_types):41 raise exceptions.ValidationError('Expected an integer, not %r' % arg)42 elif min is not None and arg < min:43 raise exceptions.ValidationError(44 'Expected number larger or equal to %d, not %r' % (min, arg))45 elif max is not None and arg > max:46 raise exceptions.ValidationError(47 'Expected number smaller or equal to %d, not %r' % (max, arg))48def check_query(arg, fields=SEARCH_FIELDS, list_values=True):49 # TODO: normalize name -> track_name50 # TODO: normalize value -> [value]51 # TODO: normalize blank -> [] or just remove field?52 # TODO: remove list_values?53 if not isinstance(arg, collections.Mapping):54 raise exceptions.ValidationError(55 'Expected a query dictionary, not {arg!r}'.format(arg=arg))56 for key, value in arg.items():57 check_choice(key, fields, msg='Expected query field to be one of '58 '{choices}, not {arg!r}')59 if list_values:60 msg = 'Expected "{key}" to be list of strings, not {arg!r}'61 _check_iterable(value, msg, key=key)62 [_check_query_value(key, v, msg) for v in value]63 else:64 _check_query_value(65 key, value, 'Expected "{key}" to be a string, not {arg!r}')66def _check_query_value(key, arg, msg):67 if not isinstance(arg, compat.string_types) or not arg.strip():68 raise exceptions.ValidationError(msg.format(arg=arg, key=key))69def check_uri(arg, msg='Expected a valid URI, not {arg!r}'):70 if not isinstance(arg, compat.string_types):71 raise exceptions.ValidationError(msg.format(arg=arg))72 elif urllib.parse.urlparse(arg).scheme == '':73 raise exceptions.ValidationError(msg.format(arg=arg))74def check_uris(arg, msg='Expected a list of URIs, not {arg!r}'):75 _check_iterable(arg, msg)...
test_custom_list.py
Source:test_custom_list.py
...43 return self._s[idx]44def test_custom_list():45 l = CustomList('foobar')46 assert_that([CustomList('foo'), CustomList('bar')]).extracting(0,-1).is_equal_to([('f','o'),('b','r')])47def test_check_iterable():48 l = CustomList('foobar')49 ab = assert_that(None)50 ab._check_iterable(l)51 ab._check_iterable(l, check_getitem=True)52 ab._check_iterable(l, check_getitem=False)53def test_check_iterable_not_iterable():54 try:55 ab = assert_that(None)56 ab._check_iterable(123, name='my-int')57 fail('should have raised error')58 except TypeError as e:59 assert_that(str(e)).contains('my-int <int> is not iterable')60def test_check_iterable_no_getitem():61 try:62 ab = assert_that(None)63 ab._check_iterable(set([1]), name='my-set')64 fail('should have raised error')65 except TypeError as e:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!