Commits

Chad Dombrova committed 76b0c48

tests passing again.

  • Participants
  • Parent commits 6ef591d
  • Branches orms_sqlalchemy

Comments (0)

Files changed (5)

File denormalize/tests/common.py

-from django import test
-
+import os
+from pprint import pprint
 import logging
 log = logging.getLogger(__name__)
 
-class ModelTestCase(test.TestCase):
-    # Source:
-    # http://stackoverflow.com/questions/502916/django-how-to-create-a-model
-    test_models_app = 'denormalize.tests'
-    _test_models_initiated = False
+class CollectionTestMixin(object):
+    collection = None
+    models = None
 
-    @classmethod
-    def setUpClass(cls, *args, **kwargs):
-        if not cls._test_models_initiated:
-            cls.create_models_from_app(cls.test_models_app)
-            cls._test_models_initiated = True
-        super(ModelTestCase, cls).setUpClass(*args, **kwargs)
-
-    @classmethod
-    def create_models_from_app(cls, app_name):
-        """
-        Manually create Models (used only for testing) from the specified string app name.
-        Models are loaded from the module "<app_name>.models"
-        """
-        from django.db import connection, DatabaseError
-        from django.db.models.loading import load_app
-
-        app = load_app(app_name)
-        from django.core.management import sql
-        from django.core.management.color import no_style
-        sql = sql.sql_create(app, no_style(), connection)
-        cursor = connection.cursor()
-        for statement in sql:
-            try:
-                cursor.execute(statement)
-            except DatabaseError, excn:
-                log.debug("DatabaseError in statement: %s",
-                    statement, exc_info=True)
-
-
-class CollectionTest(object):
     def setUp(self):
-        self.models = self.get_models()
-        self.models.create_test_books()
+        print "SETTING UP!!!"
+        self.objs = self.models.create_test_books()
         #book = Book.objects.get(title=u"Cooking for Geeks")
         #m = book._meta
         #import code; _d = dict(); _d.update(globals()); _d.update(locals()); code.interact(local=_d)
-        self.objs = self.get_test_instances()
-
-    def get_models(self):
-        raise NotImplementedError()
-
-    def get_book(self):
-        raise NotImplementedError()
 
     def test_dump(self):
-        bookcol = BookCollection()
+        bookcol = self.collection()
         doc = bookcol.dump(self.objs['book'])
 
         # Basic fields
         self.assertFalse('email' in author, "The author email was not excluded")
 
     def test_dump_collection(self):
-        bookcol = BookCollection()
+        bookcol = self.collection()
         docs = list(bookcol.dump_collection())
         self.assertEqual(len(docs), 2)
         if 'pprint' in os.environ:
             pprint(docs)
 
     def test_get_related_models(self):
-        bookcol = BookCollection()
+        bookcol = self.collection()
         deps = bookcol.get_related_models()
         for rel in bookcol.select_related + bookcol.prefetch_related:
             # Difference between the filter name and the path name
         self.assertIs(deps['extra_info']['model']._model, self.models.ExtraBookInfo)
         self.assertIs(deps['chapter']['model']._model, self.models.Chapter)
         self.assertIs(deps['authors']['model']._model, self.models.Author)
-        self.assertIs(deps['tags']['model']._model, Tag)
+        self.assertIs(deps['tags']['model']._model, self.models.Tag)
         self.assertIs(deps['publisher__links']['model']._model, self.models.PublisherLink)

File denormalize/tests/test_backends.py

 from ..backend.locmem import LocMemBackend
 from ..context import delay_sync, sync_together
 
-from .common import ModelTestCase
 from .test_django import models
+from .test_django.common import ModelTestCase
 from .test_django.test_collections import BookCollection
 
 
         self.assertEqual(doc['authors'][0]['name'], author.name)
 
         # Change something that's shared (m2m)
-        tag = Tag.objects.get(name="technology")
+        tag = models.Tag.objects.get(name="technology")
         tag.name = "tech"
         tag.save()
         doc = backend.get_doc(bookcol, 1)
         self.assertTrue('tech' in doc['tags'])
 
         # Add a chapter to a book
-        book1 = Book.objects.get(id=1)
-        chapter = Chapter.objects.create(book=book1, title="Conclusion")
+        book1 = models.Book.objects.get(id=1)
+        chapter = models.Chapter.objects.create(book=book1, title="Conclusion")
         doc = backend.get_doc(bookcol, 1)
         self.assertTrue("Conclusion" in (x['title'] for x in doc['chapter_set']))
 

File denormalize/tests/test_django/common.py

+from django import test
+
+import logging
+log = logging.getLogger(__name__)
+
+class ModelTestCase(test.TestCase):
+    # Source:
+    # http://stackoverflow.com/questions/502916/django-how-to-create-a-model
+    test_models_app = 'denormalize.tests.test_django'
+    _test_models_initiated = False
+
+    @classmethod
+    def setUpClass(cls, *args, **kwargs):
+        if not cls._test_models_initiated:
+            print "INITIATING", cls
+            cls.create_models_from_app(cls.test_models_app)
+            cls._test_models_initiated = True
+        super(ModelTestCase, cls).setUpClass(*args, **kwargs)
+
+    @classmethod
+    def create_models_from_app(cls, app_name):
+        """
+        Manually create Models (used only for testing) from the specified string app name.
+        Models are loaded from the module "<app_name>.models"
+        """
+        from django.db import connection, DatabaseError
+        from django.db.models.loading import load_app
+
+        app = load_app(app_name)
+        from django.core.management import sql
+        from django.core.management.color import no_style
+        sql = sql.sql_create(app, no_style(), connection)
+        cursor = connection.cursor()
+        for statement in sql:
+            try:
+                cursor.execute(statement)
+            except DatabaseError, excn:
+                log.debug("DatabaseError in statement: %s",
+                    statement, exc_info=True)

File denormalize/tests/test_django/models.py

     tech.books.add(cooking_for_geeks)
     tech.books.add(mongodb)
 
+    return {'book': Book.objects.get(title=u"Cooking for Geeks")}
+
 ################################################################
 # To test some django reverse behavior
 

File denormalize/tests/test_django/test_collections.py

 from ...models import *
-from ..common import ModelTestCase, CollectionTest
+from ..common import CollectionTestMixin
+from .common import ModelTestCase
 from . import models
 
 # TODO: check if the SQL queries are efficient (all data should be fetched in one query)
         return super(BookCollection, self).dump_obj(model, obj, path)
 
 
-class DjangoCollectionTest(ModelTestCase, CollectionTest):
-    def get_test_objects(self):
-        return {'book': Book.objects.get(title=u"Cooking for Geeks")}
-
-    def get_models(self):
-        return models
+# CollectionTestMixin must come first so that its setUp gets called
+class DjangoCollectionTest(CollectionTestMixin, ModelTestCase):
+    collection = BookCollection
+    models = models
 
 
 class DjangoORMTest(ModelTestCase):