How to use test_serialized_rollback method in pytest-django

Best Python code snippet using pytest-django_python

test_database.py

Source:test_database.py Github

copy

Full Screen

...92 result = django_testdir.runpytest_subprocess("-v", "--reuse-db")93 result.stdout.fnmatch_lines(94 ["*test_django_db_reset_sequences_requested PASSED*"]95 )96 def test_serialized_rollback(self, db: None, django_testdir) -> None:97 django_testdir.create_app_file(98 """99 from django.db import migrations100 def load_data(apps, schema_editor):101 Item = apps.get_model("app", "Item")102 Item.objects.create(name="loaded-in-migration")103 class Migration(migrations.Migration):104 dependencies = [105 ("app", "0001_initial"),106 ]107 operations = [108 migrations.RunPython(load_data),109 ]110 """,111 "migrations/0002_data_migration.py",112 )113 django_testdir.create_test_module(114 """115 import pytest116 from .app.models import Item117 @pytest.mark.django_db(transaction=True, serialized_rollback=True)118 def test_serialized_rollback_1():119 assert Item.objects.filter(name="loaded-in-migration").exists()120 @pytest.mark.django_db(transaction=True)121 def test_serialized_rollback_2(django_db_serialized_rollback):122 assert Item.objects.filter(name="loaded-in-migration").exists()123 Item.objects.create(name="test2")124 @pytest.mark.django_db(transaction=True, serialized_rollback=True)125 def test_serialized_rollback_3():126 assert Item.objects.filter(name="loaded-in-migration").exists()127 assert not Item.objects.filter(name="test2").exists()128 """129 )130 result = django_testdir.runpytest_subprocess("-v")131 assert result.ret == 0132 @pytest.fixture133 def mydb(self, all_dbs: None) -> None:134 # This fixture must be able to access the database135 Item.objects.create(name="spam")136 def test_mydb(self, mydb: None) -> None:137 if not connection.features.supports_transactions:138 pytest.skip("transactions required for this test")139 # Check the fixture had access to the db140 item = Item.objects.get(name="spam")141 assert item142 def test_fixture_clean(self, all_dbs: None) -> None:143 # Relies on the order: test_mydb created an object144 # See https://github.com/pytest-dev/pytest-django/issues/17145 assert Item.objects.count() == 0146 @pytest.fixture147 def fin(self, request, all_dbs: None) -> None:148 # This finalizer must be able to access the database149 request.addfinalizer(lambda: Item.objects.create(name="spam"))150 def test_fin(self, fin: None) -> None:151 # Check finalizer has db access (teardown will fail if not)152 pass153 @pytest.mark.skipif(get_django_version() < (3, 2), reason="Django >= 3.2 required")154 def test_durable_transactions(self, all_dbs: None) -> None:155 with transaction.atomic(durable=True):156 item = Item.objects.create(name="foo")157 assert Item.objects.get() == item158class TestDatabaseFixturesAllOrder:159 @pytest.fixture160 def fixture_with_db(self, db: None) -> None:161 Item.objects.create(name="spam")162 @pytest.fixture163 def fixture_with_transdb(self, transactional_db: None) -> None:164 Item.objects.create(name="spam")165 @pytest.fixture166 def fixture_with_reset_sequences(self, django_db_reset_sequences: None) -> None:167 Item.objects.create(name="spam")168 @pytest.fixture169 def fixture_with_serialized_rollback(self, django_db_serialized_rollback: None) -> None:170 Item.objects.create(name="ham")171 def test_trans(self, fixture_with_transdb: None) -> None:172 pass173 def test_db(self, fixture_with_db: None) -> None:174 pass175 def test_db_trans(self, fixture_with_db: None, fixture_with_transdb: None) -> None:176 pass177 def test_trans_db(self, fixture_with_transdb: None, fixture_with_db: None) -> None:178 pass179 def test_reset_sequences(180 self,181 fixture_with_reset_sequences: None,182 fixture_with_transdb: None,183 fixture_with_db: None,184 ) -> None:185 pass186 # The test works when transactions are not supported, but it interacts187 # badly with other tests.188 @pytest.mark.skipif('not connection.features.supports_transactions')189 def test_serialized_rollback(190 self,191 fixture_with_serialized_rollback: None,192 fixture_with_db: None,193 ) -> None:194 pass195class TestDatabaseMarker:196 "Tests for the django_db marker."197 @pytest.mark.django_db198 def test_access(self) -> None:199 Item.objects.create(name="spam")200 @pytest.mark.django_db201 def test_clean_db(self) -> None:202 # Relies on the order: test_access created an object.203 assert Item.objects.count() == 0...

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 pytest-django 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