This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
forked from jazzband/django-nose
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding unittests for NoseTestSuiteRunner._get_models_for_connection.
- Loading branch information
Andrii Tsymbala
committed
Jun 10, 2012
1 parent
5747417
commit a08d977
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |