mirror of
https://github.com/developersIndia/deviras.git
synced 2024-12-02 11:40:45 +05:30
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.
This commit is contained in:
parent
0ec5da9287
commit
981bd089b5
8
main.py
8
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
|
||||||
@ -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()
|
||||||
|
30
test_titles_updater.py
Normal file
30
test_titles_updater.py
Normal file
@ -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()
|
Loading…
Reference in New Issue
Block a user