From 263cf5e6c33d319bc3a0797ae72259fadde7c6ed Mon Sep 17 00:00:00 2001 From: William Lam Date: Wed, 12 May 2021 14:43:29 -0400 Subject: [PATCH] Version 0.2.4.0 Release - reward codes are now automatically sent (#10) # Version 0.2.4.0 Release - Reward codes are now automatically sent - Improved mock test cases with test_core.py - Test coverage improved from 87% to 92%. - Moved `core` object instantiation outside of the while loop in `main.py` - Fixed a calculation error with sleep time when encountering a Reddit API Exception in `send_message_to_winners` --- .gitignore | 2 + README.md | 3 +- hon_patch_notes_game_bot/__init__.py | 2 +- .../cache/reward_codes.txt | 5 ++ hon_patch_notes_game_bot/communications.py | 35 ++++++++++--- hon_patch_notes_game_bot/config/config.py | 1 + hon_patch_notes_game_bot/core.py | 9 +++- hon_patch_notes_game_bot/main.py | 16 +++--- hon_patch_notes_game_bot/utils.py | 16 ++++++ poetry.lock | 51 ++++++++++--------- pyproject.toml | 2 +- tests/cache/reward_codes.txt | 5 ++ tests/test_communications.py | 9 +++- tests/test_core.py | 25 +++++++-- tests/test_utils.py | 3 ++ 15 files changed, 134 insertions(+), 50 deletions(-) create mode 100644 hon_patch_notes_game_bot/cache/reward_codes.txt create mode 100644 tests/cache/reward_codes.txt diff --git a/.gitignore b/.gitignore index 075d156..72c4576 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,8 @@ *.json winners_list.txt patch_notes.txt +reward_codes.txt +cache/ # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/README.md b/README.md index 03c8a0d..4ea3c6f 100644 --- a/README.md +++ b/README.md @@ -85,12 +85,13 @@ Navigate to the project root directory in your terminal. - To run the script, use `./scripts.sh start` - To run unit tests, use `./scripts.sh test` - To reset the cache & database, use `./scripts.sh reset` before running `./scripts.sh start` -- To pick a list of winners from the existing local database, use `./scripts.sh winners 20`, where the "20" can be replaced with an integer to specify the number of winners picked ## More Usage Notes For unreleased patch notes, **do not commit and push the branch to source control before their official release**. +For `hon_patch_notes_game_bot/cache/reward_codes.txt`, populate the file manually (this is done via private means). + To change up some commonly changed configuration before starting a new run, go to `hon_patch_notes_game_bot/config/config.py` and edit them. ## Extra Documentation diff --git a/hon_patch_notes_game_bot/__init__.py b/hon_patch_notes_game_bot/__init__.py index d10b9e2..dd386d7 100644 --- a/hon_patch_notes_game_bot/__init__.py +++ b/hon_patch_notes_game_bot/__init__.py @@ -1 +1 @@ -__version__ = "0.2.3.1" +__version__ = "0.2.4.0" diff --git a/hon_patch_notes_game_bot/cache/reward_codes.txt b/hon_patch_notes_game_bot/cache/reward_codes.txt new file mode 100644 index 0000000..5f53782 --- /dev/null +++ b/hon_patch_notes_game_bot/cache/reward_codes.txt @@ -0,0 +1,5 @@ +REWARD_CODE_1 +REWARD_CODE_2 +REWARD_CODE_3 +REWARD_CODE_4 +REWARD_CODE_5 \ No newline at end of file diff --git a/hon_patch_notes_game_bot/communications.py b/hon_patch_notes_game_bot/communications.py index 9af537e..0f976f9 100644 --- a/hon_patch_notes_game_bot/communications.py +++ b/hon_patch_notes_game_bot/communications.py @@ -129,8 +129,12 @@ def send_message_to_staff( continue -def send_message_to_winners( - reddit: Reddit, winners_list: List[str], version_string: str, gold_coin_reward: int +def send_message_to_winners( # noqa: C901 + reddit: Reddit, + winners_list: List[str], + reward_codes_list: List[str], + version_string: str, + gold_coin_reward: int, ): """ Sends the winners list results to a list of recipients via Private Message (PM). @@ -150,6 +154,7 @@ def send_message_to_winners( Attributes: reddit: the PRAW Reddit instance winners_list: a list of winning recipients for the PM + reward_codes_list: a list of reward codes for each winner version_string: the version of the patch notes gold_coin_reward: the number of Gold Coins intended for the reward """ @@ -159,17 +164,27 @@ def send_message_to_winners( failed_recipients_list = [] for recipient in winners_list: + reward_code = ( + "N/A - all possible reward codes have been used up.\n\n" + f"Please contact {STAFF_MEMBER_THAT_HANDS_OUT_REWARDS} for a code to be issued manually." + ) + if len(reward_codes_list) > 0: + reward_code = reward_codes_list[0] + message = ( f"Congratulations {recipient}!\n\n" f"You have been chosen by the bot as a winner for the {version_string} Patch Notes Guessing Game!\n\n" - f"Please send /u/{STAFF_MEMBER_THAT_HANDS_OUT_REWARDS} a Private Message (PM) to request a code" - f" for {str(gold_coin_reward)} Gold Coins.\n\n" - "Be sure to check your Reddit Chat inbox as well (not just your Reddit mail inbox)!\n\n" + f"Your reward code for {str(gold_coin_reward)} Gold Coins is: **{reward_code}**.\n\n" + "You can redeem your reward code here: https://www.heroesofnewerth.com/redeem/\n\n" "Thank you for participating in the game! =)" ) try: reddit.redditor(recipient).message(subject=subject_line, message=message) - print(f"Winner message sent to {recipient}") + print(f"Winner message sent to {recipient}, with code: {reward_code}") + + # Pop reward code from list only if the message was sent successfully + if len(reward_codes_list) > 0: + reward_codes_list.pop(0) # Reddit API Exception except RedditAPIException as redditException: @@ -198,7 +213,7 @@ def send_message_to_winners( else: # Use named groups from regex capture and assign them to a dictionary sleep_time_regex_groups = regex_capture.groupdict(default=0) - secondsToSleep = int( + secondsToSleep = 60 * int( sleep_time_regex_groups.get("minutes") # type: ignore ) + int( sleep_time_regex_groups.get("seconds") # type: ignore @@ -223,5 +238,9 @@ def send_message_to_winners( failed_recipients = len(failed_recipients_list) if failed_recipients > 0 and failed_recipients < len(winners_list): send_message_to_winners( - reddit, failed_recipients_list, version_string, gold_coin_reward + reddit, + failed_recipients_list, + reward_codes_list, + version_string, + gold_coin_reward, ) diff --git a/hon_patch_notes_game_bot/config/config.py b/hon_patch_notes_game_bot/config/config.py index fbcc272..7ac531b 100644 --- a/hon_patch_notes_game_bot/config/config.py +++ b/hon_patch_notes_game_bot/config/config.py @@ -37,6 +37,7 @@ SUBMISSION_CONTENT_PATH: str = "config/submission_content.md" COMMUNITY_SUBMISSION_CONTENT_PATH: str = "config/community_patch_notes_compilation.md" WINNERS_LIST_FILE_PATH: str = "cache/winners_list.txt" +REWARD_CODES_FILE_PATH: str = "cache/reward_codes.txt" BLANK_LINE_REPLACEMENT: str = "..." # ================ diff --git a/hon_patch_notes_game_bot/core.py b/hon_patch_notes_game_bot/core.py index e4b59d8..cc8c73e 100644 --- a/hon_patch_notes_game_bot/core.py +++ b/hon_patch_notes_game_bot/core.py @@ -22,6 +22,7 @@ from hon_patch_notes_game_bot.utils import ( get_patch_notes_line_number, generate_submission_compiled_patch_notes_template_line, + get_reward_codes_list, is_game_expired, output_winners_list_to_file, ) @@ -39,6 +40,7 @@ NUM_WINNERS, STAFF_RECIPIENTS_LIST, WINNERS_LIST_FILE_PATH, + REWARD_CODES_FILE_PATH, ) @@ -60,6 +62,8 @@ def __init__( self.submission = submission self.community_submission = community_submission self.patch_notes_file = patch_notes_file + self.reward_codes_filepath = REWARD_CODES_FILE_PATH + self.game_end_time = GAME_END_TIME def has_exceeded_revealed_line_count(self) -> bool: """ @@ -309,6 +313,7 @@ def perform_post_game_actions(self): send_message_to_winners( reddit=self.reddit, winners_list=winners_list, + reward_codes_list=get_reward_codes_list(self.reward_codes_filepath), version_string=version_string, gold_coin_reward=GOLD_COIN_REWARD, ) @@ -488,7 +493,7 @@ def loop(self): # noqa: C901 # Check unread replies try: # Stop indefinite loop if current time is greater than the closing time. - if is_game_expired(GAME_END_TIME): + if is_game_expired(self.game_end_time): print("Reddit Bot script ended via time deadline") return False @@ -523,7 +528,7 @@ def loop(self): # noqa: C901 return False # Stop indefinite loop if current time is greater than the closing time. - if is_game_expired(GAME_END_TIME): + if is_game_expired(self.game_end_time): print("Reddit Bot script ended via time deadline") return False diff --git a/hon_patch_notes_game_bot/main.py b/hon_patch_notes_game_bot/main.py index 19c207c..092adaa 100644 --- a/hon_patch_notes_game_bot/main.py +++ b/hon_patch_notes_game_bot/main.py @@ -41,18 +41,20 @@ def main(): COMMUNITY_SUBMISSION_CONTENT_PATH, ) + # Create core object + core = Core( + reddit=reddit, + db=database, + submission=submission, + community_submission=community_submission, + patch_notes_file=patch_notes_file, + ) + # =============================================================== # Core loop to listen to unread comment messages on Reddit # =============================================================== print("Reddit Bot's core loop started") while 1: - core = Core( - reddit=reddit, - db=database, - submission=submission, - community_submission=community_submission, - patch_notes_file=patch_notes_file, - ) if not core.loop(): print("Reddit Bot script ended via core loop end conditions") break diff --git a/hon_patch_notes_game_bot/utils.py b/hon_patch_notes_game_bot/utils.py index 3941cdb..2f4abc6 100644 --- a/hon_patch_notes_game_bot/utils.py +++ b/hon_patch_notes_game_bot/utils.py @@ -15,6 +15,7 @@ MAX_NUM_GUESSES, MAX_PERCENT_OF_LINES_REVEALED, NUM_WINNERS, + REWARD_CODES_FILE_PATH, ) @@ -164,3 +165,18 @@ def processed_community_notes_thread_submission_content( submission_content += f"\n\n**Guesses in this thread will not be responded to by the bot. [Visit the main thread instead!]({main_submission_url})**\n\nFeel free to discuss patch changes here liberally (based on the currently revealed notes)! :)" # noqa: E501 return submission_content + + +def get_reward_codes_list( + reward_codes_filepath: str = REWARD_CODES_FILE_PATH, +) -> List[str]: + """ + Retrieves the reward codes from `REWARD_CODES_FILE_PATH` as a list of strings. + + Returns: + - A list of reward code strings + """ + with open(reward_codes_filepath, "r") as file: + reward_codes_file_content = file.readlines() + reward_codes_list = [line.rstrip() for line in reward_codes_file_content] + return reward_codes_list diff --git a/poetry.lock b/poetry.lock index 02c01bf..45ecc4e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -16,17 +16,17 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" [[package]] name = "attrs" -version = "20.3.0" +version = "21.2.0" description = "Classes Without Boilerplate" category = "dev" optional = false -python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" [package.extras] -dev = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "zope.interface", "furo", "sphinx", "pre-commit"] -docs = ["furo", "sphinx", "zope.interface"] -tests = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "zope.interface"] -tests_no_zope = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (>=4.3.0)", "six"] +dev = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "mypy", "pytest-mypy-plugins", "zope.interface", "furo", "sphinx", "sphinx-notfound-page", "pre-commit"] +docs = ["furo", "sphinx", "zope.interface", "sphinx-notfound-page"] +tests = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "mypy", "pytest-mypy-plugins", "zope.interface"] +tests_no_zope = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "mypy", "pytest-mypy-plugins"] [[package]] name = "black" @@ -66,11 +66,14 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" [[package]] name = "click" -version = "7.1.2" +version = "8.0.0" description = "Composable command line interface toolkit" category = "dev" optional = false -python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +python-versions = ">=3.6" + +[package.dependencies] +colorama = {version = "*", markers = "platform_system == \"Windows\""} [[package]] name = "colorama" @@ -93,7 +96,7 @@ toml = ["toml"] [[package]] name = "flake8" -version = "3.9.1" +version = "3.9.2" description = "the modular source code checker: pep8 pyflakes and co" category = "dev" optional = false @@ -273,7 +276,7 @@ python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*" [[package]] name = "pytest" -version = "6.2.3" +version = "6.2.4" description = "pytest: simple powerful testing with Python" category = "dev" optional = false @@ -347,7 +350,7 @@ socks = ["PySocks (>=1.5.6,!=1.5.7)", "win-inet-pton"] [[package]] name = "six" -version = "1.15.0" +version = "1.16.0" description = "Python 2 and 3 compatibility utilities" category = "main" optional = false @@ -416,7 +419,7 @@ brotli = ["brotlipy (>=0.6.0)"] [[package]] name = "websocket-client" -version = "0.58.0" +version = "0.59.0" description = "WebSocket client for Python with low level API options" category = "main" optional = false @@ -452,8 +455,8 @@ atomicwrites = [ {file = "atomicwrites-1.4.0.tar.gz", hash = "sha256:ae70396ad1a434f9c7046fd2dd196fc04b12f9e91ffb859164193be8b6168a7a"}, ] attrs = [ - {file = "attrs-20.3.0-py2.py3-none-any.whl", hash = "sha256:31b2eced602aa8423c2aea9c76a724617ed67cf9513173fd3a4f03e3a929c7e6"}, - {file = "attrs-20.3.0.tar.gz", hash = "sha256:832aa3cde19744e49938b91fea06d69ecb9e649c93ba974535d08ad92164f700"}, + {file = "attrs-21.2.0-py2.py3-none-any.whl", hash = "sha256:149e90d6d8ac20db7a955ad60cf0e6881a3f20d37096140088356da6c716b0b1"}, + {file = "attrs-21.2.0.tar.gz", hash = "sha256:ef6aaac3ca6cd92904cdd0d83f629a15f18053ec84e6432106f7a4d04ae4f5fb"}, ] black = [ {file = "black-19.10b0-py36-none-any.whl", hash = "sha256:1b30e59be925fafc1ee4565e5e08abef6b03fe455102883820fe5ee2e4734e0b"}, @@ -468,8 +471,8 @@ chardet = [ {file = "chardet-4.0.0.tar.gz", hash = "sha256:0d6f53a15db4120f2b08c94f11e7d93d2c911ee118b6b30a04ec3ee8310179fa"}, ] click = [ - {file = "click-7.1.2-py2.py3-none-any.whl", hash = "sha256:dacca89f4bfadd5de3d7489b7c8a566eee0d3676333fbb50030263894c38c0dc"}, - {file = "click-7.1.2.tar.gz", hash = "sha256:d2b5255c7c6349bc1bd1e59e08cd12acbbd63ce649f2588755783aa94dfb6b1a"}, + {file = "click-8.0.0-py3-none-any.whl", hash = "sha256:e90e62ced43dc8105fb9a26d62f0d9340b5c8db053a814e25d95c19873ae87db"}, + {file = "click-8.0.0.tar.gz", hash = "sha256:7d8c289ee437bcb0316820ccee14aefcb056e58d31830ecab8e47eda6540e136"}, ] colorama = [ {file = "colorama-0.4.4-py2.py3-none-any.whl", hash = "sha256:9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2"}, @@ -530,8 +533,8 @@ coverage = [ {file = "coverage-5.5.tar.gz", hash = "sha256:ebe78fe9a0e874362175b02371bdfbee64d8edc42a044253ddf4ee7d3c15212c"}, ] flake8 = [ - {file = "flake8-3.9.1-py2.py3-none-any.whl", hash = "sha256:3b9f848952dddccf635be78098ca75010f073bfe14d2c6bda867154bea728d2a"}, - {file = "flake8-3.9.1.tar.gz", hash = "sha256:1aa8990be1e689d96c745c5682b687ea49f2e05a443aff1f8251092b0014e378"}, + {file = "flake8-3.9.2-py2.py3-none-any.whl", hash = "sha256:bf8fd333346d844f616e8d47905ef3a3384edae6b4e9beb0c5101e25e3110907"}, + {file = "flake8-3.9.2.tar.gz", hash = "sha256:07528381786f2a6237b061f6e96610a4167b226cb926e2aa2b6b1d78057c576b"}, ] idna = [ {file = "idna-2.10-py2.py3-none-any.whl", hash = "sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0"}, @@ -614,8 +617,8 @@ pyparsing = [ {file = "pyparsing-2.4.7.tar.gz", hash = "sha256:c203ec8783bf771a155b207279b9bccb8dea02d8f0c9e5f8ead507bc3246ecc1"}, ] pytest = [ - {file = "pytest-6.2.3-py3-none-any.whl", hash = "sha256:6ad9c7bdf517a808242b998ac20063c41532a570d088d77eec1ee12b0b5574bc"}, - {file = "pytest-6.2.3.tar.gz", hash = "sha256:671238a46e4df0f3498d1c3270e5deb9b32d25134c99b7d75370a68cfbe9b634"}, + {file = "pytest-6.2.4-py3-none-any.whl", hash = "sha256:91ef2131a9bd6be8f76f1f08eac5c5317221d6ad1e143ae03894b862e8976890"}, + {file = "pytest-6.2.4.tar.gz", hash = "sha256:50bcad0a0b9c5a72c8e4e7c9855a3ad496ca6a881a3641b4260605450772c54b"}, ] pytest-cov = [ {file = "pytest-cov-2.11.1.tar.gz", hash = "sha256:359952d9d39b9f822d9d29324483e7ba04a3a17dd7d05aa6beb7ea01e359e5f7"}, @@ -673,8 +676,8 @@ requests = [ {file = "requests-2.25.1.tar.gz", hash = "sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804"}, ] six = [ - {file = "six-1.15.0-py2.py3-none-any.whl", hash = "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced"}, - {file = "six-1.15.0.tar.gz", hash = "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259"}, + {file = "six-1.16.0-py2.py3-none-any.whl", hash = "sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254"}, + {file = "six-1.16.0.tar.gz", hash = "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926"}, ] tinydb = [ {file = "tinydb-4.4.0-py3-none-any.whl", hash = "sha256:30b0f718ebb288e42d2f69f3e1b18928739f25153e6b5308a234e95c1673de71"}, @@ -730,8 +733,8 @@ urllib3 = [ {file = "urllib3-1.26.4.tar.gz", hash = "sha256:e7b021f7241115872f92f43c6508082facffbd1c048e3c6e2bb9c2a157e28937"}, ] websocket-client = [ - {file = "websocket_client-0.58.0-py2.py3-none-any.whl", hash = "sha256:44b5df8f08c74c3d82d28100fdc81f4536809ce98a17f0757557813275fbb663"}, - {file = "websocket_client-0.58.0.tar.gz", hash = "sha256:63509b41d158ae5b7f67eb4ad20fecbb4eee99434e73e140354dc3ff8e09716f"}, + {file = "websocket-client-0.59.0.tar.gz", hash = "sha256:d376bd60eace9d437ab6d7ee16f4ab4e821c9dae591e1b783c58ebd8aaf80c5c"}, + {file = "websocket_client-0.59.0-py2.py3-none-any.whl", hash = "sha256:2e50d26ca593f70aba7b13a489435ef88b8fc3b5c5643c1ce8808ff9b40f0b32"}, ] zipp = [ {file = "zipp-3.4.1-py3-none-any.whl", hash = "sha256:51cb66cc54621609dd593d1787f286ee42a5c0adbb4b29abea5a63edc3e03098"}, diff --git a/pyproject.toml b/pyproject.toml index b99565a..667bc74 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ authors = ["William Lam "] description = "" name = "hon_patch_notes_game_bot" -version = "0.2.1" +version = "0.2.4.0" [tool.poetry.dependencies] praw = "^7.2.0" diff --git a/tests/cache/reward_codes.txt b/tests/cache/reward_codes.txt new file mode 100644 index 0000000..5f53782 --- /dev/null +++ b/tests/cache/reward_codes.txt @@ -0,0 +1,5 @@ +REWARD_CODE_1 +REWARD_CODE_2 +REWARD_CODE_3 +REWARD_CODE_4 +REWARD_CODE_5 \ No newline at end of file diff --git a/tests/test_communications.py b/tests/test_communications.py index 3aa35bd..708c320 100644 --- a/tests/test_communications.py +++ b/tests/test_communications.py @@ -4,11 +4,14 @@ from praw.exceptions import RedditAPIException from hon_patch_notes_game_bot import communications +from hon_patch_notes_game_bot.utils import get_reward_codes_list from hon_patch_notes_game_bot.config.config import ( COMMUNITY_SUBMISSION_CONTENT_PATH, GOLD_COIN_REWARD, + REWARD_CODES_FILE_PATH, STAFF_RECIPIENTS_LIST, SUBMISSION_CONTENT_PATH, + WINNERS_LIST_FILE_PATH, ) @@ -28,7 +31,7 @@ def assert_test(mock_reddit): assert ( communications.send_message_to_staff( mock_reddit, - winners_list_path="./tests/cache/winners_list.txt", + winners_list_path=f"./tests/{WINNERS_LIST_FILE_PATH}", staff_recipients=STAFF_RECIPIENTS_LIST, version_string="4.9.3", gold_coin_reward=GOLD_COIN_REWARD, @@ -59,6 +62,9 @@ def assert_test(mock_reddit): communications.send_message_to_winners( mock_reddit, winners_list=["User1", "User2"], + reward_codes_list=get_reward_codes_list( + f"tests/{REWARD_CODES_FILE_PATH}" + ), version_string="4.9.3", gold_coin_reward=GOLD_COIN_REWARD, ) @@ -104,4 +110,3 @@ def test_init_submissions(self): ) is not None ) - diff --git a/tests/test_core.py b/tests/test_core.py index c6b76ea..a5ea55b 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -2,7 +2,8 @@ from unittest import TestCase from pytest import mark -from datetime import datetime +from dateutil import tz +from datetime import datetime, timedelta from praw.exceptions import RedditAPIException from requests.models import Response from praw.models import Comment, Submission @@ -10,7 +11,11 @@ from hon_patch_notes_game_bot import core from hon_patch_notes_game_bot.user import RedditUser -from hon_patch_notes_game_bot.config.config import MIN_ACCOUNT_AGE_DAYS +from hon_patch_notes_game_bot.config.config import ( + MIN_ACCOUNT_AGE_DAYS, + REWARD_CODES_FILE_PATH, +) +from hon_patch_notes_game_bot.utils import get_reward_codes_list @mark.usefixtures( @@ -134,6 +139,7 @@ def test_fix_corrupted_community_submission_edit(self): assert self.core.fix_corrupted_community_submission_edit() is None def test_perform_post_game_actions(self): + self.core.reward_codes_filepath = f"tests/{REWARD_CODES_FILE_PATH}" assert self.core.perform_post_game_actions() is None def test_update_patch_notes_table_in_db(self): @@ -232,17 +238,28 @@ def test_loop(self, mock_core, sleep_func=Mock()): self.mock_author.comment_karma = 9001 self.mock_author.created_utc = 1609390800 # December 31, 2020 at 00:00:00 self.mock_reddit.inbox.unread = Mock(return_value=[self.mock_comment]) + + # Game expires, so False is expected assert not self.core.loop() self.core.db.delete_patch_notes_line_number(patch_notes_line_number) # Teardown + # Game does not expire yet; set expiration time to some time in the future + future_date = datetime.now(tz.UTC) + timedelta(days=30) + self.core.game_end_time = str(future_date) + assert self.core.loop() + self.core.db.delete_patch_notes_line_number(patch_notes_line_number) # Teardown + # Exception tests mock_response = Mock(spec=Response) mock_response.status_code = 503 mock_response.json.return_value = {} + + # Server error exception self.mock_reddit.inbox.unread.side_effect = ServerError(mock_response) - assert not self.core.loop() + assert self.core.loop() self.core.db.delete_patch_notes_line_number(patch_notes_line_number) # Teardown + # General exception self.mock_reddit.inbox.unread.side_effect = Exception("General Exception") - assert not self.core.loop() + assert self.core.loop() self.core.db.delete_patch_notes_line_number(patch_notes_line_number) # Teardown diff --git a/tests/test_utils.py b/tests/test_utils.py index 426da35..7e2e47c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -54,3 +54,6 @@ def test_convert_time_string_to_wolframalpha_query_url(): util.convert_time_string_to_wolframalpha_query_url(time_string) == expected_url ) + +def test_get_reward_codes_list(): + assert util.get_reward_codes_list("tests/cache/reward_codes.txt")