Example #1
0
def test_link_search_by_tag():
    """Ensure that it is possible to search for a link based on tags."""

    db = Database(":memory:", create=True, verbose=True)
    session = db.session

    function = Tag(name="function")
    python = Tag(name="python")

    enumerate_ = Link(name="enumerate", url="https://docs.python.org/3/enumerate.html")
    enumerate_.tags.append(python)
    enumerate_.tags.append(function)

    malloc = Link(name="malloc", url="https://docs.c.org/c11/malloc.html")
    malloc.tags.append(function)

    github = Link(name="Github", url="https://github.com")

    for item in [python, function, enumerate_, malloc, github]:
        session.add(item)

    db.commit()

    links = Link.search(db, tags=["function"])
    assert {l.url for l in links} == {
        "https://docs.python.org/3/enumerate.html",
        "https://docs.c.org/c11/malloc.html",
    }

    # Ensure multiple tags are ANDed
    assert len(Link.search(db, tags=["function", "c"])) == 0
Example #2
0
def add_link(filepath, url, name, tags):
    db = Database(filepath, create=True)

    if tags is None:
        Link.add(db, name=name, url=url)
        return 0

    link = Link(name=name, url=url)
    new_tags = []

    for t in tags:
        existing = Tag.get(db, name=t)

        if existing is not None:
            link.tags.append(existing)
            continue

        tag = Tag(name=t)
        link.tags.append(tag)
        new_tags.append(tag)

    Link.add(db, items=[link], commit=False)

    if len(new_tags) > 0:
        Tag.add(db, items=new_tags, commit=False)

    db.commit()
Example #3
0
def test_tag_link():
    """Ensure that it is possible to associate a tag with a link."""

    db = Database(":memory:", create=True, verbose=True)
    session = db.session

    tag = Tag(name="search-engine")
    link = Link(name="Google", url="https://www.google.com/")

    link.tags.append(tag)

    session.add(tag)
    session.add(link)
    db.commit()

    tag.id = 1
    link = Link.get(db, 1)

    assert link.tags[0] == tag
    assert tag.links[0] == link