Skip to content
This repository
Browse code

Restore the model cache after mocking, and make a few style tweaks.

  • Loading branch information...
commit 04839d236affdc00d5bbe39734d1edc3c7810b61 1 parent a08d977
Erik Rose authored September 09, 2012
9  django_nose/runner.py
@@ -16,9 +16,8 @@
16 16
 from django.core.management.base import BaseCommand
17 17
 from django.core.management.color import no_style
18 18
 from django.core.management.commands.loaddata import Command
19  
-from django.db import connections, transaction, DEFAULT_DB_ALIAS
  19
+from django.db import connections, transaction, DEFAULT_DB_ALIAS, models
20 20
 from django.db.backends.creation import BaseDatabaseCreation
21  
-from django.db.models.loading import cache
22 21
 from django.test.simple import DjangoTestSuiteRunner
23 22
 from django.utils.importlib import import_module
24 23
 
@@ -267,10 +266,10 @@ class NoseTestSuiteRunner(BasicNoseRunner):
267 266
     """
268 267
 
269 268
     def _get_models_for_connection(self, connection):
270  
-        """Get a list of models for connection."""
  269
+        """Return a list of models for a connection."""
271 270
         tables = connection.introspection.get_table_list(connection.cursor())
272  
-        exists = lambda m: m._meta.db_table in tables
273  
-        return filter(exists, cache.get_models())
  271
+        return [m for m in models.loading.cache.get_models() if
  272
+                m._meta.db_table in tables]
274 273
 
275 274
     def setup_databases(self):
276 275
         for alias in connections:
40  unittests/test_databases.py
... ...
@@ -1,11 +1,13 @@
  1
+from contextlib import contextmanager
1 2
 from unittest import TestCase
2 3
 
3 4
 from django.db.models.loading import cache
4 5
 
5 6
 from django_nose.runner import NoseTestSuiteRunner
6 7
 
7  
-class GetModelsForConnection(TestCase):
8  
-    tables = ['test_table%d' % i for i in range(5)]
  8
+
  9
+class GetModelsForConnectionTests(TestCase):
  10
+    tables = ['test_table%d' % i for i in xrange(5)]
9 11
 
10 12
     def _connection_mock(self, tables):
11 13
         class FakeIntrospection(object):
@@ -24,37 +26,45 @@ class FakeModel(object):
24 26
 
25 27
         return FakeModel()
26 28
 
  29
+    @contextmanager
27 30
     def _cache_mock(self, tables=[]):
28 31
         def get_models(*args, **kwargs):
29 32
             return [self._model_mock(t) for t in tables]
30 33
 
31  
-        setattr(cache, 'get_models', get_models)
  34
+        old = cache.get_models
  35
+        cache.get_models = get_models
  36
+        yield
  37
+        cache.get_models = old
32 38
 
33 39
     def setUp(self):
34 40
         self.runner = NoseTestSuiteRunner()
35 41
 
36 42
     def test_no_models(self):
37  
-        """For any DB with no tables - return nothing."""
  43
+        """For a DB with no tables, return nothing."""
38 44
         connection = self._connection_mock([])
39  
-        self._cache_mock(['table1', 'table2'])
40  
-        self.assertEqual(self.runner._get_models_for_connection(connection), [])
  45
+        with self._cache_mock(['table1', 'table2']):
  46
+            self.assertEqual(
  47
+                self.runner._get_models_for_connection(connection), [])
41 48
 
42 49
     def test_wrong_models(self):
43  
-        """In no tables exists for models - return nothing."""
  50
+        """If no tables exists for models, return nothing."""
44 51
         connection = self._connection_mock(self.tables)
45  
-        self._cache_mock(['table1', 'table2'])
46  
-        self.assertEqual(self.runner._get_models_for_connection(connection), [])
  52
+        with self._cache_mock(['table1', 'table2']):
  53
+            self.assertEqual(
  54
+                self.runner._get_models_for_connection(connection), [])
47 55
 
48 56
     def test_some_models(self):
49  
-        """If some of the models has appropriate table in db - return matching models."""
  57
+        """If some of the models has appropriate table in the DB, return matching models."""
50 58
         connection = self._connection_mock(self.tables)
51  
-        self._cache_mock(self.tables[1:3])
52  
-        result_tables = [m._meta.db_table for m in self.runner._get_models_for_connection(connection)]
  59
+        with self._cache_mock(self.tables[1:3]):
  60
+            result_tables = [m._meta.db_table for m in
  61
+                             self.runner._get_models_for_connection(connection)]
53 62
         self.assertEqual(result_tables, self.tables[1:3])
54 63
 
55 64
     def test_all_models(self):
56  
-        """If all the models has appropriate table in db - return all models."""
  65
+        """If all the models have appropriate tables in the DB, return them all."""
57 66
         connection = self._connection_mock(self.tables)
58  
-        self._cache_mock(self.tables)
59  
-        result_tables = [m._meta.db_table for m in self.runner._get_models_for_connection(connection)]
  67
+        with self._cache_mock(self.tables):
  68
+            result_tables = [m._meta.db_table for m in
  69
+                             self.runner._get_models_for_connection(connection)]
60 70
         self.assertEqual(result_tables, self.tables)

0 notes on commit 04839d2

Please sign in to comment.
Something went wrong with that request. Please try again.