How to use assert_type_value method in tavern

Best Python code snippet using tavern

test_parser.py

Source:test_parser.py Github

copy

Full Screen

2from typing import Any, Literal, Union3import pytest4from .exceptions import *5from .parser import ConfigSchema6def assert_type_value(value, assert_type: type, assert_value):7 __tracebackhide__ = True8 assert isinstance(value, assert_type)9 assert value == assert_value10class TestSimple:11 def test_none(self):12 class C(ConfigSchema):13 field: None14 parsed = C('{"field": null}')15 assert parsed.field is None16 with pytest.raises(TypeError):17 parsed = C('{"field": 1}')18 def test_bool(self):19 class C(ConfigSchema):20 field: bool21 parsed = C('{"field": true}')22 assert parsed.field is True23 with pytest.raises(TypeError):24 parsed = C('{"field": 1}')25 def test_int(self):26 class C(ConfigSchema):27 field: int28 parsed = C('{"field": 1}')29 assert_type_value(parsed.field, int, 1)30 with pytest.raises(TypeError):31 parsed = C('{"field": "hi"}')32 def test_float(self):33 class C(ConfigSchema):34 field: float35 parsed = C('{"field": 1.0}')36 assert_type_value(parsed.field, float, 1.0)37 with pytest.raises(TypeError):38 parsed = C('{"field": "hi"}')39 def test_str(self):40 class C(ConfigSchema):41 field: str42 parsed = C('{"field": "hi"}')43 assert_type_value(parsed.field, str, 'hi')44 with pytest.raises(TypeError):45 parsed = C('{"field": 1}')46 def test_invalid(self):47 with pytest.raises(ConfigSchemaError):48 class C(ConfigSchema):49 field: bytes50 def test_multi(self):51 class C(ConfigSchema):52 fieldA: int53 fieldB: str54 parsed = C('{"fieldA": 1, "fieldB": "hi"}')55 assert_type_value(parsed.fieldA, int, 1)56 assert_type_value(parsed.fieldB, str, 'hi')57 def test_missing_field(self):58 class C(ConfigSchema):59 fieldA: int60 fieldB: int61 with pytest.raises(MissingFieldError, match='fieldA'):62 C('{"fieldB": 1}')63class TestCollections:64 def test_tuple(self):65 class C(ConfigSchema):66 field: tuple67 parsed = C('{"field": [null, true, "hi"]}')68 assert isinstance(parsed.field, tuple)69 assert parsed.field[0] is None70 assert parsed.field[1] is True71 assert_type_value(parsed.field[2], str, 'hi')72 def test_tuple_args(self):73 class C(ConfigSchema):74 field: tuple[bool, int, str]75 parsed = C('{"field": [true, 1, "hi"]}')76 assert parsed.field[0] is True77 assert_type_value(parsed.field[1], int, 1)78 assert_type_value(parsed.field[2], str, 'hi')79 def test_tuple_args_type_err(self):80 class C(ConfigSchema):81 field: tuple[bool, int, str]82 with pytest.raises(TypeError):83 parsed = C('{"field": [true, true, true]}')84 def test_tuple_args_len_err(self):85 class C(ConfigSchema):86 field: tuple[bool, int, str]87 with pytest.raises(ValueError, match=r'length.+3'):88 parsed = C('{"field": [true, 1, "hi", "extra"]}')89 def test_list(self):90 class C(ConfigSchema):91 field: list92 parsed = C('{"field": [null, true, "hi"]}')93 assert isinstance(parsed.field, list)94 assert parsed.field[0] is None95 assert parsed.field[1] is True96 assert_type_value(parsed.field[2], str, 'hi')97 def test_list_args(self):98 class C(ConfigSchema):99 field: list[int]100 parsed = C('{"field": [1, 2, 3]}')101 assert isinstance(parsed.field, list)102 assert_type_value(parsed.field[0], int, 1)103 assert_type_value(parsed.field[1], int, 2)104 assert_type_value(parsed.field[2], int, 3)105 def test_list_args_type_err(self):106 class C(ConfigSchema):107 field: list[int]108 with pytest.raises(TypeError):109 parsed = C('{"field": [1, 2, false]}')110 def test_dict(self):111 # Assume dict[str, Any]112 class C(ConfigSchema):113 field: dict114 parsed = C('{"field": {"bool": true, "int": 1, "str": "hi"}}')115 assert isinstance(parsed.field, dict)116 assert parsed.field['bool'] is True117 assert_type_value(parsed.field['int'], int, 1)118 assert_type_value(parsed.field['str'], str, 'hi')119 def test_dict_args(self):120 class C(ConfigSchema):121 field: dict[str, int]122 parsed = C('{"field": {"one": 1, "two": 2}}')123 assert isinstance(parsed.field, dict)124 assert_type_value(parsed.field['one'], int, 1)125 assert_type_value(parsed.field['two'], int, 2)126 def test_dict_args_type_err(self):127 class C(ConfigSchema):128 field: dict[str, int]129 with pytest.raises(TypeError):130 parsed = C('{"field": {"one": 1, "two": "two"}}')131 def test_dict_key_not_str(self):132 with pytest.raises(TypeError):133 class C(ConfigSchema):134 field: dict[int, str]135class TestSpecialTyping:136 def test_any(self):137 class C(ConfigSchema):138 field: Any139 parsed = C('{"field": true}')140 assert parsed.field is True141 parsed = C('{"field": 1}')142 assert_type_value(parsed.field, int, 1)143 parsed = C('{"field": "hi"}')144 assert_type_value(parsed.field, str, 'hi')145 def test_union(self):146 class C(ConfigSchema):147 field: Union[int, str]148 def test_union_err(self):149 with pytest.raises(ConfigSchemaError):150 class C(ConfigSchema):151 field: Union[int, bytes]152 def test_union_nested(self):153 class C(ConfigSchema):154 field: Union[bool, Union[int, Union[float, str]]]155 def test_union_nested_err(self):156 with pytest.raises(ConfigSchemaError):157 class C(ConfigSchema):158 field: Union[bool, Union[int, Union[float, bytes]]]159 def test_literal(self):160 class C(ConfigSchema):161 field: Literal[5]162 parsed = C('{"field": 5}')163 assert_type_value(parsed.field, int, 5)164 with pytest.raises(ValueError):165 C('{"field": 4}')166 def test_literal_multi(self):167 class C(ConfigSchema):168 field: Literal[5, 4]169 parsed = C('{"field": 5}')170 assert_type_value(parsed.field, int, 5)171 parsed = C('{"field": 4}')172 assert_type_value(parsed.field, int, 4)173 with pytest.raises(ValueError):174 C('{"field": 3}')175class TestNestedSchemas:176 def test_simple(self):177 class C(ConfigSchema):178 nested: _Nested179 class _Nested(ConfigSchema):180 field: int181 parsed = C('{"nested": {"field": 1}}')182 assert isinstance(parsed.nested, C._Nested)183 assert_type_value(parsed.nested.field, int, 1)184 def test_same_field_names(self):185 class C(ConfigSchema):186 field: str187 nested: _Nested188 class _Nested(ConfigSchema):189 field: int190 parsed = C('{"field": "hi", "nested": {"field": 1}}')191 assert_type_value(parsed.field, str, "hi")192 assert isinstance(parsed.nested, C._Nested)193 assert_type_value(parsed.nested.field, int, 1)194 def test_super_nested(self):195 class C(ConfigSchema):196 nested1: _Nested197 class _Nested(ConfigSchema):198 nested2: _Nested199 class _Nested(ConfigSchema):200 nested3: _Nested201 class _Nested(ConfigSchema):202 field: str203 parsed = C('''204 {205 "nested1": {206 "nested2": {207 "nested3": {208 "field": "hi"209 }210 }211 }212 }213 ''')214 assert_type_value(parsed.nested1.nested2.nested3.field, str, 'hi')215class TestDefaults:216 @staticmethod217 @pytest.fixture218 def simple_schema():219 class C(ConfigSchema):220 intField = 3221 strField = 'hi'222 return C223 @staticmethod224 @pytest.fixture225 def tuple_schema():226 class C(ConfigSchema):227 field = (1, 2, 3)228 return C229 @staticmethod230 @pytest.fixture231 def list_schema():232 class C(ConfigSchema):233 field = [1, 2, 3]234 return C235 @staticmethod236 @pytest.fixture237 def dict_schema():238 class C(ConfigSchema):239 field = {'one': 1, 'two': 2}240 return C241 def test_empty(self, simple_schema):242 parsed = simple_schema('{}')243 assert_type_value(parsed.intField, int, 3)244 assert_type_value(parsed.strField, str, 'hi')245 def test_any(self):246 class C(ConfigSchema):247 field: Any = 1248 parsed = C('{}')249 assert_type_value(parsed.field, int, 1)250 parsed = C('{"field": 2}')251 assert_type_value(parsed.field, int, 2)252 parsed = C('{"field": "hi"}')253 assert_type_value(parsed.field, str, 'hi')254 def test_missing_one(self, simple_schema):255 parsed = simple_schema('{"intField": 2}')256 assert_type_value(parsed.intField, int, 2)257 assert_type_value(parsed.strField, str, 'hi')258 parsed = simple_schema('{"strField": "hello"}')259 assert_type_value(parsed.intField, int, 3)260 assert_type_value(parsed.strField, str, 'hello')261 def test_bad_type_parsing(self, simple_schema):262 with pytest.raises(TypeError):263 parsed = simple_schema('{"strField": 5}')264 def test_bad_type_definition(self):265 with pytest.raises(ConfigSchemaError):266 class C(ConfigSchema):267 field: int = 'hi'268 def test_tuple(self, tuple_schema):269 parsed = tuple_schema('{}')270 assert parsed.field == (1, 2, 3)271 def test_tuple_type_inference(self, tuple_schema):272 parsed = tuple_schema('{"field": [3, 4, 5]}')273 assert parsed.field == (3, 4, 5)274 with pytest.raises(TypeError):...

