def start_consumer_service(self): consumer.start_consumer() return "OK", 200 #/Search/<string:user_input> # def user_search_query_service(self, user_input): # new_input = user_input + " covid-19 " + " CDC " # if len(new_input) > 500: # return "BAD REQUEST\n", 400 # else: # twitterClient = twitterapi.TwitterClient() # tweets = twitterClient.search_for_tweet(new_input,100) # tweetDicts = self.makeListOfTweets(tweets) # if(len(tweetDicts) == 0): # return "NOT FOUND\n", 404 # else: # return jsonify(tweetDicts), 200 # def makeListOfTweets(self, tweets): # blob = sT.Text_Sentiment() # tweetsDict = [] # for tweet in tweets: # polarity = blob.get_sentiment_polarity(tweet.text) # tweetEntry = { # "key": tweet.id, # "sentiment": polarity # } # tweetsDict.append(tweetEntry) # return tweetsDict
def test_consumer(self): """ Consumes the event in test environment and if there are any messages validates them in the database. """ with self.assertLogs("consumer", level="DEBUG") as f: consumer.start_consumer(self.config_file_name) output = ''.join(f.output) self.assertIn("Messages consumed successfully", output) # extract name and phone number from log and validate against database number_match = re.match(r".*'phone_number': '(.*?)'.*", output) name_match = re.match(r".*'name': '(.*?)'.*", output) if number_match and name_match: config = get_config(self.config_file_name) db_con = None try: db_con = consumer.get_db_connection(config["POSTGRES"]) cursor = db_con.cursor() select_query = ''' select * from persons WHERE PhoneNumber = %s AND Name = %s ''' cursor.execute(select_query, (number_match.group(1), name_match.group(1))) records = cursor.fetchall() self.assertEqual(len(records), 1) finally: if db_con: db_con.close()
import time from consumer import start_consumer def callback(message): time.sleep(1) client_tag = 'A' start_consumer(client_tag, callback, [])
import time from consumer import start_consumer def callback(message): time.sleep(3) client_tag = 'E' start_consumer(client_tag, callback, ['B', 'D'])