From f975cb529f8609aad427dd315e8085a6060ef1b8 Mon Sep 17 00:00:00 2001 From: kbe Date: Fri, 25 Jul 2025 14:17:12 +0200 Subject: [PATCH] test: More coverage on methods. All tests are passed. --- TODO | 1 - test/test_session_config.py | 71 ++++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/TODO b/TODO index 0a3e3e1..ea90a69 100644 --- a/TODO +++ b/TODO @@ -32,6 +32,5 @@ No tests for concurrent booking scenarios -Test private methods like _make_request through their public interfaces or consider making them protected Add integration tests for the complete booking flow Improve edge case coverage in existing tests \ No newline at end of file diff --git a/test/test_session_config.py b/test/test_session_config.py index b3dc037..3b17971 100644 --- a/test/test_session_config.py +++ b/test/test_session_config.py @@ -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"]) \ No newline at end of file + pytest.main([__file__, "-v"])