def test_get_user(): '''Tests if a user's score persists & is retrievable''' print '\nTesting get user- ', # reset data on redis database for testing clear_data() sample_data = [ { "name": "abcd", "text": "abcdeedcba" }, { "name": "abcd", "text": "abcdeedcba" }, { "name": "efgh", "text": "abcddcba" }, { "name": "ijkl", "text": "abcdeffedcba" }, { "name": "mnop", "text": "abba" }, { "name": "qrst", "text": "123456789987654321" }, { "name": "uvwx", "text": "a" }, ] user_names = {item['name'] for item in sample_data} expected_get_user = {} for item in sample_data: expected_get_user[item["name"]] = expected_get_user.get( item["name"], 0) + get_score(item["text"]) #print 'expected_get_user- ',expected_get_user # submit data for item in sample_data: client.play(item) # retrieve user data recieved_get_user = { user: float(client.get_user(user)['data']['score']) for user in user_names } #print 'recieved_get_user- ',recieved_get_user if recieved_get_user == expected_get_user: print 'PASSED!' else: print 'FAILED!'
def test_play(): '''Tests if a user's score is incremented properly''' print '\nTesting play- ', # reset data on redis database for testing clear_data() sample_data = [ { "name": "abcd", "text": "abcdeedcba" }, { "name": "abcd", "text": "abcdeedcba" }, { "name": "ijkl", "text": "abcdeffedcba" }, { "name": "mnop", "text": "abba" }, { "name": "qrst", "text": "123456789987654321" }, { "name": "qrst", "text": "123456789987654321" }, ] expected_scores_dict = {} for item in sample_data: expected_scores_dict[item["name"]] = expected_scores_dict.get( item["name"], 0) + get_score(item["text"]) #print expected_scores_dict # submit data for item in sample_data: client.play(item) # retrieve halloffame data recieved_scores_dict = { item["name"]: client.get_user(item["name"])["data"]["score"] for item in sample_data } #print 'recieved_scores_dict- ', recieved_scores_dict if recieved_scores_dict == recieved_scores_dict: print 'PASSED!' else: print 'FAILED!'
def process_message(message: str, channel): if not client.get_awaiting_response(channel): logging.debug("Got unexpected reply from {} ({})".format( channel, client.get_user(channel)["user_name"])) return issue = client.get_conversation(channel)[0] team = issue[1] if message.strip() not in cfg["teams"][team]["allowed_responses"]: send_message(channel, "Invalid response, please try again") return logging.debug("conversation is {}\nissue is {}".format( client.get_conversation(channel), issue)) client.add_answer(team, client.get_user(channel)["user_name"], issue[0], message.strip()) client.pop_conversation(channel) if client.get_conversation(channel): send_message( channel, "Thanks! next: \n\n" + "\n".join(client.get_conversation(channel)[0][2:])) else: send_message(channel, "Good Job! you're done!") client.set_awaiting_response(channel, False)
def parse_slack_output(slack_rtm_output): ''' The slack real time messaging API is an events firehose. This parsing function returns none unless a message is directed at the bot based on its id ''' logging.debug(slack_rtm_output) if slack_rtm_output and len(slack_rtm_output) > 0: for output in slack_rtm_output: if output and output["type"] == "message" and "bot_id" not in output \ and 'text' in output \ and client.get_awaiting_response(output['channel']): # return text after the @ mention, whitespace removed logging.debug("Response triggered from {}".format( client.get_user(output["channel"])["user_name"])) return output['text'].strip().lower(), \ output['channel'], output['user'] return None, None, None
#!/usr/bin/python import client as open_jub import getpass import mail_util from expected_errors import * if __name__ == '__main__': token = None while token == None: try: user = raw_input('Username: ') pwd = getpass.getpass() token = open_jub.login(user, pwd) except LoginException as e: print e print "Logged in! Token: %s" % token me = open_jub.get_user("kkafadarov") sender = "*****@*****.**" data = "Hey boo" title = "Test" print mail_util.send_mail(me, sender, data, title) print open_jub.is_authenticated(token)
def test_threadsafety_for_play(): '''Tests if score is consistent if multiple clients hit the server at the same time''' print '\nTesting threadsafety- ', # reset data on redis database for testing clear_data() # prepare sample data # make sure all text are palindrome for test case to work sample_data = [ { "name": "abcd", "text": "abcddcba" }, { "name": "efgh", "text": "abcddcba" }, { "name": "ijkl", "text": "abcdeffedcba" }, { "name": "mnop", "text": "abcddcba" }, ] counts = [50, 100, 200, 500] user_names = [item['name'] for item in sample_data] scores_dict = { item["name"]: len(item["text"]) / 2.0 for item in sample_data } expected_scores = [score for play, score in scores_dict.items()] data = [] for i in range(4): for count in range(counts[i]): data.append(sample_data[i]) # update expected scores list expected_scores[i] = expected_scores[i] * counts[i] random.shuffle(data) # Make the Pool of workers no_of_workers = 4 pool = ThreadPool(no_of_workers) start_time = time.time() # Open the urls in their own workers # and return the results # print 'Initiating ', no_of_workers,' threads to hit the server ', sum(counts), ' times...' results = pool.map(client.play, data) # close the pool and wait for the work to finish pool.close() pool.join() #print 'user_names- ', user_names #print 'counts- ', counts #print 'scores- ', scores #print 'expected_scores- ', expected_scores received_scores = [ float(client.get_user(user)['data']["score"]) for user in user_names ] #print 'received_scores- ', received_scores if received_scores == expected_scores: print 'PASSED!' else: print 'FAILED!'
def test_get_user_success(self): post_user("francis", self.birthday) r = get_user("francis") self.assertEqual(r.status_code, 200)
def test_get_user_not_there(self): delete_user("harrypotter") r = get_user("harrypotter") self.assertEqual(r.status_code, 404)
import client client = client.ExampleServiceClient() client.get_user('//myapiservice.com/users/1')
else: print( "That did not seem like a yes. You have not been added to the room. If you wish to join it, use '--join'" ) elif response.status_code == 409: print( "A room already exists with that roomname, try again.") continue elif response.status_code == 404: print("Wrong syntax for the roomname") elif message.startswith('--userinfo'): """Searches for specified user among online users.""" searched_user = check_if_followed_by_argument( username, '--userinfo', message, None, True) response = get_user(searched_user, username) print(response.json()) elif message == '--showusers': """Shows all users online""" response = get_all_users(username) print("The following users are currently online:") print(response.json()) elif message == '--showrooms': """Show all existing rooms""" response = get_all_rooms(username) print("Rooms:") print(response.json()) elif message.startswith('--showroomusers'):
def test_user_structure(self): msg = client.get_user("", "") expected_keys = ["account_name", "status"] self.assertTrue(all([i in msg for i in expected_keys]))