How to use _save method in assertpy

Best Python code snippet using assertpy_python

transactions.py

Source:transactions.py Github

copy

Full Screen

...13 requires = [Register]14 def assertRegister(self, vals):15 query = Register.select().order_by(Register.value)16 self.assertEqual([register.value for register in query], vals)17 def _save(self, *vals):18 Register.insert([{Register.value: val} for val in vals]).execute()19def requires_nested(fn):20 return skip_if(IS_CRDB and not IS_CRDB_NESTED_TX,21 'nested transaction support is required')(fn)22class TestTransaction(BaseTransactionTestCase):23 def test_simple(self):24 self.assertFalse(db.in_transaction())25 with db.atomic():26 self.assertTrue(db.in_transaction())27 self._save(1)28 self.assertFalse(db.in_transaction())29 self.assertRegister([1])30 # Explicit rollback, implicit commit.31 with db.atomic() as txn:32 self._save(2)33 txn.rollback()34 self.assertTrue(db.in_transaction())35 self._save(3)36 self.assertFalse(db.in_transaction())37 self.assertRegister([1, 3])38 # Explicit rollbacks.39 with db.atomic() as txn:40 self._save(4)41 txn.rollback()42 self._save(5)43 txn.rollback()44 self.assertRegister([1, 3])45 @requires_nested46 def test_transactions(self):47 self.assertFalse(db.in_transaction())48 with db.atomic():49 self.assertTrue(db.in_transaction())50 self._save(1)51 self.assertRegister([1])52 with db.atomic() as txn:53 self._save(2)54 txn.rollback()55 self._save(3)56 with db.atomic() as sp1:57 self._save(4)58 with db.atomic() as sp2:59 self._save(5)60 sp2.rollback()61 with db.atomic() as sp3:62 self._save(6)63 with db.atomic() as sp4:64 self._save(7)65 with db.atomic() as sp5:66 self._save(8)67 self.assertRegister([1, 3, 4, 6, 7, 8])68 sp4.rollback()69 self.assertRegister([1, 3, 4, 6])70 self.assertRegister([1, 3, 4, 6])71 def test_commit_rollback(self):72 with db.atomic() as txn:73 self._save(1)74 txn.commit()75 self._save(2)76 txn.rollback()77 self.assertRegister([1])78 with db.atomic() as txn:79 self._save(3)80 txn.rollback()81 self._save(4)82 self.assertRegister([1, 4])83 @requires_nested84 def test_commit_rollback_nested(self):85 with db.atomic() as txn:86 self.test_commit_rollback()87 txn.rollback()88 self.assertRegister([])89 with db.atomic():90 self.test_commit_rollback()91 self.assertRegister([1, 4])92 def test_nesting_transaction_obj(self):93 self.assertRegister([])94 with db.transaction() as txn:95 self._save(1)96 with db.transaction() as txn2:97 self._save(2)98 txn2.rollback() # Actually issues a rollback.99 self.assertRegister([])100 self._save(3)101 self.assertRegister([3])102 with db.transaction() as txn:103 self._save(4)104 with db.transaction() as txn2:105 with db.transaction() as txn3:106 self._save(5)107 txn3.commit() # Actually commits.108 self._save(6)109 txn2.rollback()110 self.assertRegister([3, 4, 5])111 @requires_nested112 def test_savepoint_commit(self):113 with db.atomic() as txn:114 self._save(1)115 txn.rollback()116 self._save(2)117 txn.commit()118 with db.atomic() as sp:119 self._save(3)120 sp.rollback()121 self._save(4)122 sp.commit()123 self.assertRegister([2, 4])124 def test_atomic_decorator(self):125 @db.atomic()126 def save(i):127 self._save(i)128 save(1)129 self.assertRegister([1])130 def text_atomic_exception(self):131 def will_fail(self):132 with db.atomic():133 self._save(1)134 self._save(None)135 self.assertRaises(IntegrityError, will_fail)136 self.assertRegister([])137 def user_error(self):138 with db.atomic():139 self._save(2)140 raise ValueError141 self.assertRaises(ValueError, user_error)142 self.assertRegister([])143 def test_manual_commit(self):144 with db.manual_commit():145 db.begin()146 self._save(1)147 db.rollback()148 db.begin()149 self._save(2)150 db.commit()151 with db.manual_commit():152 db.begin()153 self._save(3)154 db.rollback()155 db.begin()156 self._save(4)157 db.commit()158 self.assertRegister([2, 4])159 def test_mixing_manual_atomic(self):160 @db.manual_commit()161 def will_fail():162 pass163 @db.atomic()164 def also_fails():165 pass166 with db.atomic():167 self.assertRaises(ValueError, will_fail)168 with db.manual_commit():169 self.assertRaises(ValueError, also_fails)170 with db.manual_commit():171 with self.assertRaises(ValueError):172 with db.atomic(): pass173 with db.atomic():174 with self.assertRaises(ValueError):175 with db.manual_commit(): pass176 def test_closing_db_in_transaction(self):177 with db.atomic():178 self.assertRaises(OperationalError, db.close)179 @requires_nested180 def test_db_context_manager(self):181 db.close()182 self.assertTrue(db.is_closed())183 with db:184 self.assertFalse(db.is_closed())185 self._save(1)186 with db:187 self._save(2)188 try:189 with db:190 self._save(3)191 raise ValueError('xxx')192 except ValueError:193 pass194 self._save(4)195 try:196 with db:197 self._save(5)198 with db:199 self._save(6)200 raise ValueError('yyy')201 except ValueError:202 pass203 self.assertFalse(db.is_closed())204 self.assertTrue(db.is_closed())205 self.assertRegister([1, 2, 4])206@requires_nested207class TestSession(BaseTransactionTestCase):208 def test_session(self):209 self.assertTrue(db.session_start())210 self.assertTrue(db.session_start())211 self.assertEqual(db.transaction_depth(), 2)212 self._save(1)213 self.assertTrue(db.session_commit())214 self.assertEqual(db.transaction_depth(), 1)215 self._save(2) # Now we're in autocommit mode.216 self.assertTrue(db.session_rollback())217 self.assertEqual(db.transaction_depth(), 0)218 self.assertTrue(db.session_start())219 self._save(3)220 self.assertTrue(db.session_rollback())221 self.assertRegister([1])222 def test_session_with_closed_db(self):223 db.close()224 self.assertTrue(db.session_start())225 self.assertFalse(db.is_closed())226 self.assertRaises(OperationalError, db.close)227 self._save(1)228 self.assertTrue(db.session_rollback())229 self.assertRegister([])230 def test_session_inside_context_manager(self):231 with db.atomic():232 self.assertTrue(db.session_start())233 self._save(1)234 self.assertTrue(db.session_commit())235 self._save(2)236 self.assertTrue(db.session_rollback())237 db.session_start()238 self._save(3)239 self.assertRegister([1, 3])240 def test_commit_rollback_mix(self):241 db.session_start()242 with db.atomic() as txn: # Will be a savepoint.243 self._save(1)244 with db.atomic() as t2:245 self._save(2)246 with db.atomic() as t3:247 self._save(3)248 t2.rollback()249 txn.commit()250 self._save(4)251 txn.rollback()252 self.assertTrue(db.session_commit())253 self.assertRegister([1])254 def test_session_rollback(self):255 db.session_start()256 self._save(1)257 with db.atomic() as txn:258 self._save(2)259 with db.atomic() as t2:260 self._save(3)261 self.assertRegister([1, 2, 3])262 self.assertTrue(db.session_rollback())263 self.assertRegister([])264 db.session_start()265 self._save(1)266 with db.transaction() as txn:267 self._save(2)268 with db.transaction() as t2:269 self._save(3)270 t2.rollback() # Rolls back everything, starts new txn.271 db.session_commit()272 self.assertRegister([])273 def test_session_commit(self):274 db.session_start()275 self._save(1)276 with db.transaction() as txn:277 self._save(2)278 with db.transaction() as t2:279 self._save(3)280 t2.commit() # Saves everything, starts new txn.281 txn.rollback()282 self.assertTrue(db.session_rollback())283 self.assertRegister([1, 2, 3])284@skip_unless(IS_SQLITE, 'requires sqlite for transaction lock type')285class TestTransactionLockType(BaseTransactionTestCase):286 def test_lock_type(self):287 db2 = new_connection(timeout=0.001)288 db2.connect()289 with self.database.atomic(lock_type='EXCLUSIVE') as txn:290 with self.assertRaises(OperationalError):291 with db2.atomic(lock_type='IMMEDIATE') as t2:292 self._save(1)293 self._save(2)294 self.assertRegister([2])295 with self.database.atomic('IMMEDIATE') as txn:296 with self.assertRaises(OperationalError):297 with db2.atomic('EXCLUSIVE') as t2:298 self._save(3)299 self._save(4)300 self.assertRegister([2, 4])301 with self.database.transaction(lock_type='DEFERRED') as txn:302 self._save(5) # Deferred -> Exclusive after our write.303 with self.assertRaises(OperationalError):304 with db2.transaction(lock_type='IMMEDIATE') as t2:305 self._save(6)...

