test: More coverage on methods. All tests are passed.
This commit is contained in:
@@ -105,5 +105,74 @@ class TestSessionConfig:
|
||||
assert sessions[1] == (0, "18:00", "")
|
||||
assert sessions[2] == (0, "00:00", "Test")
|
||||
|
||||
def test_load_preferred_sessions_partial_json(self):
|
||||
"""Test behavior when the config file contains partial JSON content"""
|
||||
# Create partial JSON content
|
||||
partial_json = '{"day_of_week": 1, "start_time": "08:00" ' # Missing closing brace
|
||||
|
||||
# Mock the open function to return partial JSON
|
||||
with patch('builtins.open', mock_open(read_data=partial_json)):
|
||||
with patch('logging.warning') as mock_warning:
|
||||
sessions = SessionConfig.load_preferred_sessions()
|
||||
|
||||
# Verify warning was logged
|
||||
mock_warning.assert_called_once()
|
||||
assert "decode" in mock_warning.call_args[0][0]
|
||||
|
||||
# Verify default sessions are returned
|
||||
assert len(sessions) == 3
|
||||
assert sessions[0] == (2, "18:30", "CONDITIONING")
|
||||
assert sessions[1] == (4, "17:00", "WEIGHTLIFTING")
|
||||
assert sessions[2] == (5, "12:30", "HYROX")
|
||||
|
||||
def test_load_preferred_sessions_incorrect_field_types(self):
|
||||
"""Test behavior when the config file contains JSON with incorrect field types"""
|
||||
# Create JSON with incorrect field types
|
||||
mock_content = json.dumps([
|
||||
{"day_of_week": "Monday", "start_time": "08:00", "session_name_contains": "Morning"},
|
||||
{"day_of_week": 1, "start_time": 800, "session_name_contains": "Morning"},
|
||||
])
|
||||
|
||||
# Mock the open function to return our mock file content
|
||||
with patch('builtins.open', mock_open(read_data=mock_content)):
|
||||
sessions = SessionConfig.load_preferred_sessions()
|
||||
|
||||
# Verify the returned sessions use the values as provided
|
||||
assert len(sessions) == 2
|
||||
assert sessions[0] == ("Monday", "08:00", "Morning")
|
||||
assert sessions[1] == (1, 800, "Morning")
|
||||
|
||||
def test_load_preferred_sessions_extra_fields(self):
|
||||
"""Test behavior when the config file contains JSON with extra fields"""
|
||||
# Create JSON with extra fields
|
||||
mock_content = json.dumps([
|
||||
{"day_of_week": 1, "start_time": "08:00", "session_name_contains": "Morning", "extra_field": "extra_value"},
|
||||
])
|
||||
|
||||
# Mock the open function to return our mock file content
|
||||
with patch('builtins.open', mock_open(read_data=mock_content)):
|
||||
sessions = SessionConfig.load_preferred_sessions()
|
||||
|
||||
# Verify the returned sessions ignore extra fields
|
||||
assert len(sessions) == 1
|
||||
assert sessions[0] == (1, "08:00", "Morning")
|
||||
|
||||
def test_load_preferred_sessions_duplicate_entries(self):
|
||||
"""Test behavior when the config file contains duplicate session entries"""
|
||||
# Create JSON with duplicate entries
|
||||
mock_content = json.dumps([
|
||||
{"day_of_week": 1, "start_time": "08:00", "session_name_contains": "Morning"},
|
||||
{"day_of_week": 1, "start_time": "08:00", "session_name_contains": "Morning"},
|
||||
])
|
||||
|
||||
# Mock the open function to return our mock file content
|
||||
with patch('builtins.open', mock_open(read_data=mock_content)):
|
||||
sessions = SessionConfig.load_preferred_sessions()
|
||||
|
||||
# Verify the returned sessions contain duplicates
|
||||
assert len(sessions) == 2
|
||||
assert sessions[0] == (1, "08:00", "Morning")
|
||||
assert sessions[1] == (1, "08:00", "Morning")
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
pytest.main([__file__, "-v"])
|
||||
|
||||
Reference in New Issue
Block a user