Commits

Luke Plant committed abf1777

Added tests for the Anonymizer class (and bulked out test coverage in general).

Comments (0)

Files changed (3)

anonymizer/tests/__init__.py

+from datetime import datetime, timedelta, date
 import os
 import sys
 
 from django.utils import unittest
 from django.test import TestCase
 
-from anonymizer import introspect
-import anonymizer.tests.testapp.models
+from anonymizer import Anonymizer, introspect
+from anonymizer.tests import testapp
 
-class TestIntrospect(unittest.TestCase):
 
-    def setUp(self):
-        self._old_INSTALLED_APPS = settings.INSTALLED_APPS
-        settings.INSTALLED_APPS = settings.INSTALLED_APPS[:] + ["anonymizer.tests.testapp"]
-        from django.db.models.loading import cache
-        cache._populate()
-
-    def tearDown(self):
-        settings.INSTALLED_APPS = self._old_INSTALLED_APPS
+class TestIntrospect(TestCase):
 
     def test_eveything(self):
-        mod = introspect.create_anonymizers_module(anonymizer.tests.testapp.models)
+        mod = introspect.create_anonymizers_module(testapp.models)
         expected = """
 from anonymizer.tests.testapp.models import Other, EverythingModel
 from anonymizer import Anonymizer
         ('address', "full_address"),
          # Skipping field o1_id
         ('something', "lorem"),
+        ('something_else', "lorem"),
+        ('some_varchar', "varchar"),
         ('birthday', "datetime"),
         ('age', "positive_small_integer"),
         ('icon', UNKNOWN_FIELD),
+        ('some_datetime', "datetime"),
+        ('some_date', "date"),
     ]
 """
         self.assertEqual(mod.strip(), expected.strip())
 
+class TestAnonymizer(TestCase):
+
+    # Nice high count, so that our handling of unique constraint with test data
+    # will likely be tested.
+    NUM_ITEMS = 1000
+
+    def setUp(self):
+        self.o1 = testapp.models.Other.objects.create()
+        for x in xrange(0, self.NUM_ITEMS):
+            d = datetime.now() + timedelta(365*x)
+            testapp.models.EverythingModel.objects.create(o1=self.o1,
+                                                          username="intial%d" % x,
+                                                          birthday=d,
+                                                          age=x,
+                                                          some_datetime=datetime.now(),
+                                                          some_date=date.today(),
+                                                          )
+
+    def test_eveything(self):
+        # Test for as much as possible in one test.
+        assert testapp.models.EverythingModel.objects.count() == self.NUM_ITEMS
+        assert testapp.models.EverythingModel._meta.get_field_by_name('username')[0].unique == True
+
+        class EverythingAnonmyizer(Anonymizer):
+            model = testapp.models.EverythingModel
+
+            attributes = [
+                ('username', 'username'),
+                ('name', "name"),
+                ('email', "email"),
+                ('address_city', "city"),
+                ('address_post_code', "uk_postcode"),
+                ('address', "full_address"),
+                ('something', "lorem"),
+                ('something_else', "similar_lorem"),
+                ('some_varchar', "varchar"),
+                ('birthday', "datetime"),
+                ('age', "positive_small_integer"),
+                ('some_datetime', "datetime"),
+                ('some_date', "date"),
+            ]
+
+        EverythingAnonmyizer().run()
+        objs = testapp.models.EverythingModel.objects.all()
+        self.assertEqual(len(objs), self.NUM_ITEMS)
+        for o in objs:
+            # check everything has been changed
+            self.assertFalse(o.username.startswith('initial'))
+            # Check for corresponding user names/emails.  This works if username
+            # is first in the list, as recommended and as introspection
+            # generates.
+            self.assertTrue(o.email.startswith(o.username))
+

anonymizer/tests/testapp/models.py

     address = models.TextField()
     o1 = models.ForeignKey(Other)
     something = models.TextField()
+    something_else = models.TextField()
+    some_varchar = models.CharField(max_length=5)
     birthday = models.DateTimeField()
     age = models.PositiveSmallIntegerField()
     icon = models.ImageField(upload_to='.')
+    some_datetime = models.DateTimeField()
+    some_date = models.DateField()
 }
 
 INSTALLED_APPS = [
-    'anonymizer'
+    'anonymizer',
+    'anonymizer.tests.testapp',
 ]
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.