Skip to content

Commit

Permalink
Adding unittests for NoseTestSuiteRunner._get_models_for_connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Tsymbala committed Jun 10, 2012
1 parent 5747417 commit a08d977
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions runtests.sh
Expand Up @@ -30,3 +30,4 @@ django_test 'django-admin.py test --settings=testapp.settings_with_south' '2' 'w
django_test 'django-admin.py test --settings=testapp.settings_old_style' '2' 'django_nose.run_tests format'
django_test 'testapp/runtests.py testapp.test_only_this' '1' 'via run_tests API'
django_test 'django-admin.py test --settings=testapp.settings_with_plugins testapp/plugin_t' '1' 'with plugins'
django_test 'django-admin.py test --settings=testapp.settings unittests' '4' 'unittests'
60 changes: 60 additions & 0 deletions unittests/test_databases.py
@@ -0,0 +1,60 @@
from unittest import TestCase

from django.db.models.loading import cache

from django_nose.runner import NoseTestSuiteRunner

class GetModelsForConnection(TestCase):
tables = ['test_table%d' % i for i in range(5)]

def _connection_mock(self, tables):
class FakeIntrospection(object):
def get_table_list(*args, **kwargs):
return tables

class FakeConnection(object):
introspection = FakeIntrospection()
cursor = lambda x: None

return FakeConnection()

def _model_mock(self, db_table):
class FakeModel(object):
_meta = type('meta', (object,), {'db_table': db_table})()

return FakeModel()

def _cache_mock(self, tables=[]):
def get_models(*args, **kwargs):
return [self._model_mock(t) for t in tables]

setattr(cache, 'get_models', get_models)

def setUp(self):
self.runner = NoseTestSuiteRunner()

def test_no_models(self):
"""For any DB with no tables - return nothing."""
connection = self._connection_mock([])
self._cache_mock(['table1', 'table2'])
self.assertEqual(self.runner._get_models_for_connection(connection), [])

def test_wrong_models(self):
"""In no tables exists for models - return nothing."""
connection = self._connection_mock(self.tables)
self._cache_mock(['table1', 'table2'])
self.assertEqual(self.runner._get_models_for_connection(connection), [])

def test_some_models(self):
"""If some of the models has appropriate table in db - return matching models."""
connection = self._connection_mock(self.tables)
self._cache_mock(self.tables[1:3])
result_tables = [m._meta.db_table for m in self.runner._get_models_for_connection(connection)]
self.assertEqual(result_tables, self.tables[1:3])

def test_all_models(self):
"""If all the models has appropriate table in db - return all models."""
connection = self._connection_mock(self.tables)
self._cache_mock(self.tables)
result_tables = [m._meta.db_table for m in self.runner._get_models_for_connection(connection)]
self.assertEqual(result_tables, self.tables)

0 comments on commit a08d977

Please sign in to comment.