Navigation Menu

Skip to content

Commit

Permalink
Add tests for manage.get_local_test_settings_file
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhuang committed May 2, 2018
1 parent b64decb commit 840c5fa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
8 changes: 3 additions & 5 deletions manage.py
Expand Up @@ -10,15 +10,13 @@ def get_local_test_settings_file(argv):
local_settings_dir = os.path.split(argv[0])[0]
assert os.path.isfile(os.path.join(local_settings_dir, "manage.py"))

from django.core.management.base import CommandParser
from django.core.management import CommandParser, CommandError
parser = CommandParser(None, usage="%(prog)s subcommand [options] [args]",
add_help=False)
parser.add_argument('--local_test_settings',
dest="local_test_settings")
try:
options, args = parser.parse_known_args(argv)
except CommandError:
raise

options, args = parser.parse_known_args(argv)

if options.local_test_settings is None:
local_settings_file = "local_settings_example.py"
Expand Down
42 changes: 42 additions & 0 deletions tests/test_misc.py
Expand Up @@ -33,6 +33,7 @@
from django.utils.formats import date_format, get_format
from django.utils.dateformat import format
from django.utils.translation import ugettext_lazy as _
from django.core.management import CommandError

from course.models import Course
from course.views import EditCourseForm
Expand All @@ -41,6 +42,8 @@
is_maintenance_mode, render_email_template, get_outbound_mail_connection,
format_datetime_local)

from manage import get_local_test_settings_file

from tests.base_test_mixins import SingleCourseTestMixin
from tests.utils import LocmemBackendTestsMixin, mail, mock
from tests.constants import DATE_TIME_PICKER_TIME_FORMAT
Expand Down Expand Up @@ -578,4 +581,43 @@ def test_attribute_error2(self):

# }}}


class GetLocalTestSettingsFileTest(unittest.TestCase):
"""test manage.get_local_test_settings_file"""

def test_use_default_local_settings_example(self):
self.assertEqual(get_local_test_settings_file(
["manage.py", "test", "foo"]), "local_settings_example.py")

def test_error_use_local_settings(self):
"""test error when use local_settings.py as test settings"""
with self.assertRaises(CommandError) as cm:
get_local_test_settings_file(
["manage.py", "test", "--local_test_settings",
"local_settings.py"])

self.assertIn(
"Using production local_settings for tests is not "
"allowed due to security reason.", str(cm.exception))

def test_error_local_test_setting_file_does_not_exist(self):
"""test error when use local_settings.py as test settings"""
invalid_file = "foo/local_test_settings.py"
with self.assertRaises(CommandError) as cm:
get_local_test_settings_file(
["manage.py", "test", "--local_test_settings",
invalid_file])

self.assertIn(
"file '%s' does not exist" % invalid_file, str(cm.exception))

def test_custom_local_test_setting_file(self):
settings_file = "foo/local_test_settings.py"
with mock.patch("os.path.isfile") as mock_is_file:
mock_is_file.return_value = True
self.assertEqual(get_local_test_settings_file(
["manage.py", "test", "foo",
"--local_test_settings", settings_file]),
settings_file)

# vim: foldmethod=marker

0 comments on commit 840c5fa

Please sign in to comment.