def test_multiple_users_exclusive(conn: connection, dbx: conftest.MockDropbox) -> None: users = ["slack_nick2", "slack_nick3"] exclusive_pic = "test_pic4" # get seed that returns nonexclusive for seed in range(0, 20): with conn.cursor() as cursor: cursor.execute(f"select setseed(0.{seed})") url1, timestamp, description = pictures.get_pic(conn, dbx, arg_list=users) assert_valid_returns(url1, timestamp, description) if not url1.endswith(exclusive_pic): break else: # no test coverage raise Exception("could not find good seed") with conn.cursor() as cursor: cursor.execute(f"select setseed(0.{seed})") url2, timestamp, description = pictures.get_pic(conn, dbx, arg_list=["kun"] + users) assert_valid_returns(url2, timestamp, description) assert url2.endswith(exclusive_pic) for _ in range(10): url3, timestamp, description = pictures.get_pic(conn, dbx, arg_list=["kun"] + users) assert_valid_returns(url3, timestamp, description) pic = next(pic for pic in conftest.pics if url3.endswith(pic.path)) assert pic.faces == [2, 3], f"Wrong picture {pic}"
def test_reduce_args(conn: connection, dbx: conftest.MockDropbox) -> None: arg_list = ["kun", "slack_nick11"] url, timestamp, description = pictures.get_pic(conn, dbx, arg_list=arg_list) assert description == ("Fant ikke bilde med `kun`, `slack_nick11`. " "Her er et bilde med `slack_nick11` i stedet.")
def cmd_pic(args: t.Optional[t.List[str]], conn: connection, dbx: dropbox.Dropbox) -> t.Dict: """if command is 'pic'""" picurl, date, description = pictures.get_pic(conn, dbx, args) pretty_date = prettify_date(date) blocks = [] image_block = {"type": "image", "image_url": picurl, "alt_text": picurl} blocks.append(image_block) context_block: t.Dict[str, t.Any] = { "type": "context", "elements": [{ "type": "mrkdwn", "text": pretty_date }], } blocks.append(context_block) if description: description_block: t.Dict[str, t.Any] = { "type": "context", "elements": [{ "type": "mrkdwn", "text": description }], } blocks.append(description_block) response = {"text": picurl, "blocks": blocks} return response
def pic(args: t.Optional[str] = None): gargling_id = get_jwt_identity() log.info(gargling_id) arg_list = args.split(",") if args is not None else [] with app.pool.get_connection() as conn: pic_url, *_ = pictures.get_pic(conn, app.dbx, arg_list=arg_list) return jsonify({"url": pic_url})
def formulate_congrat(recipient: Recipient, conn: connection, dbx: Dropbox) -> t.Dict: sentence = queries.random_sentence(conn)["sentence"] text = ( f"Hurra! Vår felles venn <@{recipient.slack_id}> fyller {recipient.age} i dag!\n" f"{sentence}") person_picurl, date, _ = pictures.get_pic(conn, dbx, [recipient.nick]) response = { "text": text, "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": text } }, { "type": "image", "image_url": person_picurl, "alt_text": person_picurl }, { "type": "image", "image_url": mort_picurl, "alt_text": mort_picurl }, ], } return response
def test_error_txt(conn: connection, dbx: conftest.MockDropbox) -> None: url, timestamp, description = pictures.get_pic(conn, dbx, arg_list=["2000"]) assert url.startswith("https") assert type(timestamp) == dt.datetime assert description.startswith("Im so stoopid") assert description.endswith("Her er et tilfeldig bilde i stedet.")
def test_error_txt_with_impossible_combination( conn: connection, dbx: conftest.MockDropbox) -> None: url, timestamp, description = pictures.get_pic(conn, dbx, arg_list=["2001", "topic3"]) assert url.startswith("https") assert type(timestamp) == dt.datetime assert description.startswith("Fant ikke") assert "Her er et bilde med" in description
def test_error_txt_with_valid(conn: connection, dbx: conftest.MockDropbox) -> None: url, timestamp, description = pictures.get_pic( conn, dbx, arg_list=["1999", "slack_nick5"]) assert url.startswith("https") assert type(timestamp) == dt.datetime assert description.startswith("Im so stoopid") assert "Her er et bilde med" in description pic = next(pic for pic in conftest.pics if url.endswith(pic.path)) assert 5 in pic.faces
def test_multiple_args(conn: connection, dbx: conftest.MockDropbox) -> None: arg_list = ["slack_nick2", "topic1", "2001"] url, timestamp, description = pictures.get_pic(conn, dbx, arg_list=arg_list) assert_valid_returns(url, timestamp, description) pic = next(pic for pic in conftest.pics if url.endswith(pic.path)) assert pic.topic == "topic1" assert pic.taken_at.year == 2001 assert 2 in pic.faces
def test_user_exclusive(conn: connection, dbx: conftest.MockDropbox) -> None: user = "******" exclusive_pic = "test_pic7" # get seed that returns nonexclusive for seed in range(1, 10): with conn.cursor() as cursor: cursor.execute(f"select setseed(0.{seed})") url1, timestamp, description = pictures.get_pic(conn, dbx, arg_list=[user]) assert_valid_returns(url1, timestamp, description) if not url1.endswith(exclusive_pic): break else: # no test coverage raise Exception("could not find good seed") with conn.cursor() as cursor: cursor.execute(f"select setseed(0.{seed})") url2, timestamp, description = pictures.get_pic(conn, dbx, arg_list=["kun", user]) assert_valid_returns(url2, timestamp, description) assert url2.endswith(exclusive_pic)
def test_multiple_users(conn: connection, dbx: conftest.MockDropbox) -> None: users = ["slack_nick11", "slack_nick3"] url, timestamp, description = pictures.get_pic(conn, dbx, arg_list=users) assert_valid_returns(url, timestamp, description) pic = next(pic for pic in conftest.pics if url.endswith(pic.path)) assert {11, 3}.issubset(pic.faces), f"Wrong picture {pic}"
def test_user(conn: connection, dbx: conftest.MockDropbox) -> None: user = "******" url, timestamp, description = pictures.get_pic(conn, dbx, arg_list=[user]) assert_valid_returns(url, timestamp, description) pic = next(pic for pic in conftest.pics if url.endswith(pic.path)) assert 3 in pic.faces
def test_year(conn: connection, dbx: conftest.MockDropbox) -> None: year = "2002" url, timestamp, description = pictures.get_pic(conn, dbx, arg_list=[year]) assert_valid_returns(url, timestamp, description) pic = next(pic for pic in conftest.pics if url.endswith(pic.path)) assert pic.taken_at.year == int(year)
def test_topic(conn: connection, dbx: conftest.MockDropbox) -> None: topic = "topic1" url, timestamp, description = pictures.get_pic(conn, dbx, arg_list=[topic]) assert_valid_returns(url, timestamp, description) pic = next(pic for pic in conftest.pics if url.endswith(pic.path)) assert pic.topic == topic
def test_random(conn: connection, dbx: conftest.MockDropbox) -> None: url, timestamp, description = pictures.get_pic(conn, dbx, arg_list=None) assert_valid_returns(url, timestamp, description)