Esempio n. 1
0
def test_connect_and_initialize_database_with_absolute_windows_path(
        monkeypatch):
    m = Mock()
    monkeypatch.setattr(database, "create_engine", m)
    win_path = PureWindowsPath("C:\\path\\to\\foo.db")
    api.connect_and_initialize_database("sqlite", str(win_path))
    assert "sqlite:///C:\\path\\to\\foo.db" in m.call_args[0]
Esempio n. 2
0
def _populate_db():
    """
    Run with `python -c "from urdailyemail import email; email._populate_db()"`
    :return:
    """
    with Session() as session:
        api.session_connect_to_ur(session, os.environ["UR_USER"],
                                  os.environ["UR_PASS"])
        purchases_soup = api.get_purchase_history(session, 8)
        purchases = {
            item.id: item
            for sublist in api.convert_purchase_history(purchases_soup)
            for item in sublist
        }
        api.connect_and_initialize_database(
            "sqlite",
            str(
                Path(os.environ["USERPROFILE"] +
                     "\Desktop\database\collection.sqlite")))
        # purchases = []
        # purchases.append(Purchase(name="Nameko", id=1540, price=3500, level=3))
        # purchases.append(Purchase(name="Guillotinette",
        #                           id=1638, price=1000, level=5))
        # purchases.append(
        #     Purchase(name="Gus Rope", id=1632, price=10000, level=3))
        # purchases.append(
        #     Purchase(name="Sopiket", id=1703, price=7000, level=3))
        # purchases.append(Purchase(name="Lucky", id=1338, price=2500, level=2))
        api.write_history_to_database([item for item in purchases.values()])
Esempio n. 3
0
def test_get_missions_from_database_with_filters(monkeypatch, mocked_missions):
    monkeypatch.setattr(Session, "get", lambda a, b: mocked_missions)
    page = api.get_missions_list(Session(), "flash")
    missions = api.convert_missions(page)
    api.connect_and_initialize_database("sqlite",
                                        "data/test_collection.sqlite")
    api.write_missions_to_database(missions)
    res = api.get_missions_from_database({"goal": 80})
    try:
        assert len(res) == 1
        assert res[0].name == "Komboka OP"
        assert res[0].progress == 0
    finally:
        with database.get_session() as session:
            session.query(mission.Mission).delete()
Esempio n. 4
0
def test_get_missions_from_database(monkeypatch, mocked_missions):
    monkeypatch.setattr(Session, "get", lambda a, b: mocked_missions)
    page = api.get_missions_list(Session(), "flash")
    missions = api.convert_missions(page)
    api.connect_and_initialize_database("sqlite",
                                        "data/test_collection.sqlite")
    api.write_missions_to_database(missions)
    res = api.get_missions_from_database()
    try:
        assert res[0].hash_id
        assert res[0].name != ""
        assert res[0].goal > 0
    finally:
        with database.get_session() as session:
            session.query(mission.Mission).delete()
Esempio n. 5
0
def test_get_history_from_database_with_filters(monkeypatch, mocked_history):
    monkeypatch.setattr(Session, "get", lambda a, b: mocked_history)
    pages = api.get_purchase_history(Session(), 1)
    purchases = [
        item for sublist in api.convert_purchase_history(pages)
        for item in sublist
    ]
    api.connect_and_initialize_database("sqlite",
                                        "data/test_collection.sqlite")
    api.write_history_to_database(purchases)
    res = api.get_history_from_database({"name": "Raoul"})
    try:
        assert len(res) == 5
        assert res[0].name == "Raoul"
        assert res[0].id == 1462
    finally:
        with database.get_session() as session:
            session.query(purchase.Purchase).delete()
Esempio n. 6
0
def test_get_history_from_database(monkeypatch, mocked_history):
    monkeypatch.setattr(Session, "get", lambda a, b: mocked_history)
    pages = api.get_purchase_history(Session(), 1)
    purchases = [
        item for sublist in api.convert_purchase_history(pages)
        for item in sublist
    ]
    api.connect_and_initialize_database("sqlite",
                                        "data/test_collection.sqlite")
    api.write_history_to_database(purchases)
    res = api.get_history_from_database()
    try:
        assert res[0].hash_id
        assert res[0].name != ""
        assert res[0].id > 0
    finally:
        with database.get_session() as session:
            session.query(purchase.Purchase).delete()