Full Screen

Full Screen

test_orm_transactions.py

Source:test_orm_transactions.py Github

copy

Full Screen

...22 def teardown():23 Register.all().delete()24 db.commit()25 request.addfinalizer(teardown)26def _save(*vals):27 for val in vals:28 Register.create(value=val)29def _values_in_register(*vals):30 db_vals = Register.all().select(orderby=Register.value).column('value')31 return db_vals == list(vals)32def test_transactions(db, cleanup):33 adapter = db._adapter34 assert adapter.in_transaction()35 _save(1)36 db.commit()37 assert _values_in_register(1)38 _save(2)39 db.rollback()40 _save(3)41 with db.atomic():42 _save(4)43 with db.atomic() as sp2:44 _save(5)45 sp2.rollback()46 with db.atomic():47 _save(6)48 with db.atomic() as sp4:49 _save(7)50 with db.atomic():51 _save(8)52 assert _values_in_register(1, 3, 4, 6, 7, 8)53 sp4.rollback()54 assert _values_in_register(1, 3, 4, 6)55 db.commit()56 assert _values_in_register(1, 3, 4, 6)57def _commit_rollback(db):58 _save(1)59 db.commit()60 _save(2)61 db.rollback()62 assert _values_in_register(1)63 _save(3)64 db.rollback()65 _save(4)66 db.commit()67 assert _values_in_register(1, 4)68def test_commit_rollback(db, cleanup):69 _commit_rollback(db)70def test_commit_rollback_nested(db, cleanup):71 with db.atomic():72 _commit_rollback(db)73 assert _values_in_register(1, 4)74def test_nested_transaction_obj(db, cleanup):75 assert _values_in_register()76 _save(1)77 with db.transaction() as txn:78 _save(2)79 txn.rollback()80 assert _values_in_register()81 _save(3)82 db.commit()83 assert _values_in_register(3)84def test_savepoint_commit(db, cleanup):85 _save(1)86 db.rollback()87 _save(2)88 db.commit()89 with db.atomic() as sp:90 _save(3)91 sp.rollback()92 _save(4)93 sp.commit()94 assert _values_in_register(2, 4)95def text_atomic_exception(db, cleanup):96 def will_fail():97 with db.atomic():98 _save(1)99 _save(None)100 with pytest.raises(Exception):101 will_fail()102 assert _values_in_register()103 def user_error():104 with db.atomic():105 _save(2)106 raise ValueError107 with pytest.raises(ValueError):108 user_error()...

