From 56e404da3561d2cbaf48563a153997b2b658b24f Mon Sep 17 00:00:00 2001 From: Justin Sadakhom <32400122+justin-sadakhom@users.noreply.github.com> Date: Sun, 7 Apr 2024 15:25:11 -0400 Subject: [PATCH] [Testing:Developer] Format utils tests (#10323) ### Please check if the PR fulfills these requirements: * [x] Tests for the changes have been added/updated (if possible) * [x] Documentation has been updated/added if relevant * [x] Screenshots are attached to Github PR if visual/UI changes were made ### What is the current behavior? - Util test files have inconsistent formatting ### What is the new behavior? - None of the test files are changed when running ~~the formatter~~ `black` formatter ### Other information? - These changes branch off the changes from [this PR](https://github.com/Submitty/Submitty/pull/10297) --- python_submitty_utils/tests/test_dateutils.py | 171 +++++++----------- python_submitty_utils/tests/test_db_utils.py | 26 +-- .../tests/test_string_utils.py | 30 ++- .../tests/test_submitty_schema_validator.py | 14 +- python_submitty_utils/tests/test_user.py | 24 ++- 5 files changed, 122 insertions(+), 143 deletions(-) diff --git a/python_submitty_utils/tests/test_dateutils.py b/python_submitty_utils/tests/test_dateutils.py index 7e567c8134b..48b5c0621c7 100644 --- a/python_submitty_utils/tests/test_dateutils.py +++ b/python_submitty_utils/tests/test_dateutils.py @@ -20,78 +20,66 @@ def test_get_current_time(self, mock_datetime): @patch( "submitty_utils.dateutils.get_current_time", - return_value=pytz_timezone('America/New_York').localize(datetime( - 2016, 10, 14, 22, 11, 32, 0 - )) + return_value=pytz_timezone("America/New_York").localize( + datetime(2016, 10, 14, 22, 11, 32, 0) + ), ) def test_write_submitty_date_default(self, current_time): date = dateutils.write_submitty_date() self.assertTrue(current_time.called) - self.assertEqual('2016-10-14 22:11:32-0400', date) + self.assertEqual("2016-10-14 22:11:32-0400", date) @patch( "submitty_utils.dateutils.get_timezone", - return_value=pytz_timezone('America/New_York') + return_value=pytz_timezone("America/New_York"), ) - def test_write_submitty_date(self, get_timezone): + def test_write_submitty_date(self, _get_timezone): testcases = ( ( - datetime(2020, 6, 12, 3, 21, 30, tzinfo=pytz_timezone('UTC')), - '2020-06-12 03:21:30+0000' - ), - ( - datetime(2020, 12, 25, 3, 21, 30, tzinfo=pytz_timezone('UTC')), - '2020-12-25 03:21:30+0000' + datetime(2020, 6, 12, 3, 21, 30, tzinfo=pytz_timezone("UTC")), + "2020-06-12 03:21:30+0000", ), ( - datetime(2020, 6, 12, 3, 21, 30, 123, tzinfo=pytz_timezone('UTC')), - '2020-06-12 03:21:30+0000' + datetime(2020, 12, 25, 3, 21, 30, tzinfo=pytz_timezone("UTC")), + "2020-12-25 03:21:30+0000", ), ( - datetime(2020, 6, 12, 3, 21, 30), - '2020-06-12 03:21:30-0400' + datetime(2020, 6, 12, 3, 21, 30, 123, tzinfo=pytz_timezone("UTC")), + "2020-06-12 03:21:30+0000", ), - ( - datetime(2020, 12, 12, 3, 21, 30), - '2020-12-12 03:21:30-0500' - ) + (datetime(2020, 6, 12, 3, 21, 30), "2020-06-12 03:21:30-0400"), + (datetime(2020, 12, 12, 3, 21, 30), "2020-12-12 03:21:30-0500"), ) for testcase in testcases: with self.subTest(i=testcase[0]): self.assertEqual( - testcase[1], - dateutils.write_submitty_date(testcase[0]) + testcase[1], dateutils.write_submitty_date(testcase[0]) ) - @patch( "submitty_utils.dateutils.get_timezone", - return_value=pytz_timezone('America/New_York') + return_value=pytz_timezone("America/New_York"), ) - def test_write_submitty_date_microseconds(self, get_timezone): + def test_write_submitty_date_microseconds(self, _get_timezone): testcases = ( ( - datetime(2020, 6, 12, 3, 21, 30, tzinfo=pytz_timezone('UTC')), - '2020-06-12 03:21:30.000+0000' - ), - ( - datetime(2020, 6, 12, 3, 21, 30, 123500, tzinfo=pytz_timezone('UTC')), - '2020-06-12 03:21:30.123+0000' + datetime(2020, 6, 12, 3, 21, 30, tzinfo=pytz_timezone("UTC")), + "2020-06-12 03:21:30.000+0000", ), ( - datetime(2020, 6, 12, 3, 21, 30, 211500), - '2020-06-12 03:21:30.211-0400' + datetime(2020, 6, 12, 3, 21, 30, 123500, tzinfo=pytz_timezone("UTC")), + "2020-06-12 03:21:30.123+0000", ), + (datetime(2020, 6, 12, 3, 21, 30, 211500), "2020-06-12 03:21:30.211-0400"), ) for testcase in testcases: with self.subTest(i=testcase[0]): self.assertEqual( - testcase[1], - dateutils.write_submitty_date(testcase[0], True) + testcase[1], dateutils.write_submitty_date(testcase[0], True) ) def test_invalid_type_write_submitty_date(self): - testcases = ('2020-06-12 03:21:30.123+0000', 10) + testcases = ("2020-06-12 03:21:30.123+0000", 10) for testcase in testcases: with self.subTest(testcase): with self.assertRaises(TypeError) as cm: @@ -99,7 +87,7 @@ def test_invalid_type_write_submitty_date(self): self.assertEqual( "Invalid type. Expected datetime or datetime string," " got .", - str(cm.exception) + str(cm.exception), ) def test_read_submitty_date(self): @@ -188,94 +176,69 @@ def test_read_submitty_date_bad_format(self): @patch( "submitty_utils.dateutils.get_current_time", - return_value=pytz_timezone('America/New_York').localize(datetime( - 2016, 10, 14, 22, 11, 32, 0 - )) + return_value=pytz_timezone("America/New_York").localize( + datetime(2016, 10, 14, 22, 11, 32, 0) + ), ) @patch( "submitty_utils.dateutils.get_timezone", - return_value=pytz_timezone('America/New_York') + return_value=pytz_timezone("America/New_York"), ) - def test_parse_datetime(self, current_time, get_timezone): + def test_parse_datetime(self, _current_time, _get_timezone): testcases = ( ( - '2016-10-14 22:11:32+0200', - datetime( - 2016, 10, 14, 22, 11, 32, 0, timezone(timedelta(hours=2)) - ) + "2016-10-14 22:11:32+0200", + datetime(2016, 10, 14, 22, 11, 32, 0, timezone(timedelta(hours=2))), ), ( - '2016-10-14 22:11:32', - datetime( - 2016, 10, 14, 22, 11, 32, 0, timezone(timedelta(hours=-4)) - ) + "2016-10-14 22:11:32", + datetime(2016, 10, 14, 22, 11, 32, 0, timezone(timedelta(hours=-4))), ), ( - '2016-10-14', - datetime( - 2016, 10, 14, 23, 59, 59, 0, timezone(timedelta(hours=-4)) - ) + "2016-10-14", + datetime(2016, 10, 14, 23, 59, 59, 0, timezone(timedelta(hours=-4))), ), ( - '+1 days', - datetime( - 2016, 10, 15, 23, 59, 59, 0, timezone(timedelta(hours=-4)) - ) + "+1 days", + datetime(2016, 10, 15, 23, 59, 59, 0, timezone(timedelta(hours=-4))), ), ( - '+3 day', - datetime( - 2016, 10, 17, 23, 59, 59, 0, timezone(timedelta(hours=-4)) - ) + "+3 day", + datetime(2016, 10, 17, 23, 59, 59, 0, timezone(timedelta(hours=-4))), ), ( - '+0 days', - datetime( - 2016, 10, 14, 23, 59, 59, 0, timezone(timedelta(hours=-4)) - ) + "+0 days", + datetime(2016, 10, 14, 23, 59, 59, 0, timezone(timedelta(hours=-4))), ), ( - '-1 days', - datetime( - 2016, 10, 13, 23, 59, 59, 0, timezone(timedelta(hours=-4)) - ) + "-1 days", + datetime(2016, 10, 13, 23, 59, 59, 0, timezone(timedelta(hours=-4))), ), ( - '-10 day', - datetime( - 2016, 10, 4, 23, 59, 59, 0, timezone(timedelta(hours=-4)) - ) + "-10 day", + datetime(2016, 10, 4, 23, 59, 59, 0, timezone(timedelta(hours=-4))), ), ( - '+1 day at 10:30:00', - datetime( - 2016, 10, 15, 10, 30, 0, 0, timezone(timedelta(hours=-4)) - ) + "+1 day at 10:30:00", + datetime(2016, 10, 15, 10, 30, 0, 0, timezone(timedelta(hours=-4))), ), ( - datetime( - 2016, 10, 4, 23, 59, 59, 0, timezone(timedelta(hours=+1)) - ), - datetime( - 2016, 10, 4, 23, 59, 59, 0, timezone(timedelta(hours=+1)) - ) + datetime(2016, 10, 4, 23, 59, 59, 0, timezone(timedelta(hours=+1))), + datetime(2016, 10, 4, 23, 59, 59, 0, timezone(timedelta(hours=+1))), ), ( - datetime( - 2016, 10, 4, 23, 59, 59, 0 - ), - datetime( - 2016, 10, 4, 23, 59, 59, 0, timezone(timedelta(hours=-4)) - ) - ) + datetime(2016, 10, 4, 23, 59, 59, 0), + datetime(2016, 10, 4, 23, 59, 59, 0, timezone(timedelta(hours=-4))), + ), ) for testcase in testcases: with self.subTest(str(testcase[0])): - self.assertEqual( - testcase[1], - dateutils.parse_datetime(testcase[0]) - ) + self.assertEqual(testcase[1], dateutils.parse_datetime(testcase[0])) + + def test_parse_datetime_none(self): + parsed_date = dateutils.parse_datetime(None) + self.assertIsNone(parsed_date) def test_parse_datetime_none(self): parsed_date = dateutils.parse_datetime(None) @@ -285,26 +248,24 @@ def test_parse_datetime_invalid_type(self): with self.assertRaises(TypeError) as cm: dateutils.parse_datetime(10) self.assertEqual( - "Invalid type, expected str, got ", - str(cm.exception) + "Invalid type, expected str, got ", str(cm.exception) ) def test_parse_datetime_invalid_format(self): with self.assertRaises(ValueError) as cm: - dateutils.parse_datetime('invalid datetime') + dateutils.parse_datetime("invalid datetime") self.assertEqual( - 'Invalid string for date parsing: invalid datetime', - str(cm.exception) + "Invalid string for date parsing: invalid datetime", str(cm.exception) ) - @patch('submitty_utils.dateutils.datetime') + @patch("submitty_utils.dateutils.datetime") def test_get_semester(self, mock): testcases = ( - (datetime(year=2021, month=1, day=1), 's21'), - (datetime(year=2020, month=6, day=22), 's20'), - (datetime(year=2019, month=7, day=1), 'f19'), - (datetime(year=2020, month=12, day=22), 'f20'), + (datetime(year=2021, month=1, day=1), "s21"), + (datetime(year=2020, month=6, day=22), "s20"), + (datetime(year=2019, month=7, day=1), "f19"), + (datetime(year=2020, month=12, day=22), "f20"), ) for testcase in testcases: with self.subTest(testcase[1]): diff --git a/python_submitty_utils/tests/test_db_utils.py b/python_submitty_utils/tests/test_db_utils.py index 6e305c20d42..a18a405dac0 100644 --- a/python_submitty_utils/tests/test_db_utils.py +++ b/python_submitty_utils/tests/test_db_utils.py @@ -6,22 +6,24 @@ class TestDbUtils(TestCase): def test_connection_string(self): self.assertEqual( - 'postgresql://test:my_pass@127.0.0.1:1111/db_name', + "postgresql://test:my_pass@127.0.0.1:1111/db_name", db_utils.generate_connect_string( - '127.0.0.1', + "127.0.0.1", 1111, - 'db_name', - 'test', - 'my_pass', - )) + "db_name", + "test", + "my_pass", + ), + ) def test_connection_string_dir(self): self.assertEqual( - 'postgresql://test:my_pass@/db_name?host=/var/run/postgresql', + "postgresql://test:my_pass@/db_name?host=/var/run/postgresql", db_utils.generate_connect_string( - '/var/run/postgresql', + "/var/run/postgresql", 5432, - 'db_name', - 'test', - 'my_pass', - )) + "db_name", + "test", + "my_pass", + ), + ) diff --git a/python_submitty_utils/tests/test_string_utils.py b/python_submitty_utils/tests/test_string_utils.py index f1d3eb9d9da..1aef83f574d 100644 --- a/python_submitty_utils/tests/test_string_utils.py +++ b/python_submitty_utils/tests/test_string_utils.py @@ -4,21 +4,31 @@ class TestUser(unittest.TestCase): - def testNegativeLength(self): - self.assertEqual(string_utils.generate_random_string(-1), '') + def test_negative_length(self): + self.assertEqual(string_utils.generate_random_string(-1), "") - def testZeroLength(self): - self.assertEqual(string_utils.generate_random_string(0), '') + def test_zero_length(self): + self.assertEqual(string_utils.generate_random_string(0), "") - def testPositiveLength(self): + def test_positive_length(self): self.assertEqual(len(string_utils.generate_random_string(1)), 1) - def testRandom(self): + def test_random(self): # Very low chance of generating the same string twice. for _ in range(10): - self.assertNotEqual(string_utils.generate_random_string(10), string_utils.generate_random_string(10)) - self.assertNotEqual(string_utils.generate_random_string(100), string_utils.generate_random_string(100)) - self.assertNotEqual(string_utils.generate_random_string(1000), string_utils.generate_random_string(1000)) + self.assertNotEqual( + string_utils.generate_random_string(10), + string_utils.generate_random_string(10), + ) + self.assertNotEqual( + string_utils.generate_random_string(100), + string_utils.generate_random_string(100), + ) + self.assertNotEqual( + string_utils.generate_random_string(1000), + string_utils.generate_random_string(1000), + ) -if __name__ == '__main__': + +if __name__ == "__main__": unittest.main() diff --git a/python_submitty_utils/tests/test_submitty_schema_validator.py b/python_submitty_utils/tests/test_submitty_schema_validator.py index 3ba3e35fa76..3b26ade25d1 100644 --- a/python_submitty_utils/tests/test_submitty_schema_validator.py +++ b/python_submitty_utils/tests/test_submitty_schema_validator.py @@ -7,11 +7,11 @@ SCHEMA_PATH = Path( Path(__file__).resolve().parent.parent.parent, - 'bin', - 'json_schemas', - 'complete_config_schema.json' + "bin", + "json_schemas", + "complete_config_schema.json", ) -CONFIGS_DIR = Path(__file__).resolve().parent / 'data' / 'complete_configs' +CONFIGS_DIR = Path(__file__).resolve().parent / "data" / "complete_configs" class TestSubmittySchemaValidator(TestCase): @@ -25,9 +25,7 @@ def tearDown(self): def test_valid_schemas(self): for entry in CONFIGS_DIR.iterdir(): - with self.subTest(f'Validating {entry.name}'): + with self.subTest(f"Validating {entry.name}"): validator.validate_complete_config_schema_using_filenames( - str(entry), - str(SCHEMA_PATH) + str(entry), str(SCHEMA_PATH) ) - self.assertTrue(True) diff --git a/python_submitty_utils/tests/test_user.py b/python_submitty_utils/tests/test_user.py index 9a28bb7be08..e05ad2c5cdc 100644 --- a/python_submitty_utils/tests/test_user.py +++ b/python_submitty_utils/tests/test_user.py @@ -5,15 +5,23 @@ class TestUser(unittest.TestCase): - def testGetPhpPassword(self): - password = user.get_php_db_password('password') + def test_get_php_password(self): + password = user.get_php_db_password("password") self.assertTrue(len(password) > 0) - proc = subprocess.Popen( - ["php", "-r", "print(password_verify('{0}', password_hash('{0}', PASSWORD_DEFAULT)));".format(password)], - stdout=subprocess.PIPE, stderr=subprocess.PIPE) - (out, _) = proc.communicate() - self.assertEqual('1', out.decode('utf-8')) + with subprocess.Popen( + [ + "php", + "-r", + f"print(password_verify('{0}', password_hash('{0}', PASSWORD_DEFAULT)));".format( + password + ), + ], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) as proc: + (out, _) = proc.communicate() + self.assertEqual("1", out.decode("utf-8")) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main()