def test_append_same_result_to_snapshot_twice__added_only_once_in_db(): in_mem_db_url = 'sqlite://' # Just making sure the line below doesn't throw s = Saver(in_mem_db_url) r = Reader(in_mem_db_url) r.engine = s.engine param_id = 1 res_id = s.get_or_create_user_id( uid=param_id, name='Testy', bday=101010, gender='m') assert res_id == param_id # Line below should not throw, just return None parser1 = 'parser1' dtime = 1122 snap_id = s.update_or_create_snapshot( uid=param_id, datetime=dtime, new_available_result=parser1) parser2 = 'parser1' dtime = 1122 snap_id = s.update_or_create_snapshot( uid=param_id, datetime=dtime, new_available_result=parser2) res_snap = r.get_snapshot(param_id, snap_id) assert parser1 in res_snap['available_results'] results_arr = json.loads(res_snap['available_results']) assert len(results_arr) == 1
def test_save_new_user(): in_mem_db_url = 'sqlite://' # Just making sure the line below doesn't throw s = Saver(in_mem_db_url) param_id = 1 res_id = s.get_or_create_user_id( uid=param_id, name='Testy', bday=101010, gender='m') assert res_id == param_id
def test_save_2_new_users_same_ids(): in_mem_db_url = 'sqlite://' # Just making sure the line below doesn't throw s = Saver(in_mem_db_url) param_id = 1 res_id = s.get_or_create_user_id( uid=param_id, name='Testy', bday=101010, gender='m') assert res_id == param_id # Line below should not throw, just return None res_id = s.get_or_create_user_id( uid=param_id, name='Pesty', bday=201010, gender='f') assert res_id is None
def test_save_new_user_assert_name_written_to_db(): in_mem_db_url = 'sqlite://' # Just making sure the line below doesn't throw s = Saver(in_mem_db_url) r = Reader(in_mem_db_url) r.engine = s.engine param_id = 1 res_id = s.get_or_create_user_id( uid=param_id, name='Testy', bday=101010, gender='m') found_users = r.get_users() assert found_users is not None assert found_users[param_id] == 'Testy'
def test_save_snapshot(): in_mem_db_url = 'sqlite://' # Just making sure the line below doesn't throw s = Saver(in_mem_db_url) param_id = 1 res_id = s.get_or_create_user_id( uid=param_id, name='Testy', bday=101010, gender='m') assert res_id == param_id # Line below should not throw, just return None snap_id = s.update_or_create_snapshot( uid=param_id, datetime=1122, new_available_result='parser1') assert snap_id is not None assert isinstance(snap_id, int)
def test_get_non_existent_user__single_user_inserted__raise_ex(): # Arrange saver = Saver('sqlite://') api.readerInst = saver # hot-wiring the 'api' to our mock db user_id = 13 uname = 'testy' bday = 10101 gender = 'm' saver.get_or_create_user_id(user_id, uname, bday, gender) # Act with pytest.raises(Exception): res = api.get_user(22)
def test_save_multi_snapshots_get_different_ids(): in_mem_db_url = 'sqlite://' # Just making sure the line below doesn't throw s = Saver(in_mem_db_url) param_id = 1 res_id = s.get_or_create_user_id( uid=param_id, name='Testy', bday=101010, gender='m') assert res_id == param_id # Line below should not throw, just return None snap_id1 = s.update_or_create_snapshot( uid=param_id, datetime=1122, new_available_result='parser1') snap_id2 = s.update_or_create_snapshot( uid=param_id, datetime=1133, new_available_result='parser1') assert snap_id1 != snap_id2
def test_get_user__user_doesnt_exist__404_returned(client): # Arrange wserver = WebServer('sqlite://') gui.serverInst = wserver saver = Saver('sqlite://') saver.engine = wserver.reader.engine saved_msg = fill_db(saver) # Act res = client.get(f'/users/9988') # Assert assert res is not None assert res._status_code == 404
def test_get_snap__no_such_snap__raise_exception(): # Arrange saver = Saver('sqlite://') api.readerInst = saver # hot-wiring the 'api' to our mock db user_id = 13 uname = 'testy' bday = 10101 gender = 'm' saver.get_or_create_user_id(user_id, uname, bday, gender) snap_id1 = saver.update_or_create_snapshot(user_id, 1122, 'pose') snap_id2 = saver.update_or_create_snapshot(user_id, 3344, 'pose') # Act with pytest.raises(Exception): api.get_snapshot(user_id, 9900)
def test_get_user__single_user__actual_user_returned(): # Arrange saver = Saver('sqlite://') api.readerInst = saver # hot-wiring the 'api' to our mock db user_id = 13 uname = 'testy' bday = 10101 gender = 'm' saver.get_or_create_user_id(user_id, uname, bday, gender) # Act res = api.get_user(user_id) # Assert assert res is not None assert res['id'] == user_id
def test_get_result_data__result_doesnt_exist__404_returned(client): # Arrange wserver = WebServer('sqlite://') gui.serverInst = wserver saver = Saver('sqlite://') saver.engine = wserver.reader.engine saved_msg = fill_db(saver) uid = str(saved_msg['user_info']['uid']) snap_id = 1 # Act res = client.get(f'/users/{uid}/snapshots/{snap_id}/pose/data') # Assert assert res is not None assert res._status_code == 404
def test_get_users_list__single_user__actual_list_returned(): # Arrange saver = Saver('sqlite://') api.readerInst = saver # hot-wiring the 'api' to our mock db user_id = 13 uname = 'testy' bday = 10101 gender = 'm' saver.get_or_create_user_id(user_id, uname, bday, gender) # Act res_list = api.get_users_list() # Assert assert user_id in res_list assert res_list[user_id] == uname
def test_get_timeline_data__user_exist__page_returned(client): # Arrange wserver = WebServer('sqlite://') gui.serverInst = wserver saver = Saver('sqlite://') saver.engine = wserver.reader.engine saved_msg = fill_db(saver) uid = str(saved_msg['user_info']['uid']) snap_id = 1 # Act res = client.get(f'/users/{uid}/timeline_data.html') # Assert assert res is not None assert res._status_code == 200
def test_get_result_data__invalid_snapshot_id__raise_exception(): # Arrange saver = Saver('sqlite://') api.readerInst = saver # hot-wiring the 'api' to our mock db user_id = 13 uname = 'testy' bday = 10101 gender = 'm' saver.get_or_create_user_id(user_id, uname, bday, gender) parser_name = 'pose' snap_id = saver.update_or_create_snapshot(user_id, 1122, parser_name) content = "{'test':'tset'}" saver.save_parser_res(parser_name, snap_id, content) # Act with pytest.raises(Exception): res = api.get_result_data(user_id, 9988, parser_name)
def test_get_users__page_returned(client): # Arrange wserver = WebServer('sqlite://') gui.serverInst = wserver saver = Saver('sqlite://') saver.engine = wserver.reader.engine saved_msg = fill_db(saver) # Act res = client.get('/users') # Assert assert res is not None assert res._status_code == 200 assert res.data is not None assert str(saved_msg['user_info']['uid']) in str(res.data) assert str(saved_msg['user_info']['name']) in str(res.data)
def test_get_snap__valid_ids__snapshot_returned(): # Arrange saver = Saver('sqlite://') api.readerInst = saver # hot-wiring the 'api' to our mock db user_id = 13 uname = 'testy' bday = 10101 gender = 'm' saver.get_or_create_user_id(user_id, uname, bday, gender) snap_id1 = saver.update_or_create_snapshot(user_id, 1122, 'pose') snap_id2 = saver.update_or_create_snapshot(user_id, 3344, 'pose') # Act snap = api.get_snapshot(user_id, snap_id1) # Assert assert snap is not None assert snap['id'] == snap_id1
def test_append_result_to_snapshot__same_snap_id_returned(): in_mem_db_url = 'sqlite://' # Just making sure the line below doesn't throw s = Saver(in_mem_db_url) param_id = 1 res_id = s.get_or_create_user_id( uid=param_id, name='Testy', bday=101010, gender='m') assert res_id == param_id # Line below should not throw, just return None parser1 = 'parser1' dtime = 1122 snap_id1 = s.update_or_create_snapshot( uid=param_id, datetime=dtime, new_available_result=parser1) parser2 = 'parser2' snap_id2 = s.update_or_create_snapshot( uid=param_id, datetime=dtime, new_available_result=parser2) assert snap_id1 == snap_id2
def test_get_parser_res__valid_ids__res_returned(): # Arrange saver = Saver('sqlite://') api.readerInst = saver # hot-wiring the 'api' to our mock db user_id = 13 uname = 'testy' bday = 10101 gender = 'm' saver.get_or_create_user_id(user_id, uname, bday, gender) parser_name = 'pose' snap_id = saver.update_or_create_snapshot(user_id, 1122, parser_name) content = "{'test':'tset'}" saver.save_parser_res(parser_name, snap_id, content) # Act res = api.get_result(user_id, snap_id, parser_name) # Assert assert res is not None assert res == content
def test_get_snaps_list__single_user_multi_snaps__actual_list_returned(): # Arrange saver = Saver('sqlite://') api.readerInst = saver # hot-wiring the 'api' to our mock db user_id = 13 uname = 'testy' bday = 10101 gender = 'm' saver.get_or_create_user_id(user_id, uname, bday, gender) snap_id1 = saver.update_or_create_snapshot(user_id, 1122, 'pose') snap_id2 = saver.update_or_create_snapshot(user_id, 3344, 'pose') # Act res = api.get_user_snapshots_list(user_id) # Assert assert res is not None assert snap_id1 in res assert snap_id2 in res assert res[snap_id2] != res[snap_id1]
def test_get_snaps_list__single_user_single_snap__actual_list_returned(): # Arrange saver = Saver('sqlite://') api.readerInst = saver # hot-wiring the 'api' to our mock db user_id = 13 uname = 'testy' bday = 10101 gender = 'm' saver.get_or_create_user_id(user_id, uname, bday, gender) dtime = 1122 parser_name = 'pose' snap_id = saver.update_or_create_snapshot(user_id, dtime, parser_name) # Act res = api.get_user_snapshots_list(user_id) # Assert assert res is not None assert snap_id in res assert res[snap_id]['datetime'] == dtime
def test_save_parser_res__results_id_retured(): in_mem_db_url = 'sqlite://' # Just making sure the line below doesn't throw s = Saver(in_mem_db_url) r = Reader(in_mem_db_url) r.engine = s.engine param_id = 1 res_id = s.get_or_create_user_id( uid=param_id, name='Testy', bday=101010, gender='m') assert res_id == param_id # Line below should not throw, just return None parser_name = 'pose' data = "{'test':'tset'}" dtime = 1122 snap_id = s.update_or_create_snapshot( uid=param_id, datetime=dtime, new_available_result=parser_name) res_id = s.save_parser_res(parser_name, snap_id, data) assert res_id is not None
def test_save_snapshot__written_to_db(): in_mem_db_url = 'sqlite://' # Just making sure the line below doesn't throw s = Saver(in_mem_db_url) r = Reader(in_mem_db_url) r.engine = s.engine param_id = 1 res_id = s.get_or_create_user_id( uid=param_id, name='Testy', bday=101010, gender='m') assert res_id == param_id # Line below should not throw, just return None parser = 'parser1' dtime = 1122 snap_id = s.update_or_create_snapshot( uid=param_id, datetime=dtime, new_available_result=parser) res_snap = r.get_snapshot(param_id, snap_id) assert res_snap is not None assert res_snap['uid'] == param_id assert res_snap['id'] == snap_id assert res_snap['datetime'] == dtime assert parser in res_snap['available_results']
def test_mem_db(): in_mem_db_url = 'sqlite://' # Just making sure the line below doesn't throw s = Saver(in_mem_db_url)