|
| 1 | +import unittest |
| 2 | +from unittest import mock |
| 3 | +from io import StringIO |
| 4 | + |
| 5 | +from django.test import TestCase |
| 6 | +from django.apps import registry |
| 7 | +from django.contrib.auth import get_user_model |
| 8 | +from django.core.management import call_command |
| 9 | +from django.urls import reverse |
| 10 | +from django.conf import settings |
| 11 | + |
| 12 | +from dbcleanup import utils, models, admin |
| 13 | + |
| 14 | + |
| 15 | +@unittest.skipUnless( |
| 16 | + settings.DATABASES['default']['ENGINE'] in ('django.db.backends.mysql'), |
| 17 | + "only mysql", |
| 18 | +) |
| 19 | +class Test(TestCase): |
| 20 | + def test_command(self): |
| 21 | + out = StringIO() |
| 22 | + call_command('dbcleanup', stdout=out, just='analyze') |
| 23 | + # there might be old tables in the testdb, do not assume empty output... |
| 24 | + baseline = {x.split(' ')[1] for x in out.getvalue().splitlines()} |
| 25 | + # cannot create tables inside TransactionTests (it's mysql), so let's "remove" a model |
| 26 | + org_list = [m for m in registry.apps.get_models() if m._meta.db_table != 'auth_user'] |
| 27 | + utils.model_tables.cache_clear() |
| 28 | + with mock.patch('django.apps.registry.apps.get_models', return_value=org_list): |
| 29 | + call_command('dbcleanup', stdout=out, just='tables') |
| 30 | + self.assertEqual( |
| 31 | + {x.split(' ')[1] for x in out.getvalue().splitlines()} - baseline, |
| 32 | + {'auth_user', 'auth_user_groups', 'auth_user_user_permissions'}, |
| 33 | + ) |
0 commit comments