Skip to content

Commit

Permalink
added tests for jedie#58
Browse files Browse the repository at this point in the history
  • Loading branch information
LegoStormtroopr committed Feb 1, 2016
1 parent 0f01881 commit 3aae9c8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
10 changes: 9 additions & 1 deletion tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ def __str__(self):
"""

@python_2_unicode_compatible
class Factory(models.Model):
class Building(models.Model):
address = models.CharField(max_length=128)
def __str__(self):
return self.address

@python_2_unicode_compatible
class Factory(Building):
name = models.CharField(max_length=128)
def __str__(self):
return self.name
Expand All @@ -67,6 +73,8 @@ def __str__(self):
class Person(models.Model):
name = models.CharField(max_length=100)
pets = models.ManyToManyField(Pet, blank=True)
# If you work someone, its at a build, but maybe not a factory!
workplace = models.ForeignKey(Building, related_name="workers", null=True)
def __str__(self):
return self.name

Expand Down
6 changes: 3 additions & 3 deletions tests/test_factory_car_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_initial_state(self):

self.assertEqual(Revision.objects.all().count(), 3)
self.assertEqual(len(self.version_ids), 3)
self.assertEqual(Version.objects.all().count(), 10)
self.assertEqual(Version.objects.all().count(), 17)

def test_select_compare(self):
response = self.client.get("/admin/tests/car/%s/history/" % self.car.pk)
Expand Down Expand Up @@ -160,14 +160,14 @@ def test_deleted_objects(self):
https://travis-ci.org/jedie/django-reversion-compare/builds/72317520
"""
with reversion.create_revision():
factory1 = Factory.objects.create(name="factory one")
factory1 = Factory.objects.create(name="factory one", address="1 Fake Plaza")
car = Car.objects.create(
name="car",
manufacturer=factory1
)

with reversion.create_revision():
factory2 = Factory.objects.create(name="factory two")
factory2 = Factory.objects.create(name="factory two", address="1 Fake Plaza")
car.manufacturer=factory2
car.save()

Expand Down
16 changes: 12 additions & 4 deletions tests/test_factory_car_reverse_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

from reversion_compare import helpers

from .models import Factory, Car
from .models import Factory, Car, Person
from .test_utils.test_cases import BaseTestCase
from .test_utils.test_data import TestData

Expand All @@ -56,10 +56,12 @@ class FactoryCarReverseRelationModelTest(BaseTestCase):
so no relation data would be stored
"""
def setUp(self):
reversion.unregister(Person)
reversion.unregister(Car)
reversion.unregister(Factory)
reversion.register(Factory, follow=["cars"])
reversion.register(Factory, follow=["building_ptr","cars","workers"])
reversion.register(Car)
reversion.register(Person)
super(FactoryCarReverseRelationModelTest, self).setUp()

test_data = TestData(verbose=False)
Expand All @@ -72,7 +74,7 @@ def test_initial_state(self):
self.assertTrue(reversion.is_registered(Car))
self.assertEqual(Revision.objects.all().count(), 3)
self.assertEqual(len(self.version_ids), 3)
self.assertEqual(Version.objects.all().count(), 13)
self.assertEqual(Version.objects.all().count(), 19)

def test_select_compare(self):
response = self.client.get("/admin/tests/factory/%s/history/" % self.factory.pk)
Expand Down Expand Up @@ -103,5 +105,11 @@ def test_diff1(self):
motor-car one from factory one supplier(s): <br />
</p>
''',
'<blockquote>version 2: discontinued car-three, add car-four</blockquote>', # edit comment
'''
<p class="highlight">
<ins>+ Bob Bobertson</ins><br />
</p>
''',
'<blockquote>version 2: discontinued car-three, add car-four, add Bob the worker</blockquote>', # edit comment
)

22 changes: 14 additions & 8 deletions tests/test_utils/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ def create_Simple_data(self):

def create_FactoryCar_data(self):
with reversion.create_revision():
manufacture = Factory.objects.create(name="factory one")
supplier1 = Factory.objects.create(name="always the same supplier")
supplier2 = Factory.objects.create(name="would be deleted supplier")
supplier3 = Factory.objects.create(name="would be removed supplier")
manufacture = Factory.objects.create(name="factory one", address="1 Fake Plaza")
supplier1 = Factory.objects.create(name="always the same supplier", address="1 Fake Plaza")
supplier2 = Factory.objects.create(name="would be deleted supplier", address="1 Fake Plaza")
supplier3 = Factory.objects.create(name="would be removed supplier", address="1 Fake Plaza")
car = Car.objects.create(
name="motor-car one",
manufacturer=manufacture
Expand Down Expand Up @@ -140,7 +140,7 @@ def create_FactoryCar_data(self):
manufacture.name = "factory I"
manufacture.save()
supplier2.delete() # - would be deleted supplier
supplier4 = Factory.objects.create(name="new, would be renamed supplier")
supplier4 = Factory.objects.create(name="new, would be renamed supplier", address="1 Fake Plaza")
car.supplier.add(supplier4) # + new, would be renamed supplier
car.supplier.remove(supplier3) # - would be removed supplier
car.save()
Expand Down Expand Up @@ -181,8 +181,8 @@ def create_Factory_reverse_relation_data(self):
from django.db import transaction

with transaction.atomic(), reversion.create_revision():
manufacturer = Factory.objects.create(name="factory one")
different_manufacturer = Factory.objects.create(name="factory two")
manufacturer = Factory.objects.create(name="factory one", address="1 Fake Plaza")
different_manufacturer = Factory.objects.create(name="factory two", address="1 Fake Plaza")
car1 = Car.objects.create(
name="motor-car one",
manufacturer=manufacturer
Expand Down Expand Up @@ -224,8 +224,14 @@ def create_Factory_reverse_relation_data(self):
manufacturer=manufacturer
)
car4.save()

worker1 = Person.objects.create(
name="Bob Bobertson",
workplace=manufacturer
)

manufacturer.save()
reversion.set_comment("version 2: discontinued car-three, add car-four")
reversion.set_comment("version 2: discontinued car-three, add car-four, add Bob the worker")

if self.verbose:
print("version 2:", manufacturer)
Expand Down

0 comments on commit 3aae9c8

Please sign in to comment.