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
def test_tag_add_single(): """Ensure that we can create a single instance of a tag.""" db = Database(":memory:", create=True, verbose=True) Tag.add(db, name="My Tag") assert Tag.get(db, 1) == Tag(id=1, name="My Tag")
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()
def test_tag_name_unique(): """Ensure that only tags with a unique name can be added to the db""" db = Database(":memory:", create=True, verbose=True) Tag.add(db, name="Tag #1") with py.test.raises(IntegrityError) as err: Tag.add(db, name="Tag #1") assert "tags.name" in str(err.value) assert "UNIQUE" in str(err.value)
def test_tag_requires_name(): """Ensure that tags require a name to be added to the db""" db = Database(":memory:", create=True, verbose=True) with py.test.raises(IntegrityError) as err: t = Tag(id=1) Tag.add(db, items=[t]) assert "tags.name" in str(err.value) assert "NOT NULL" in str(err.value)
def test_add_link_with_new_tags(workdir): """Ensure that we can add a link with tags that don't exist in te database""" filepath = pathlib.Path(workdir.name, "links.db") add_link( str(filepath), url="https://www.google.com", name="Google", tags=["search", "google"], ) db = Database(str(filepath), create=False) link = Link.search(db, name="Google")[0] assert link.url == "https://www.google.com" tags = {Tag.get(db, name="search"), Tag.get(db, name="google")} assert set(link.tags) == tags
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
def test_tag_get_by_name(): """Ensure that it is possible to get a tag by its name.""" db = Database(":memory:", create=True, verbose=True) tags = [ Tag(name="Tag #1"), Tag(name="Tag #2"), Tag(name="Tag #3"), ] Tag.add(db, items=tags) assert Tag.get(db, name="Tag #2").name == "Tag #2"
def test_add_tag_many_instaces(): """Ensure that we can add many tag instances to the database.""" db = Database(":memory:", create=True, verbose=True) tags = [ Tag(name="Tag #1"), Tag(name="Tag #2"), Tag(name="Tag #3"), ] Tag.add(db, items=tags) tags[0].id = 1 tags[1].id = 2 tags[2].id = 3 results = Tag.search(db) assert results[0] == tags[0] assert results[1] == tags[1] assert results[2] == tags[2]
def test_tag_add_many_dicts(): """Ensure that we can add many tags with their dictionary representation""" db = Database(":memory:", create=True, verbose=True) tags = [ {"name": "Tag #1"}, {"name": "Tag #2"}, {"name": "Tag #3"}, ] Tag.add(db, items=tags) tags[0]["id"] = 1 tags[1]["id"] = 2 tags[2]["id"] = 3 results = Tag.search(db) assert results[0] == Tag(**tags[0]) assert results[1] == Tag(**tags[1]) assert results[2] == Tag(**tags[2])