From c21ac0c3df91db9d00f45562ebb70df72ae3c255 Mon Sep 17 00:00:00 2001 From: Aarif Date: Mon, 22 Feb 2021 20:04:27 +0500 Subject: [PATCH] replaced unittest assertions pytest assertions (#26573) --- .../djangolib/testing/tests/test_utils.py | 6 +-- openedx/core/djangolib/testing/utils.py | 11 ++-- .../djangolib/tests/test_blockstore_cache.py | 22 ++++---- openedx/core/djangolib/tests/test_js_utils.py | 54 +++++++++---------- openedx/core/djangolib/tests/test_markup.py | 54 +++++++++---------- .../tests/test_oauth2_retirement_utils.py | 2 +- .../djangolib/tests/test_translation_utils.py | 4 +- 7 files changed, 72 insertions(+), 81 deletions(-) diff --git a/openedx/core/djangolib/testing/tests/test_utils.py b/openedx/core/djangolib/testing/tests/test_utils.py index a350790057..8793388e27 100644 --- a/openedx/core/djangolib/testing/tests/test_utils.py +++ b/openedx/core/djangolib/testing/tests/test_utils.py @@ -24,13 +24,13 @@ class TestGetMockRequest(TestCase): def test_mock_request_is_request(self): request = get_mock_request(USER_MODEL()) - self.assertIsInstance(request, HttpRequest) + assert isinstance(request, HttpRequest) def test_user_is_attached_to_mock_request(self): user = USER_MODEL() request = get_mock_request(user) - self.assertIs(request.user, user) + assert request.user is user def test_mock_request_without_user(self): request = get_mock_request() - self.assertIsInstance(request.user, AnonymousUser) + assert isinstance(request.user, AnonymousUser) diff --git a/openedx/core/djangolib/testing/utils.py b/openedx/core/djangolib/testing/utils.py index 1c3a6307ef..ff1217fb7e 100644 --- a/openedx/core/djangolib/testing/utils.py +++ b/openedx/core/djangolib/testing/utils.py @@ -190,13 +190,10 @@ class _AssertNumQueriesContext(CaptureQueriesContext): return filtered_queries = [query for query in self.captured_queries if is_unfiltered_query(query)] executed = len(filtered_queries) - self.test_case.assertEqual( - executed, self.num, - u"%d queries executed, %d expected\nCaptured queries were:\n%s" % ( - executed, self.num, - '\n'.join( - query['sql'] for query in filtered_queries - ) + + assert executed == self.num, ( + u'%d queries executed, %d expected\nCaptured queries were:\n%s' % ( + executed, self.num, '\n'.join((query['sql'] for query in filtered_queries)) ) ) diff --git a/openedx/core/djangolib/tests/test_blockstore_cache.py b/openedx/core/djangolib/tests/test_blockstore_cache.py index 38a7757881..0e5b3f898c 100644 --- a/openedx/core/djangolib/tests/test_blockstore_cache.py +++ b/openedx/core/djangolib/tests/test_blockstore_cache.py @@ -44,8 +44,8 @@ class BundleCacheTest(TestWithBundleMixin, unittest.TestCase): cache.set(key1, value1) value2 = {"this is": "a dict", "for": "key2"} cache.set(key2, value2) - self.assertEqual(cache.get(key1), value1) - self.assertEqual(cache.get(key2), value2) + assert cache.get(key1) == value1 + assert cache.get(key2) == value2 # Now publish a new version of the bundle: api.write_draft_file(self.draft.uuid, "test.txt", "we need a changed file in order to publish a new version") @@ -53,8 +53,8 @@ class BundleCacheTest(TestWithBundleMixin, unittest.TestCase): # Now the cache should be invalidated # (immediately since we set MAX_BLOCKSTORE_CACHE_DELAY to 0) - self.assertEqual(cache.get(key1), None) - self.assertEqual(cache.get(key2), None) + assert cache.get(key1) is None + assert cache.get(key2) is None def test_bundle_draft_cache(self): """ @@ -69,16 +69,16 @@ class BundleCacheTest(TestWithBundleMixin, unittest.TestCase): cache.set(key1, value1) value2 = {"this is": "a dict", "for": "key2"} cache.set(key2, value2) - self.assertEqual(cache.get(key1), value1) - self.assertEqual(cache.get(key2), value2) + assert cache.get(key1) == value1 + assert cache.get(key2) == value2 # Now make a change to the draft (doesn't matter if we commit it or not) api.write_draft_file(self.draft.uuid, "test.txt", "we need a changed file in order to publish a new version") # Now the cache should be invalidated # (immediately since we set MAX_BLOCKSTORE_CACHE_DELAY to 0) - self.assertEqual(cache.get(key1), None) - self.assertEqual(cache.get(key2), None) + assert cache.get(key1) is None + assert cache.get(key2) is None @unittest.skipUnless(settings.RUN_BLOCKSTORE_TESTS, "Requires a running Blockstore server") @@ -99,7 +99,7 @@ class BundleCacheClearTest(TestWithBundleMixin, unittest.TestCase): key1 = ("some", "key", "1") value1 = "value1" cache.set(key1, value1) - self.assertEqual(cache.get(key1), value1) + assert cache.get(key1) == value1 # Now publish a new version of the bundle: api.write_draft_file(self.draft.uuid, "test.txt", "we need a changed file in order to publish a new version") @@ -108,7 +108,7 @@ class BundleCacheClearTest(TestWithBundleMixin, unittest.TestCase): # Now the cache will not be immediately invalidated; it takes up to MAX_BLOCKSTORE_CACHE_DELAY seconds. # Since this is a new bundle and we _just_ accessed the cache for the first time, we can be confident # it won't yet be automatically invalidated. - self.assertEqual(cache.get(key1), value1) + assert cache.get(key1) == value1 # Now "clear" the cache, forcing the check of the new version: cache.clear() - self.assertEqual(cache.get(key1), None) + assert cache.get(key1) is None diff --git a/openedx/core/djangolib/tests/test_js_utils.py b/openedx/core/djangolib/tests/test_js_utils.py index 15d714c535..822cefd408 100644 --- a/openedx/core/djangolib/tests/test_js_utils.py +++ b/openedx/core/djangolib/tests/test_js_utils.py @@ -46,7 +46,7 @@ class TestJSUtils(TestCase): ) escaped_json = dump_js_escaped_json(malicious_dict) - self.assertEqual(expected_escaped_json, escaped_json) + assert expected_escaped_json == escaped_json def test_dump_js_escaped_json_with_custom_encoder_escapes_unsafe_html(self): """ @@ -66,7 +66,7 @@ class TestJSUtils(TestCase): ) escaped_json = dump_js_escaped_json(malicious_dict, cls=self.SampleJSONEncoder) - self.assertEqual(expected_custom_escaped_json, escaped_json) + assert expected_custom_escaped_json == escaped_json def test_js_escaped_string_escapes_unsafe_html(self): """ @@ -78,14 +78,14 @@ class TestJSUtils(TestCase): r"\u003C/script\u003E\u003Cscript\u003Ealert(\u0027hello, \u0027)\u003B\u003C/script\u003E" ) escaped_string_for_js = js_escaped_string(malicious_js_string) - self.assertEqual(expected_escaped_string_for_js, escaped_string_for_js) + assert expected_escaped_string_for_js == escaped_string_for_js def test_js_escaped_string_with_none(self): """ Test js_escaped_string returns empty string for None """ escaped_string_for_js = js_escaped_string(None) - self.assertEqual(u"", escaped_string_for_js) + assert u'' == escaped_string_for_js def test_mako(self): """ @@ -142,32 +142,32 @@ class TestJSUtils(TestCase): r""test-=&\\;'\"<>\u2603"}" ) self._validate_expectation_of_json_for_html(test_dict, expected_json_for_html) - self.assertIn(""test_tuple": [1, 2, 3]", out) - self.assertIn(""test_number": 3.5", out) - self.assertIn(""test_bool": false", out) - self.assertIn(""test_string": "test-=&\\\\;'\\"<>\\u2603"", out) - self.assertIn(u"data-test-string='test-=&\\;'"<>☃'", out) - self.assertIn("data-test-tuple='[1, 2, 3]'", out) - self.assertIn("data-test-number='3.5'", out) - self.assertIn("data-test-bool='false'", out) + assert '"test_tuple": [1, 2, 3]' in out + assert '"test_number": 3.5' in out + assert '"test_bool": false' in out + assert '"test_string": "test-=&\\\\;'\\"<>\\u2603"' in out + assert u"data-test-string='test-=&\\;'"<>☃'" in out + assert "data-test-tuple='[1, 2, 3]'" in out + assert "data-test-number='3.5'" in out + assert "data-test-bool='false'" in out expected_string_for_js_in_dict = r'''test-=\u0026\\;'\"\u003c\u003e\u2603''' self._validate_expectation_of_string_for_js(test_dict['test_string'], expected_string_for_js_in_dict) location_of_dict_in_out = re.search("var test_dict.*}", out) var_dict_in_out = out[location_of_dict_in_out.span()[0]:location_of_dict_in_out.span()[1]] - self.assertIn('"test_number": 3.5', var_dict_in_out) - self.assertIn('"test_string": "test-=\\u0026\\\\;\'\\"\\u003c\\u003e\\u2603"', var_dict_in_out) - self.assertIn('"test_tuple": [1, 2, 3]', var_dict_in_out) - self.assertIn('"test_bool": false', var_dict_in_out) + assert '"test_number": 3.5' in var_dict_in_out + assert '"test_string": "test-=\\u0026\\\\;\'\\"\\u003c\\u003e\\u2603"' in var_dict_in_out + assert '"test_tuple": [1, 2, 3]' in var_dict_in_out + assert '"test_bool": false' in var_dict_in_out expected_string_for_js = u"test\\u002D\\u003D\\u0026\\u005C\\u003B\\u0027\\u0022\\u003C\\u003E☃" self._validate_expectation_of_string_for_js(test_dict['test_string'], expected_string_for_js) - self.assertIn("var test_string = '" + expected_string_for_js + "'", out) - self.assertIn("var test_none_string = ''", out) - self.assertIn("var test_tuple = [1, 2, 3]", out) - self.assertIn("var test_number = 3.5", out) - self.assertIn("var test_bool = false", out) - self.assertIn("var test_none_json = null", out) + assert ("var test_string = '" + expected_string_for_js) + "'" in out + assert "var test_none_string = ''" in out + assert 'var test_tuple = [1, 2, 3]' in out + assert 'var test_number = 3.5' in out + assert 'var test_bool = false' in out + assert 'var test_none_json = null' in out def _validate_expectation_of_json_for_html(self, test_dict, expected_json_for_html_string): """ @@ -191,10 +191,10 @@ class TestJSUtils(TestCase): # tuples become arrays in json, so it is parsed to a list that is # switched back to a tuple before comparing parsed_expected_dict['test_tuple'] = tuple(parsed_expected_dict['test_tuple']) - self.assertEqual(test_dict['test_string'], parsed_expected_dict['test_string']) - self.assertEqual(test_dict['test_tuple'], parsed_expected_dict['test_tuple']) - self.assertEqual(test_dict['test_number'], parsed_expected_dict['test_number']) - self.assertEqual(test_dict['test_bool'], parsed_expected_dict['test_bool']) + assert test_dict['test_string'] == parsed_expected_dict['test_string'] + assert test_dict['test_tuple'] == parsed_expected_dict['test_tuple'] + assert test_dict['test_number'] == parsed_expected_dict['test_number'] + assert test_dict['test_bool'] == parsed_expected_dict['test_bool'] def _validate_expectation_of_string_for_js(self, test_string, expected_string_for_js): """ @@ -212,4 +212,4 @@ class TestJSUtils(TestCase): """ parsed_expected_string = json.loads('"' + expected_string_for_js + '"') - self.assertEqual(test_string, parsed_expected_string) + assert test_string == parsed_expected_string diff --git a/openedx/core/djangolib/tests/test_markup.py b/openedx/core/djangolib/tests/test_markup.py index d99f2db265..bc3062ba3e 100644 --- a/openedx/core/djangolib/tests/test_markup.py +++ b/openedx/core/djangolib/tests/test_markup.py @@ -30,8 +30,8 @@ class FormatHtmlTest(unittest.TestCase): ) def test_simple(self, before_after): (before, after) = before_after - self.assertEqual(six.text_type(Text(_(before))), after) # pylint: disable=translation-of-non-string - self.assertEqual(six.text_type(Text(before)), after) + assert six.text_type(Text(_(before))) == after # pylint: disable=translation-of-non-string + assert six.text_type(Text(before)) == after def test_formatting(self): # The whole point of this function is to make sure this works: @@ -39,10 +39,7 @@ class FormatHtmlTest(unittest.TestCase): start=HTML(""), end=HTML(""), ) - self.assertEqual( - six.text_type(out), - u"Point & click here!", - ) + assert six.text_type(out) == u"Point & click here!" def test_nested_formatting(self): # Sometimes, you have plain text, with html inserted, and the html has @@ -51,10 +48,7 @@ class FormatHtmlTest(unittest.TestCase): start=HTML(u"").format(email="A&B"), end=HTML(""), ) - self.assertEqual( - six.text_type(out), - u"Send email", - ) + assert six.text_type(out) == u"Send email" def test_mako(self): # The default_filters used here have to match the ones in edxmako. @@ -70,12 +64,12 @@ class FormatHtmlTest(unittest.TestCase): default_filters=['decode.utf8', 'h'], ) out = template.render() - self.assertEqual(out.strip(), u"A & B & C") + assert out.strip() == u'A & B & C' def test_ungettext(self): for i in [1, 2]: out = Text(ungettext(u"1 & {}", u"2 & {}", i)).format(HTML(u"<>")) - self.assertEqual(out, u"{} & <>".format(i)) + assert out == u'{} & <>'.format(i) def test_strip_all_tags_but_br_filter(self): """ Verify filter removes every tags except br """ @@ -90,8 +84,8 @@ class FormatHtmlTest(unittest.TestCase): ) rendered_template = template.render() - self.assertIn('
', rendered_template) - self.assertNotIn('