Пример #1
0
def test_saving(fix_requests, fix_input):
    from campbot import CampBot

    fix_input.set_response(lambda x: "y")

    area = CampBot().wiki.get_area(area_id=14273)
    area.save("Test")
Пример #2
0
def test_add_user_to_group(fix_requests):
    from campbot import CampBot

    bot = CampBot()
    bot.forum.add_users_to_group("Association", [
        "rabot",
    ])
Пример #3
0
def test_checkers(fix_requests):
    from campbot.checkers import LengthTest, ReTest, HistoryTest, MainWaypointTest, RouteTypeTest
    from campbot import CampBot
    from campbot.objects import Contribution

    bot = CampBot()

    route = bot.wiki.get_route(123)
    contrib = Contribution(bot, {"document": route, "user": {}})

    LengthTest("fr")(None, contrib, contrib)

    LengthTest("fr")(None, None, contrib)
    LengthTest("fr")(None, contrib, None)

    HistoryTest("fr")(None, contrib, contrib)
    HistoryTest("fr")(None, None, contrib)
    MainWaypointTest()(None, contrib, contrib)
    MainWaypointTest()(None, None, contrib)
    RouteTypeTest()(None, contrib, contrib)
    RouteTypeTest()(None, None, contrib)

    t = ReTest("x", "fr")
    t.patterns.append("e")
    t(None, contrib, contrib)
Пример #4
0
def test_all_replacements(fix_requests):
    from campbot import CampBot
    from campbot.objects import Route
    from campbot.processors import get_automatic_replacments

    source = """
    deja en éte\u0301
    """

    corrected = """
    déjà en été
    """

    bot = CampBot()
    processors = get_automatic_replacments(bot)
    r = Route(bot, {
        "locales": [
            {
                "lang": "fr",
                "description": source
            }
        ]
    })

    for processor in processors:
        if processor.ready_for_production:
            processor(r, ["fr"])

    assert r.get_locale("fr").description == corrected
Пример #5
0
    def complete_contributions(self):
        bot = CampBot(min_delay=0.01)

        highest_version_id = self.get_highest_version_id("contribution")

        cur = self._conn.cursor()

        for i, contrib in enumerate(
                bot.wiki.get_contributions(oldest_date="1990-12-25")):
            if highest_version_id >= contrib.version_id:
                break

            print(contrib.written_at, contrib.version_id,
                  contrib.user.username)

            doc = contrib.document
            try:
                cur.execute(
                    "INSERT INTO contribution"
                    "(document_id, type, version_id, user_id, written_at)"
                    "VALUES (?,?,?,?,?)",
                    (
                        doc.document_id,
                        doc.type,
                        contrib.version_id,
                        contrib.user.user_id,
                        contrib.written_at,
                    ),
                )
            except sqlite3.IntegrityError:
                pass

        self._conn.commit()
Пример #6
0
    def complete(self):

        bot = CampBot(min_delay=0.01)

        still_done = []
        highest_version_id = self.get_highest_version_id()

        cur = self._conn.cursor()

        for i, contrib in enumerate(
                bot.wiki.get_contributions(oldest_date="2018-01-24")):
            if highest_version_id >= contrib.version_id:
                break

            key = (contrib.document.document_id, contrib.document.type)
            if key not in still_done:
                still_done.append(key)
                doc = contrib.get_full_document()

                self.insert(
                    cur=cur,
                    contrib=contrib,
                    version_id=contrib.version_id,
                    base_doc=doc,
                )
                print(i, contrib.written_at, key, "inserted")

        self._conn.commit()

        self.complete_contributions()
Пример #7
0
def test_post_message(fix_requests):
    from campbot import CampBot

    url = "https://forum.camptocamp.org/t/topoguide-verifications-automatiques/201480"

    bot = CampBot()

    bot.forum.post_message("coucou", url)
Пример #8
0
def get_campbot(args):
    from campbot import CampBot

    proxies = {}

    if "HTTPS_PROXY" in os.environ:
        proxies["https"] = os.environ["HTTPS_PROXY"]

    if "CAMPBOT_CREDENTIALS" in os.environ and not args["--login"]:
        args["--login"], args["--password"] = os.environ[
            "CAMPBOT_CREDENTIALS"].split("@", 1)

    bot = CampBot(proxies=proxies, min_delay=args["--delay"])

    if args["--login"] and args["--password"]:
        bot.login(login=args["--login"], password=args["--password"])

    return bot
