From 633a03084a8c144791968ead0109550e66ac9d77 Mon Sep 17 00:00:00 2001 From: Curt Bruns Date: Thu, 15 Jan 2015 11:35:05 -0700 Subject: [PATCH] Increase unit test coverage in hacking test Added a few unit tests to increase the test coverage from ~60% up to 90%. Also modified the assertEqual of all tests to be in the form of assertEqual(EXPECTED, ACTUAL) format. Change-Id: If53cfae138c3e31d21e34aa86048d02a19fb6346 --- cinder/tests/test_hacking.py | 103 ++++++++++++++++++++++------------- 1 file changed, 65 insertions(+), 38 deletions(-) diff --git a/cinder/tests/test_hacking.py b/cinder/tests/test_hacking.py index 1849eecab..455f1049b 100644 --- a/cinder/tests/test_hacking.py +++ b/cinder/tests/test_hacking.py @@ -51,65 +51,92 @@ class HackingTestCase(test.TestCase): def test_no_vi_headers(self): - lines = ['Line 1\n', 'Line 2\n', 'Line 3\n', 'Line 4\n', 'Line 5\n' - 'Line 6\n', 'Line 7\n', 'Line 8\n', 'Line 9\n', 'Line 10\n'] + lines = ['Line 1\n', 'Line 2\n', 'Line 3\n', 'Line 4\n', 'Line 5\n', + 'Line 6\n', 'Line 7\n', 'Line 8\n', 'Line 9\n', 'Line 10\n', + 'Line 11\n'] - self.assertEqual(checks.no_vi_headers( - "Test string foo", 1, lines), None) - self.assertEqual(len(list(checks.no_vi_headers( + self.assertEqual(None, checks.no_vi_headers( + "Test string foo", 1, lines)) + self.assertEqual(2, len(list(checks.no_vi_headers( "# vim: et tabstop=4 shiftwidth=4 softtabstop=4", - 2, lines))), 2) - self.assertEqual(len(list(checks.no_vi_headers( + 2, lines)))) + self.assertEqual(2, len(list(checks.no_vi_headers( "# vim: et tabstop=4 shiftwidth=4 softtabstop=4", - 8, lines))), 2) - self.assertEqual(checks.no_vi_headers( + 8, lines)))) + self.assertEqual(None, checks.no_vi_headers( "Test end string for vi", - 9, lines), None) + 9, lines)) + # vim header outside of boundary (first/last 5 lines) + self.assertEqual(None, checks.no_vi_headers( + "# vim: et tabstop=4 shiftwidth=4 softtabstop=4", + 6, lines)) def test_no_translate_debug_logs(self): - self.assertEqual(len(list(checks.no_translate_debug_logs( - "LOG.debug(_('foo'))", "cinder/scheduler/foo.py"))), 1) + self.assertEqual(1, len(list(checks.no_translate_debug_logs( + "LOG.debug(_('foo'))", "cinder/scheduler/foo.py")))) - self.assertEqual(len(list(checks.no_translate_debug_logs( - "LOG.debug('foo')", "cinder/scheduler/foo.py"))), 0) + self.assertEqual(0, len(list(checks.no_translate_debug_logs( + "LOG.debug('foo')", "cinder/scheduler/foo.py")))) - self.assertEqual(len(list(checks.no_translate_debug_logs( - "LOG.info(_('foo'))", "cinder/scheduler/foo.py"))), 0) + self.assertEqual(0, len(list(checks.no_translate_debug_logs( + "LOG.info(_('foo'))", "cinder/scheduler/foo.py")))) def test_check_explicit_underscore_import(self): - self.assertEqual(len(list(checks.check_explicit_underscore_import( + self.assertEqual(1, len(list(checks.check_explicit_underscore_import( "LOG.info(_('My info message'))", - "cinder/tests/other_files.py"))), 1) - self.assertEqual(len(list(checks.check_explicit_underscore_import( + "cinder/tests/other_files.py")))) + self.assertEqual(1, len(list(checks.check_explicit_underscore_import( "msg = _('My message')", - "cinder/tests/other_files.py"))), 1) - self.assertEqual(len(list(checks.check_explicit_underscore_import( + "cinder/tests/other_files.py")))) + self.assertEqual(0, len(list(checks.check_explicit_underscore_import( "from cinder.i18n import _", - "cinder/tests/other_files.py"))), 0) - self.assertEqual(len(list(checks.check_explicit_underscore_import( + "cinder/tests/other_files.py")))) + self.assertEqual(0, len(list(checks.check_explicit_underscore_import( "LOG.info(_('My info message'))", - "cinder/tests/other_files.py"))), 0) - self.assertEqual(len(list(checks.check_explicit_underscore_import( + "cinder/tests/other_files.py")))) + self.assertEqual(0, len(list(checks.check_explicit_underscore_import( "msg = _('My message')", - "cinder/tests/other_files.py"))), 0) - self.assertEqual(len(list(checks.check_explicit_underscore_import( + "cinder/tests/other_files.py")))) + self.assertEqual(0, len(list(checks.check_explicit_underscore_import( "from cinder.i18n import _, _LW", - "cinder/tests/other_files2.py"))), 0) - self.assertEqual(len(list(checks.check_explicit_underscore_import( + "cinder/tests/other_files2.py")))) + self.assertEqual(0, len(list(checks.check_explicit_underscore_import( "msg = _('My message')", - "cinder/tests/other_files2.py"))), 0) - self.assertEqual(len(list(checks.check_explicit_underscore_import( + "cinder/tests/other_files2.py")))) + self.assertEqual(0, len(list(checks.check_explicit_underscore_import( "_ = translations.ugettext", - "cinder/tests/other_files3.py"))), 0) - self.assertEqual(len(list(checks.check_explicit_underscore_import( + "cinder/tests/other_files3.py")))) + self.assertEqual(0, len(list(checks.check_explicit_underscore_import( "msg = _('My message')", - "cinder/tests/other_files3.py"))), 0) + "cinder/tests/other_files3.py")))) + # Complete code coverage by falling through all checks + self.assertEqual(0, len(list(checks.check_explicit_underscore_import( + "LOG.info('My info message')", + "cinder/tests/other_files4.py")))) def test_check_no_log_audit(self): - self.assertEqual(len(list(checks.check_no_log_audit( - "LOG.audit('My test audit log')"))), 1) - self.assertEqual(len(list(checks.check_no_log_audit( - "LOG.info('My info test log.')"))), 0) + self.assertEqual(1, len(list(checks.check_no_log_audit( + "LOG.audit('My test audit log')")))) + self.assertEqual(0, len(list(checks.check_no_log_audit( + "LOG.info('My info test log.')")))) + + def test_no_mutable_default_args(self): + self.assertEqual(0, len(list(checks.no_mutable_default_args( + "def foo (bar):")))) + self.assertEqual(1, len(list(checks.no_mutable_default_args( + "def foo (bar=[]):")))) + self.assertEqual(1, len(list(checks.no_mutable_default_args( + "def foo (bar={}):")))) + + def test_check_assert_called_once(self): + self.assertEqual(0, len(list(checks.check_assert_called_once( + ".assert_called_with(", "cinder/tests/test1.py")))) + self.assertEqual(0, len(list(checks.check_assert_called_once( + ".assert_called_with(", "cinder/blah.py")))) + self.assertEqual(1, len(list(checks.check_assert_called_once( + ".assert_called_once(", "cinder/tests/test1.py")))) + self.assertEqual(0, len(list(checks.check_assert_called_once( + ".assertEqual(", "cinder/tests/test1.py")))) def test_oslo_namespace_imports_check(self): self.assertEqual(1, len(list(checks.check_oslo_namespace_imports( -- 2.45.2