Best Python code snippet using pytest-asyncio_python
test_hash.py
Source:test_hash.py
1try:2 from collections import Counter3except ImportError:4 from .counter_recipe import Counter5from pytest import raises6from .env import NInt, get_session, key7from .env import session8from sider.types import Hash9from sider.transaction import Transaction10from sider.exceptions import CommitError11fixture_a = {'a': 'b', 'c': 'd'}12fixture_b = {1: 'a', 2: 'b'}13def test_iterate(session):14 hash_ = session.set(key('test_hash_iterate'), fixture_a, Hash)15 assert set(hash_) == set('ac')16 hashx = session.set(key('test_hashx_iterate'), fixture_b, Hash(NInt))17 assert set(hashx) == set([1, 2])18def test_length(session):19 hash_ = session.set(key('test_hash_length'), fixture_a, Hash)20 assert len(hash_) == 221 hashx = session.set(key('test_hashx_length'), fixture_b, Hash(NInt))22 assert len(hashx) == 223def test_contains(session):24 hash_ = session.set(key('test_hash_contains'), fixture_a, Hash)25 assert 'a' in hash_26 assert 'b' not in hash_27 assert 'c' in hash_28 assert 1 not in hash_29 hashx = session.set(key('test_hashx_contains'), fixture_b, Hash(NInt))30 assert 1 in hashx31 assert 2 in hashx32 assert 4 not in hashx33 assert '1' not in hashx34 assert '4' not in hashx35def test_getitem(session):36 hash_ = session.set(key('test_hash_getitem'), fixture_a, Hash)37 assert hash_['a'] == 'b'38 with raises(KeyError):39 hash_['b']40 assert hash_['c'] == 'd'41 with raises(TypeError):42 hash_[object()]43 with raises(TypeError):44 hash_[1]45 hashx = session.set(key('test_hashx_getitem'), fixture_b, Hash(NInt))46 assert hashx[1] == 'a'47 assert hashx[2] == 'b'48 with raises(KeyError):49 hashx[3]50 with raises(TypeError):51 hashx[object()]52 with raises(TypeError):53 hashx['a']54def test_equals(session):55 hash_ = session.set(key('test_hash_equals'), fixture_a, Hash)56 hash2 = session.set(key('test_hash_equals2'), fixture_a, Hash)57 fixture_c = dict(fixture_a)58 fixture_c['e'] = 'f'59 hash3 = session.set(key('test_hash_equals3'), fixture_c, Hash)60 hash4 = session.set(key('test_hash_equals4'), fixture_b, Hash(NInt))61 emptyhash = session.set(key('test_hash_equals5'), {}, Hash)62 emptyhash2 = session.set(key('test_hash_equals5'), {}, Hash(NInt))63 assert hash_ == fixture_a64 assert hash_ != fixture_b65 assert hash_ != fixture_c66 assert hash_ == hash2 and hash2 == hash_67 assert hash_ != hash3 and hash3 != hash_68 assert hash_ != hash4 and hash4 != hash_69 assert emptyhash == emptyhash2 and emptyhash2 == emptyhash70def test_keys(session):71 hash_ = session.set(key('test_hash_keys'), fixture_a, Hash)72 assert set(hash_.keys()) == set('ac')73 assert len(hash_.keys()) == 274 assert 'a' in hash_.keys()75 assert 'b' not in hash_.keys()76 assert 'c' in hash_.keys()77 hashx = session.set(key('test_hashx_keys'), fixture_b, Hash(NInt))78 assert set(hashx.keys()) == set([1, 2])79 assert len(hashx.keys()) == 280 assert 1 in hashx.keys()81 assert 2 in hashx.keys()82 assert 3 not in hashx.keys()83def test_values(session):84 hash_ = session.set(key('test_hash_values'), fixture_a, Hash)85 assert Counter(hash_.values()) == Counter('bd')86 assert len(hash_.values()) == 287 assert 'a' not in hash_.values()88 assert 'b' in hash_.values()89 assert 'd' in hash_.values()90 hashx = session.set(key('test_hashx_values'), {1: 2, 3: 4, 5: 2},91 Hash(NInt, NInt))92 assert Counter(hashx.values()) == Counter({2: 2, 4: 1})93 assert len(hashx.values()) == 394 assert 2 in hashx.values()95 assert 3 not in hashx.values()96 assert 4 in hashx.values()97def test_items(session):98 hash_ = session.set(key('test_hash_items'), fixture_a, Hash)99 assert set(hash_.items()) == set([('a', 'b'), ('c', 'd')])100 assert len(hash_.items()) == 2101 assert ('a', 'b') in hash_.items()102 assert ('b', 'b') not in hash_.items()103 assert ('c', 'c') not in hash_.items()104 assert ('c', 'd') in hash_.items()105 hashx = session.set(key('test_hashx_items'), fixture_b, Hash(NInt))106 assert set(hashx.items()) == set([(1, 'a'), (2, 'b')])107 assert len(hashx.items()) == 2108 assert (1, 'a') in hashx.items()109 assert (1, 'b') not in hashx.items()110 assert (2, 'a') not in hashx.items()111 assert (2, 'b') in hashx.items()112def test_clear(session):113 hash_ = session.set(key('test_hash_clear'), fixture_a, Hash)114 hash_.clear()115 assert len(hash_) == 0116 assert not list(hash_)117 hashx = session.set(key('test_hashx_clear'), fixture_b, Hash(NInt))118 hashx.clear()119 assert len(hashx) == 0120 assert not list(hashx)121def test_delitem(session):122 hash_ = session.set(key('test_hash_delitem'), fixture_a, Hash)123 del hash_['a']124 assert len(hash_) == 1125 assert 'a' not in hash_.keys()126 with raises(KeyError):127 del hash_['a']128 with raises(TypeError):129 del hash_[1]130 hashx = session.set(key('test_hashx_delitem'), fixture_b, Hash(NInt))131 del hashx[1]132 assert len(hashx) == 1133 assert 1 not in hashx.keys()134 with raises(KeyError):135 del hashx[1]136 with raises(TypeError):137 del hashx['a']138def test_delitem_t(session):139 session2 = get_session()140 keyid = key('test_hash_delitem_t')141 hash_ = session.set(keyid, fixture_a, Hash)142 hashx = session2.get(keyid, Hash)143 with Transaction(session, [keyid]):144 len(hash_)145 del hash_['a']146 assert 'a' in hashx147 with raises(CommitError):148 len(hash_)149 assert len(hash_) == 1150 assert 'a' not in hash_151 assert 'a' not in hashx152def test_setitem(session):153 hash_ = session.set(key('test_hash_setitem'), fixture_a, Hash)154 hash_['a'] = 'changed'155 assert len(hash_) == 2156 assert hash_['a'] == 'changed'157 assert 'a' in hash_.keys()158 hash_['new'] = 'added'159 assert len(hash_) == 3160 assert hash_['new'] == 'added'161 assert 'new' in hash_.keys()162 with raises(TypeError):163 hash_[1] = 'a'164 with raises(TypeError):165 hash_['abc'] = 1166 hashx = session.set(key('test_hashx_setitem'), fixture_b, Hash(NInt))167 hashx[1] = 'changed'168 assert len(hashx) == 2169 assert hashx[1] == 'changed'170 assert 1 in hashx.keys()171 hashx[1234] = 'added'172 assert len(hashx) == 3173 assert hashx[1234] == 'added'174 assert 1234 in hashx.keys()175 with raises(TypeError):176 hashx[1] = 1234177 with raises(TypeError):178 hashx['invalid'] = 'val'179def test_setdefault(session):180 hash_ = session.set(key('test_hash_setdefault'), fixture_a, Hash)181 curval = hash_.setdefault('a', 'would not get changed')182 assert curval == hash_['a'] == 'b'183 assert len(hash_) == 2184 curval = hash_.setdefault('added', 'default value')185 assert len(hash_) == 3186 assert 'added' in hash_187 assert curval == hash_['added'] == 'default value'188 with raises(TypeError):189 hash_.setdefault(1, 'default')190 with raises(TypeError):191 hash_.setdefault('key', 1234)192 hashx = session.set(key('test_hashx_setdefault'), fixture_b, Hash(NInt))193 curval = hashx.setdefault(1, 'would not get changed')194 assert curval == hashx[1] == 'a'195 assert len(hashx) == 2196 curval = hashx.setdefault(1234, 'default value')197 assert len(hashx) == 3198 assert 1234 in hashx199 assert curval == hashx[1234] == 'default value'200 with raises(TypeError):201 hashx.setdefault('invalid', 'default')202 with raises(TypeError):203 hashx.setdefault('key', 1234)204def test_setdefault_t(session):205 session2 = get_session()206 keyid = key('test_hash_setdefault_t')207 hash_ = session.set(keyid, fixture_a, Hash)208 hashx = session2.get(keyid, Hash)209 with Transaction(session, [keyid]):210 curval = hash_.setdefault('a', 'would not get changed')211 assert curval == hashx['a'] == 'b'212 assert curval == hash_['a'] == hashx['a'] == 'b'213 assert len(hash_) == len(hashx) == 2214 with Transaction(session, [keyid]):215 curval = hash_.setdefault('added', 'default value')216 assert curval == 'default value'217 assert 'added' not in hashx218 with raises(CommitError):219 len(hash_)220 assert len(hash_) == len(hashx) == 3221 assert 'added' in hash_222 assert 'added' in hashx223 assert curval == hash_['added'] == hashx['added'] == 'default value'224def test_update(session):225 hash_ = session.set(key('test_hash_update'), fixture_a, Hash)226 hash_.update({'c': 'changed', 'new': 'value'})227 assert dict(hash_) == {'a': 'b', 'c': 'changed', 'new': 'value'}228 hash_.update([('new2', 'value'), ('new', 'changed')])229 assert dict(hash_) == {'a': 'b', 'c': 'changed',230 'new': 'changed', 'new2': 'value'}231 hash_.update({'b': 'new', 'a': 'changed'}, d='new', new2='changed')232 assert dict(hash_) == {'a': 'changed', 'b': 'new', 'c': 'changed',233 'd': 'new', 'new': 'changed', 'new2': 'changed'}234 hash_.update([('e', 'new'), ('new', 'changed2')],235 c='changed2', f='new')236 assert dict(hash_) == {'a': 'changed', 'b': 'new', 'c': 'changed2',237 'd': 'new', 'e': 'new', 'f': 'new',238 'new': 'changed2', 'new2': 'changed'}239 hash_.update(b='changed', g='new')240 assert dict(hash_) == {'a': 'changed', 'b': 'changed', 'c': 'changed2',241 'd': 'new', 'e': 'new', 'f': 'new', 'g': 'new',242 'new': 'changed2', 'new2': 'changed'}243 with raises(TypeError):244 hash_.update({1: 'val'})245 with raises(TypeError):246 hash_.update({'key': 1234})247 with raises(TypeError):248 hash_.update([(1, 'val')])249 with raises(TypeError):250 hash_.update([('key', 1234)])251 with raises(TypeError):252 hash_.update(key=1234)253 with raises(TypeError):254 hash_.update([1, 2, 3, 4])255 with raises(ValueError):256 hash_.update([(1, 2, 3), (4, 5, 6)])257 with raises(TypeError):258 hash_.update(1234)259 hashx = session.set(key('test_hashx_update'), fixture_b, Hash(NInt))260 hashx.update({2: 'changed', 3: 'new'})261 assert dict(hashx) == {1: 'a', 2: 'changed', 3: 'new'}262 hashx.update([(3, 'changed'), (4, 'new')])263 assert dict(hashx) == {1: 'a', 2: 'changed', 3: 'changed', 4: 'new'}264 with raises(TypeError):265 hashx.update({'invalid': 'val'})266 with raises(TypeError):267 hashx.update({1234: 4567})268 with raises(TypeError):269 hashx.update([('invalid', 'val')])270 with raises(TypeError):271 hashx.update([(1234, 4567)])272 with raises(TypeError):273 hashx.update(invalid='val')274 with raises(TypeError):275 hashx.update([1, 2, 3, 4])276 with raises(ValueError):277 hashx.update([(1, 2, 3), (4, 5, 6)])278 with raises(TypeError):279 hashx.update(1234)280def test_massive_update(session):281 huge_data = dict(('{0}'.format(i), chr(ord('a') + (i % 26)) * i)282 for i in range(1235))283 hash_ = session.get(key('test_hash_massive_update'), Hash)284 hash_.update(huge_data)285 assert dict(hash_) == huge_data286def test_repr(session):287 keyid = key('test_hash_repr')288 hash_ = session.set(keyid, {1: 2, 3: 4, 5: 6}, Hash(NInt, NInt))289 expected = '<sider.hash.Hash (' + repr(keyid) + ') {1: 2, 3: 4, 5: 6}>'...
test_provider.py
Source:test_provider.py
1import nose.tools as t2import os3import vel.internals.provider as v4import vel.internals.parser as p5import vel.exceptions as e6def data_function(a, b):7 return a + b8def test_simple_instantiation():9 provider = v.Provider({10 'a': 1,11 'b': 2,12 })13 t.assert_equal(provider.instantiate_from_data(1), 1)14 t.assert_equal(provider.instantiate_from_data("abc"), "abc")15 t.assert_equal(provider.instantiate_from_data([1, 2, 3]), [1, 2, 3])16 t.assert_equal(provider.instantiate_from_data({"a": "a", "b": "b"}), {"a": "a", "b": "b"})17def test_instantiate_function_call():18 provider = v.Provider({19 'a': 1,20 'b': 2,21 })22 t.assert_equal(provider.resolve_and_call(data_function), 3)23 t.assert_equal(provider.resolve_and_call(data_function, extra_env={'b': 4}), 5)24def test_simple_injection():25 provider = v.Provider({26 'a': 1,27 'b': 2,28 'one': {29 'name': 'vel.internals.tests.fixture_a'30 },31 'two': {32 'name': 'vel.internals.tests.fixture_a',33 'a': 5,34 'b': 635 },36 'three': {37 'name': 'vel.internals.tests.fixture_b',38 'd': 'd'39 }40 })41 one = provider.instantiate_by_name('one')42 t.assert_is_instance(one, dict)43 t.assert_equal(one['a'], 1)44 t.assert_equal(one['b'], 2)45 two = provider.instantiate_by_name('two')46 t.assert_is_instance(two, dict)47 t.assert_equal(two['a'], 5)48 t.assert_equal(two['b'], 6)49 three = provider.instantiate_by_name('three')50 t.assert_is_instance(three, dict)51 t.assert_equal(id(three['one']), id(one))52 t.assert_not_equal(id(three['one']), id(two))53 t.assert_equal(three['d'], 'd')54def test_parameter_resolution():55 os.environ['TEST_VAR'] = '10'56 provider = v.Provider({57 'a': 1,58 'b': p.Parameter("xxx"),59 'one': {60 'name': 'vel.internals.tests.fixture_a'61 },62 'two': {63 'name': 'vel.internals.tests.fixture_a',64 'b': p.Parameter('yyy')65 },66 'three': {67 'name': 'vel.internals.tests.fixture_a',68 'b': p.Parameter('yyy', 7)69 },70 'four': {71 'name': 'vel.internals.tests.fixture_a',72 'b': p.EnvironmentVariable('TEST_VAR')73 },74 }, parameters={'xxx': 5})75 one = provider.instantiate_by_name('one')76 t.assert_equal(one['b'], 5)77 with t.assert_raises(e.VelException):78 provider.instantiate_by_name('two')79 three = provider.instantiate_by_name('three')80 t.assert_equal(three['b'], 7)81 four = provider.instantiate_by_name('four')82 t.assert_equal(four['b'], '10')83def test_render_configuration():84 os.environ['TEST_VAR'] = '10'85 provider = v.Provider({86 'a': 1,87 'b': p.Parameter("xxx"),88 'one': {89 'name': 'vel.internals.tests.fixture_a'90 },91 'two': {92 'name': 'vel.internals.tests.fixture_a',93 'b': p.Parameter('yyy', 5)94 },95 'three': {96 'name': 'vel.internals.tests.fixture_a',97 'b': p.Parameter('yyy', 7)98 },99 'four': {100 'name': 'vel.internals.tests.fixture_a',101 'b': p.EnvironmentVariable('TEST_VAR')102 },103 }, parameters={'xxx': 5})104 configuration = provider.render_configuration()105 t.assert_equal(configuration, {106 'a': 1,107 'b': 5,108 'one': {109 'name': 'vel.internals.tests.fixture_a'110 },111 'two': {112 'name': 'vel.internals.tests.fixture_a',113 'b': 5114 },115 'three': {116 'name': 'vel.internals.tests.fixture_a',117 'b': 7118 },119 'four': {120 'name': 'vel.internals.tests.fixture_a',121 'b': '10'122 },...
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!!