Esempio n. 7
0
def test_write_missions_to_database(monkeypatch, mocked_missions):
    monkeypatch.setattr(Session, "get", lambda a, b: mocked_missions)
    page = api.get_missions_list(Session(), "flash")
    missions = api.convert_missions(page)
    mission_hashes = [miss.hash_id for miss in missions]
    api.connect_and_initialize_database("sqlite",
                                        "data/test_collection.sqlite")
    api.write_missions_to_database(missions)
    try:
        with database.get_session() as session:
            res = [
                item[0]
                for item in session.query(mission.Mission.hash_id).all()
            ]
            for hash_id in mission_hashes:
                assert hash_id in res
    finally:
        with database.get_session() as session:
            session.query(mission.Mission).delete()
Esempio n. 8
0
def test_write_history_to_database(monkeypatch, mocked_history):
    monkeypatch.setattr(Session, "get", lambda a, b: mocked_history)
    pages = api.get_purchase_history(Session(), 1)
    purchases = [
        item for sublist in api.convert_purchase_history(pages)
        for item in sublist
    ]
    purchase_hashes = [purch.hash_id for purch in purchases]
    api.connect_and_initialize_database("sqlite",
                                        "data/test_collection.sqlite")
    api.write_history_to_database(purchases)
    try:
        with database.get_session() as session:
            res = [
                item[0]
                for item in session.query(purchase.Purchase.hash_id).all()
            ]
            for hash_id in purchase_hashes:
                assert hash_id in res
    finally:
        with database.get_session() as session:
            session.query(purchase.Purchase).delete()
Esempio n. 9
0
def test_connect_and_initialize_database_with_absolute_posix_path(monkeypatch):
    m = Mock()
    monkeypatch.setattr(database, "create_engine", m)
    posix_path = PurePosixPath("/absolute/path/to/foo.db")
    api.connect_and_initialize_database("sqlite", str(posix_path))
    assert "sqlite:////absolute/path/to/foo.db" in m.call_args[0]
Esempio n. 10
0
def test_connect_and_initialize_database_with_relative_path(monkeypatch):
    m = Mock()
    monkeypatch.setattr(database, "create_engine", m)
    api.connect_and_initialize_database("sqlite", "files/my.db")
    assert "sqlite:///files/my.db" in m.call_args[0]
Esempio n. 11
0
def test_connect_and_initialize_database_no_path():
    api.connect_and_initialize_database("sqlite")
    assert 'sqlite:///data/collection.sqlite' == str(database.engine.url)
Esempio n. 12
0
def generate_email():
    """
    Run from command line via python -c "from urdailyemail import email; email.generate_email()"
    """
    with Session() as session:
        # Set up database and session initialization
        url = "https://ohbucketmybucket.s3-us-west-1.amazonaws.com/collection.sqlite"
        # file_path = Path(os.environ["USERPROFILE"] +
        #                  "\Desktop\database\collection.sqlite")
        file_path = os.environ["DB_FILE_PATH"]
        with get(url) as response, open(file_path, 'wb') as out_file:
            out_file.write(response.content)
        api.connect_and_initialize_database("sqlite", str(Path(file_path)))
        api.session_connect_to_ur(session, os.environ["UR_USER"],
                                  os.environ["UR_PASS"])

        # Get purchases from database to check against
        full_purchases = api.get_history_from_database()
        purchases = list(
            islice((purchase for purchase in full_purchases),
                   int(os.environ["PURCHASE_COUNT"])))

        # Get market offers for purchases
        offers = market.get_market_offers(
            session, [purchase.id for purchase in purchases])

        # Get black market flash mission
        try:
            missions = api.convert_missions(
                api.get_missions_list(session, "black market"))
            bm_missions = [
                mission for mission in missions
                if "BLACK MARKET" in mission.name
            ]
            bm_char_offers = {}
            for bm_mission in bm_missions:
                bm_mission_character = re.search('\\d(.+)(\\W)>',
                                                 bm_mission.name)
                if bm_mission_character:
                    bm_mission_character = str(
                        bm_mission_character.group(1)).strip()
                else:
                    logging.error("Failed to process %s", bm_mission.name)
                try:
                    bm_offers = market.get_market_offers(
                        session, [bm_mission_character])
                    bm_char_offers[
                        bm_mission.name] = bm_offers[bm_mission_character]
                except Exception as exception:
                    logging.error(
                        "Failed while generating black market offers: %s",
                        str(exception))
        except Exception as exception:
            logging.exception(f"62 - {exception}")
            bm_missions = [api.get_missions_list(session, "black market").text]
            bm_char_offers = None

        # Create table for email
        table_rows = create_table_rows(purchases, offers)
        mission_table = create_market_mission_table(bm_missions,
                                                    bm_char_offers)
        final_content = create_table(table_rows, mission_table)
        _send_email(final_content)
    return "OK"