From da192a7686506c26e69f512f0f77f101fc8f6676 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Fri, 17 Jan 2025 02:54:05 -0800 Subject: [PATCH] Convert gym unit test mock from Python str to HTML Convert the mock for gym unit tests from a Python string to an HTML file. This makes the mocking of gym unit tests more consistent with how unit tests for other modules are mocked. This also avoids unnecessarily mocking entire module functions, as only the HTTP requests made by those functions need to be mocked. Since the mock is only supposed to contain an HTML HTTP response, it makes more sense to keep it as HTML. --- tests/gym_test.py | 35 ++++++----- tests/mocks/gym_mocks.py | 121 --------------------------------------- tests/samples/gym.html | 119 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 134 deletions(-) delete mode 100644 tests/mocks/gym_mocks.py create mode 100644 tests/samples/gym.html diff --git a/tests/gym_test.py b/tests/gym_test.py index ac96630..e31f135 100644 --- a/tests/gym_test.py +++ b/tests/gym_test.py @@ -1,19 +1,22 @@ +from pathlib import Path import unittest import responses + from pittapi import gym -from tests.mocks.gym_mocks import mock_gym_html + +SAMPLE_PATH = Path() / "tests" / "samples" class GymTest(unittest.TestCase): def __init__(self, *args, **kwargs): unittest.TestCase.__init__(self, *args, **kwargs) - @responses.activate - def test_fetch_gym_info(self): - - responses.add(responses.GET, gym.GYM_URL, body=mock_gym_html, status=200) + with open(SAMPLE_PATH / "gym.html", "r") as f: + self.mock_gym_data = f.read() - gym_info = gym.get_all_gyms_info() + @responses.activate + def test_get_all_gyms_info(self): + responses.add(responses.GET, gym.GYM_URL, body=self.mock_gym_data, status=200) expected_info = [ gym.Gym(name="Baierl Rec Center", last_updated="07/09/2024 09:05 AM", current_count=100, percent_full=50), gym.Gym( @@ -35,35 +38,41 @@ def test_fetch_gym_info(self): gym.Gym(name="Pitt Sports Dome", last_updated="07/09/2024 09:05 AM", current_count=15, percent_full=20), ] + gym_info = gym.get_all_gyms_info() + self.assertEqual(gym_info, expected_info) @responses.activate def test_get_gym_info(self): - responses.add(responses.GET, gym.GYM_URL, body=mock_gym_html, status=200) - - gym_info = gym.get_gym_info("Baierl Rec Center") + responses.add(responses.GET, gym.GYM_URL, body=self.mock_gym_data, status=200) expected_info = gym.Gym( name="Baierl Rec Center", last_updated="07/09/2024 09:05 AM", current_count=100, percent_full=50 ) + + gym_info = gym.get_gym_info("Baierl Rec Center") + self.assertEqual(gym_info, expected_info) @responses.activate def test_invalid_gym_name(self): - responses.add(responses.GET, gym.GYM_URL, body=mock_gym_html, status=200) + responses.add(responses.GET, gym.GYM_URL, body=self.mock_gym_data, status=200) gym_info = gym.get_gym_info("Invalid Gym Name") + self.assertIsNone(gym_info) @responses.activate def test_valid_gym_name_not_all_info(self): - responses.add(responses.GET, gym.GYM_URL, body=mock_gym_html, status=200) + responses.add(responses.GET, gym.GYM_URL, body=self.mock_gym_data, status=200) gym_info = gym.get_gym_info("Bellefield Hall: Court & Dance Studio") + self.assertIsNone(gym_info) @responses.activate def test_percentage_value_error(self): - responses.add(responses.GET, gym.GYM_URL, body=mock_gym_html, status=200) + responses.add(responses.GET, gym.GYM_URL, body=self.mock_gym_data, status=200) + + gym_info = gym.get_gym_info("Bellefield Hall: Fitness Center & Weight Room") - gym_info = gym.get_gym_info("Bellefield Hall: Fitness Center & Weight Roomo") self.assertIsNone(gym_info) diff --git a/tests/mocks/gym_mocks.py b/tests/mocks/gym_mocks.py deleted file mode 100644 index 07a936a..0000000 --- a/tests/mocks/gym_mocks.py +++ /dev/null @@ -1,121 +0,0 @@ -mock_gym_html = """ -
- Baierl Rec Center -
- (Open) -
- Last Count: 100 -
- Updated: 07/09/2024 09:05 AM -
- 50% - - - -
-
-
- Bellefield Hall: Fitness Center & Weight Room -
- (Open) -
- Last Count: 50 -
- Updated: 07/09/2024 09:05 AM -
- non% - - - -
-
-
- Bellefield Hall: Court & Dance Studio -
- (Open) -
- - -
- 38% - - - -
-
-
- Trees Hall: Fitness Center -
- (Open) -
- Last Count: 70 -
- Updated: 07/09/2024 09:05 AM -
- 58% - - - -
-
-
- Trees Hall: Courts -
- (Open) -
- Last Count: 20 -
- Updated: 07/09/2024 09:05 AM -
- 33% - - - -
-
-
- Trees Hall: Racquetball Courts & Multipurpose Room -
- (Open) -
- Last Count: 10 -
- Updated: 07/09/2024 09:05 AM -
- 25% - - - -
-
-
- William Pitt Union -
- (Open) -
- Last Count: 25 -
- Updated: 07/09/2024 09:05 AM -
- 25% - - - -
-
-
- Pitt Sports Dome -
- (Open) -
- Last Count: 15 -
- Updated: 07/09/2024 09:05 AM -
- 20% - - - -
-
- """ diff --git a/tests/samples/gym.html b/tests/samples/gym.html new file mode 100644 index 0000000..680f68d --- /dev/null +++ b/tests/samples/gym.html @@ -0,0 +1,119 @@ +
+ Baierl Rec Center +
+ (Open) +
+ Last Count: 100 +
+ Updated: 07/09/2024 09:05 AM +
+ 50% + + + +
+
+
+ Bellefield Hall: Fitness Center & Weight Room +
+ (Open) +
+ Last Count: 50 +
+ Updated: 07/09/2024 09:05 AM +
+ non% + + + +
+
+
+ Bellefield Hall: Court & Dance Studio +
+ (Open) +
+ + +
+ 38% + + + +
+
+
+ Trees Hall: Fitness Center +
+ (Open) +
+ Last Count: 70 +
+ Updated: 07/09/2024 09:05 AM +
+ 58% + + + +
+
+
+ Trees Hall: Courts +
+ (Open) +
+ Last Count: 20 +
+ Updated: 07/09/2024 09:05 AM +
+ 33% + + + +
+
+
+ Trees Hall: Racquetball Courts & Multipurpose Room +
+ (Open) +
+ Last Count: 10 +
+ Updated: 07/09/2024 09:05 AM +
+ 25% + + + +
+
+
+ William Pitt Union +
+ (Open) +
+ Last Count: 25 +
+ Updated: 07/09/2024 09:05 AM +
+ 25% + + + +
+
+
+ Pitt Sports Dome +
+ (Open) +
+ Last Count: 15 +
+ Updated: 07/09/2024 09:05 AM +
+ 20% + + + +
+