def test_trivia_command_callback(self): query = BotQuery() query.has_callback_query = True query.callback_query = BotQuery() query.callback_query.callback_data = "payload here/trivia" result = CommandParser.parse_command(query) self.assertIsInstance(result, TriviaCommand) self.assertIs(query, result.query)
def test_trivia_callback_query_correct(self): query = BotQuery() query.has_callback_query = True query.callback_query = BotQuery() query.callback_query.callback_data = "CC/trivia" trivia_command = TriviaCommand(query) with patch("commands.trivia_command.requests.post") as mock_post: trivia_command.execute() response_text = mock_post.call_args[1]["json"]["text"] self.assertTrue(response_text.endswith("*C* is correct!"))
def test_top_given_year(self): query = BotQuery() trends_command = TrendsCommand(query, ["/trends", "tOp", "2011"]) with patch("commands.trends_command.TrendsCommand.execute_top_searches" ) as mock_execute_top_searches: trends_command.execute() mock_execute_top_searches.assert_called_once_with("2011")
def test_graph_bad_unknown_mode(self): query = BotQuery() trends_command = TrendsCommand(query, ["/trends", "mymode"]) with patch("commands.trends_command.TrendsCommand.handle_normal_query" ) as mock_handle_normal_query: trends_command.execute() mock_handle_normal_query.assert_called_once_with( "Unable to fetch the trends: Unknown mode 'mymode'")
def test_edited_message_disallow(self): query = BotQuery() query.is_edited = True invalid_command = InvalidCommand(query) self.assertTrue(invalid_command.skip_edited()) invalid_command.handle_normal_query = Mock() invalid_command.execute() self.assertFalse(invalid_command.handle_normal_query.called)
def test_trivia_callback_query(self): query = BotQuery() query.has_callback_query = True trivia_command = TriviaCommand(query) trivia_command.handle_callback_query = Mock() trivia_command.execute() trivia_command.handle_callback_query.assert_called_once()
def test_graph_bad_no_mode(self): query = BotQuery() trends_command = TrendsCommand(query, ["/trends"]) with patch("commands.trends_command.TrendsCommand.handle_normal_query" ) as mock_handle_normal_query: trends_command.execute() mock_handle_normal_query.assert_called_once_with( "Unable to fetch the trends: No mode provided. Use '/trends help' for more info" )
def test_trivia_help_text(self): query = BotQuery() trivia_command = TriviaCommand(query, ["/trivia", "help"]) with patch("trivia.Trivia.get_help_text", return_value="help text"): trivia_command.handle_normal_query = Mock() trivia_command.execute() trivia_command.handle_normal_query.assert_called_once_with( "help text")
def test_graph_bad_graph_no_keywords(self): query = BotQuery() trends_command = TrendsCommand(query, ["/trends", "graph"]) with patch("commands.trends_command.TrendsCommand.handle_normal_query" ) as mock_handle_normal_query: trends_command.execute() mock_handle_normal_query.assert_called_once_with( "Unable to fetch the trends: No keywords provided for graphing" )
def test_trending_default(self): query = BotQuery() trends_command = TrendsCommand(query, ["/trends", "trending"]) with patch( "commands.trends_command.TrendsCommand.execute_trending_searches" ) as mock_execute_trending_searches: trends_command.execute() mock_execute_trending_searches.assert_called_once_with( "united_states")
def test_graph_good_default(self): query = BotQuery() trends_command = TrendsCommand( query, ["/trends", "graPH", "subject1", "subject2"]) with patch("commands.trends_command.TrendsCommand.execute_graph" ) as mock_execute_graph: trends_command.execute() mock_execute_graph.assert_called_once_with( {"keywords": ["subject1", "subject2"]})
def test_edited_message_disallow(self): query = BotQuery() query.is_edited = True help_command = HelpCommand(query) self.assertTrue(help_command.skip_edited()) help_command.handle_normal_query = Mock() help_command.execute() self.assertFalse(help_command.handle_normal_query.called)
def test_get_help_text(self, mock_get_help_text): mock_get_help_text.return_value = "halp" query = BotQuery() trends_command = TrendsCommand( query, ["/trends", "help", "other", "useless", "arg"]) with patch("commands.trends_command.TrendsCommand.handle_normal_query" ) as mock_handle_normal_query: trends_command.execute() mock_handle_normal_query.assert_called_once_with("halp")
def test_fail_not_ok(self): query = BotQuery() joke_command = JokeCommand(query) joke_command.handle_normal_query = Mock() with patch("commands.joke_command.requests.get") as mock_get: mock_get.return_value.ok = False joke_command.execute() joke_command.handle_normal_query.assert_called_once_with( "Oops! Can't fetch joke right now.")
def test_trending_location_1(self): query = BotQuery() trends_command = TrendsCommand(query, ["/trends", "trending", "MEXICO"]) with patch( "commands.trends_command.TrendsCommand.execute_trending_searches" ) as mock_execute_trending_searches: trends_command.execute() mock_execute_trending_searches.assert_called_once_with("mexico")
def test_trending_location_2(self): query = BotQuery() trends_command = TrendsCommand(query, ["/trends", "trenDing", "South Korea"]) with patch( "commands.trends_command.TrendsCommand.execute_trending_searches" ) as mock_execute_trending_searches: trends_command.execute() mock_execute_trending_searches.assert_called_once_with( "south_korea")
def test_graph_bad_no_time_argument(self): query = BotQuery() trends_command = TrendsCommand(query, ["/trends", "graph", "github", "-t"]) with patch("commands.trends_command.TrendsCommand.handle_normal_query" ) as mock_handle_normal_query: trends_command.execute() mock_handle_normal_query.assert_called_once_with( "Unable to fetch the trends: No argument provided for option '-t'" )
def test_private_chat(self): query = BotQuery() query.is_private = True query.user_first_name = "Lambda" help_command = HelpCommand(query) self.assertIs(help_command.query, query) help_command.handle_normal_query = Mock() help_command.execute() self.assertTrue(help_command.handle_normal_query.called)
def test_trivia_fetch_question_fail_unhandled_error(self, _): query = BotQuery() trivia_command = TriviaCommand(query, ["/trivia"]) with patch("trivia.Trivia.fetch_question", side_effect=Exception("trivia error")): trivia_command.handle_normal_query = Mock() trivia_command.execute() trivia_command.handle_normal_query.assert_called_once_with( "Failed to fetch trivia question!")
def test_fail_exception(self): query = BotQuery() fact_command = FactCommand(query) fact_command.handle_normal_query = Mock() with patch("commands.fact_command.requests.get", side_effect=Exception("Fact server is down")) as mock_get: with patch("commands.fact_command.print") as mock_print: fact_command.execute() self.assertTrue(mock_print.called) fact_command.handle_normal_query.assert_called_once_with( "Oops! Can't fetch fact right now.")
def test_fail_not_ok(self): query = BotQuery() fact_command = FactCommand(query) fact_command.handle_normal_query = Mock() with patch("commands.fact_command.requests.get") as mock_get: with patch("commands.fact_command.print") as mock_print: mock_get.return_value.ok = False fact_command.execute() fact_command.handle_normal_query.assert_called_once_with( "Oops! Can't fetch fact right now.") self.assertFalse(mock_print.called)
def test_private_chat(self): query = BotQuery() query.is_private = True query.user_first_name = "Lambda" start_command = StartCommand(query) self.assertIs(start_command.query, query) start_command.handle_normal_query = Mock() start_command.execute() start_command.handle_normal_query.assert_called_once_with( "Hello Lambda! Welcome to the DAS bot!")
def test_trivia_fetch_question_success(self): query = BotQuery() trivia_command = TriviaCommand(query, ["/trivia"]) trivia_mock = Trivia() trivia_mock.formatted_response = lambda: "trivia question" with patch("trivia.Trivia.fetch_question", return_value=trivia_mock): trivia_command.handle_normal_query = Mock() trivia_command.execute() trivia_command.handle_normal_query.assert_called_once_with( "trivia question")
def test_group_chat(self): query = BotQuery() query.is_group = True query.user_first_name = "Foobar" query.chat_title = "Anonymous" help_command = HelpCommand(query) self.assertIs(help_command.query, query) help_command.handle_normal_query = Mock() help_command.execute() self.assertTrue(help_command.handle_normal_query.called)
def test_edited_message_allow(self): query = BotQuery() query.is_edited = True invalid_command = InvalidCommand(query) invalid_command.ignore_edited = False self.assertFalse(invalid_command.skip_edited()) invalid_command.handle_normal_query = Mock() invalid_command.execute() invalid_command.handle_normal_query.assert_called_once_with( "Sorry! I don't understand. Try /help")
def test_successful_request(self): query = BotQuery() joke_command = JokeCommand(query) joke_command.handle_normal_query = Mock() with patch("commands.joke_command.requests.get") as mock_get: mock_get.return_value.ok = True mock_get.return_value.content = b"Dad joke here!" joke_command.execute() joke_command.handle_normal_query.assert_called_once_with( "Dad joke here!")
def test_edited_message_allow(self): query = BotQuery() query.is_edited = True start_command = StartCommand(query) start_command.ignore_edited = False self.assertFalse(start_command.skip_edited()) start_command.handle_normal_query = Mock() start_command.execute() start_command.handle_normal_query.assert_called_once_with( "Greetings from the DAS bot!")
def test_group_chat(self): query = BotQuery() query.is_group = True query.user_first_name = "Foobar" query.chat_title = "Anonymous" start_command = StartCommand(query) self.assertIs(start_command.query, query) start_command.handle_normal_query = Mock() start_command.execute() start_command.handle_normal_query.assert_called_once_with( "Hello Anonymous! I am the DAS Bot, Foobar has summoned me here!")
def test_fail_throw_exception(self): query = BotQuery() joke_command = JokeCommand(query) joke_command.handle_normal_query = Mock() with patch("commands.joke_command.requests.get", side_effect=Exception("Joke server is down")): with patch("commands.joke_command.print" ) as mock_print: # Supressing printing side effects joke_command.execute() self.assertTrue(mock_print.called) joke_command.handle_normal_query.assert_called_once_with( "Oops! Can't fetch joke right now.")
def test_graph_good_time_short(self): query = BotQuery() trends_command = TrendsCommand( query, ["/trends", "graph", "-t", "12d", "subject1", "subject2"]) with patch("commands.trends_command.TrendsCommand.execute_graph" ) as mock_execute_graph: trends_command.execute() mock_execute_graph.assert_called_once_with({ "keywords": ["subject1", "subject2"], "--time": "12d" })