|
1 | 1 | import sys
|
| 2 | +from textwrap import dedent |
2 | 3 |
|
3 | 4 | import pytest
|
4 | 5 |
|
| 6 | +from pytest_django.lazy_django import get_django_version |
| 7 | + |
5 | 8 | from .db_helpers import (db_exists, drop_database, mark_database, mark_exists,
|
6 | 9 | skip_if_sqlite_in_memory)
|
7 | 10 |
|
@@ -191,3 +194,109 @@ def test_a():
|
191 | 194 |
|
192 | 195 | result = django_testdir.runpytest('--tb=short', '-vv', '-n1')
|
193 | 196 | result.stdout.fnmatch_lines(['*PASSED*test_a*'])
|
| 197 | + |
| 198 | + |
| 199 | +def test_initial_data(django_testdir_initial): |
| 200 | + """Test that initial data gets loaded.""" |
| 201 | + django_testdir_initial.create_test_module(''' |
| 202 | + import pytest |
| 203 | + |
| 204 | + from .app.models import Item |
| 205 | + |
| 206 | + @pytest.mark.django_db |
| 207 | + def test_inner_south(): |
| 208 | + assert [x.name for x in Item.objects.all()] \ |
| 209 | + == ["mark_initial_data"] |
| 210 | + ''') |
| 211 | + |
| 212 | + result = django_testdir_initial.runpytest('--tb=short', '-v') |
| 213 | + result.stdout.fnmatch_lines(['*test_inner_south*PASSED*']) |
| 214 | + |
| 215 | + |
| 216 | +# NOTE: South tries to monkey-patch management._commands, which has been |
| 217 | +# replaced by lru_cache and would cause an AttributeError. |
| 218 | +@pytest.mark.skipif(get_django_version() >= (1, 7), |
| 219 | + reason='South fails with Django 1.7.') |
| 220 | +@pytest.mark.skipif(sys.version_info[:2] == (3, 4), |
| 221 | + reason='South fails on Python 3.4.') |
| 222 | +class TestSouth: |
| 223 | + """Test interaction with initial_data and South.""" |
| 224 | + |
| 225 | + @pytest.mark.extra_settings(dedent(""" |
| 226 | + INSTALLED_APPS += [ 'south', ] |
| 227 | + SOUTH_TESTS_MIGRATE = True |
| 228 | + SOUTH_MIGRATION_MODULES = { |
| 229 | + 'app': 'app.south_migrations', |
| 230 | + } |
| 231 | + """)) |
| 232 | + def test_initial_data_south(self, django_testdir_initial, settings): |
| 233 | + django_testdir_initial.create_test_module(''' |
| 234 | + import pytest |
| 235 | + |
| 236 | + from .app.models import Item |
| 237 | + |
| 238 | + @pytest.mark.django_db |
| 239 | + def test_inner_south(): |
| 240 | + assert [x.name for x in Item.objects.all()] \ |
| 241 | + == ["mark_initial_data"] |
| 242 | + ''') |
| 243 | + |
| 244 | + result = django_testdir_initial.runpytest('--tb=short', '-v') |
| 245 | + result.stdout.fnmatch_lines(['*test_inner_south*PASSED*']) |
| 246 | + |
| 247 | + @pytest.mark.extra_settings(dedent(""" |
| 248 | + INSTALLED_APPS += [ 'south', ] |
| 249 | + SOUTH_TESTS_MIGRATE = True |
| 250 | + SOUTH_MIGRATION_MODULES = { |
| 251 | + 'app': 'tpkg.app.south_migrations', |
| 252 | + } |
| 253 | + """)) |
| 254 | + def test_initial_south_migrations(self, django_testdir_initial, settings): |
| 255 | + testdir = django_testdir_initial |
| 256 | + testdir.create_test_module(''' |
| 257 | + import pytest |
| 258 | + |
| 259 | + @pytest.mark.django_db |
| 260 | + def test_inner_south(): |
| 261 | + pass |
| 262 | + ''') |
| 263 | + |
| 264 | + testdir.mkpydir('tpkg/app/south_migrations') |
| 265 | + p = testdir.tmpdir.join( |
| 266 | + "tpkg/app/south_migrations/0001_initial").new(ext="py") |
| 267 | + p.write(dedent(""" |
| 268 | + from south.v2 import SchemaMigration |
| 269 | + |
| 270 | + class Migration(SchemaMigration): |
| 271 | + def forwards(self, orm): |
| 272 | + print("mark_south_migration_forwards") |
| 273 | + """), ensure=True) |
| 274 | + |
| 275 | + result = testdir.runpytest('--tb=short', '-v', '-s') |
| 276 | + result.stdout.fnmatch_lines(['*mark_south_migration_forwards*']) |
| 277 | + |
| 278 | + @pytest.mark.extra_settings(dedent(""" |
| 279 | + INSTALLED_APPS += [ 'south', ] |
| 280 | + SOUTH_TESTS_MIGRATE = False |
| 281 | + SOUTH_MIGRATION_MODULES = { |
| 282 | + 'app': 'tpkg.app.south_migrations', |
| 283 | + } |
| 284 | + """)) |
| 285 | + def test_south_no_migrations(self, django_testdir_initial, settings): |
| 286 | + testdir = django_testdir_initial |
| 287 | + testdir.create_test_module(''' |
| 288 | + import pytest |
| 289 | + |
| 290 | + @pytest.mark.django_db |
| 291 | + def test_inner_south(): |
| 292 | + pass |
| 293 | + ''') |
| 294 | + |
| 295 | + testdir.mkpydir('tpkg/app/south_migrations') |
| 296 | + p = testdir.tmpdir.join( |
| 297 | + "tpkg/app/south_migrations/0001_initial").new(ext="py") |
| 298 | + p.write('raise Exception("This should not get imported.")', |
| 299 | + ensure=True) |
| 300 | + |
| 301 | + result = testdir.runpytest('--tb=short', '-v') |
| 302 | + result.stdout.fnmatch_lines(['*test_inner_south*PASSED*']) |
0 commit comments