Best Python code snippet using molecule_python
test_sequences.py
Source:test_sequences.py
...235 inc = seq.increment or 1236 eq_(values, list(range(start, start + inc * 3, inc)))237 finally:238 seq.drop(testing.db)239 def _has_sequence(self, connection, name):240 return testing.db.dialect.has_sequence(connection, name)241 def test_nextval_unsupported(self):242 """test next_value() used on non-sequence platform243 raises NotImplementedError."""244 s = Sequence("my_seq")245 d = sqlite.dialect()246 assert_raises_message(247 NotImplementedError,248 "Dialect 'sqlite' does not support sequence increments.",249 s.next_value().compile,250 dialect=d,251 )252 def test_checkfirst_sequence(self, connection):253 s = Sequence("my_sequence")254 s.create(connection, checkfirst=False)255 assert self._has_sequence(connection, "my_sequence")256 s.create(connection, checkfirst=True)257 s.drop(connection, checkfirst=False)258 assert not self._has_sequence(connection, "my_sequence")259 s.drop(connection, checkfirst=True)260 def test_checkfirst_metadata(self, connection):261 m = MetaData()262 Sequence("my_sequence", metadata=m)263 m.create_all(connection, checkfirst=False)264 assert self._has_sequence(connection, "my_sequence")265 m.create_all(connection, checkfirst=True)266 m.drop_all(connection, checkfirst=False)267 assert not self._has_sequence(connection, "my_sequence")268 m.drop_all(connection, checkfirst=True)269 def test_checkfirst_table(self, connection):270 m = MetaData()271 s = Sequence("my_sequence")272 t = Table("t", m, Column("c", Integer, s, primary_key=True))273 t.create(connection, checkfirst=False)274 assert self._has_sequence(connection, "my_sequence")275 t.create(connection, checkfirst=True)276 t.drop(connection, checkfirst=False)277 assert not self._has_sequence(connection, "my_sequence")278 t.drop(connection, checkfirst=True)279 @testing.provide_metadata280 def test_table_overrides_metadata_create(self, connection):281 metadata = self.metadata282 Sequence("s1", metadata=metadata)283 s2 = Sequence("s2", metadata=metadata)284 s3 = Sequence("s3")285 t = Table("t", metadata, Column("c", Integer, s3, primary_key=True))286 assert s3.metadata is metadata287 t.create(connection, checkfirst=True)288 s3.drop(connection)289 # 't' is created, and 's3' won't be290 # re-created since it's linked to 't'.291 # 's1' and 's2' are, however.292 metadata.create_all(connection)293 assert self._has_sequence(connection, "s1")294 assert self._has_sequence(connection, "s2")295 assert not self._has_sequence(connection, "s3")296 s2.drop(connection)297 assert self._has_sequence(connection, "s1")298 assert not self._has_sequence(connection, "s2")299 metadata.drop_all(connection)300 assert not self._has_sequence(connection, "s1")301 assert not self._has_sequence(connection, "s2")302 @testing.requires.returning303 @testing.provide_metadata304 def test_freestanding_sequence_via_autoinc(self, connection):305 t = Table(306 "some_table",307 self.metadata,308 Column(309 "id",310 Integer,311 autoincrement=True,312 primary_key=True,313 default=Sequence(314 "my_sequence", metadata=self.metadata315 ).next_value(),...
Manipulations.py
Source:Manipulations.py
...190 while True:191 yield b-a192 193 a,b = b,next(sequence)194def hypersequence(sequence):195 """196 Returns the a(n)th term of the sequence a(n)197 For example if a(n) = 1, 3, 5, 7, 9, 11, 13...198 hypersequence(a(n)) = 1, 5, 9, 13, 17...199 """200 201 L = []202 pr = 0203 204 for n,v in enumerate(sequence,1):205 if pr >= v:206 raise ValueError("Can only create a hypersequence of a sequence that is entirely positive and strictly increasing")207 208 L.append(v)209 210 if n in L:211 yield v212 del L[0]213 214 pr = v215def run_length_encoding(sequence,reverse=False):216 """217 Encodes sequence the length of the runs followed by the number that is repeated218 Alternatively returns the number and then the length of the run219 """220 221 ctr = 0222 cur = next(sequence)223 224 if reverse:225 for i in sequence:226 ctr += 1227 228 if i != cur:229 yield cur230 yield ctr231 232 ctr = 0233 cur = i234 235 yield cur236 yield ctr+1237 238 else:239 for i in sequence:240 ctr += 1241 242 if i != cur:243 yield ctr244 yield cur245 246 ctr = 0247 cur = i248 249 yield ctr+1250 yield cur251def inv_run_length_encoding(sequence,reverse=False):252 """253 Convert a Run Length Encoding to the original sequence254 """255 256 if reverse:257 for a,b in chunk_by_n(sequence,2):258 for i in range(b):259 yield a260 261 else:262 for a,b in chunk_by_n(sequence,2):263 for i in range(a):264 yield b265def run_lengths(sequence):266 """267 Returns the length of the runs in the sequence268 """269 270 ctr = 0271 cur = next(sequence)272 273 for i in sequence:274 ctr += 1275 276 if i != cur:277 yield ctr278 279 ctr = 0280 cur = i281 282 yield ctr+1283def n_rep_a(sequence,rep_sequence):284 """285 Take elements pairwise and repeat the left value as many times as specified by the right argument286 """287 288 for n,a in zip(sequence,rep_sequence):289 yield from repeat(n,a)290def records(sequence):291 292 r = next(sequence)293 yield r294 295 for s in sequence:296 if s > r:297 yield s298 r = s299def permute(sequence,permutation):300 """301 Reorder the sequence according to the permutation.302 The permutation must be a permutation of the non-negative integers or behavior is undefined.303 """304 305 L = []306 307 for pos in permutation:308 while pos > (len(L)-1):309 L.append(next(sequence))310 311 yield L[pos]312def memoize_multiplicative(function):313 """314 Given a multiplicative function over the naturals calculate the values for315 all naturals by evaluating explicitly at prime powers and using the 316 multiplicative property for all others317 WARNING: Currently only useful if the function is extremely expensive to compute318 """319 320 # For any multiplicative function f(1) = 1321 yield 1322 323 D = {}324 325 for n in count(2,1):326 p = prime_power_factorization(n)327 328 if len(p) == 1:329 D[n] = function(n)330 331 yield prod([D[i] for i in p])332def memoize_total_multiplicative(function):333 """334 Given a total multiplicative function over the naturals calculate the 335 values for all naturals by evaluating explicitly at primes and using the 336 multiplicative property for all others337 WARNING: Currently only useful if the function is extremely expensive to compute338 """339 340 # For any multiplicative function f(1) = 1341 yield 1342 343 D = {}344 345 for n in count(2,1):346 p = prime_factorization(n)347 348 if len(p) == 1:349 D[n] = function(n)350 351 yield prod([D[i] for i in p])352def memoize_additive(function):353 """354 Given an additive function over the naturals calculate the values for355 all naturals by evaluating explicitly at prime powers and using the 356 additive property for all others357 WARNING: Currently only useful if the function is extremely expensive to compute358 """359 360 D = {1: function(1)}361 362 for n in count(2,1):363 p = prime_power_factorization(n)364 365 if len(p) == 1:366 D[n] = function(n)367 368 yield sum([D[i] for i in p])+D[1]369def memoize_total_additive(function):370 """371 Given atotal additive function over the naturals calculate the values for372 all naturals by evaluating explicitly at primes and using the additive373 property for all others374 WARNING: Currently only useful if the function is extremely expensive to compute375 """376 377 # For any total additive function f(1) = 0378 yield 0379 380 D = {}381 382 for n in count(2,1):383 p = prime_factorization(n)384 385 if len(p) == 1:386 D[n] = function(n)387 388 yield sum([D[i] for i in p])389# To avoid potential circular reference from Sequences.Primes in following functions390def _primes_copy():391 yield 2392 yield 3393 yield 5394 395 D = { 9: 3, 25: 5 }396 MASK = cycle((1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0))397 MODULOS = frozenset( (1, 7, 11, 13, 17, 19, 23, 29) )398 399 for q in compress(count(7,2),MASK):400 p = D.pop(q, None)401 402 if p is None:403 D[q*q] = q404 yield q405 406 else:407 x = q + 2*p408 409 while x in D or (x%30) not in MODULOS:410 x += 2*p411 412 D[x] = p413def prime_subsequence(sequence):414 """415 Given a monotonically increasing sequence return all the prime elements416 WARNING: Doesn't check for monotonically increasing property417 """418 419 prime = _primes_copy()420 421 a = next(prime)422 b = next(sequence)423 424 while True:425 if a == b:426 yield a427 ...
test_ir_sequence.py
Source:test_ir_sequence.py
...16def registry(model):17 return openerp.modules.registry.RegistryManager.get(DB)[model]18def cursor():19 return openerp.modules.registry.RegistryManager.get(DB).db.cursor()20def drop_sequence(code):21 cr = cursor()22 for model in ['ir.sequence', 'ir.sequence.type']:23 s = registry(model)24 ids = s.search(cr, ADMIN_USER_ID, [('code', '=', code)])25 s.unlink(cr, ADMIN_USER_ID, ids)26 cr.commit()27 cr.close()28class test_ir_sequence_standard(unittest2.TestCase):29 """ A few tests for a 'Standard' (i.e. PostgreSQL) sequence. """30 def test_ir_sequence_create(self):31 """ Try to create a sequence object. """32 cr = cursor()33 d = dict(code='test_sequence_type', name='Test sequence type')34 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})35 assert c36 d = dict(code='test_sequence_type', name='Test sequence')37 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})38 assert c39 cr.commit()40 cr.close()41 def test_ir_sequence_search(self):42 """ Try a search. """43 cr = cursor()44 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID, [], {})45 assert ids46 cr.commit()47 cr.close()48 def test_ir_sequence_draw(self):49 """ Try to draw a number. """50 cr = cursor()51 n = registry('ir.sequence').next_by_code(cr, ADMIN_USER_ID, 'test_sequence_type', {})52 assert n53 cr.commit()54 cr.close()55 def test_ir_sequence_draw_twice(self):56 """ Try to draw a number from two transactions. """57 cr0 = cursor()58 cr1 = cursor()59 n0 = registry('ir.sequence').next_by_code(cr0, ADMIN_USER_ID, 'test_sequence_type', {})60 assert n061 n1 = registry('ir.sequence').next_by_code(cr1, ADMIN_USER_ID, 'test_sequence_type', {})62 assert n163 cr0.commit()64 cr1.commit()65 cr0.close()66 cr1.close()67 @classmethod68 def tearDownClass(cls):69 drop_sequence('test_sequence_type')70class test_ir_sequence_no_gap(unittest2.TestCase):71 """ Copy of the previous tests for a 'No gap' sequence. """72 def test_ir_sequence_create_no_gap(self):73 """ Try to create a sequence object. """74 cr = cursor()75 d = dict(code='test_sequence_type_2', name='Test sequence type')76 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})77 assert c78 d = dict(code='test_sequence_type_2', name='Test sequence',79 implementation='no_gap')80 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})81 assert c82 cr.commit()83 cr.close()84 def test_ir_sequence_draw_no_gap(self):85 """ Try to draw a number. """86 cr = cursor()87 n = registry('ir.sequence').next_by_code(cr, ADMIN_USER_ID, 'test_sequence_type_2', {})88 assert n89 cr.commit()90 cr.close()91 def test_ir_sequence_draw_twice_no_gap(self):92 """ Try to draw a number from two transactions.93 This is expected to not work.94 """95 cr0 = cursor()96 cr1 = cursor()97 cr1._default_log_exceptions = False # Prevent logging a traceback98 msg_re = '^could not obtain lock on row in relation "ir_sequence"$'99 with self.assertRaisesRegexp(psycopg2.OperationalError, msg_re):100 n0 = registry('ir.sequence').next_by_code(cr0, ADMIN_USER_ID, 'test_sequence_type_2', {})101 assert n0102 n1 = registry('ir.sequence').next_by_code(cr1, ADMIN_USER_ID, 'test_sequence_type_2', {})103 cr0.close()104 cr1.close()105 @classmethod106 def tearDownClass(cls):107 drop_sequence('test_sequence_type_2')108class test_ir_sequence_change_implementation(unittest2.TestCase):109 """ Create sequence objects and change their ``implementation`` field. """110 def test_ir_sequence_1_create(self):111 """ Try to create a sequence object. """112 cr = cursor()113 d = dict(code='test_sequence_type_3', name='Test sequence type')114 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})115 assert c116 d = dict(code='test_sequence_type_3', name='Test sequence')117 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})118 assert c119 d = dict(code='test_sequence_type_4', name='Test sequence type')120 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})121 assert c122 d = dict(code='test_sequence_type_4', name='Test sequence',123 implementation='no_gap')124 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})125 assert c126 cr.commit()127 cr.close()128 def test_ir_sequence_2_write(self):129 cr = cursor()130 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID,131 [('code', 'in', ['test_sequence_type_3', 'test_sequence_type_4'])], {})132 registry('ir.sequence').write(cr, ADMIN_USER_ID, ids,133 {'implementation': 'standard'}, {})134 registry('ir.sequence').write(cr, ADMIN_USER_ID, ids,135 {'implementation': 'no_gap'}, {})136 cr.commit()137 cr.close()138 def test_ir_sequence_3_unlink(self):139 cr = cursor()140 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID,141 [('code', 'in', ['test_sequence_type_3', 'test_sequence_type_4'])], {})142 registry('ir.sequence').unlink(cr, ADMIN_USER_ID, ids, {})143 cr.commit()144 cr.close()145 @classmethod146 def tearDownClass(cls):147 drop_sequence('test_sequence_type_3')148 drop_sequence('test_sequence_type_4')149class test_ir_sequence_generate(unittest2.TestCase):150 """ Create sequence objects and generate some values. """151 def test_ir_sequence_create(self):152 """ Try to create a sequence object. """153 cr = cursor()154 d = dict(code='test_sequence_type_5', name='Test sequence type')155 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})156 assert c157 d = dict(code='test_sequence_type_5', name='Test sequence')158 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})159 assert c160 cr.commit()161 cr.close()162 cr = cursor()163 f = lambda *a: registry('ir.sequence').next_by_code(cr, ADMIN_USER_ID, 'test_sequence_type_5', {})164 assert all(str(x) == f() for x in xrange(1,10))165 cr.commit()166 cr.close()167 def test_ir_sequence_create_no_gap(self):168 """ Try to create a sequence object. """169 cr = cursor()170 d = dict(code='test_sequence_type_6', name='Test sequence type')171 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})172 assert c173 d = dict(code='test_sequence_type_6', name='Test sequence')174 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})175 assert c176 cr.commit()177 cr.close()178 cr = cursor()179 f = lambda *a: registry('ir.sequence').next_by_code(cr, ADMIN_USER_ID, 'test_sequence_type_6', {})180 assert all(str(x) == f() for x in xrange(1,10))181 cr.commit()182 cr.close()183 @classmethod184 def tearDownClass(cls):185 drop_sequence('test_sequence_type_5')186 drop_sequence('test_sequence_type_6')187if __name__ == '__main__':188 unittest2.main()...
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!!