Skip to content

Commit dcfcb89

Browse files
committed
Split migrations and model definition tests into another test suite
1 parent a2e9d65 commit dcfcb89

File tree

11 files changed

+225
-206
lines changed

11 files changed

+225
-206
lines changed

runtests.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
'simple_history.tests',
1818
'simple_history.tests.custom_user',
1919
'simple_history.tests.external',
20-
'simple_history.tests.migration_test_app',
20+
'simple_history.registry_tests.migration_test_app',
2121

2222
'simple_history',
2323

@@ -51,13 +51,6 @@
5151

5252

5353
def main():
54-
# reset the test app migrations
55-
for migration_path in [
56-
join(dirname(__file__), 'simple_history', 'tests', 'migrations'),
57-
join(dirname(__file__), 'simple_history', 'tests', 'migration_test_app', 'migrations'),
58-
]:
59-
for migration_file_path in listdir(migration_path):
60-
unlink(join(migration_path, migration_file_path))
6154

6255
if not settings.configured:
6356
settings.configure(**DEFAULT_SETTINGS)
@@ -68,9 +61,10 @@ def main():
6861
except ImportError:
6962
from django.test.simple import DjangoTestSuiteRunner
7063
failures = DjangoTestSuiteRunner(failfast=False).run_tests(['tests'])
64+
failures |= DjangoTestSuiteRunner(failfast=False).run_tests(['registry_tests'])
7165
else:
72-
failures = DiscoverRunner(failfast=False).run_tests(
73-
['simple_history.tests'])
66+
failures = DiscoverRunner(failfast=False).run_tests(['simple_history.tests'])
67+
failures |= DiscoverRunner(failfast=False).run_tests(['simple_history.registry_tests'])
7468
sys.exit(failures)
7569

