From 981bd089b51c23618bdb788834b78be663e49738 Mon Sep 17 00:00:00 2001 From: Animesh-Ghosh Date: Sun, 21 Aug 2022 02:04:05 +0530 Subject: [PATCH 1/3] Add Tests for get_titles function Added docstring for main.py script and updated it to only invoke update_titles function if the file was run directly. Added initial tests for titles updater script. --- dataset.json | 2 +- main.py | 10 +++++++--- test_titles_updater.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 test_titles_updater.py diff --git a/dataset.json b/dataset.json index 0164533..4a7ea97 100644 --- a/dataset.json +++ b/dataset.json @@ -17,4 +17,4 @@ "releasing the MVP", "finding JIRA tickets" ] -} \ No newline at end of file +} diff --git a/main.py b/main.py index 9b04b1b..5a07e67 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,7 @@ +''' + This script is used for changing the text below total members & live + members count in the developersIndia subreddit +''' import praw import os import random @@ -10,7 +14,7 @@ reddit_pass = os.environ["REDDIT_PASSWORD"] def get_titles(): with open('dataset.json', 'r') as f: - data = json.load(f) + data = json.load(f) titles = data['titles'] currentlyViewingText, subscribersText = random.sample(titles, 2) @@ -38,5 +42,5 @@ def update_titles(): widgets.refresh() widgets.id_card.mod.update(subscribersText=titles[1]) - -update_titles() +if __name__ == '__main__': + update_titles() diff --git a/test_titles_updater.py b/test_titles_updater.py new file mode 100644 index 0000000..111ef99 --- /dev/null +++ b/test_titles_updater.py @@ -0,0 +1,30 @@ +import unittest +from unittest.mock import patch + +# patch out the environ dictionary +# used to instantiate global variables in the titles_updater script +# maybe move the call to os.environ in the script to the update_titles method? +environ_patcher = patch.dict('os.environ', { + 'REDDIT_CLIENT_ID': '', + 'REDDIT_CLIENT_SECRET': '', + 'REDDIT_PASSWORD': '' +}) +environ_patcher.start() + +from main import get_titles + +# TODO: write tests for update_titles method after figuring out +# which objects to mock and with what +class TestTitlesUpdater(unittest.TestCase): + @patch('main.json.load', return_value={'titles': ['foo', 'bar', 'baz']}) + def test_get_titles_returns_a_list(self, _): + titles = get_titles() + self.assertIsInstance(titles, list) + + @patch('main.json.load', return_value={'titles': ['foo', 'bar', 'baz']}) + def test_get_titles_contains_titles_from_dataset_file(self, mock_json_load): + titles = get_titles() + + self.assertTrue(all(map(lambda title: title in mock_json_load.return_value['titles'], titles))) + +environ_patcher.stop() From 04445c24c260ccf7b13c93f56debbe223f8100b1 Mon Sep 17 00:00:00 2001 From: Animesh-Ghosh Date: Sun, 21 Aug 2022 23:05:50 +0530 Subject: [PATCH 2/3] Update note for future contributors Updated note for contributors/reviewers. --- test_titles_updater.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test_titles_updater.py b/test_titles_updater.py index 111ef99..1f8114e 100644 --- a/test_titles_updater.py +++ b/test_titles_updater.py @@ -13,8 +13,7 @@ environ_patcher.start() from main import get_titles -# TODO: write tests for update_titles method after figuring out -# which objects to mock and with what +# NOTE: not sure about how exactly update_titles can be tested class TestTitlesUpdater(unittest.TestCase): @patch('main.json.load', return_value={'titles': ['foo', 'bar', 'baz']}) def test_get_titles_returns_a_list(self, _): From 38418a0336c22e175e6b707446282d3bdf01e6e1 Mon Sep 17 00:00:00 2001 From: Animesh-Ghosh Date: Wed, 31 Aug 2022 14:30:55 +0530 Subject: [PATCH 3/3] Update README with instructions to run tests Updated README with instructions to run the tests. Renamed test file and class. --- README.md | 7 +++++++ test_titles_updater.py => test_get_titles.py | 3 +-- 2 files changed, 8 insertions(+), 2 deletions(-) rename test_titles_updater.py => test_get_titles.py (89%) diff --git a/README.md b/README.md index 26ca621..61788d9 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,13 @@ ![r/developersIndia About widget](https://user-images.githubusercontent.com/34342551/185678556-e4c911c9-fb12-49da-9ca6-8f8ce2ad9b5a.png) +## Tests + +To run the tests, simply run the following command in the Python virtual environment: + +```bash +$ python -m unittest +``` ## Resources diff --git a/test_titles_updater.py b/test_get_titles.py similarity index 89% rename from test_titles_updater.py rename to test_get_titles.py index 1f8114e..89a417d 100644 --- a/test_titles_updater.py +++ b/test_get_titles.py @@ -13,8 +13,7 @@ environ_patcher.start() from main import get_titles -# NOTE: not sure about how exactly update_titles can be tested -class TestTitlesUpdater(unittest.TestCase): +class TestGetTitles(unittest.TestCase): @patch('main.json.load', return_value={'titles': ['foo', 'bar', 'baz']}) def test_get_titles_returns_a_list(self, _): titles = get_titles()