Full Screen

Full Screen

keras55_save_load_npy.py

Source:keras55_save_load_npy.py Github

copy

Full Screen

1from sklearn.datasets import load_iris, load_boston, load_breast_cancer, load_diabetes, load_wine2from tensorflow.keras.datasets import mnist, fashion_mnist, cifar10, cifar1003import numpy as np4iris = load_iris()5x_iris = iris.data6y_iris = iris.target7boston = load_boston()8x_boston = boston.data9y_boston = boston.target10cancer = load_breast_cancer()11x_cancer = cancer.data12y_cancer = cancer.target13diabetes = load_diabetes()14x_diabetes = diabetes.data15y_diabetes = diabetes.target16wine = load_wine()17x_wine = wine.data18y_wine = wine.target19(x_mnist_train, y_mnist_train), (x_mnist_test, y_mnist_test) = mnist.load_data()20(x_fashion_train, y_fashion_train), (x_fashion_test, y_fashion_test) = fashion_mnist.load_data()21(x_cifar10_train, y_cifar10_train), (x_cifar10_test, y_cifar10_test) = cifar10.load_data()22(x_cifar100_train, y_cifar100_train), (x_cifar100_test, y_cifar100_test) = cifar100.load_data()23# 필요한 데이터 loading24x_data = np.load('./_save/_npy/keras55_x_iris.npy')25y_data = np.load('./_save/_npy/keras55_y_iris.npy')26# np.save('./_save/NPY/keras55_x_iris.npy', arr=x_iris)27# np.save('./_save/NPY/keras55_y_iris.npy', arr=y_iris)28# np.save('./_save/NPY/keras55_x_boston.npy', arr=x_boston)29# np.save('./_save/NPY/keras55_y_boston.npy', arr=y_boston)30# np.save('./_save/NPY/keras55_x_cancer.npy', arr=x_cancer)31# np.save('./_save/NPY/keras55_y_cancer.npy', arr=y_cancer)32# np.save('./_save/NPY/keras55_x_diabetes.npy', arr=x_diabetes)33# np.save('./_save/NPY/keras55_y_diabetes.npy', arr=y_diabetes)34# np.save('./_save/NPY/keras55_x_wine.npy', arr=x_wine)35# np.save('./_save/NPY/keras55_y_wine.npy', arr=y_wine)36# np.save('./_save/NPY/keras55_x_mnist_train.npy', arr=x_mnist_train)37# np.save('./_save/NPY/keras55_x_mnist_test.npy', arr=x_mnist_test)38# np.save('./_save/NPY/keras55_y_mnist_train.npy', arr=y_mnist_train)39# np.save('./_save/NPY/keras55_y_mnist_test.npy', arr=y_mnist_test)40# np.save('./_save/NPY/keras55_x_fashion_train.npy', arr=x_fashion_train)41# np.save('./_save/NPY/keras55_x_fashion_test.npy', arr=x_fashion_test)42# np.save('./_save/NPY/keras55_y_fashion_train.npy', arr=y_fashion_train)43# np.save('./_save/NPY/keras55_y_fashion_test.npy', arr=y_fashion_test)44# np.save('./_save/NPY/keras55_x_cifar10_train.npy', arr=x_cifar10_train)45# np.save('./_save/NPY/keras55_x_cifar10_test.npy', arr=x_cifar10_test)46# np.save('./_save/NPY/keras55_y_cifar10_train.npy', arr=y_cifar10_train)47# np.save('./_save/NPY/keras55_y_cifar10_test.npy', arr=y_cifar10_test)48# np.save('./_save/NPY/keras55_x_cifar100_train.npy', arr=x_cifar100_train)49# np.save('./_save/NPY/keras55_x_cifar100_test.npy', arr=x_cifar100_test)50# np.save('./_save/NPY/keras55_y_cifar100_train.npy', arr=y_cifar100_train)...

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 assertpy 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