Full Screen

Full Screen

test_casts.py

Source:test_casts.py Github

copy

Full Screen

...26 monkeypatch.setenv(key, val)27 elif request.param == 'envfile':28 env.read_envfile('tests/envfile')29# Helper function30def assert_type_value(cast, expected, result):31 assert cast == type(result)32 assert expected == result33def test_var_not_present():34 with pytest.raises(ConfigurationError):35 env('NOT_PRESENT')36def test_var_not_present_with_default():37 default_val = 'default val'38 assert default_val, env('NOT_PRESENT', default=default_val)39def test_default_none():40 assert_type_value(type(None), None, env('NOT_PRESENT', default=None))41def test_implicit_nonbuiltin_type():42 with pytest.raises(AttributeError):43 env.foo('FOO')44def test_str():45 expected = str(env_vars['STR'])46 assert_type_value(str, expected, env('STR'))47 assert_type_value(str, expected, env.str('STR'))48def test_int():49 expected = int(env_vars['INT'])50 assert_type_value(int, expected, env('INT', cast=int))51 assert_type_value(int, expected, env.int('INT'))52def test_float():53 expected = float(env_vars['FLOAT'])54 assert_type_value(float, expected, env.float('FLOAT'))55def test_bool():56 assert_type_value(bool, True, env.bool('BOOL_TRUE'))57 assert_type_value(bool, False, env.bool('BOOL_FALSE'))58def test_list():59 list_str = ['foo', 'bar']60 assert_type_value(list, list_str, env('LIST_STR', cast=list))61 assert_type_value(list, list_str, env.list('LIST_STR'))62 assert_type_value(list, list_str, env.list('LIST_STR_WITH_SPACES'))63 list_int = [1, 2, 3]64 assert_type_value(list, list_int, env('LIST_INT', cast=list,65 subcast=int))66 assert_type_value(list, list_int, env.list('LIST_INT', subcast=int))67 assert_type_value(list, list_int, env.list('LIST_INT_WITH_SPACES',68 subcast=int))69 assert_type_value(list, [], env.list('BLANK', subcast=int))70def test_dict():71 dict_str = dict(key1='val1', key2='val2')72 assert_type_value(dict, dict_str, env.dict('DICT_STR'))73 assert_type_value(dict, dict_str, env('DICT_STR', cast=dict))74 dict_int = dict(key1=1, key2=2)75 assert_type_value(dict, dict_int, env('DICT_INT', cast=dict,76 subcast=int))77 assert_type_value(dict, dict_int, env.dict('DICT_INT', subcast=int))78 assert_type_value(dict, {}, env.dict('BLANK'))79def test_json():80 expected = {'foo': 'bar', 'baz': [1, 2, 3]}81 assert_type_value(dict, expected, env.json('JSON'))82def test_url():83 url = urlparse.urlparse('https://example.com/path?query=1')84 assert_type_value(url.__class__, url, env.url('URL'))85def proxied_value():86 assert_type_value(str, 'bar', env('PROXIED'))87def test_preprocessor():88 assert_type_value(str, 'FOO', env('STR', preprocessor=lambda89 v: v.upper()))90def test_postprocessor(monkeypatch):91 """92 Test a postprocessor which turns a redis url into a Django compatible93 cache url.94 """95 redis_url = 'redis://:redispass@127.0.0.1:6379/0'96 monkeypatch.setenv('redis_url', redis_url)97 expected = {'BACKEND': 'django_redis.cache.RedisCache',98 'LOCATION': '127.0.0.1:6379:0',99 'OPTIONS': {'PASSWORD': 'redispass'}}100 def django_redis(url):101 return {102 'BACKEND': 'django_redis.cache.RedisCache',103 'LOCATION': '{}:{}:{}'.format(url.hostname, url.port, url.path.strip('/')),104 'OPTIONS': {'PASSWORD': url.password}}105 assert_type_value(dict, expected, env.url('redis_url',106 postprocessor=django_redis))107def test_schema():108 env = Env(STR=str, STR_DEFAULT=dict(cast=str, default='default'),109 INT=int, LIST_STR=list, LIST_INT=dict(cast=list, subcast=int))110 assert_type_value(str, 'foo', env('STR'))111 assert_type_value(str, 'default', env('STR_DEFAULT'))112 assert_type_value(int, 42, env('INT'))113 assert_type_value(list, ['foo', 'bar'], env('LIST_STR'))114 assert_type_value(list, [1, 2, 3], env('LIST_INT'))115 # Overrides116 assert_type_value(str, '42', env('INT', cast=str))117 assert_type_value(str, 'manual_default', env('STR_DEFAULT',...

