def test_untag_b(simple_ti: TagIndex): simple_ti.untag(docs=["doc_1", "doc_2"], tags=["tag_a", "tag_b"]) no_doc(ti=simple_ti, doc="doc_1") assert simple_ti.doc_to_tags["doc_2"] == {"tag_c"} for tag in ["tag_a", "tag_b"]: no_tag(ti=simple_ti, tag=tag) no_conflicts(ti=simple_ti)
def test_query_and_symbols(simple_ti: TagIndex): assert simple_ti.query("tag_a and tag_b") == {"doc_1", "doc_2"} assert simple_ti.query("tag_a and tag_c") == {"doc_2"} assert simple_ti.query("tag_a and tag_d") == set() assert simple_ti.query("tag_a and tag_e") == set() no_tag(ti=simple_ti, tag="tag_e") no_conflicts(ti=simple_ti)
def test_get_docs(simple_ti: TagIndex): assert simple_ti.get_docs(tag="tag_a") == {"doc_1", "doc_2"} assert simple_ti.get_docs(tag="tag_b") == {"doc_1", "doc_2"} assert simple_ti.get_docs(tag="tag_c") == {"doc_2"} assert simple_ti.get_docs(tag="tag_d") == {"doc_3"} assert simple_ti.get_docs(tag="tag_e") == set() no_tag(ti=simple_ti, tag="tag_e") no_conflicts(ti=simple_ti)
def test_query_complex(simple_ti: TagIndex): assert simple_ti.query("not (tag_a and tag_c) and (not tag_b)") == { "doc_3" } assert simple_ti.query("tag_c or not (tag_a and tag_b)") == { "doc_2", "doc_3" } assert simple_ti.query("tag_c or not tag_a and tag_b") == {"doc_2"} no_conflicts(ti=simple_ti)
def test_merge_tags_b(simple_ti: TagIndex): simple_ti.merge_tags(old_tags=["tag_a", "tag_c"], new_tag="tag_e") for tag in ["tag_a", "tag_c"]: no_tag(ti=simple_ti, tag=tag) yes_tag(ti=simple_ti, tag="tag_e") assert simple_ti.tag_to_docs["tag_e"] == {"doc_1", "doc_2"} assert simple_ti.doc_to_tags["doc_1"] == {"tag_e", "tag_b"} assert simple_ti.doc_to_tags["doc_2"] == {"tag_b", "tag_e"} no_conflicts(ti=simple_ti)
def test_merge_tags_a(simple_ti: TagIndex): simple_ti.merge_tags(old_tags="tag_c", new_tag="tag_b") no_tag(ti=simple_ti, tag="tag_c") assert simple_ti.doc_to_tags["doc_2"] == {"tag_a", "tag_b"} simple_ti.merge_tags(old_tags=["tag_a", "tag_b"], new_tag="tag_d") for doc in ["doc_1", "doc_2"]: assert simple_ti.doc_to_tags[doc] == {"tag_d"} for tag in ["tag_a", "tag_b"]: no_tag(ti=simple_ti, tag=tag) no_conflicts(ti=simple_ti)
def test_conflicts_a(simple_ti: TagIndex): simple_ti.tag_to_docs["tag_99"] = {"doc_a"} assert simple_ti.conflicts == { "'doc_a' in tag_to_docs['tag_99'] but 'tag_99' not in doc_to_tags['doc_a']" } yes_conflicts(ti=simple_ti) simple_ti.doc_to_tags["doc_z"] = {"tag_1"} assert simple_ti.conflicts == { "'doc_a' in tag_to_docs['tag_99'] but 'tag_99' not in doc_to_tags['doc_a']", "'tag_1' in doc_to_tags['doc_z'] but 'doc_z' not in tag_to_docs['tag_1']", } yes_conflicts(ti=simple_ti)
def simple_ti(): ti = TagIndex() ti.doc_to_tags["doc_1"] = {"tag_a", "tag_b"} ti.doc_to_tags["doc_2"] = {"tag_a", "tag_b", "tag_c"} ti.doc_to_tags["doc_3"] = {"tag_d"} ti.tag_to_docs["tag_a"] = {"doc_1", "doc_2"} ti.tag_to_docs["tag_b"] = {"doc_1", "doc_2"} ti.tag_to_docs["tag_c"] = {"doc_2"} ti.tag_to_docs["tag_d"] = {"doc_3"} return ti
def get_ti(): try: dtdb = Path(os.environ["DTDB"]) except KeyError: dtdb = Path(Path.home(), ".dtdb.json") try: ti = TagIndex.from_json(file_name=dtdb) except FileNotFoundError: print(f"Tag index created at.") ti = initialize(location=dtdb) return TIFile.from_json(file_name=dtdb)
def test_rename_doc(simple_ti: TagIndex): simple_ti.rename_doc(old_doc_name="doc_2", new_doc_name="doc_4") assert simple_ti.doc_to_tags["doc_4"] == {"tag_a", "tag_b", "tag_c"} no_doc(ti=simple_ti, doc="doc_2") with pytest.raises(ValueError): # new doc name already exists simple_ti.rename_doc(old_doc_name="doc_99", new_doc_name="doc_4") with pytest.raises(ValueError): # new doc name already exists simple_ti.rename_doc(old_doc_name="doc_2", new_doc_name="doc_3") with pytest.raises(ValueError): # old doc name doesn't exist simple_ti.rename_doc(old_doc_name="doc_99", new_doc_name="doc_100") no_conflicts(ti=simple_ti)
def test_untag_a(simple_ti: TagIndex): simple_ti.untag(docs="doc_2", tags="tag_b") assert simple_ti.doc_to_tags["doc_2"] == {"tag_a", "tag_c"} assert simple_ti.tag_to_docs["tag_b"] == {"doc_1"} simple_ti.untag(docs="doc_2", tags=["tag_a", "tag_c"]) no_doc(ti=simple_ti, doc="doc_2") no_tag(ti=simple_ti, tag="tag_c") assert simple_ti.tag_to_docs["tag_a"] == {"doc_1"} simple_ti.untag(docs="doc_3", tags="fake_tag") no_tag(ti=simple_ti, tag="fake_tag") assert simple_ti.doc_to_tags["doc_3"] == {"tag_d"} simple_ti.untag(docs="doc_99", tags="tag_a") no_doc(ti=simple_ti, doc="doc_99") assert simple_ti.tag_to_docs["tag_a"] == {"doc_1"} no_conflicts(ti=simple_ti)
def test_remove_doc(simple_ti: TagIndex): simple_ti.remove_doc(doc_name="doc_3") no_doc(ti=simple_ti, doc="doc_3") no_tag(ti=simple_ti, tag="tag_d") with pytest.raises(ValueError): simple_ti.remove_doc("doc_3") simple_ti.remove_doc(doc_name="doc_1") no_doc(ti=simple_ti, doc="doc_1") assert simple_ti.tag_to_docs["tag_a"] == {"doc_2"} assert simple_ti.tag_to_docs["tag_b"] == {"doc_2"} no_conflicts(ti=simple_ti)
def test_remove_tag(simple_ti: TagIndex): simple_ti.remove_tag(tag="tag_d") no_tag(ti=simple_ti, tag="tag_d") no_doc(ti=simple_ti, doc="doc_3") simple_ti.remove_tag(tag="tag_a") no_tag(ti=simple_ti, tag="tag_a") assert simple_ti.doc_to_tags["doc_1"] == {"tag_b"} assert simple_ti.doc_to_tags["doc_2"] == {"tag_b", "tag_c"} with pytest.raises(ValueError): simple_ti.remove_tag(tag="tag_99") no_tag(ti=simple_ti, tag="tag_99") no_conflicts(ti=simple_ti)
def test_tag(simple_ti: TagIndex): simple_ti.tag(docs="doc_9", tags="tag_x") assert simple_ti.doc_to_tags["doc_9"] == {"tag_x"} assert simple_ti.tag_to_docs["tag_x"] == {"doc_9"} simple_ti.tag(docs=["doc_9", "doc_10"], tags=["tag_y", "tag_z"]) assert simple_ti.doc_to_tags["doc_9"] == {"tag_x", "tag_y", "tag_z"} assert simple_ti.doc_to_tags["doc_10"] == {"tag_y", "tag_z"} assert simple_ti.tag_to_docs["tag_y"] == {"doc_9", "doc_10"} assert simple_ti.tag_to_docs["tag_z"] == {"doc_9", "doc_10"} with pytest.raises(ValueError): simple_ti.tag(docs="doc_1", tags="tRuE") with pytest.raises(ValueError): simple_ti.tag(docs="doc_1", tags="fALSE") with pytest.raises(ValueError): simple_ti.tag(docs="doc_1", tags="0") with pytest.raises(ValueError): simple_ti.tag(docs="doc_1", tags="1") no_conflicts(ti=simple_ti)
def test_query_invalid(simple_ti: TagIndex): with pytest.raises(ParseError): simple_ti.query("tag_a or (") no_conflicts(ti=simple_ti)
def test_query_not_symbol(simple_ti: TagIndex): assert simple_ti.query("not tag_d") == {"doc_1", "doc_2"} assert simple_ti.query("not tag_c") == {"doc_1", "doc_3"} assert simple_ti.query("not tag_e") == {"doc_1", "doc_2", "doc_3"} no_tag(ti=simple_ti, tag="tag_e") no_conflicts(ti=simple_ti)
def test_query_or_symbols(simple_ti: TagIndex): assert simple_ti.query("tag_c or tag_d") == {"doc_2", "doc_3"} assert simple_ti.query("tag_a or tag_c") == {"doc_1", "doc_2"} no_conflicts(ti=simple_ti)
def test_query_or_not(simple_ti: TagIndex): assert simple_ti.query("tag_c or not tag_b") == {"doc_2", "doc_3"} assert simple_ti.query("not tag_b or tag_c") == {"doc_2", "doc_3"} assert simple_ti.query("not (tag_b or tag_c)") == {"doc_3"} no_conflicts(ti=simple_ti)
def _initialize(location: str): ti = TagIndex() ti.to_json(file_name=location) return ti
def test_query_and_not(simple_ti: TagIndex): assert simple_ti.query("tag_a and not tag_c") == {"doc_1"} assert simple_ti.query("not tag_c and tag_a") == {"doc_1"} assert simple_ti.query("not (tag_a and tag_c)") == {"doc_1", "doc_3"} no_conflicts(ti=simple_ti)