def test_binary_type(self): if six.PY2: with StdoutStderrBuffer() as buffer: print("print str") sys.stdout.write("stdout.write str\n") sys.stderr.write("stderr.write str") self.assertEqual_dedent( buffer.get_output(), """ print str stdout.write str stderr.write str """) elif six.PY3: # The print function will use repr with StdoutStderrBuffer() as buffer: print(b"print binary_type") sys.stdout.write(b"stdout.write binary_type\n") sys.stderr.write(b"stderr.write binary_type") self.assertEqual_dedent( buffer.get_output(), """ b'print binary_type' stdout.write binary_type stderr.write binary_type """) else: self.fail()
def test_print_mailbox_mime_image(self): assert_pformat_equal(len(mail.outbox), 0) ok = SendMail( template_base="mail_test.{ext}", mail_context={"foo": "first", "bar": "second"}, subject="test test_print_mailbox()", recipient_list="*****@*****.**", attachments=[MIMEImage(b"GIF89a This is not a gif picture ;)")], ).send() assert_pformat_equal(ok, True) assert_pformat_equal(len(mail.outbox), 1) with StdoutStderrBuffer() as buff: print_mailbox(mail.outbox) output = buff.get_output() print(output) self.assertIn("*** Mail No. 1: ***", output) self.assertIn("subject: test test_print_mailbox()", output) self.assertIn("<!-- START 'mail_test.txt' -->", output) self.assertIn("<!-- END 'mail_test.txt' -->", output) self.assertIn("from_email: webmaster@localhost", output) self.assertIn("to: ['*****@*****.**']", output) self.assertIn("attachments:", output) self.assertIn("MIMEImage object", output) self.assertIn("b'GIF89a", output)
def test_print_mailbox(self): self.assertEqual(len(mail.outbox), 0) ok = SendMail(template_base="mail_test.{ext}", mail_context={ "foo": "first", "bar": "second" }, subject="test test_print_mailbox()", recipient_list="*****@*****.**").send() self.assertEqual(ok, True) self.assertEqual(len(mail.outbox), 1) with StdoutStderrBuffer() as buff: print_mailbox(mail.outbox) output = buff.get_output() print(output) self.assertIn("*** Mail No. 1: ***", output) self.assertIn("subject: test test_print_mailbox()", output) self.assertIn("<!-- START 'mail_test.txt' -->", output) self.assertIn("<!-- END 'mail_test.txt' -->", output) self.assertIn("from_email: webmaster@localhost", output) self.assertIn("to: ['*****@*****.**']", output)
def test_django_check(self): """ call './manage.py check' directly via 'call_command' """ with StdoutStderrBuffer() as buff: call_command('check') output = buff.get_output() self.assertIn('System check identified no issues (0 silenced).', output)
def test_diffsettings(self): """ Check some settings """ with StdoutStderrBuffer() as buff: call_command('diffsettings') output = buff.get_output() print(output) self.assertIn('user_secrets_tests.settings', output) # SETTINGS_MODULE
def test_destination_not_exists(self): with StdoutStderrBuffer() as buff: with self.assertRaises(SystemExit): call_command("export_filer_images", "does/not/exists") output = buff.get_output() print(output) self.assertIn("does/not/exists' is not a existing directory", output)
def test_image_info(self): with StdoutStderrBuffer() as buff: call_command("template_info") output = buff.get_output() print(output) self.assertNotIn("ERROR", output) self.assertIn("There are 2 public pages:", output) self.assertIn("INHERIT", output)
def setUp(self): super().setUp() self.temp_path = Path().cwd() # isolated_filesystem does made a chdir to /tmp/... # We can't use the created temp path directly, # because the installer will only use a not existing directory self.instance_root = Path(self.temp_path, "instance") # /tmp/TestClassNameXXX/instance self.project_name = clean_string(self._testMethodName) # "test_func_name" self.instance_path = Path(self.instance_root, self.project_name) # /tmp/TestClassNameXXX/instance/test_func_name with StdoutStderrBuffer() as buffer: create_instance( dest = self.instance_root, name = self.project_name, remove = False, exist_ok = False, ) output = buffer.get_output() try: self.assertIn( "Create instance with name '%s' at: %s..." % ( self.project_name, self.instance_root ), output ) self.manage_file_path = Path(self.instance_root, "manage.py") shebang=get_python3_shebang() self.assertIn("Update shebang in '%s' to '%s'" % (self.manage_file_path, shebang), output) self.assertIn("Update filecontent '%s/settings.py'" % self.instance_path, output) self.assertIn("Page instance created here: '%s'" % self.instance_root, output) self.assertNotIn("ERROR", output) self.assertTrue(self.instance_path.is_dir(), "Not a directory: '%s'" % self.instance_path) self.assertTrue(self.manage_file_path.is_file(), "File not found: '%s'" % self.manage_file_path) self.assertTrue(os.access(self.manage_file_path, os.X_OK), "File '%s' not executeable!" % self.manage_file_path) with self.manage_file_path.open("r") as f: manage_content=f.read() except Exception: print(output) raise try: self.assertIn(shebang, manage_content) self.assertIn("%s.settings" % self.project_name, manage_content) except Exception: print(manage_content) raise # Needed until https://github.com/divio/django-cms/issues/5079 is fixed: self.createcachetable()
def test_django_cms_check(self): """ call './manage.py cms check' directly via 'call_command' """ with StdoutStderrBuffer() as buff: call_command('cms', 'check') output = buff.get_output() print(output) self.assertIn('Installation okay', output) self.assertNotIn('ERROR', output)
def test_no_images(self): with StdoutStderrBuffer() as buff: with self.assertRaises(SystemExit): call_command("export_filer_images", self.temp_path) output = buff.get_output() print(output) self.assertIn("There are 0 images in database.", output) self.assertIn("ERROR: There are not images in database!", output)
def test_diffsettings(self): """ Check some settings """ with StdoutStderrBuffer() as buff: call_command('diffsettings') output = buff.get_output() print(output) self.assertIn('django_tools_test_project.test_settings', output) # SETTINGS_MODULE
def test_text_type(self): with StdoutStderrBuffer() as buffer: print(six.text_type("print text_type")) sys.stdout.write(six.text_type("stdout.write text_type\n")) sys.stderr.write(six.text_type("stderr.write text_type")) self.assertEqual_dedent(buffer.get_output(), """ print text_type stdout.write text_type stderr.write text_type """)
def test_wrong_username_given(self): with StdoutStderrBuffer() as buff: call_command("permission_info", "not existing username") output = buff.get_output() print(output) self.assertIn("Username 'not existing username' doesn't exists", output) self.assertNotIn("Traceback", output) self.assertNotIn("ERROR", output)
def test_no_username_given(self): with StdoutStderrBuffer() as buff: call_command("permission_info") output = buff.get_output() print(output) self.assertIn("All existing users are:", output) self.assertIn("normal_test_user, staff_test_user, superuser", output) self.assertIn("(3 users)", output) self.assertNotIn("Traceback", output) self.assertNotIn("ERROR", output)
def test_update_permissions(self): with StdoutStderrBuffer() as buff: call_command("update_permissions") output = buff.get_output() print(output) self.assertIn("Create permissions for:", output) self.assertIn(" * auth", output) self.assertIn(" * django_tools_test_app", output) self.assertNotIn("Traceback", output) self.assertNotIn("ERROR", output)
def test(self): with StdoutStderrBuffer() as buffer: with PrintQueries("Create object"): User.objects.all().count() output = buffer.get_output() # print(output) self.assertIn("*** Create object ***", output) # FIXME: Will fail if not SQLite/MySQL is used?!? self.assertIn("1 - QUERY = 'SELECT COUNT(", output) self.assertIn('FROM "auth_user"', output)
def test_diffsettings(self): """ Check some settings """ with StdoutStderrBuffer() as buff: call_command('diffsettings') output = buff.get_output() print(output) self.assertIn('django_tools_test_project.test_settings', output) # SETTINGS_MODULE self.assertIn('CELERY_ALWAYS_EAGER = True', output) self.assertIn('BROKER_TRANSPORT = \'memory\'', output)
def test_binary_type(self): # The print function will use repr with StdoutStderrBuffer() as buffer: print(b"print binary_type") sys.stdout.write(b"stdout.write binary_type\n") sys.stderr.write(b"stderr.write binary_type") assert_equal_dedent( buffer.get_output(), """ b'print binary_type' stdout.write binary_type stderr.write binary_type """, )
def test_remove_obsolete_permissions(self): superuser = self.UserModel.objects.filter(is_superuser=True, is_active=True)[0] encrypted_password = superuser.password # Create with more permissions: get_or_create_user_and_group( username="******", groupname="testgroup", permissions=get_filtered_permissions( exclude_app_labels=("admin",), exclude_codenames=("delete_group",), exclude_permissions=((ContentType, "delete_contenttype"),), ), encrypted_password=encrypted_password, ) with StdoutStderrBuffer() as buff: get_or_create_user_and_group( username="******", groupname="testgroup", permissions=get_filtered_permissions( exclude_app_labels=("admin", "sites"), exclude_models=(Session,), exclude_codenames=("delete_user", "delete_group"), exclude_permissions=((ContentType, "add_contenttype"), (ContentType, "delete_contenttype")), ), encrypted_password=encrypted_password, ) output = buff.get_output() print(output) assert_equal_dedent( output, """ Check 'admin' Check 'sites' remove permission: auth | user | Can delete user remove permission: contenttypes | content type | Can add content type remove permission: sessions | session | Can add session remove permission: sessions | session | Can change session remove permission: sessions | session | Can delete session remove permission: sessions | session | Can view session remove permission: sites | site | Can add site remove permission: sites | site | Can change site remove permission: sites | site | Can delete site remove permission: sites | site | Can view site Add 74 permissions to 'testgroup' Group testgroup has 74 permissions """, )
def test_print_sql(self): with StdoutStderrBuffer() as buffer: with PrintQueries("Create object"): self.UserModel.objects.all().count() output = buffer.get_output() # print(output) self.assertIn("*** Create object ***", output) # FIXME: Will fail if not SQLite/MySQL is used?!? if django.VERSION < (1, 9): self.assertIn("1 - QUERY = 'SELECT COUNT(", output) else: self.assertIn("1 - SELECT COUNT(", output) self.assertIn('FROM "auth_user"', output)
def test_list_all_plugins(self): with StdoutStderrBuffer() as buff: call_command("list_page_by_plugin") output = buff.get_output() print(output) self.assertEqual_dedent( output, """ No plugin-type given. All CMS plugin types: 12 instances: 'djangocms_text_ckeditor.TextPlugin' There are 1 plugins. """)
def test_exception_plus(): try: var = "Test 123" raise RuntimeError(var) except RuntimeError: with StdoutStderrBuffer() as buff: print_exc_plus(stop_local_vars=["site-packages"]) output = buff.get_output() print(output) assert "RuntimeError: Test 123" in output assert "Locals by frame, most recent call first:" in output assert ", in test_exception_plus" in output assert "var = 'Test 123'" in output
def test_database_info(self): with StdoutStderrBuffer() as buff: call_command("database_info") output = buff.get_output() print(output) self.assertIn("engine...............: 'sqlite3'", output) self.assertTrue( "name.................: ':memory:'" in output or "name.................: 'file:memorydb_default?mode=memory&cache=shared'" in output) self.assertIn("There are 1 connections.", output) self.assertIn("'CONN_MAX_AGE': 0,", output)
def test_list_all_plugins(self): with StdoutStderrBuffer() as buff: call_command("generate_add_plugin_test_code") output = buff.get_output() print(output) self.assertEqual_dedent( output, """ No plugin-type given. All CMS plugin types: 12 instances: 'djangocms_text_ckeditor.TextPlugin' 4 instances: 'test_cms_plugin.RelatedPlugin' There are 2 plugins. """ )
def test_export(self): user = self.login(usertype='superuser') instance = ImageModel() dummy = ImageDummy(width=100, height=50).create_temp_filer_info_image(text="foobar", user=user) instance.image = dummy instance.save() with StdoutStderrBuffer() as buff: call_command("export_filer_images", self.temp_path) output = buff.get_output() print(output) self.assertIn("1 items - django_cms_tools_test_project.test_cms_plugin.ImageModel", output) self.assertIn("All filer files saved:", output) self.assertIn("1 filer files saved", output)
def test_wrong_plugin_type(self): with StdoutStderrBuffer() as buff: call_command("list_page_by_plugin", "foobar_app.FooBarPlugin") output = buff.get_output() print(output) self.assertEqual_dedent( output, """ ERROR: Given plugin type 'foobar_app.FooBarPlugin' doesn't exists! Hint: Maybe you mean: 'FooBarPlugin' ?!? All CMS plugin types: 12 instances: 'djangocms_text_ckeditor.TextPlugin' There are 1 plugins. """)
def test_image_info(self): with StdoutStderrBuffer() as buff: call_command("orphaned_plugin_info") output = buff.get_output() print(output) self.assertEqual_dedent( output, """ _______________________________________________________________________________ Django CMS orphaned plugin info There are 0 CMS plugin types... 0 CMS plugins checked 0 uninstalled CMS plugins 0 unsaved CMS plugins --- END --- """)
def test_image_info(self): with StdoutStderrBuffer() as buff: call_command("cms_plugin_info") output = buff.get_output() print(output) self.assertEqual_dedent( output, """ There are 6 CMS plugins: Django CMS Tools Test * RelatedPlugin (Related Plugin) Generic * AliasPlugin (Alias) * AnchorPlugin (Anchor) * DropDownAnchorMenuPlugin (Drop-Down Anchor Menu) * PlaceholderPlugin (Placeholder) * TextPlugin (Text) """)
def test_clear_cache(self): cache.set("key_foo", "bar", 30) self.assertEqual(cache.get("key_foo"), "bar") with StdoutStderrBuffer() as buff: call_command("clear_cache") output = buff.get_output() print(output) self.assertEqual(cache.get("key_foo"), None) self.assertIn("Clear caches:", output) self.assertIn("Clear 'LocMemCache'", output) self.assertIn("done.", output) self.assertNotIn("Traceback", output) self.assertNotIn("ERROR", output)
def test_normal_test_user(self): with StdoutStderrBuffer() as buff: call_command("permission_info", "normal_test_user") output = buff.get_output() print(output) self.assertIn("Display effective user permissions", output) self.assertIn("is_active : yes", output) self.assertIn("is_staff : no", output) self.assertIn("is_superuser : no", output) self.assertIn("[ ] auth.add_user", output) self.assertIn("[ ] sites.delete_site", output) self.assertNotIn("[*]", output) # normal user hasn't any permissions ;) self.assertNotIn("missing:", output) # no missing permissions self.assertNotIn("Traceback", output) self.assertNotIn("ERROR", output)