def test_create_survey(): my_backend = Backend(cfg, init=True) #check that survey tables do not exist assert not __table_exists("survey_" + sleep_survey["id"]) assert not __table_exists("survey_" + stress_survey["id"]) #create survey my_backend.create_survey(sleep_survey) my_backend.create_survey(stress_survey) #check that survey tables were created assert __table_exists("survey_" + sleep_survey["id"]) assert __table_exists("survey_" + stress_survey["id"]) #check that the surveys were added to the surveys table db = sqlite3.connect(DB_path) cursor = db.cursor() sql = ''' SELECT * FROM surveys WHERE id=? ''' cursor.execute(sql, (sleep_survey["id"], )) resp_sleep = cursor.fetchone() assert json.loads(resp_sleep[1]) == sleep_survey cursor.execute(sql, (stress_survey["id"], )) resp_stress = cursor.fetchone() assert json.loads(resp_stress[1]) == stress_survey #try to create a survey that already exists with pytest.raises(RuntimeError): my_backend.create_survey(sleep_survey) with pytest.raises(RuntimeError): my_backend.create_survey(stress_survey)
def test_join_survey(): my_backend = Backend(cfg, init=True) my_backend.create_survey(sleep_survey) #check that user has not joined survey db = sqlite3.connect(DB_path) cursor = db.cursor() sql = ''' SELECT * FROM user_surveys WHERE user_id=? AND survey_id=? ''' cursor.execute(sql, (user_id, sleep_survey["id"])) resp = cursor.fetchone() assert resp is None #user joins survey my_backend.join_survey(user_id, sleep_survey["id"]) #check user joined survey cursor.execute(sql, (user_id, sleep_survey["id"])) resp = cursor.fetchone() assert resp[0] == user_id and \ resp[1] == sleep_survey["id"] and \ resp[2] == None and \ resp[3] == None #user joins survey that does not exist with pytest.raises(RuntimeError): my_backend.join_survey(user_id, "random_survey") #user joins a survey that was already joined with pytest.raises(RuntimeError): my_backend.join_survey(user_id, sleep_survey["id"])
def test_save_answer(): my_backend = Backend(cfg, init=True) my_backend.create_survey(sleep_survey) sql = '''SELECT * FROM survey_sleep WHERE user_id=? and id=?''' db = sqlite3.connect(DB_path) cursor = db.cursor() #save resp_id = my_backend.save_answer(user_id, sleep_survey["id"], { "sleep_hours": 9, "sleep_quality": 5, "ts": 200 }) cursor.execute(sql, (user_id, resp_id)) resp = cursor.fetchall() assert resp[0] == (resp_id, user_id, u'200', '9', '5', None)
def test_report_and_delete(): my_backend = Backend(back_cfg, create=True) #add sleep and stress surveys my_backend.create_survey(json.load(open("DATA/surveys/sleep.json", 'r'))) my_backend.create_survey(json.load(open("DATA/surveys/stress.json", 'r'))) sleek = Sleek4Slack(my_backend) survey_id = "sleep" am_check = "10:00am" pm_check = "5:00pm" assert sleek.join(["join", "sleep", am_check, pm_check], context)[0] assert sleek.join(["join", "stress", am_check, pm_check], context)[0] #add two responses resp_id_1 = my_backend.save_answer(user_id, survey_id, { "sleep_hours": 9, "sleep_quality": 5, "ts": 100 }) resp_id_2 = my_backend.save_answer(user_id, survey_id, { "sleep_hours": 5, "sleep_quality": 2, "ts": 200, "notes": "some note" }) #check responses are there resp = my_backend.get_report(user_id, survey_id) assert resp[0] == (resp_id_2, user_id, '200', '5', '2', "some note") assert resp[1] == (resp_id_1, user_id, '100', '9', '5', None) my_backend.delete_answers(user_id, survey_id) resp = my_backend.get_report(user_id, survey_id) assert resp == []
def test_get_user(): #create DB my_backend = Backend(cfg, init=True) my_backend.add_user(user_id) #check that user was correctly created db = sqlite3.connect(DB_path) cursor = db.cursor() sql = ''' SELECT * FROM users WHERE id=? ''' cursor.execute(sql, (user_id, )) resp = cursor.fetchone() assert resp[USER_ID] == user_id and \ resp[USER_ACTIVE] == 1 #load user user = my_backend.get_users(user_id=user_id)[0] assert user[USER_ID] == user_id and \ resp[USER_ACTIVE] == 1
def test_leave(): my_backend = Backend(back_cfg, create=True) #add sleep and stress surveys sleep_survey = json.load(open("DATA/surveys/sleep.json", 'r')) my_backend.create_survey(sleep_survey) stress_survey = json.load(open("DATA/surveys/stress.json", 'r')) my_backend.create_survey(stress_survey) sleek = Sleek4Slack(my_backend) #try to leave a survey that does not exist bad_survey_id = "saleep" ret = sleek.leave(["leave", bad_survey_id], context) assert ret == out.SURVEY_UNKNOWN.format(bad_survey_id.upper()) #try to leave stress survey (it was not joined) bad_survey_id = stress_survey["id"] ret = sleek.leave(["leave", bad_survey_id], context) assert ret == out.SURVEY_NOT_SUBSCRIBED.format(bad_survey_id.upper()) #join sleep survey survey_id = sleep_survey["id"] ret = sleek.join(["join", survey_id], context) assert out.SURVEY_JOIN_OK.format(survey_id.upper()) == ret[1] data = my_backend.list_surveys(some_user)[0] assert data[0] == some_user assert data[1] == "sleep" #try to leave stress survey ret = sleek.leave(["leave", survey_id], context) assert out.SURVEY_LEAVE_OK.format(survey_id.upper()) == ret[1] for x in my_backend.list_surveys(some_user): assert "sleep" not in x[0]
def test_survey(): my_backend = Backend(back_cfg, create=True) #add survey survey_sleep = json.load(open("DATA/surveys/sleep.json", 'r')) survey_stress = json.load(open("DATA/surveys/stress.json", 'r')) my_backend.create_survey(survey_sleep) my_backend.create_survey(survey_stress) sleek = Sleek4Slack(my_backend) survey_id = survey_sleep["id"] am_check = "10:00am" pm_check = "5:00pm" sleek.join(["join", survey_id, am_check, pm_check], context) #check that there is no response yet resp = my_backend.get_report(user_id, survey_id) assert resp == [] #survey does not exist resp = sleek.survey(["survey", "bad_survey"], context) assert resp == out.SURVEY_UNKNOWN.format("BAD_SURVEY") #user did not subscribe to this survey resp = sleek.survey(["survey", "stress"], context) assert resp == out.SURVEY_NOT_SUBSCRIBED.format("STRESS") survey = sleek.survey(["survey", "sleep"], context, display=False) assert survey == survey_sleep #check that a survey thread is created assert sleek.survey_threads[context["thread_ts"]] == (some_user, survey_id, None)
def test_add_user(): #create DB my_backend = Backend(cfg, init=True) #make sure user does not exist assert my_backend.get_users(user_id=user_id) == [] #create user assert my_backend.add_user(user_id) db = sqlite3.connect(DB_path) cursor = db.cursor() sql = ''' SELECT * FROM users WHERE id=? ''' cursor.execute(sql, (user_id, )) resp = cursor.fetchone() assert resp[USER_ID] == user_id and \ resp[USER_ACTIVE] == 1 #adding the same user should fail assert not my_backend.add_user(user_id)
def test_load_surveys(): my_backend = Backend(back_cfg, create=True) sleek = Sleek4Slack(my_backend) assert len(sleek.current_surveys) == 0 survey = json.load(open("DATA/surveys/sleep.json", 'r')) my_backend.create_survey(survey) sleek = Sleek4Slack(my_backend) assert len(sleek.current_surveys) == 1 assert my_backend.get_survey("sleep") is not None #try to create the same survey with pytest.raises(RuntimeError): my_backend.create_survey(survey)
def test_save_answer(): my_backend = Backend(back_cfg, create=True) survey_sleep = json.load(open("DATA/surveys/sleep.json", 'r')) survey_stress = json.load(open("DATA/surveys/stress.json", 'r')) my_backend.create_survey(survey_sleep) my_backend.create_survey(survey_stress) sleek = Sleek4Slack(my_backend) survey_id = "sleep" am_check = "10:00am" pm_check = "5:00pm" sleek.join(["join", survey_id, am_check, pm_check], context) survey = sleek.survey(["survey", "sleep"], context, display=False) assert survey == survey_sleep #check that a survey thread is created assert sleek.survey_threads[context["thread_ts"]] == (some_user, survey_id, None) #incorrect number of answers bad_resp = "1" resp = sleek.parse_answer(context["thread_ts"], bad_resp) assert resp == out.ANSWERS_TOO_FEW.format(2, 1) bad_resp = "1 2 3 4" resp = sleek.parse_answer(context["thread_ts"], bad_resp) assert resp == out.ANSWERS_TOO_MANY.format(2, 4) #invalid choice 1 bad_resp = "-1 2" bad_q_id = "sleep_hours" #sleep_quality resp = sleek.parse_answer(context["thread_ts"], bad_resp) assert resp == out.ANSWERS_BAD_CHOICE.format(bad_q_id) #invalid choice 2 bad_resp = "1 20" bad_q_id = "sleep_quality" #sleep_quality resp = sleek.parse_answer(context["thread_ts"], bad_resp) assert resp == out.ANSWERS_BAD_CHOICE.format(bad_q_id), resp good_resp = "1 2" resp = sleek.parse_answer(context["thread_ts"], good_resp) resp = sleek.save_answer(context["thread_ts"]) assert resp == out.ANSWERS_SAVE_OK
def test_get_survey(): my_backend = Backend(cfg, init=True) #create survey my_backend.create_survey(sleep_survey) my_backend.create_survey(stress_survey) #check that the surveys were added to the surveys table db = sqlite3.connect(DB_path) cursor = db.cursor() sql = ''' SELECT * FROM surveys WHERE id=? ''' #sleep survey cursor.execute(sql, (sleep_survey["id"], )) resp_sleep = cursor.fetchone() assert json.loads(resp_sleep[1]) == sleep_survey #check that get_survey returns the same resp_sleep_get = my_backend.get_survey(sleep_survey["id"]) assert resp_sleep_get == sleep_survey, resp_sleep_get #stress survey cursor.execute(sql, (stress_survey["id"], )) resp_stress = cursor.fetchone() assert json.loads(resp_stress[1]) == stress_survey #check that get_survey returns the same resp_stress_get = my_backend.get_survey(stress_survey["id"]) assert resp_stress_get == stress_survey
def test_reminder(): my_backend = Backend(back_cfg, create=True) sleep_survey = json.load(open("DATA/surveys/sleep.json", 'r')) survey_id = sleep_survey["id"] my_backend.create_survey(sleep_survey) sleek = Sleek4Slack(my_backend) am_check = "10:00am" pm_check = "05:00pm" #join survey ret = sleek.join(["join", survey_id], context) data = my_backend.list_surveys(some_user)[0] assert data[0] == some_user assert data[1] == survey_id assert data[2] == None assert data[3] == None #try to reschedule new_am_check = "06:00am" new_pm_check = "06:20pm" bad_am = "6:am" bad_pm = ":20pm" #bad inputs #invalid am time ret = sleek.reminder(["reminder", survey_id, bad_am], context) assert out.INVALID_TIME.format(bad_am) == ret #invalid pm time ret = sleek.reminder(["reminder", survey_id, bad_pm], context) assert out.INVALID_TIME.format(bad_pm) == ret #valid AM time but *invalid* PM time ret = sleek.reminder(["reminder", survey_id, am_check, bad_am], context) assert out.INVALID_TIME.format(bad_am) == ret #*valid* AM time but invalid PM time ret = sleek.reminder(["reminder", survey_id, bad_pm, pm_check], context) assert out.INVALID_TIME.format(bad_pm) == ret #new am reminder ret = sleek.reminder(["reminder", survey_id, new_am_check], context) assert out.REMINDER_OK.format(survey_id.upper(), new_am_check.upper()) == ret[1] data = my_backend.list_surveys(some_user)[0] assert data[0] == some_user assert data[1] == survey_id assert data[2] == new_am_check.upper() assert data[3] == None #new pm reminder ret = sleek.reminder(["reminder", survey_id, new_pm_check], context) assert out.REMINDER_OK.format(survey_id.upper(), new_pm_check.upper()) == ret[1] data = my_backend.list_surveys(some_user)[0] assert data[0] == some_user assert data[1] == survey_id assert data[2] == new_am_check.upper() assert data[3] == new_pm_check.upper() #old reminders ret = sleek.reminder(["reminder", survey_id, am_check, pm_check], context) assert out.REMINDER_OK_2.format(survey_id.upper(), am_check.upper(), pm_check.upper()) == ret[1] data = my_backend.list_surveys(some_user)[0] assert data[0] == some_user assert data[1] == survey_id assert data[2] == am_check.upper() assert data[3] == pm_check.upper()
def test_get_notes(): my_backend = Backend(cfg, init=True) #create sleep survey my_backend.create_survey(sleep_survey) #save a few answers my_backend.save_answer( user_id, sleep_survey["id"], { "sleep_hours": 9, "sleep_quality": 5, "ts": 200, "notes": u"this a nifty note" }) my_backend.save_answer( user_id, sleep_survey["id"], { "sleep_hours": 8, "sleep_quality": 4, "ts": 300, "notes": u"this a another note" }) my_backend.save_answer( user_id, sleep_survey["id"], { "sleep_hours": 7, "sleep_quality": 3, "ts": 400, "notes": u"this a neatfull note" }) #check answers ok report = my_backend.get_notes(user_id, sleep_survey["id"]) print "rep", report assert report[0] == (u'400', u"this a neatfull note") assert report[1] == (u'300', u"this a another note") assert report[2] == (u'200', u"this a nifty note")
def reset_DB(): b = Backend(confs, init=True) b.create_survey(sleep_survey)
def test_list_surveys(): my_backend = Backend(cfg, init=True) #create surveys my_backend.create_survey(sleep_survey) my_backend.create_survey(stress_survey) #list surveys resp = my_backend.list_surveys() assert resp[0][0] == "sleep" assert resp[1][0] == "stress" resp = my_backend.list_surveys(user_id=user_id) assert resp == [] my_backend.join_survey(user_id, "sleep") resp = my_backend.list_surveys(user_id=user_id) assert resp[0][1] == "sleep"
def test_toggle_user(): my_backend = Backend(cfg, init=True) #create user my_backend.add_user(user_id) #check it's active user = my_backend.get_users(user_id=user_id)[0] assert user[USER_ACTIVE] == 1 #disable user my_backend.toggle_user(user_id, active=False) #check it's inactive user = my_backend.get_users(user_id=user_id)[0] assert user[USER_ACTIVE] == 0 #enable user my_backend.toggle_user(user_id, active=True) #check it's active user = my_backend.get_users(user_id=user_id)[0] assert user[USER_ACTIVE] == 1
def test_delete_answers(): my_backend = Backend(cfg, init=True) #create sleep survey my_backend.create_survey(sleep_survey) #save a few answers my_backend.save_answer(user_id, sleep_survey["id"], { "sleep_hours": 9, "sleep_quality": 5, "ts": 200 }) my_backend.save_answer(user_id, sleep_survey["id"], { "sleep_hours": 8, "sleep_quality": 4, "ts": 300 }) my_backend.save_answer(user_id, sleep_survey["id"], { "sleep_hours": 7, "sleep_quality": 3, "ts": 400 }) #check answers ok report = my_backend.get_report(user_id, sleep_survey["id"]) print "rep", report assert report[0][1:] == (user_id, u'400', '7', '3', None) assert report[1][1:] == (user_id, u'300', '8', '4', None) assert report[2][1:] == (user_id, u'200', '9', '5', None) #delete answers my_backend.delete_answers(user_id, sleep_survey["id"]) #check no answers report = my_backend.get_report(user_id, sleep_survey["id"]) assert report == []
def test_reminders(): my_backend = Backend(cfg, init=True) my_backend.create_survey(sleep_survey) #user joins survey survey_id = sleep_survey["id"] my_backend.join_survey(user_id, sleep_survey["id"]) rems = my_backend.list_surveys(user_id=user_id) assert rems[0] == (user_id, survey_id, None, None) #add AM reminder my_backend.save_reminder(user_id, sleep_survey["id"], "10:00AM") rems = my_backend.list_surveys(user_id=user_id) assert rems[0] == (user_id, survey_id, "10:00AM", None) #new AM reminder my_backend.save_reminder(user_id, sleep_survey["id"], "9:00AM") rems = my_backend.list_surveys(user_id=user_id) assert rems[0] == (user_id, survey_id, "9:00AM", None) #add PM reminder my_backend.save_reminder(user_id, sleep_survey["id"], "2:00PM") rems = my_backend.list_surveys(user_id=user_id) assert rems[0] == (user_id, survey_id, "9:00AM", "2:00PM") #remove reminders my_backend.save_reminder(user_id, sleep_survey["id"], None) rems = my_backend.list_surveys(user_id=user_id) assert rems[0] == (user_id, survey_id, None, None)
def test_get_report(): my_backend = Backend(cfg, init=True) #create sleep survey my_backend.create_survey(sleep_survey) #save a few answers my_backend.save_answer( user_id, sleep_survey["id"], { "sleep_hours": 9, "sleep_quality": 5, "ts": 200, "notes": u"don’t look at this wáilde note" }) my_backend.save_answer( user_id, sleep_survey["id"], { "sleep_hours": 8, "sleep_quality": 4, "ts": 300, "notes": u"this a another note" }) my_backend.save_answer( user_id, sleep_survey["id"], { "sleep_hours": 7, "sleep_quality": 3, "ts": 400, "notes": u"this a nifty note" }) #check answers ok report = my_backend.get_report(user_id, sleep_survey["id"]) print "rep", report assert report[0][1:] == (user_id, u'400', '7', '3', u"this a nifty note") assert report[1][1:] == (user_id, u'300', '8', '4', u"this a another note") assert report[2][1:] == (user_id, u'200', '9', '5', u"don’t look at this wáilde note")
def test_join(): my_backend = Backend(back_cfg, create=True) sleep_survey = json.load(open("DATA/surveys/sleep.json", 'r')) my_backend.create_survey(sleep_survey) sleek = Sleek4Slack(my_backend) survey_id = sleep_survey["id"] #test wrong inputs am_check = "10:00am" pm_check = "05:00pm" bad_am = "19dasam" bad_pm = "19daspm" #bad inputs #try to join a survey that does not exist bad_survey_id = "something" ret = sleek.join(["join", bad_survey_id, am_check, pm_check], context) assert out.SURVEY_UNKNOWN.format(bad_survey_id.upper()) == ret #invalid am time ret = sleek.join(["join", survey_id, bad_am], context) assert ret == out.INVALID_TIME.format(bad_am) #invalid pm time ret = sleek.join(["join", survey_id, bad_pm], context) assert ret == out.INVALID_TIME.format(bad_pm) #valid AM time but *invalid* PM time ret = sleek.join(["join", survey_id, am_check, bad_am], context) assert ret == out.INVALID_TIME.format(bad_am) #*valid* AM time but invalid PM time ret = sleek.join(["join", survey_id, bad_pm, pm_check], context) assert ret == out.INVALID_TIME.format(bad_pm) #check that user has not joined the sleep survey data = my_backend.list_surveys(some_user) assert data == [] ret = sleek.join(["join", survey_id], context) assert out.SURVEY_JOIN_OK.format(survey_id.upper()) == ret[1] data = my_backend.list_surveys(some_user)[0] assert data[0] == some_user assert data[1] == survey_id #joining the same survey again ret = sleek.join(["join", survey_id, am_check, pm_check], context) assert ret == out.SURVEY_IS_SUBSCRIBED.format(survey_id.upper()) #leave survey my_backend.leave_survey(some_user, survey_id) #join survey with AM reminder ret = sleek.join(["join", survey_id, am_check], context) assert out.SURVEY_JOIN_OK.format(survey_id.upper()) == ret[1] assert out.REMINDER_OK.format(survey_id.upper(), am_check.upper()) == ret[2] data = my_backend.list_surveys(some_user)[0] assert data[0] == some_user assert data[1] == survey_id assert data[2] == am_check.upper() assert data[3] == None my_backend.leave_survey(some_user, survey_id) #join survey with PM reminder ret = sleek.join(["join", survey_id, pm_check], context) assert out.SURVEY_JOIN_OK.format(survey_id.upper()) == ret[1] assert out.REMINDER_OK.format(survey_id.upper(), pm_check.upper()) == ret[2] data = my_backend.list_surveys(some_user)[0] assert data[0] == some_user assert data[1] == survey_id assert data[2] == None assert data[3] == pm_check.upper() my_backend.leave_survey(some_user, survey_id) #join survey with both reminders ret = sleek.join(["join", survey_id, pm_check, am_check], context) assert out.SURVEY_JOIN_OK.format(survey_id.upper()) == ret[1] assert out.REMINDER_OK_2.format(survey_id.upper(), am_check.upper(), pm_check.upper()) == ret[2] data = my_backend.list_surveys(some_user)[0] assert data[0] == some_user assert data[1] == survey_id assert data[2] == am_check.upper() assert data[3] == pm_check.upper() my_backend.leave_survey(some_user, survey_id)
def get_api_token(key, method="env"): if method == "env": return os.getenv(key) else: raise NotImplementedError if __name__ == "__main__": parser = get_parser() args = parser.parse_args() confs = json.load(open(args.cfg, 'r')) if args.init: db_type = confs["backend_type"] if db_type == "local": db = Backend(confs, init=True) print "[backend @ {} was initialized]".format(confs["local_DB"]) elif db_type == "postgre": db = PostgreBackend(confs, init=True) print "[backend @ {} was initialized]".format( confs["remote_DB"]["host"]) if args.surveys is not None: if db_type == "local": db = Backend(confs) elif db_type == "postgre": db = PostgreBackend(confs) db.load_surveys(args.surveys) print "loaded surveys" elif args.connect: try: api_token_id = confs["api_token_id"]