7670

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.9.12 on 2017-01-18 21:58
3+
from __future__ import unicode_literals
4+
5+
from django.conf import settings
6+
from django.db import migrations, models
7+
import django.db.models.deletion
8+
9+
10+
class Migration(migrations.Migration):
11+
12+
initial = True
13+
14+
dependencies = [
15+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
16+
]
17+
18+
operations = [
19+
migrations.CreateModel(
20+
name='DoYouKnow',
21+
fields=[
22+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
23+
],
24+
),
25+
migrations.CreateModel(
26+
name='HistoricalYar',
27+
fields=[
28+
('id', models.IntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')),
29+
('history_id', models.AutoField(primary_key=True, serialize=False)),
30+
('history_date', models.DateTimeField()),
31+
('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)),
32+
('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)),
33+
],
34+
options={
35+
'verbose_name': 'historical yar',
36+
'ordering': ('-history_date', '-history_id'),
37+
'get_latest_by': 'history_date',
38+
},
39+
),
40+
migrations.CreateModel(
41+
name='Yar',
42+
fields=[
43+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
44+
],
45+
),
46+
migrations.CreateModel(
47+
name='WhatIMean',
48+
fields=[
49+
('doyouknow_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='migration_test_app.DoYouKnow')),
50+
],
51+
bases=('migration_test_app.doyouknow',),
52+
),
53+
migrations.AddField(
54+
model_name='yar',
55+
name='what',
56+
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='migration_test_app.WhatIMean'),
57+
),
58+
migrations.AddField(
59+
model_name='historicalyar',
60+
name='what',
61+
field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='migration_test_app.WhatIMean'),
62+
),
63+
]

simple_history/registry_tests/migration_test_app/migrations/__init__.py

Whitespace-only changes.

simple_history/registry_tests/models.py

Whitespace-only changes.
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
from __future__ import unicode_literals
2+
3+
from datetime import datetime, timedelta
4+
from six.moves import cStringIO as StringIO
5+
import unittest
6+
7+
import django
8+
from django.contrib.auth import get_user_model
9+
from django.core import management
10+
from django.test import TestCase
11+
12+
from simple_history import exceptions, register
13+
from ..tests.models import (
14+
Poll, Choice, Voter, Restaurant, HistoricalPoll, HistoricalChoice,
15+
HistoricalState, HistoricalCustomFKError,
16+
UserAccessorDefault, UserAccessorOverride,
17+
TrackedAbstractBaseA, TrackedAbstractBaseB,
18+
TrackedWithAbstractBase, TrackedWithConcreteBase,
19+
InheritTracking1, InheritTracking2, InheritTracking3, InheritTracking4,
20+
)
21+
22+
try:
23+
from django.apps import apps
24+
except ImportError: # Django < 1.7
25+
from django.db.models import get_model
26+
else:
27+
get_model = apps.get_model
28+
29+
User = get_user_model()
30+
today = datetime(2021, 1, 1, 10, 0)
31+
tomorrow = today + timedelta(days=1)
32+
yesterday = today - timedelta(days=1)
33+
34+
35+
class RegisterTest(TestCase):
36+
def test_register_no_args(self):
37+
self.assertEqual(len(Choice.history.all()), 0)
38+
poll = Poll.objects.create(pub_date=today)
39+
choice = Choice.objects.create(poll=poll, votes=0)
40+
self.assertEqual(len(choice.history.all()), 1)
41+
42+
def test_register_separate_app(self):
43+
def get_history(model):
44+
return model.history
45+
46+
self.assertRaises(AttributeError, get_history, User)
47+
self.assertEqual(len(User.histories.all()), 0)
48+
user = User.objects.create(username='bob', password='pass')
49+
self.assertEqual(len(User.histories.all()), 1)
50+
self.assertEqual(len(user.histories.all()), 1)
51+
52+
def test_reregister(self):
53+
with self.assertRaises(exceptions.MultipleRegistrationsError):
54+
register(Restaurant, manager_name='again')
55+
56+
def test_register_custome_records(self):
57+
self.assertEqual(len(Voter.history.all()), 0)
58+
poll = Poll.objects.create(pub_date=today)
59+
choice = Choice.objects.create(poll=poll, votes=0)
60+
user = User.objects.create(username='voter')
61+
voter = Voter.objects.create(choice=choice, user=user)
62+
self.assertEqual(len(voter.history.all()), 1)
63+
expected = 'Voter object changed by None as of '
64+
self.assertEqual(expected,
65+
str(voter.history.all()[0])[:len(expected)])
66+
67+
68+
class TestUserAccessor(unittest.TestCase):
69+
70+
def test_accessor_default(self):
71+
register(UserAccessorDefault)
72+
assert not hasattr(User, 'historicaluseraccessordefault_set')
73+
74+
def test_accessor_override(self):
75+
register(UserAccessorOverride,
76+
user_related_name='my_history_model_accessor')
77+
assert hasattr(User, 'my_history_model_accessor')
78+
79+
80+
class TestTrackingInheritance(TestCase):
81+
82+
def test_tracked_abstract_base(self):
83+
self.assertEqual(
84+
[
85+
f.attname
86+
for f in TrackedWithAbstractBase.history.model._meta.fields
87+
],
88+
[
89+
'id', 'history_id', 'history_date', 'history_user_id',
90+
'history_type',
91+
],
92+
)
93+
94+
def test_tracked_concrete_base(self):
95+
self.assertEqual(
96+
[
97+
f.attname
98+
for f in TrackedWithConcreteBase.history.model._meta.fields
99+
],
100+
[
101+
'id', 'trackedconcretebase_ptr_id', 'history_id',
102+
'history_date', 'history_user_id', 'history_type',
103+
],
104+
)
105+
106+
def test_multiple_tracked_bases(self):
107+
with self.assertRaises(exceptions.MultipleRegistrationsError):
108+
class TrackedWithMultipleAbstractBases(
109+
TrackedAbstractBaseA, TrackedAbstractBaseB):
110+
pass
111+
112+
def test_tracked_abstract_and_untracked_concrete_base(self):
113+
self.assertEqual(
114+
[f.attname for f in InheritTracking1.history.model._meta.fields],
115+
[
116+
'id', 'untrackedconcretebase_ptr_id', 'history_id',
117+
'history_date', 'history_user_id', 'history_type',
118+
],
119+
)
120+
121+
def test_indirect_tracked_abstract_base(self):
122+
self.assertEqual(
123+
[f.attname for f in InheritTracking2.history.model._meta.fields],
124+
[
125+
'id', 'baseinherittracking2_ptr_id', 'history_id',
126+
'history_date', 'history_user_id', 'history_type',
127+
],
128+
)
129+
130+
def test_indirect_tracked_concrete_base(self):
131+
self.assertEqual(
132+
[f.attname for f in InheritTracking3.history.model._meta.fields],
133+
[
134+
'id', 'baseinherittracking3_ptr_id', 'history_id',
135+
'history_date', 'history_user_id', 'history_type',
136+
],
137+
)
138+
139+
def test_registering_with_tracked_abstract_base(self):
140+
with self.assertRaises(exceptions.MultipleRegistrationsError):
141+
register(InheritTracking4)
142+
143+
144+
@unittest.skipUnless(django.get_version() >= "1.7", "Requires 1.7 migrations")
145+
class TestMigrate(TestCase):
146+
147+
def test_makemigration_command(self):
148+
management.call_command(
149+
'makemigrations', 'migration_test_app', stdout=StringIO())
150+
151+
def test_migrate_command(self):
152+
management.call_command(
153+
'migrate', 'migration_test_app', fake=True, stdout=StringIO())

simple_history/tests/migration_test_app/migrations/0001_initial.py

Lines changed: 0 additions & 63 deletions
This file was deleted.

simple_history/tests/tests/test_commands.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
from contextlib import contextmanager
2-
from six.moves import cStringIO as StringIO
32
from datetime import datetime
4-
try:
5-
from unittest import skipUnless
6-
except ImportError:
7-
from unittest2 import skipUnless
8-
import django
3+
4+
from six.moves import cStringIO as StringIO
95
from django.test import TestCase
106
from django.core import management
7+
118
from simple_history import models as sh_models
129
from simple_history.management.commands import populate_history
1310

@@ -106,15 +103,3 @@ def test_no_historical(self):
106103
stdout=out)
107104
self.assertIn(populate_history.Command.NO_REGISTERED_MODELS,
108105
out.getvalue())
109-
110-
111-
@skipUnless(django.get_version() >= "1.7", "Requires 1.7 migrations")
112-
class TestMigrate(TestCase):
113-
114-
def test_makemigration_command(self):
115-
management.call_command(
116-
'makemigrations', 'migration_test_app', stdout=StringIO())
117-
118-
def test_migrate_command(self):
119-
management.call_command(
120-
'migrate', 'migration_test_app', fake=True, stdout=StringIO())

0 commit comments

Comments
 (0)