def test_help_no_doc(hallo_getter): hallo, test_server, test_channel, test_user = hallo_getter({"hallo_control"}) # Manually add FunctionMock to function dispatcher hallo.function_dispatcher.load_function(None, FunctionMockNoDoc) try: hallo.function_dispatcher.dispatch( EventMessage(test_server, None, test_user, "help function no doc") ) data = test_server.get_send_data(1, test_user, EventMessage) assert "error" in data[0].text.lower() assert "no documentation exists" in data[0].text.lower() finally: hallo.function_dispatcher.unload_function(None, FunctionMockNoDoc)
def test_help_mock_func(hallo_getter): hallo, test_server, test_channel, test_user = hallo_getter({"hallo_control"}) # Manually add FunctionMock to function dispatcher hallo.function_dispatcher.load_function(None, FunctionMock) try: hallo.function_dispatcher.dispatch( EventMessage(test_server, None, test_user, "help function mock") ) data = test_server.get_send_data(1, test_user, EventMessage) assert "error" not in data[0].text.lower() assert "example help, please ignore" in data[0].text.lower() finally: hallo.function_dispatcher.unload_function(None, FunctionMock)
def test_specify_invalid_type(self): names_1a = len(self.test_unit1a.name_list) self.function_dispatcher.dispatch( EventMessage( self.server, None, self.test_user, "convert unit add name type=new_type unit=unit1a new_name", )) data = self.server.get_send_data(1, self.test_user, EventMessage) assert "unrecognised type" in data[0].text.lower() assert (len(self.test_unit1a.name_list) == names_1a ), "Shouldn't have added new name"
def test_caps_channel_toggle(hallo_getter): hallo, test_server, test_chan, test_user = hallo_getter( {"channel_control"}) test_chan1 = test_server.get_channel_by_address("other_channel".lower(), "other_channel") test_chan1.in_channel = True test_chan1.use_caps_lock = False hallo.function_dispatcher.dispatch( EventMessage(test_server, test_chan, test_user, "channel caps other_channel")) data = test_server.get_send_data(1, test_chan, EventMessage) assert "error" not in data[0].text.lower() assert "toggle" in data[0].text.lower() assert test_chan1.use_caps_lock # Try toggling again hallo.function_dispatcher.dispatch( EventMessage(test_server, test_chan, test_user, "channel caps other_channel")) data = test_server.get_send_data(1, test_chan, EventMessage) assert "error" not in data[0].text.lower() assert "toggle" in data[0].text.lower() assert not test_chan1.use_caps_lock
def test_caps_no_bool(hallo_getter): hallo, test_server, test_chan, test_user = hallo_getter( {"channel_control"}) test_chan1 = test_server.get_channel_by_address("other_channel".lower(), "other_channel") test_chan1.in_channel = False test_chan1.use_caps_lock = False hallo.function_dispatcher.dispatch( EventMessage(test_server, test_chan, test_user, "channel caps other_channel word")) data = test_server.get_send_data(1, test_chan, EventMessage) assert "error" in data[0].text.lower() assert not test_chan1.use_caps_lock
def test_no_type(self): decimals1 = self.test_repo.get_type_by_name("test_type1").decimals decimals2 = self.test_repo.get_type_by_name("test_type2").decimals self.function_dispatcher.dispatch( EventMessage(self.server, None, self.test_user, "convert set type decimals 5")) data = self.server.get_send_data(1, self.test_user, EventMessage) assert "this is not a recognised conversion type" in data[ 0].text.lower() assert (self.test_repo.get_type_by_name("test_type1").decimals == decimals1), "Decimals shouldn't have changed." assert (self.test_repo.get_type_by_name("test_type2").decimals == decimals2), "Decimals shouldn't have changed."
def test_ambiguous_unit(self): self.function_dispatcher.dispatch( EventMessage( self.server, None, self.test_user, "convert unit prefix group unit=same_name test_group1", )) data = self.server.get_send_data(1, self.test_user, EventMessage) assert "unit name is too ambiguous" in data[0].text.lower() assert "please specify with unit= and type=" in data[0].text.lower() assert self.test_unit1b.valid_prefix_group is None assert self.test_unit2b.valid_prefix_group is None
def test_dragon_deer(hallo_getter): hallo, test_server, test_chan, test_user = hallo_getter({"ascii_art"}) found_deer = False for _ in range(1000): hallo.function_dispatcher.dispatch( EventMessage(test_server, None, test_user, "dragon")) data = test_server.get_send_data() assert "error" not in data[ 0].text, "Dragon output should not contain errors." assert "\n" in data[0].text, "Dragon output should be multiple lines." if "deer" in data[0].text: found_deer = True assert found_deer, "In 1000 runs, at least 1 call to dragon should return deer."
def test_type_specified_set_group_none_3(self): self.test_unit1b.valid_prefix_group = self.test_group1 self.function_dispatcher.dispatch( EventMessage( self.server, None, self.test_user, "convert unit prefix group type=test_type1 unit=same_name none", )) data = self.server.get_send_data(1, self.test_user, EventMessage) assert 'set "none" as the prefix group' in data[0].text.lower() assert 'for the "unit1b" unit' in data[0].text.lower() assert self.test_unit1b.valid_prefix_group is None
def test_no_units_match_specified_unit_and_del_name_3(self): names1a = len(self.test_unit1a.name_list) self.function_dispatcher.dispatch( EventMessage( self.server, None, self.test_user, "convert unit remove name unit=unit1aa not_a_name", )) data = self.server.get_send_data(1, self.test_user, EventMessage) assert "there are no units matching that description" in data[ 0].text.lower() assert len(self.test_unit1a.name_list) == names1a
def test_slowclap_chan_not_in_chan(hallo_getter): hallo, test_server, test_channel, test_user = hallo_getter({"silly"}) test_chan2 = test_server.get_channel_by_address("another_chan".lower(), "another_chan") test_chan2.in_channel = False time_start = time.time() hallo.function_dispatcher.dispatch( EventMessage(test_server, None, test_user, "slowclap " + test_chan2.name)) time_end = time.time() data = test_server.get_send_data(1, test_user, EventMessage) assert time_end - time_start < 2, "Slowclap error should take less than 2 seconds." assert "error" in data[0].text.lower()
def test_prefix_group_none_2(self): self.test_unit2b.valid_prefix_group = self.test_group1 self.function_dispatcher.dispatch( EventMessage( self.server, None, self.test_user, "convert unit prefix group unit=unit2b prefixes=none", )) data = self.server.get_send_data(1, self.test_user, EventMessage) assert 'set "none" as the prefix group' in data[0].text.lower() assert 'for the "unit2b" unit' in data[0].text.lower() assert self.test_unit2b.valid_prefix_group is None
def test_one_die(self): # Set RNG self.roller.answer = 4 # Check function self.function_dispatcher.dispatch( EventMessage(self.server, None, self.test_user, "roll 1d6")) data = self.server.get_send_data(1, self.test_user, EventMessage) assert "i roll 4" in data[0].text.lower(), "Should say it rolled 4." assert (self.roller.last_min == 1 ), "1 should be the minimum value for a die roll." assert (self.roller.last_max == 6 ), "6 Should be the maximum value for the d6 roll." assert self.roller.last_count == 1, "Should have only rolled 1 die."
def test_roll_range(self): # Set RNG self.roller.answer = 47 # Check function self.function_dispatcher.dispatch( EventMessage(self.server, None, self.test_user, "roll 10-108")) data = self.server.get_send_data(1, self.test_user, EventMessage) assert "i roll 47" in data[0].text.lower(), "Should say it rolled 47." assert (self.roller.last_min == 10 ), "10 should be the minimum value for the range." assert (self.roller.last_max == 108 ), "108 Should be the maximum value for the range." assert self.roller.last_count == 1, "Should have only picked one number."
def test_create_from_input(hallo_getter, requests_mock): dailys_times = ["WakeUpTime", "12:00:00", "SleepTime"] dailys_moods = ["happiness", "anger", "tiredness", "boisterousness"] # Setup stuff command_name = "setup dailys field" command_args = "mood" hallo_obj, test_server, test_chan, test_user = hallo_getter({"dailys"}) evt = EventMessage( test_server, test_chan, test_user, "{} {}".format(command_name, command_args), ) evt.split_command_text(command_name, command_args) spreadsheet = DailysSpreadsheetMock(test_user, test_chan) requests_mock.get("{}/stats/mood/static/".format(spreadsheet.dailys_url), json=[{ "date": "static", "source": "Mock test data", "stat_name": "mood", "data": { "moods": dailys_moods, "times": dailys_times } }]) # Try and create dailys field field = DailysMoodField.create_from_input(evt, spreadsheet) assert field.spreadsheet == spreadsheet assert isinstance(field.times, list) assert len(field.times) == 3 assert DailysMoodField.TIME_WAKE in field.times assert DailysMoodField.TIME_SLEEP in field.times assert time(12, 0, 0) in field.times assert isinstance(field.moods, list) assert len(field.moods) == 4 assert field.moods == dailys_moods
def test_trigger_sleep_query(hallo_getter): hallo_obj, test_server, test_chan, test_user = hallo_getter({"dailys"}) # Setup moods = ["Happiness", "Anger", "Tiredness"] evt_sleep = EventMessage(test_server, test_chan, test_user, "night") saved_data = dict() saved_data[DailysMoodField.TIME_WAKE] = dict() saved_data[DailysMoodField.TIME_WAKE]["message_id"] = 1232 saved_data[str(time(14, 0, 0))] = dict() saved_data[str(time(14, 0, 0))]["message_id"] = 1234 for mood in moods: saved_data[DailysMoodField.TIME_WAKE][mood] = 3 saved_data[str(time(14, 0, 0))][mood] = 2 spreadsheet = DailysSpreadsheetMock( test_user, test_chan, saved_data={"mood": { evt_sleep.get_send_time().date(): saved_data }}, ) # Setup field times = [ DailysMoodField.TIME_WAKE, time(14, 0, 0), DailysMoodField.TIME_SLEEP ] field = DailysMoodField(spreadsheet, times, moods) # Send message field.passive_trigger(evt_sleep) # Check mood query is sent notif_dict = spreadsheet.saved_data["mood"][ evt_sleep.get_send_time().date()] assert DailysMoodField.TIME_SLEEP in notif_dict assert "message_id" in notif_dict[DailysMoodField.TIME_SLEEP] # Check query is given data_wake = test_server.get_send_data(1, test_chan, EventMessage) assert "how are you feeling" in data_wake[0].text.lower() assert DailysMoodField.TIME_SLEEP in data_wake[0].text assert all([mood in data_wake[0].text for mood in moods])
def test_create_from_input_no_password(self): # Setup cmd_name = "setup dailys field" cmd_args = "duolingo {}".format(self.TEST_USERNAME) evt = EventMessage( self.server, self.test_chan, self.test_user, "{} {}".format(cmd_name, cmd_args), ) evt.split_command_text(cmd_name, cmd_args) spreadsheet = DailysSpreadsheetMock(self.test_user, self.test_chan) # Create from input try: DailysDuolingoField.create_from_input(evt, spreadsheet) assert ( False ), "Should have failed to create DailysDuolingoField due to missing password." except DailysException as e: assert ( "you must specify both a duolingo username, and password" in str(e).lower() ), "Exception did not prompt to specify a username and password."
def test_more_than_two(self): self.function_dispatcher.dispatch( EventMessage( self.server, None, self.test_user, "convert set 5 unit1b is 3 unit1a = 5 unit1b", )) data = self.server.get_send_data(1, self.test_user, EventMessage) assert "are you specifying 3 units?" in data[0].text.lower() assert "convert set <value> <old unit> to <new unit>" in data[ 0].text.lower() assert self.mock_add.arg is None assert self.mock_set.arg is None
def test_word_list(self): # Set RNG self.roller.answer = 1 # Check function self.function_dispatcher.dispatch( EventMessage(self.server, None, self.test_user, "ouija")) data = self.server.get_send_data(1, self.test_user, EventMessage) assert "message from the other side" in data[0].text words = data[0].text.split("...")[1].strip()[:-1] assert len(words.split()) == 1, "Only one word should be returned." assert (words in self.chooser.last_choices ), "Word should be one of the choices given to chooser" assert (len(self.chooser.last_choices) > 1), "Should be more than 1 word in word list."
def test_one_word(self): # Set RNG self.roller.answer = 1 # Check function self.function_dispatcher.dispatch( EventMessage(self.server, None, self.test_user, "ouija")) data = self.server.get_send_data(1, self.test_user, EventMessage) assert "message from the other side" in data[0].text words = data[0].text.split("...")[1].strip()[:-1] assert len(words.split()) == 1, "Only one word should be returned." assert (words in self.chooser.last_choices ), "Word should be one of the choices given to chooser" assert self.roller.last_min == 1, "Minimum word count should be one" assert self.roller.last_max == 3, "Maximum word count should be three"
def test_reply_irc_client(hallo_getter): hallo, test_server, test_channel, test_user = hallo_getter({"silly"}) reply_func = hallo.function_dispatcher.get_function_by_name("reply") reply_obj = hallo.function_dispatcher.get_function_object( reply_func) # type: Reply # Check irc client response response = reply_obj.passive_run( EventMessage(test_server, test_channel, test_user, "Which IRC client should I use?"), hallo, ) assert "irssi" in response.text assert "hexchat" in response.text assert "mibbit" in response.text
def test_unrecognised_type(self): type1_units = len(self.test_type1.unit_list) self.function_dispatcher.dispatch( EventMessage( self.server, None, self.test_user, "convert remove unit type=new_type unit1b", )) data = self.server.get_send_data(1, self.test_user, EventMessage) assert ("type is not recognised" in data[0].text), "Error should say type does not exist" assert (len(self.test_type1.unit_list) == type1_units ), "Shouldn't have removed the unit"
def test_logs_channel_off(hallo_getter): hallo, test_server, test_chan, test_user = hallo_getter( {"channel_control"}) test_chan1 = test_server.get_channel_by_address("other_channel".lower(), "other_channel") test_chan1.in_channel = True test_chan1.logging = True hallo.function_dispatcher.dispatch( EventMessage(test_server, test_chan, test_user, "channel logging other_channel off")) data = test_server.get_send_data(1, test_chan, EventMessage) assert "error" not in data[0].text.lower() assert "logging set off" in data[0].text.lower() assert not test_chan1.logging
def test_extra_word_split(self): self.function_dispatcher.dispatch( EventMessage( self.server, None, self.test_user, "convert unit prefix group unit1a test_group1 blah", )) data = self.server.get_send_data(1, self.test_user, EventMessage) assert ("could not parse where unit name ends and prefix group begins" in data[0].text.lower()) assert ("please specify with unit=<name> prefix_group=<name>" in data[0].text.lower()) assert self.test_unit1a.valid_prefix_group is None
def test_boop_user_chan_hallo_not_in_channel(hallo_getter): hallo, test_server, test_channel, test_user = hallo_getter({"silly"}) test_channel.add_user(test_user) test_chan2 = test_server.get_channel_by_address("another_chan".lower(), "another_chan") test_chan2.in_channel = False test_user2 = test_server.get_user_by_address("another_user", "another_user") test_user2.online = True hallo.function_dispatcher.dispatch( EventMessage(test_server, test_channel, test_user, "boop another_user another_chan")) data = test_server.get_send_data(1, test_channel, EventMessage) assert "error" in data[0].text.lower()
def test_reply_podbay_doors(hallo_getter): hallo, test_server, test_channel, test_user = hallo_getter({"silly"}) reply_func = hallo.function_dispatcher.get_function_by_name("reply") reply_obj = hallo.function_dispatcher.get_function_object( reply_func) # type: Reply # Check pod bay doors response = reply_obj.passive_run( EventMessage(test_server, test_channel, test_user, "open the pod bay doors hallo."), hallo, ) assert test_user.name in response.text assert "i'm sorry" in response.text.lower() assert "afraid i cannot do that" in response.text.lower()
def message_channel(self, text): """ :type text: str :rtype : EventMessage """ evt = EventMessage( self.spreadsheet.destination.server, self.spreadsheet.destination, self.spreadsheet.user, text, inbound=False, ) self.spreadsheet.user.server.send(evt) return evt
def test_remove_by_unit_and_del_name_3(self): self.test_unit1b.add_name("added_name") names1b = len(self.test_unit1b.name_list) self.function_dispatcher.dispatch( EventMessage( self.server, None, self.test_user, "convert unit remove name unit=unit1b added_name", )) data = self.server.get_send_data(1, self.test_user, EventMessage) assert 'removed name "added_name"' in data[0].text.lower() assert 'from "unit1b" unit' in data[0].text.lower() assert len(self.test_unit1b.name_list) == names1b - 1
def test_specified_type_with_duplicated_unit_name(self): type1_units = len(self.test_type1.unit_list) self.function_dispatcher.dispatch( EventMessage( self.server, None, self.test_user, "convert remove unit type=test_type1 same_name", )) data = self.server.get_send_data(1, self.test_user, EventMessage) assert ('removed unit "unit1b"' in data[0].text.lower()), "Didn't remove unit correctly" assert (len(self.test_type1.unit_list) == type1_units - 1), "Should have removed the unit"
def test_no_number_given(self): decimals = self.test_repo.get_type_by_name("test_type1").decimals self.function_dispatcher.dispatch( EventMessage( self.server, None, self.test_user, "convert set type decimals test_type1", )) data = self.server.get_send_data(1, self.test_user, EventMessage) assert "please specify" in data[0].text.lower() assert "a number of decimal places" in data[0].text.lower() assert (self.test_repo.get_type_by_name("test_type1").decimals == decimals), "Decimals shouldn't have changed."