2018-07-27 03:04:58 +08:00
|
|
|
import datetime
|
2021-12-31 23:30:48 +08:00
|
|
|
from io import StringIO
|
2018-07-27 03:04:58 +08:00
|
|
|
from unittest import mock
|
|
|
|
|
2021-12-31 23:30:48 +08:00
|
|
|
from django.core.management.base import OutputWrapper
|
2018-07-27 03:04:58 +08:00
|
|
|
from django.db.migrations.questioner import (
|
|
|
|
InteractiveMigrationQuestioner,
|
|
|
|
MigrationQuestioner,
|
|
|
|
)
|
2021-07-17 15:46:24 +08:00
|
|
|
from django.db.models import NOT_PROVIDED
|
2016-01-26 03:27:35 +08:00
|
|
|
from django.test import SimpleTestCase
|
2021-12-31 23:30:48 +08:00
|
|
|
from django.test.utils import override_settings
|
2016-01-26 03:27:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
class QuestionerTests(SimpleTestCase):
|
|
|
|
@override_settings(
|
|
|
|
INSTALLED_APPS=["migrations"],
|
|
|
|
MIGRATION_MODULES={"migrations": None},
|
|
|
|
)
|
|
|
|
def test_ask_initial_with_disabled_migrations(self):
|
|
|
|
questioner = MigrationQuestioner()
|
|
|
|
self.assertIs(False, questioner.ask_initial("migrations"))
|
2018-07-27 03:04:58 +08:00
|
|
|
|
2021-11-07 22:34:16 +08:00
|
|
|
def test_ask_not_null_alteration(self):
|
|
|
|
questioner = MigrationQuestioner()
|
|
|
|
self.assertIsNone(
|
|
|
|
questioner.ask_not_null_alteration("field_name", "model_name")
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2021-07-17 15:46:24 +08:00
|
|
|
|
|
|
|
@mock.patch("builtins.input", return_value="2")
|
|
|
|
def test_ask_not_null_alteration_not_provided(self, mock):
|
2021-12-31 23:30:48 +08:00
|
|
|
questioner = InteractiveMigrationQuestioner(
|
|
|
|
prompt_output=OutputWrapper(StringIO())
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2021-12-31 23:30:48 +08:00
|
|
|
question = questioner.ask_not_null_alteration("field_name", "model_name")
|
2021-07-17 15:46:24 +08:00
|
|
|
self.assertEqual(question, NOT_PROVIDED)
|
2021-11-07 22:34:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
class QuestionerHelperMethodsTests(SimpleTestCase):
|
2021-12-31 23:30:48 +08:00
|
|
|
def setUp(self):
|
|
|
|
self.prompt = OutputWrapper(StringIO())
|
|
|
|
self.questioner = InteractiveMigrationQuestioner(prompt_output=self.prompt)
|
2021-11-07 22:34:16 +08:00
|
|
|
|
|
|
|
@mock.patch("builtins.input", return_value="datetime.timedelta(days=1)")
|
|
|
|
def test_questioner_default_timedelta(self, mock_input):
|
2021-12-31 23:30:48 +08:00
|
|
|
value = self.questioner._ask_default()
|
2021-11-07 22:34:16 +08:00
|
|
|
self.assertEqual(value, datetime.timedelta(days=1))
|
|
|
|
|
|
|
|
@mock.patch("builtins.input", return_value="")
|
|
|
|
def test_questioner_default_no_user_entry(self, mock_input):
|
2021-12-31 23:30:48 +08:00
|
|
|
value = self.questioner._ask_default(default="datetime.timedelta(days=1)")
|
2021-11-07 22:34:16 +08:00
|
|
|
self.assertEqual(value, datetime.timedelta(days=1))
|
|
|
|
|
|
|
|
@mock.patch("builtins.input", side_effect=["", "exit"])
|
|
|
|
def test_questioner_no_default_no_user_entry(self, mock_input):
|
2021-12-31 23:30:48 +08:00
|
|
|
with self.assertRaises(SystemExit):
|
2021-11-07 22:34:16 +08:00
|
|
|
self.questioner._ask_default()
|
|
|
|
self.assertIn(
|
|
|
|
"Please enter some code, or 'exit' (without quotes) to exit.",
|
2021-12-31 23:30:48 +08:00
|
|
|
self.prompt.getvalue(),
|
2021-11-07 22:34:16 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
@mock.patch("builtins.input", side_effect=["bad code", "exit"])
|
|
|
|
def test_questioner_no_default_bad_user_entry_code(self, mock_input):
|
2021-12-31 23:30:48 +08:00
|
|
|
with self.assertRaises(SystemExit):
|
2021-11-07 22:34:16 +08:00
|
|
|
self.questioner._ask_default()
|
2021-12-31 23:30:48 +08:00
|
|
|
self.assertIn("Invalid input: ", self.prompt.getvalue())
|
2021-11-07 22:34:16 +08:00
|
|
|
|
|
|
|
@mock.patch("builtins.input", side_effect=["", "n"])
|
|
|
|
def test_questioner_no_default_no_user_entry_boolean(self, mock_input):
|
2021-12-31 23:30:48 +08:00
|
|
|
value = self.questioner._boolean_input("Proceed?")
|
2021-11-07 22:34:16 +08:00
|
|
|
self.assertIs(value, False)
|
|
|
|
|
|
|
|
@mock.patch("builtins.input", return_value="")
|
|
|
|
def test_questioner_default_no_user_entry_boolean(self, mock_input):
|
2021-12-31 23:30:48 +08:00
|
|
|
value = self.questioner._boolean_input("Proceed?", default=True)
|
2021-11-07 22:34:16 +08:00
|
|
|
self.assertIs(value, True)
|
|
|
|
|
|
|
|
@mock.patch("builtins.input", side_effect=[10, "garbage", 1])
|
|
|
|
def test_questioner_bad_user_choice(self, mock_input):
|
|
|
|
question = "Make a choice:"
|
2021-12-31 23:30:48 +08:00
|
|
|
value = self.questioner._choice_input(question, choices="abc")
|
2021-11-07 22:34:16 +08:00
|
|
|
expected_msg = f"{question}\n" f" 1) a\n" f" 2) b\n" f" 3) c\n"
|
2021-12-31 23:30:48 +08:00
|
|
|
self.assertIn(expected_msg, self.prompt.getvalue())
|
2021-11-07 22:34:16 +08:00
|
|
|
self.assertEqual(value, 1)
|