Commits

Anonymous committed 7b837b0

Unit tests galore.

Comments (0)

Files changed (8)

     """
     Returns the full python name of an app - e.g. django.contrib.auth
     """
-    print app.__name__
     return app.__name__[:-11]
 
 
     """
     Returns the migration class implied by 'name'.
     """
-    module = __import__(app.__name__ + "." + name, '', '', ['Migration'])
-    return module.Migration
+    try:
+        module = __import__(app.__name__ + "." + name, '', '', ['Migration'])
+        return module.Migration
+    except ImportError:
+        raise ValueError("Migration %s:%s does not exist." % (get_app_name(app), name))
 
 
 def all_migrations():

tests/__init__.py

 
-from south.tests.db import *
+from south.tests.db import *
+from south.tests.logic import *
Add a comment to this file

tests/fakeapp/__init__.py

Empty file added.

tests/fakeapp/migrations/0001_spam.py

+from south.db import db
+from django.db import models
+
+class Migration:
+    
+    def forwards(self):
+        
+        # Model 'Spam'
+        db.create_table("southtest_spam", (
+            ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
+            ('weight', models.FloatField()),
+            ('expires', models.DateTimeField()),
+            ('name', models.CharField(max_length=255))
+        ))
+    
+    def backwards(self):
+        
+        db.delete_table("southtest_spam")
+

tests/fakeapp/migrations/0002_eggs.py

+from south.db import db
+from django.db import models
+
+class Migration:
+    
+    def forwards(self):
+        
+        Spam = db.mock_model(model_name='Spam', db_table='southtest_spam', db_tablespace='', pk_field_name='id', pk_field_type=models.AutoField)
+        
+        db.create_table("southtest_spam", (
+            ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
+            ('size', models.FloatField()),
+            ('quantity', models.IntegerField()),
+            ('spam', models.ForeignKey(Spam)),
+        ))
+    
+    def backwards(self):
+        
+        db.delete_table("southtest_spam")
+
Add a comment to this file

tests/fakeapp/migrations/__init__.py

Empty file added.

Add a comment to this file

tests/fakeapp/models.py

Empty file added.

+import unittest
+import sys
+import os
+
+from south import migration
+
+# Add the tests directory so fakeapp is on sys.path
+test_root = os.path.dirname(__file__)
+sys.path.append(test_root)
+
+
+class TestMigrationLogic(unittest.TestCase):
+
+    """
+    Tests if the various logic functions in migration actually work.
+    """
+
+    def create_fake_app(self, name):
+        
+        class Fake:
+            pass
+        
+        fake = Fake()
+        fake.__name__ = name
+        return fake
+
+
+    def create_test_app(self):
+        
+        class Fake:
+            pass
+        
+        fake = Fake()
+        fake.__name__ = "fakeapp.migrations"
+        fake.__file__ = os.path.join(test_root, "fakeapp", "migrations", "__init__.py")
+        return fake
+    
+    
+    def monkeypatch(self):
+        """Swaps out various Django calls for fake ones for our own nefarious purposes."""
+        
+        def new_get_apps():
+            return ['fakeapp']
+        
+        from django.db import models
+        from django.conf import settings
+        models.get_apps_old, models.get_apps = models.get_apps, new_get_apps
+        settings.INSTALLED_APPS, settings.OLD_INSTALLED_APPS = (
+            ["fakeapp"],
+            settings.INSTALLED_APPS,
+        )
+        self.redo_app_cache()
+    setUp = monkeypatch
+    
+    
+    def unmonkeypatch(self):
+        """Undoes what monkeypatch did."""
+        
+        from django.db import models
+        from django.conf import settings
+        models.get_apps = models.get_apps_old
+        settings.INSTALLED_APPS = settings.OLD_INSTALLED_APPS
+        self.redo_app_cache()
+    tearDown = unmonkeypatch
+    
+    
+    def redo_app_cache(self):
+        from django.db.models.loading import AppCache
+        a = AppCache()
+        a.loaded = False
+        a._populate()
+    
+
+    def test_get_app_name(self):
+        self.assertEqual(
+            "southtest",
+            migration.get_app_name(self.create_fake_app("southtest.migrations")),
+        )
+        self.assertEqual(
+            "baz",
+            migration.get_app_name(self.create_fake_app("foo.bar.baz.migrations")),
+        )
+    
+    
+    def test_get_migrated_apps(self):
+        
+        P1 = __import__("fakeapp.migrations", {}, {}, [''])
+        
+        self.assertEqual(
+            [P1],
+            list(migration.get_migrated_apps()),
+        )
+    
+    
+    def test_get_app(self):
+        
+        P1 = __import__("fakeapp.migrations", {}, {}, [''])
+        
+        self.assertEqual(P1, migration.get_app("fakeapp"))
+        self.assertEqual(P1, migration.get_app(self.create_fake_app("fakeapp.models")))
+    
+    
+    def test_get_app_fullname(self):
+        self.assertEqual(
+            "southtest",
+            migration.get_app_fullname(self.create_fake_app("southtest.migrations")),
+        )
+        self.assertEqual(
+            "foo.bar.baz",
+            migration.get_app_fullname(self.create_fake_app("foo.bar.baz.migrations")),
+        )
+    
+    
+    def test_get_migration_names(self):
+        
+        app = self.create_test_app()
+        
+        self.assertEqual(
+            ["0001_spam", "0002_eggs"],
+            migration.get_migration_names(app),
+        )
+    
+    
+    def test_get_migration_classes(self):
+        
+        app = self.create_test_app()
+        
+        # Can't use vanilla import, modules beginning with numbers aren't in grammar
+        M1 = __import__("fakeapp.migrations.0001_spam", {}, {}, ['Migration']).Migration
+        M2 = __import__("fakeapp.migrations.0002_eggs", {}, {}, ['Migration']).Migration
+        
+        self.assertEqual(
+            [M1, M2],
+            list(migration.get_migration_classes(app)),
+        )
+    
+    
+    def test_get_migration(self):
+        
+        app = self.create_test_app()
+        
+        # Can't use vanilla import, modules beginning with numbers aren't in grammar
+        M1 = __import__("fakeapp.migrations.0001_spam", {}, {}, ['Migration']).Migration
+        M2 = __import__("fakeapp.migrations.0002_eggs", {}, {}, ['Migration']).Migration
+        
+        self.assertEqual(M1, migration.get_migration(app, "0001_spam"))
+        self.assertEqual(M2, migration.get_migration(app, "0002_eggs"))
+        
+        self.assertRaises(ValueError, migration.get_migration, app, "0001_jam")
+    
+    
+    def test_all_migrations(self):
+        
+        app = migration.get_app("fakeapp")
+        
+        self.assertEqual(
+            {app: {
+                "0001_spam": migration.get_migration(app, "0001_spam"),
+                "0002_eggs": migration.get_migration(app, "0002_eggs"),
+            }},
+            migration.all_migrations(),
+        )
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.