Пример #9
0
def test_weird(fix_requests, fix_input):
    from campbot import CampBot, __main__
    import os

    fix_input.set_response(lambda x: "n")

    bot = CampBot()

    obj = bot.wiki.get_article(123)
    obj.document_id = 1
    assert obj["document_id"] == 1
    assert obj.search(["xxx"], "fr") == False
    assert obj.save("test", True) is None

    user = bot.wiki.get_profile(123)
    assert user.get_last_contribution(newest_date="1970-01-01") is None

    obj = bot.wiki.get_waypoint(123)
    assert obj.get_invalidity_reason() == "elevation is missing"
    obj.elevation = 12
    assert obj.get_invalidity_reason() is None
    obj.waypoint_type = "hut"
    obj.custodianship = None
    assert obj.get_invalidity_reason() == "custodianship is missing"

    obj = bot.wiki.get_wiki_object(123, "o")
    assert obj.is_personal() == True

    os.environ["CAMPBOT_LOGIN"] = "******"
    os.environ["CAMPBOT_PASSWORD"] = "******"
    args = get_main_args("clean")
    assert args["--login"] == "x"
    assert args["--password"] == "y"
    args["--login"] = False
    __main__.main(args)

    with open("contributors.txt", "w") as f:
        f.write("666|1\n")
        f.write("667|1\n")
        f.write("667|100\n")
        f.write("668|100\n")
        f.write("123|1\n")

    bot.get_new_contributors()
Пример #10
0
def test_distance(fix_requests):
    from campbot import CampBot, utils

    bot = CampBot()

    item1 = bot.wiki.get_route(123)
    item2 = bot.wiki.get_wiki_object_version(293549, "r", "fr", 880880)
    item3 = bot.wiki.get_waypoint(123)

    assert utils.compute_distance(item1, item2) is None
    assert 571.9 < utils.compute_distance(item1, item3) < 572
Пример #11
0
    def re_update(self):
        from campbot import CampBot

        # c = self._conn.execute("SELECT document.document_id, document.type FROM document "
        #                        "LEFT OUTER JOIN string_property "
        #                        "    ON string_property.document_id = document.document_id "
        #                        "WHERE field IS NULL")

        c = self._conn.execute(
            "SELECT locale.document_id, document.type FROM locale "
            "LEFT JOIN document on document.document_id=locale.document_id "
            "WHERE field='blob'")

        result = c.fetchall()

        bot = CampBot(min_delay=0.01)
        cur = None

        for i, (document_id, typ) in enumerate(result):
            if i % 50 == 0:
                self._conn.commit()
                cur = self._conn.cursor()

            t = time()
            try:
                doc = bot.wiki.get_wiki_object(item_id=document_id,
                                               document_type=typ)
                get_time = int((time() - t) * 1000)
                doc["document_id"] = document_id  # for redirects...
            except HTTPError as e:
                if e.response.status_code == 404:
                    print(document_id, "is deleted")
                    self._delete(document_id, cur)
                else:
                    raise
            else:
                t = time()
                self.insert(cur=cur, base_doc=doc)
                print(
                    "{}/{}".format(i, len(result)),
                    document_id,
                    typ,
                    get_time,
                    int((time() - t) * 1000),
                )

        self._conn.commit()
Пример #12
0
def test_all_replacements(fix_requests):
    from campbot import CampBot
    from campbot.objects import Route
    from campbot.processors import get_automatic_replacments

    source = """
    deja en éte\u0301
    """

    corrected = """
    déjà en été
    """

    bot = CampBot()
    processors = get_automatic_replacments(bot)
    r = Route(
        bot,
        {
            "locales": [
                {
                    "lang": "fr",
                    "title": source,
                    "description": source,
                    "slope": source,
                    "external_resources": source,
                }
            ]
        },
    )

    for processor in processors:
        if processor.ready_for_production:
            processor(r, ["fr"])

    assert r.get_locale("fr").description == corrected
    assert r.get_locale("fr").title == source, "title field must no be corrected"
    assert r.get_locale("fr").slope == source, "slope field must no be corrected"
    assert (
        r.get_locale("fr").external_resources == source
    ), "external_resources field must no be corrected"
Пример #13
0
def test_dump(fix_requests, fix_dump):
    from campbot.dump import Dump, get_document_types, _search
    from campbot import CampBot

    get_document_types()

    route = CampBot().wiki.get_route(123)

    class Contrib:
        document = route

    dump = Dump()
    dump.insert(dump._conn.cursor(), route, 1687340, Contrib)
    dump._conn.commit()
    dump.complete()
    dump.select(123)
    dump.search("r")
    dump.get_all_ids()

    _search("r")

    del dump
    os.remove("test.db")
Пример #14
0
def test_get_closest_documents(fix_requests):
    from campbot import CampBot, objects

    bot = CampBot()

    bot.find_closest_documents(objects.Waypoint, 289284, 6175526, 2000)
Пример #15
0
def test_login(fix_requests):
    from campbot import CampBot

    CampBot().login("x", "y")
