2018-07-26 19:04:58 +00:00
|
|
|
import datetime
|
2021-12-31 15:30:48 +00:00
|
|
|
from io import StringIO
|
2018-07-26 19:04:58 +00:00
|
|
|
from unittest import mock
|
|
|
|
|
2021-12-31 15:30:48 +00:00
|
|
|
from django.core.management.base import OutputWrapper
|
2018-07-26 19:04:58 +00:00
|
|
|
from django.db.migrations.questioner import (
|
2022-02-03 19:24:19 +00:00
|
|
|
InteractiveMigrationQuestioner,
|
|
|
|
MigrationQuestioner,
|
2018-07-26 19:04:58 +00:00
|
|
|
)
|
2021-07-17 07:46:24 +00:00
|
|
|
from django.db.models import NOT_PROVIDED
|
2016-01-25 19:27:35 +00:00
|
|
|
from django.test import SimpleTestCase
|
2021-12-31 15:30:48 +00:00
|
|
|
from django.test.utils import override_settings
|
2016-01-25 19:27:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class QuestionerTests(SimpleTestCase):
|
|
|
|
@override_settings(
|
2022-02-03 19:24:19 +00:00
|
|
|
INSTALLED_APPS=["migrations"],
|
|
|
|
MIGRATION_MODULES={"migrations": None},
|
2016-01-25 19:27:35 +00:00
|
|
|
)
|
|
|
|
def test_ask_initial_with_disabled_migrations(self):
|
|
|
|
questioner = MigrationQuestioner()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertIs(False, questioner.ask_initial("migrations"))
|
2018-07-26 19:04:58 +00:00
|
|
|
|
2021-11-07 14:34:16 +00:00
|
|
|
def test_ask_not_null_alteration(self):
|
|
|
|
questioner = MigrationQuestioner()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertIsNone(
|
|
|
|
questioner.ask_not_null_alteration("field_name", "model_name")
|
|
|
|
)
|
2021-07-17 07:46:24 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@mock.patch("builtins.input", return_value="2")
|
2021-07-17 07:46:24 +00:00
|
|
|
def test_ask_not_null_alteration_not_provided(self, mock):
|
2022-02-03 19:24:19 +00:00
|
|
|
questioner = InteractiveMigrationQuestioner(
|
|
|
|
prompt_output=OutputWrapper(StringIO())
|
|
|
|
)
|
|
|
|
question = questioner.ask_not_null_alteration("field_name", "model_name")
|
2021-07-17 07:46:24 +00:00
|
|
|
self.assertEqual(question, NOT_PROVIDED)
|
2021-11-07 14:34:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class QuestionerHelperMethodsTests(SimpleTestCase):
|
2021-12-31 15:30:48 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.prompt = OutputWrapper(StringIO())
|
|
|
|
self.questioner = InteractiveMigrationQuestioner(prompt_output=self.prompt)
|
2021-11-07 14:34:16 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@mock.patch("builtins.input", return_value="datetime.timedelta(days=1)")
|
2021-11-07 14:34:16 +00:00
|
|
|
def test_questioner_default_timedelta(self, mock_input):
|
2021-12-31 15:30:48 +00:00
|
|
|
value = self.questioner._ask_default()
|
2021-11-07 14:34:16 +00:00
|
|
|
self.assertEqual(value, datetime.timedelta(days=1))
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@mock.patch("builtins.input", return_value="")
|
2021-11-07 14:34:16 +00:00
|
|
|
def test_questioner_default_no_user_entry(self, mock_input):
|
2022-02-03 19:24:19 +00:00
|
|
|
value = self.questioner._ask_default(default="datetime.timedelta(days=1)")
|
2021-11-07 14:34:16 +00:00
|
|
|
self.assertEqual(value, datetime.timedelta(days=1))
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@mock.patch("builtins.input", side_effect=["", "exit"])
|
2021-11-07 14:34:16 +00:00
|
|
|
def test_questioner_no_default_no_user_entry(self, mock_input):
|
2021-12-31 15:30:48 +00:00
|
|
|
with self.assertRaises(SystemExit):
|
2021-11-07 14:34:16 +00:00
|
|
|
self.questioner._ask_default()
|
|
|
|
self.assertIn(
|
|
|
|
"Please enter some code, or 'exit' (without quotes) to exit.",
|
2021-12-31 15:30:48 +00:00
|
|
|
self.prompt.getvalue(),
|
2021-11-07 14:34:16 +00:00
|
|
|
)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@mock.patch("builtins.input", side_effect=["bad code", "exit"])
|
2024-11-18 13:39:55 +00:00
|
|
|
def test_questioner_no_default_syntax_error(self, mock_input):
|
2021-12-31 15:30:48 +00:00
|
|
|
with self.assertRaises(SystemExit):
|
2021-11-07 14:34:16 +00:00
|
|
|
self.questioner._ask_default()
|
2024-11-18 13:39:55 +00:00
|
|
|
self.assertIn("SyntaxError: invalid syntax", self.prompt.getvalue())
|
|
|
|
|
|
|
|
@mock.patch("builtins.input", side_effect=["datetim", "exit"])
|
|
|
|
def test_questioner_no_default_name_error(self, mock_input):
|
|
|
|
with self.assertRaises(SystemExit):
|
|
|
|
self.questioner._ask_default()
|
|
|
|
self.assertIn(
|
|
|
|
"NameError: name 'datetim' is not defined", self.prompt.getvalue()
|
|
|
|
)
|
|
|
|
|
|
|
|
@mock.patch("builtins.input", side_effect=["datetime.dat", "exit"])
|
|
|
|
def test_questioner_no_default_attribute_error(self, mock_input):
|
|
|
|
with self.assertRaises(SystemExit):
|
|
|
|
self.questioner._ask_default()
|
|
|
|
self.assertIn(
|
|
|
|
"AttributeError: module 'datetime' has no attribute 'dat'",
|
|
|
|
self.prompt.getvalue(),
|
|
|
|
)
|
2021-11-07 14:34:16 +00:00
|
|
|
|
2024-11-18 13:41:24 +00:00
|
|
|
@mock.patch("builtins.input", side_effect=[KeyboardInterrupt()])
|
|
|
|
def test_questioner_no_default_keyboard_interrupt(self, mock_input):
|
2024-12-17 08:30:33 +00:00
|
|
|
with self.assertRaises(SystemExit):
|
2024-11-18 13:41:24 +00:00
|
|
|
self.questioner._ask_default()
|
2024-12-17 08:30:33 +00:00
|
|
|
self.assertIn("Cancelled.\n", self.prompt.getvalue())
|
2024-11-18 13:41:24 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@mock.patch("builtins.input", side_effect=["", "n"])
|
2021-11-07 14:34:16 +00:00
|
|
|
def test_questioner_no_default_no_user_entry_boolean(self, mock_input):
|
2022-02-03 19:24:19 +00:00
|
|
|
value = self.questioner._boolean_input("Proceed?")
|
2021-11-07 14:34:16 +00:00
|
|
|
self.assertIs(value, False)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@mock.patch("builtins.input", return_value="")
|
2021-11-07 14:34:16 +00:00
|
|
|
def test_questioner_default_no_user_entry_boolean(self, mock_input):
|
2022-02-03 19:24:19 +00:00
|
|
|
value = self.questioner._boolean_input("Proceed?", default=True)
|
2021-11-07 14:34:16 +00:00
|
|
|
self.assertIs(value, True)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@mock.patch("builtins.input", side_effect=[10, "garbage", 1])
|
2021-11-07 14:34:16 +00:00
|
|
|
def test_questioner_bad_user_choice(self, mock_input):
|
2022-02-03 19:24:19 +00:00
|
|
|
question = "Make a choice:"
|
|
|
|
value = self.questioner._choice_input(question, choices="abc")
|
|
|
|
expected_msg = f"{question}\n" f" 1) a\n" f" 2) b\n" f" 3) c\n"
|
2021-12-31 15:30:48 +00:00
|
|
|
self.assertIn(expected_msg, self.prompt.getvalue())
|
2021-11-07 14:34:16 +00:00
|
|
|
self.assertEqual(value, 1)
|
2024-12-17 08:30:33 +00:00
|
|
|
|
|
|
|
@mock.patch("builtins.input", side_effect=[KeyboardInterrupt()])
|
|
|
|
def test_questioner_no_choice_keyboard_interrupt(self, mock_input):
|
|
|
|
question = "Make a choice:"
|
|
|
|
with self.assertRaises(SystemExit):
|
|
|
|
self.questioner._choice_input(question, choices="abc")
|
|
|
|
expected_msg = (
|
|
|
|
f"{question}\n"
|
|
|
|
f" 1) a\n"
|
|
|
|
f" 2) b\n"
|
|
|
|
f" 3) c\n"
|
|
|
|
f"Select an option: \n"
|
|
|
|
f"Cancelled.\n"
|
|
|
|
)
|
|
|
|
self.assertIn(expected_msg, self.prompt.getvalue())
|