def test_basic(self): """ #15346, #15573 - create_default_site() creates an example site only if none exist. """ with captured_stdout() as stdout: create_default_site(self.app_config) self.assertEqual(Site.objects.count(), 1) self.assertIn("Creating example.com", stdout.getvalue()) with captured_stdout() as stdout: create_default_site(self.app_config) self.assertEqual(Site.objects.count(), 1) self.assertEqual("", stdout.getvalue())
def _run_autocomplete(self): util = ManagementUtility(argv=sys.argv) with captured_stdout() as stdout: try: util.autocomplete() except SystemExit: pass return stdout.getvalue().strip().split('\n')
def test_interactive_true_without_dependent_objects(self): """ interactive mode deletes stale content types even if there aren't any dependent objects. """ with mock.patch('builtins.input', return_value='yes'): with captured_stdout() as stdout: call_command('remove_stale_contenttypes', verbosity=2) self.assertIn("Deleting stale content type", stdout.getvalue()) self.assertEqual(ContentType.objects.count(), self.before_count)
def test_interactive_true_with_dependent_objects(self): """ interactive mode (the default) deletes stale content types and warns of dependent objects. """ post = Post.objects.create(title='post', content_type=self.content_type) # A related object is needed to show that a custom collector with # can_fast_delete=False is needed. ModelWithNullFKToSite.objects.create(post=post) with mock.patch('builtins.input', return_value='yes'): with captured_stdout() as stdout: call_command('remove_stale_contenttypes', verbosity=2, stdout=stdout) self.assertEqual(Post.objects.count(), 0) output = stdout.getvalue() self.assertIn('- Content type for contenttypes_tests.Fake', output) self.assertIn('- 1 contenttypes_tests.Post object(s)', output) self.assertIn('- 1 contenttypes_tests.ModelWithNullFKToSite', output) self.assertIn('Deleting stale content type', output) self.assertEqual(ContentType.objects.count(), self.before_count)
def test_command_help(self): with captured_stdout(), captured_stderr(): # `call_command` bypasses the parser; by calling # `execute_from_command_line` with the help subcommand we # ensure that there are no issues with the parser itself. execute_from_command_line(['djmodels-admin', 'help', 'compilemessages'])
def test_stdin_read(self, select): with captured_stdin() as stdin, captured_stdout() as stdout: stdin.write('print(100)\n') stdin.seek(0) call_command('shell') self.assertEqual(stdout.getvalue().strip(), '100')
def test_timedelta_default(self, mock): questioner = InteractiveMigrationQuestioner() with captured_stdout(): value = questioner._ask_default() self.assertEqual(value, datetime.timedelta(days=1))
def test_contenttypes_removed_in_apps_without_models(self): ContentType.objects.create(app_label='no_models', model='Fake') with mock.patch('builtins.input', return_value='yes'), captured_stdout() as stdout: call_command('remove_stale_contenttypes', verbosity=2) self.assertIn("Deleting stale content type 'no_models | Fake'", stdout.getvalue()) self.assertEqual(ContentType.objects.count(), self.before_count)
def test_interactive_false(self): """non-interactive mode deletes stale content types.""" with captured_stdout() as stdout: call_command('remove_stale_contenttypes', interactive=False, verbosity=2) self.assertIn('Deleting stale content type', stdout.getvalue()) self.assertEqual(ContentType.objects.count(), self.before_count)