mirror of
https://github.com/developersIndia/deviras.git
synced 2024-11-27 01:02:02 +05:30
Merge pull request #6 from Animesh-Ghosh/add-tests-with-mock
Add Tests for `get_titles` function
This commit is contained in:
commit
c9068a9315
@ -13,6 +13,13 @@
|
|||||||
|
|
||||||
![r/developersIndia About widget](https://user-images.githubusercontent.com/34342551/185678556-e4c911c9-fb12-49da-9ca6-8f8ce2ad9b5a.png)
|
![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
|
## Resources
|
||||||
|
|
||||||
|
10
main.py
10
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 praw
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
@ -10,7 +14,7 @@ reddit_pass = os.environ["REDDIT_PASSWORD"]
|
|||||||
|
|
||||||
def get_titles():
|
def get_titles():
|
||||||
with open('dataset.json', 'r') as f:
|
with open('dataset.json', 'r') as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
|
|
||||||
titles = data['titles']
|
titles = data['titles']
|
||||||
currentlyViewingText, subscribersText = random.sample(titles, 2)
|
currentlyViewingText, subscribersText = random.sample(titles, 2)
|
||||||
@ -38,5 +42,5 @@ def update_titles():
|
|||||||
widgets.refresh()
|
widgets.refresh()
|
||||||
widgets.id_card.mod.update(subscribersText=titles[1])
|
widgets.id_card.mod.update(subscribersText=titles[1])
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
update_titles()
|
update_titles()
|
||||||
|
28
test_get_titles.py
Normal file
28
test_get_titles.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
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()
|
||||||
|
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()
|
Loading…
Reference in New Issue
Block a user