Full Screen

Full Screen

test_env.py

Source:test_env.py Github

copy

Full Screen

...33 monkeypatch.setenv(key, val)34 elif request.param == 'envfile':35 env.read_envfile('tests/envfile')36# Helper function37def assert_type_value(cast, expected, result):38 assert cast == type(result)39 assert expected == result40def test_var_not_present():41 with pytest.raises(ConfigurationError):42 env('NOT_PRESENT')43def test_var_not_present_with_default():44 default_val = 'default val'45 assert default_val, env('NOT_PRESENT', default=default_val)46def test_default_none():47 assert_type_value(type(None), None, env('NOT_PRESENT', default=None))48def test_implicit_nonbuiltin_type():49 with pytest.raises(AttributeError):50 env.foo('FOO')51def test_str():52 expected = str(env_vars['HYDRA_STR'])53 assert_type_value(str, expected, env('STR'))54 assert_type_value(str, expected, env.str('STR'))55def test_int():56 expected = int(env_vars['HYDRA_INT'])57 assert_type_value(int, expected, env('INT', cast=int))58 assert_type_value(int, expected, env.int('INT'))59def test_float():60 expected = float(env_vars['HYDRA_FLOAT'])61 assert_type_value(float, expected, env.float('FLOAT'))62def test_bool():63 assert_type_value(bool, True, env.bool('BOOL_TRUE'))64 assert_type_value(bool, False, env.bool('BOOL_FALSE'))65def test_list():66 list_str = ['foo', 'bar']67 assert_type_value(list, list_str, env('LIST_STR', cast=list))68 assert_type_value(list, list_str, env.list('LIST_STR'))69 assert_type_value(list, list_str, env.list('LIST_STR_WITH_SPACES'))70 list_int = [1, 2, 3]71 assert_type_value(list, list_int, env('LIST_INT', cast=list,72 subcast=int))73 assert_type_value(list, list_int, env.list('LIST_INT', subcast=int))74 assert_type_value(list, list_int, env.list('LIST_INT_WITH_SPACES',75 subcast=int))76 assert_type_value(list, [], env.list('BLANK', subcast=int))77def test_dict():78 dict_str = dict(key1='val1', key2='val2')79 assert_type_value(dict, dict_str, env.dict('DICT_STR'))80 assert_type_value(dict, dict_str, env('DICT_STR', cast=dict))81 dict_int = dict(key1=1, key2=2)82 assert_type_value(dict, dict_int, env('DICT_INT', cast=dict,83 subcast=int))84 assert_type_value(dict, dict_int, env.dict('DICT_INT', subcast=int))85 assert_type_value(dict, {}, env.dict('BLANK'))86def test_json():87 expected = {'foo': 'bar', 'baz': [1, 2, 3]}88 assert_type_value(dict, expected, env.json('JSON'))89def test_url():90 url = urlparse('https://example.com/path?query=1')91 assert_type_value(url.__class__, url, env.url('URL'))92def proxied_value():93 assert_type_value(str, 'bar', env('PROXIED'))94def test_preprocessor():95 assert_type_value(str, 'FOO', env('STR', preprocessor=lambda96 v: v.upper()))97def test_postprocessor(monkeypatch):98 """99 Test a postprocessor which turns a redis url into a Django compatible100 cache url.101 """102 redis_url = 'redis://:redispass@127.0.0.1:6379/0'103 monkeypatch.setenv('HYDRA_REDIS_URL', redis_url)104 expected = {'BACKEND': 'django_redis.cache.RedisCache',105 'LOCATION': '127.0.0.1:6379:0',106 'OPTIONS': {'PASSWORD': 'redispass'}}107 def django_redis(url):108 return {109 'BACKEND': 'django_redis.cache.RedisCache',110 'LOCATION': '{}:{}:{}'.format(url.hostname, url.port, url.path.strip('/')),111 'OPTIONS': {'PASSWORD': url.password}}112 assert_type_value(dict, expected, env.url('redis_url',...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tavern automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful