def test_timelength(): """ Test timelength functions work like they should """ # Test unit in dict for unit in UNIT_DICT: time_length = TimeLength(unit, 5) # Test unit not in dict with pytest.raises(ValueError) as error: time_length = TimeLength("foo", 5) # Test Timeout Calculation time_length = TimeLength("hours", 5) assert time_length.timeout <= UNIT_DICT["hours"] * 5 + time.time() assert "Invalid unit foo" in str(error)
def set_reminder(self, unit, amount_of_time, msg): """ Write a reminder to disk """ try: time_length = TimeLength(unit, amount_of_time) except ValueError as error: raise ReminderError(str(error)) from error reminder_data = { "msg": msg, "unique_id": time_length.unique_id, "timeout": time_length.timeout, "channel": self._channel, } handler = ReminderFileHandler(self._user) reminder = handler.write(**reminder_data) return f"```{reminder}```"
def create_poll(): """ Create a poll object for testing """ time_length = TimeLength("years", 5) kwargs = { "time_length": time_length, "choices": ["a", "b", "c"], "votes": { "1": [], "2": [], "3": [] }, "prompt": "foo", "channel_id": "bar", } poll = Poll(**kwargs) poll.save() yield poll poll.delete()
def poll(self, *args): """ Start a poll """ if len(args) == 0: return "text", POLL_HELP_MSG if args[0] == "help": return "text", POLL_HELP_MSG choices = [ phrase.strip() for phrase in self._user_msg.content.split(";") ] try: expiry_str = (choices.pop(-1) if "ends in" in choices[-1].lower() else "ends in 1 hour") except (KeyError, IndexError, ValueError): return "text", POLL_HELP_MSG words = expiry_str.split(" ") try: poll = Poll( time_length=TimeLength(unit=words[3], amount_of_time=words[2])) except ValueError: return "text", POLL_HELP_MSG poll.prompt = choices.pop(0) poll.choices = choices poll.channel_id = self._channel.id poll.votes = {idx: [] for idx in range(len(poll.choices))} poll.save() return_str = f"```{poll.prompt} ({expiry_str})\n\n" for choice_num in range(len(poll.choices)): return_str += f"{choice_num+1}.\t{poll.choices[choice_num]}\n" return_str += (f'\n\nType or DM me "!vote {poll.poll_id} ' '<choice number>" to vote```') return "text", return_str
def test_poll_save_load(capsys): """ Test saving and loading a poll """ # Test saving a poll poll = Poll(time_length=TimeLength("hours", 5), votes=[], prompt="foo", channel_id=1234) poll.save() assert os.path.isfile(f"{POLL_DIR}/{poll.poll_id}.json") # Test Loading a poll other_poll = Poll() other_poll.load(poll.poll_id) assert poll.poll_id == other_poll.poll_id assert poll.expiry == other_poll.expiry assert poll.prompt == other_poll.prompt assert poll.choices == other_poll.choices assert poll.votes == other_poll.votes assert poll.channel_id == other_poll.channel_id # Test error loading poll with open(f"{POLL_DIR}/1234.json", "w") as stream: stream.write("{}") with pytest.raises(ValueError) as error: other_poll.load("1234") assert "Something went wrong with poll 1234" in str(error) # Test delete poll.delete() other_poll.delete() assert not os.path.isfile(poll.poll_id) poll.delete() assert f"Warning: {poll.poll_id} does not exist" in capsys.readouterr().out
def test_different_ids(): """ Test timelengths have different IDs 10 times """ time_length_id = TimeLength("second", 1).unique_id for _ in range(10): assert time_length_id != TimeLength("second", 1).unique_id