|
8 | 8 |
|
9 | 9 | import pytest
|
10 | 10 |
|
11 |
| -from django.db import connection |
| 11 | +from django.db import connection, transaction |
12 | 12 | from django.conf import settings as real_settings
|
13 | 13 | from django.core import mail
|
14 | 14 | from django.test.client import Client, RequestFactory
|
@@ -50,6 +50,68 @@ def test_rf(rf):
|
50 | 50 | assert isinstance(rf, RequestFactory)
|
51 | 51 |
|
52 | 52 |
|
| 53 | +@pytest.mark.django_db |
| 54 | +def test_django_assert_num_queries_db(django_assert_num_queries): |
| 55 | + with django_assert_num_queries(3): |
| 56 | + Item.objects.create(name='foo') |
| 57 | + Item.objects.create(name='bar') |
| 58 | + Item.objects.create(name='baz') |
| 59 | + |
| 60 | + with pytest.raises(pytest.fail.Exception): |
| 61 | + with django_assert_num_queries(2): |
| 62 | + Item.objects.create(name='quux') |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.django_db(transaction=True) |
| 66 | +def test_django_assert_num_queries_transactional_db(transactional_db, django_assert_num_queries): |
| 67 | + with transaction.atomic(): |
| 68 | + |
| 69 | + with django_assert_num_queries(3): |
| 70 | + Item.objects.create(name='foo') |
| 71 | + Item.objects.create(name='bar') |
| 72 | + Item.objects.create(name='baz') |
| 73 | + |
| 74 | + with pytest.raises(pytest.fail.Exception): |
| 75 | + with django_assert_num_queries(2): |
| 76 | + Item.objects.create(name='quux') |
| 77 | + |
| 78 | + |
| 79 | +def test_django_assert_num_queries_output(django_testdir): |
| 80 | + django_testdir.create_test_module(""" |
| 81 | + from django.contrib.contenttypes.models import ContentType |
| 82 | + import pytest |
| 83 | +
|
| 84 | + @pytest.mark.django_db |
| 85 | + def test_queries(django_assert_num_queries): |
| 86 | + with django_assert_num_queries(1): |
| 87 | + list(ContentType.objects.all()) |
| 88 | + ContentType.objects.count() |
| 89 | + """) |
| 90 | + result = django_testdir.runpytest_subprocess('--tb=short') |
| 91 | + result.stdout.fnmatch_lines(['*Expected to perform 1 queries but 2 were done*']) |
| 92 | + assert result.ret == 1 |
| 93 | + |
| 94 | + |
| 95 | +def test_django_assert_num_queries_output_verbose(django_testdir): |
| 96 | + django_testdir.create_test_module(""" |
| 97 | + from django.contrib.contenttypes.models import ContentType |
| 98 | + import pytest |
| 99 | +
|
| 100 | + @pytest.mark.django_db |
| 101 | + def test_queries(django_assert_num_queries): |
| 102 | + with django_assert_num_queries(11): |
| 103 | + list(ContentType.objects.all()) |
| 104 | + ContentType.objects.count() |
| 105 | + """) |
| 106 | + result = django_testdir.runpytest_subprocess('--tb=short', '-v') |
| 107 | + result.stdout.fnmatch_lines([ |
| 108 | + '*Expected to perform 11 queries but 2 were done*', |
| 109 | + '*Queries:*', |
| 110 | + '*========*', |
| 111 | + ]) |
| 112 | + assert result.ret == 1 |
| 113 | + |
| 114 | + |
53 | 115 | class TestSettings:
|
54 | 116 | """Tests for the settings fixture, order matters"""
|
55 | 117 |
|
|
0 commit comments