From 10c66919a86883a91a71d75f1a7fa0ff58bd4c8d Mon Sep 17 00:00:00 2001 From: Mads Jensen Date: Thu, 26 Sep 2019 22:44:06 +0200 Subject: [PATCH] Changed assertTrue/assertFalse to better test methods. assertTrue and assertFalse should not be used to test for not None or None values. --- tests/core/test_accounts.py | 8 ++------ tests/core/test_models.py | 18 +++++++++--------- tests/core/test_template_filters.py | 24 ++++++++++++------------ tests/core/test_views.py | 6 +++--- tests/plugins/images/test_views.py | 8 ++++---- 5 files changed, 30 insertions(+), 34 deletions(-) diff --git a/tests/core/test_accounts.py b/tests/core/test_accounts.py index 66571436..243c665e 100644 --- a/tests/core/test_accounts.py +++ b/tests/core/test_accounts.py @@ -86,9 +86,7 @@ class LogoutViewTests(RequireRootArticleMixin, DjangoClientTestBase): def test_logout_account_handling(self): self.client.get(wiki_settings.LOGOUT_URL) user = auth.get_user(self.client) - # Can be changed to assertIs(... False) when Django 1.11 is dropped - # Django 1.11 returns CallableBoolean - self.assertFalse(auth.get_user(self.client).is_authenticated) + self.assertIs(auth.get_user(self.client).is_authenticated, False) self.assertIsInstance(user, AnonymousUser) @@ -104,9 +102,7 @@ class LoginTestViews(RequireRootArticleMixin, TestBase): wiki_settings.LOGIN_URL, {'username': SUPERUSER1_USERNAME, 'password': SUPERUSER1_PASSWORD} ) - # Can be changed to assertIs(... False) when Django 1.11 is dropped - # Django 1.11 returns CallableBoolean - self.assertTrue(self.superuser1.is_authenticated) + self.assertIs(self.superuser1.is_authenticated, True) self.assertEqual(auth.get_user(self.client), self.superuser1) diff --git a/tests/core/test_models.py b/tests/core/test_models.py index 4ad5a8a0..2d2b4811 100644 --- a/tests/core/test_models.py +++ b/tests/core/test_models.py @@ -38,17 +38,17 @@ class ArticleModelTest(TestCase): a = Article.objects.create() - self.assertFalse(a.current_revision) - self.assertFalse(a.owner) - self.assertFalse(a.group) + self.assertIsNone(a.current_revision) + self.assertIsNone(a.owner) + self.assertIsNone(a.group) - self.assertTrue(a.created) - self.assertTrue(a.modified) + self.assertIsNotNone(a.created) + self.assertIsNotNone(a.modified) - self.assertTrue(a.group_read) - self.assertTrue(a.group_write) - self.assertTrue(a.other_read) - self.assertTrue(a.other_write) + self.assertIsNotNone(a.group_read) + self.assertIsNotNone(a.group_write) + self.assertIsNotNone(a.other_read) + self.assertIsNotNone(a.other_write) # XXX maybe redundant test def test_model_manager_class(self): diff --git a/tests/core/test_template_filters.py b/tests/core/test_template_filters.py index 6e76855c..c84f55bd 100644 --- a/tests/core/test_template_filters.py +++ b/tests/core/test_template_filters.py @@ -193,7 +193,7 @@ class CanRead(TemplateTestCase): u = User.objects.create(username='Nobody', password='pass') output = can_read(a, u) - self.assertTrue(output) + self.assertIs(output, True) output = self.render({'article': a, 'user': u}) self.assertIn('True', output) @@ -205,7 +205,7 @@ class CanRead(TemplateTestCase): u = User.objects.create(username='Noman', password='pass') output = can_read(a, u) - self.assertFalse(output) + self.assertIs(output, False) output = self.render({'article': a, 'user': u}) self.assertIn('False', output) @@ -226,7 +226,7 @@ class CanWrite(TemplateTestCase): u = User.objects.create(username='Nobody', password='pass') output = can_write(a, u) - self.assertTrue(output) + self.assertIs(output, True) output = self.render({'article': a, 'user': u}) self.assertIn('True', output) @@ -238,7 +238,7 @@ class CanWrite(TemplateTestCase): u = User.objects.create(username='Noman', password='pass') output = can_write(a, u) - self.assertFalse(output) + self.assertIs(output, False) output = self.render({'article': a, 'user': u}) self.assertIn('False', output) @@ -259,7 +259,7 @@ class CanDelete(TemplateTestCase): u = User.objects.create(username='Nobody', password='pass') output = can_delete(a, u) - self.assertTrue(output) + self.assertIs(output, True) output = self.render({'article': a, 'user': u}) self.assertIn('True', output) @@ -271,7 +271,7 @@ class CanDelete(TemplateTestCase): u = User.objects.create(username='Noman', password='pass') output = can_delete(a, u) - self.assertFalse(output) + self.assertIs(output, False) output = self.render({'article': a, 'user': u}) self.assertIn('False', output) @@ -292,7 +292,7 @@ class CanModerate(TemplateTestCase): u = User.objects.create(username='Nobody', password='pass') output = can_moderate(a, u) - self.assertTrue(output) + self.assertIs(output, True) output = self.render({'article': a, 'user': u}) self.assertIn('True', output) @@ -303,7 +303,7 @@ class CanModerate(TemplateTestCase): u = User.objects.create(username='Noman', password='pass') output = can_moderate(a, u) - self.assertFalse(output) + self.assertIs(output, False) output = self.render({'article': a, 'user': u}) self.assertIn('False', output) @@ -321,7 +321,7 @@ class IsLocked(TemplateTestCase): a = Article.objects.create() output = is_locked(a) - self.assertFalse(output) + self.assertIsNone(output) output = self.render({'article': a}) self.assertIn('None', output) @@ -335,10 +335,10 @@ class IsLocked(TemplateTestCase): ArticleRevision.objects.create(article=b) output = is_locked(a) - self.assertFalse(output) + self.assertIs(output, False) output = is_locked(b) - self.assertFalse(output) + self.assertIs(output, False) output = self.render({'article': a}) self.assertIn('False', output) @@ -349,7 +349,7 @@ class IsLocked(TemplateTestCase): ArticleRevision.objects.create(article=a, locked=True) output = is_locked(a) - self.assertTrue(output) + self.assertIs(output, True) output = self.render({'article': a}) self.assertIn('True', output) diff --git a/tests/core/test_views.py b/tests/core/test_views.py index d85c0327..7e70438e 100644 --- a/tests/core/test_views.py +++ b/tests/core/test_views.py @@ -508,9 +508,9 @@ class SearchViewTest(RequireRootArticleMixin, ArticleWebTestUtils, DjangoClientT def contains_title(articles, title): return any(article.current_revision.title == title for article in articles) - self.assertTrue(contains_title(articles, 'Test0')) - self.assertFalse(contains_title(articles, 'Test1')) - self.assertTrue(contains_title(articles, 'Subtest0')) + self.assertIs(contains_title(articles, 'Test0'), True) + self.assertIs(contains_title(articles, 'Test1'), False) + self.assertIs(contains_title(articles, 'Subtest0'), True) def test_hierarchy_search_404(self): diff --git a/tests/plugins/images/test_views.py b/tests/plugins/images/test_views.py index 1888d8bf..9899d73b 100644 --- a/tests/plugins/images/test_views.py +++ b/tests/plugins/images/test_views.py @@ -51,7 +51,7 @@ class ImageTests(RequireRootArticleMixin, ArticleWebTestUtils, DjangoClientTestB if isinstance(plugin_instance, ImagePlugin): plugin_index = cnt break - self.assertTrue(plugin_index >= 0, "Image plugin not activated") + self.assertGreaterEqual(plugin_index, 0, msg="Image plugin not activated") base_edit_url = reverse('wiki:edit', kwargs={'path': path}) url = base_edit_url + '?f=form{0:d}'.format(plugin_index) filestream = self._create_gif_filestream_from_base64(self.test_data) @@ -183,7 +183,7 @@ class ImageTests(RequireRootArticleMixin, ArticleWebTestUtils, DjangoClientTestB image = models.Image.objects.get() self.assertEqual(models.Image.objects.count(), 1) self.assertEqual(image.current_revision.previous_revision.revision_number, before_edit_rev) - self.assertTrue(image.current_revision.deleted) + self.assertIs(image.current_revision.deleted, True) # RESTORE before_edit_rev = image.current_revision.revision_number @@ -209,7 +209,7 @@ class ImageTests(RequireRootArticleMixin, ArticleWebTestUtils, DjangoClientTestB image_revision = image.current_revision.imagerevision f_path = image_revision.image.file.name - self.assertTrue(os.path.exists(f_path)) + self.assertIs(os.path.exists(f_path), True) response = self.client.post( reverse('wiki:images_purge', kwargs={ @@ -221,7 +221,7 @@ class ImageTests(RequireRootArticleMixin, ArticleWebTestUtils, DjangoClientTestB response, reverse('wiki:images_index', kwargs={'path': ''}) ) self.assertEqual(models.Image.objects.count(), 0) - self.assertFalse(os.path.exists(f_path)) + self.assertIs(os.path.exists(f_path), False) @wiki_override_settings(ACCOUNT_HANDLING=True) def test_login_on_revision_add(self): -- 2.45.2