Пример #16
0
def test_wiki(fix_requests, fix_input):
    from campbot import CampBot

    fix_input.set_response(lambda x: "y")

    route = CampBot().wiki.get_route(route_id=293549)
    route.is_personal()
    route.get_title("fr")

    xreport = CampBot().wiki.get_xreport(xreport_id=293549)
    xreport.is_personal()

    image = CampBot().wiki.get_image(image_id=1005116)
    image.is_personal()

    waypoint = CampBot().wiki.get_waypoint(waypoint_id=952999)
    waypoint.get_invalidity_reason()

    article = CampBot().wiki.get_article(article_id=1003911)
    article.is_personal()

    area = CampBot().wiki.get_area(area_id=14273)
    area.save("Test")

    CampBot().wiki.get_profile(profile_id=293549)
    CampBot().wiki.get_book(book_id=293549)
    CampBot().wiki.get_map(map_id=293549)

    list(CampBot().wiki.get_routes({}))
    list(CampBot().wiki.get_waypoints({}))
    list(CampBot().wiki.get_outings({}))
    list(CampBot().wiki.get_xreports({}))

    for _ in CampBot().wiki.get_route_ids():
        break

    for _ in CampBot().wiki.get_xreport_ids():
        break

    for _ in CampBot().wiki.get_document_ids(document_type="r"):
        break

    for _ in CampBot().wiki.get_documents(document_type="r"):
        break

    assert CampBot().wiki.ui_url == "https://www.camptocamp.org"

    version = CampBot().wiki.get_wiki_object_version(293549, "r", "fr",
                                                     1738922)
    assert version.get_diff_url("fr") is not None
    assert version.get_locale_length("fr") != 0

    CampBot().wiki.get_wiki_object_version(None, "", "", None)

    version = CampBot().wiki.get_wiki_object_version(293549, "r", "fr",
                                                     1738922)
    version.previous_version_id = None
    assert version.get_diff_url("fr") is not None

    route = CampBot().wiki.get_wiki_object(item_id=293549, document_type="r")
    assert route.get_url() == "https://www.camptocamp.org/routes/293549"
    assert (route.get_history_url("fr") ==
            "https://www.camptocamp.org/routes/history/293549/fr")

    CampBot().wiki.get_contributions(oldest_date="2017-12-12",
                                     newest_date="2017-12-13")

    with pytest.raises(Exception):
        CampBot().wiki.get_user(forum_name="unknown")

    user = CampBot().wiki.get_user(forum_name="CharlesB")
    user.is_personal()
    user.save("test", ask_before_saving=False)

    contrib = user.get_last_contribution()
    contrib.get_full_document()
    contrib.user.get_wiki_user()
    contrib.user.is_newbie()
    contrib.user.get_contributions_url()
Пример #17
0
def test_checkers(fix_requests):
    from campbot.checkers import get_fixed_tests, ReTest
    from campbot import CampBot
    from campbot.objects import Version, Contribution

    bot = CampBot()

    route = bot.wiki.get_route(123)
    waypoint = bot.wiki.get_waypoint(123)

    changes = (
        (
            Contribution(bot, {
                "user": {
                    "user_id": 3199
                },
                "document": {
                    "type": "r"
                }
            }),
            Version(bot, {
                "document": route,
                "user": {}
            }),
            Version(bot, {
                "document": route,
                "user": {}
            }),
        ),
        (
            Contribution(bot, {
                "user": {
                    "user_id": 3199
                },
                "document": {
                    "type": "w"
                }
            }),
            Version(bot, {
                "document": waypoint,
                "user": {}
            }),
            Version(bot, {
                "document": waypoint,
                "user": {}
            }),
        ),
    )

    for test in get_fixed_tests("fr"):
        for contrib, old_version, new_version in changes:
            test(contrib, old_version,
                 Version(bot, {
                     "document": None,
                     "user": {}
                 }))
            test(contrib, old_version, new_version)
            test(contrib, None, new_version)
            test(contrib, old_version, None)

    for contrib, old_version, new_version in changes:
        t = ReTest("x", "fr")
        t.patterns.append("e")
        t(contrib, old_version, new_version)
Пример #18
0
def test_forum(fix_requests):
    from campbot import CampBot

    CampBot().forum.get_group_members(group_name="Association")
    CampBot().forum.get_participants(MESSAGE_URL)
    CampBot().forum.get_post(url=MESSAGE_URL)
Пример #19
0
def test_get_users_from_route(fix_requests):
    from campbot import CampBot

    bot = CampBot()
    bot.get_users_from_route(123)
Пример #20
0
def test_get_voters(fix_requests):
    from campbot import CampBot, objects

    bot = CampBot()

    bot.forum.get_voters(1234, "poll", "option_id")