Skip to content

Commit

Permalink
[Testing:Developer] Format utils tests (Submitty#10323)
Browse files Browse the repository at this point in the history
### 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?
<!-- List issue if it fixes/closes/implements one using the "Fixes
#<number>" or "Closes #<number>" syntax -->

- 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?
<!-- Is this a breaking change? -->
<!-- How did you test -->
- These changes branch off the changes from [this
PR](Submitty#10297)
  • Loading branch information
justin-sadakhom authored Apr 7, 2024
1 parent 95ea59d commit 56e404d
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 143 deletions.
171 changes: 66 additions & 105 deletions python_submitty_utils/tests/test_dateutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,86 +20,74 @@ 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:
dateutils.write_submitty_date(10)
self.assertEqual(
"Invalid type. Expected datetime or datetime string,"
" got <class 'int'>.",
str(cm.exception)
str(cm.exception),
)

def test_read_submitty_date(self):
Expand Down Expand Up @@ -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)
Expand All @@ -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 <class 'int'>",
str(cm.exception)
"Invalid type, expected str, got <class 'int'>", 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]):
Expand Down
26 changes: 14 additions & 12 deletions python_submitty_utils/tests/test_db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@
class TestDbUtils(TestCase):
def test_connection_string(self):
self.assertEqual(
'postgresql://test:[email protected]:1111/db_name',
"postgresql://test:[email protected]: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",
),
)
30 changes: 20 additions & 10 deletions python_submitty_utils/tests/test_string_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
14 changes: 6 additions & 8 deletions python_submitty_utils/tests/test_submitty_schema_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Loading

0 comments on commit 56e404d

Please sign in to comment.