refactor: pave the way for non-regex key/value deletions

This commit is contained in:
Nathaniel Landau
2023-03-21 22:50:01 -04:00
parent 08999cb055
commit fdb1b8b5bc
11 changed files with 154 additions and 129 deletions

View File

@@ -144,7 +144,7 @@ def test_add_metadata_tag(test_application, mocker, capsys) -> None:
assert captured == Regex(r"SUCCESS +\| Added metadata to \d+ notes", re.DOTALL)
def test_delete_inline_tag_1(test_application, mocker, capsys) -> None:
def test_delete_tag_1(test_application, mocker, capsys) -> None:
"""Test renaming an inline tag.
GIVEN an application
@@ -159,10 +159,10 @@ def test_delete_inline_tag_1(test_application, mocker, capsys) -> None:
)
mocker.patch(
"obsidian_metadata.models.application.Questions.ask_selection",
side_effect=["delete_inline_tag", "back"],
side_effect=["delete_tag", "back"],
)
mocker.patch(
"obsidian_metadata.models.application.Questions.ask_existing_inline_tag",
"obsidian_metadata.models.application.Questions.ask_existing_tag",
return_value="breakfast",
)
@@ -172,7 +172,7 @@ def test_delete_inline_tag_1(test_application, mocker, capsys) -> None:
assert captured == Regex(r"SUCCESS +\| Deleted inline tag: breakfast in \d+ notes", re.DOTALL)
def test_delete_inline_tag_2(test_application, mocker, capsys) -> None:
def test_delete_tag_2(test_application, mocker, capsys) -> None:
"""Test renaming an inline tag.
GIVEN an application
@@ -187,10 +187,10 @@ def test_delete_inline_tag_2(test_application, mocker, capsys) -> None:
)
mocker.patch(
"obsidian_metadata.models.application.Questions.ask_selection",
side_effect=["delete_inline_tag", "back"],
side_effect=["delete_tag", "back"],
)
mocker.patch(
"obsidian_metadata.models.application.Questions.ask_existing_inline_tag",
"obsidian_metadata.models.application.Questions.ask_existing_tag",
return_value="not_a_tag_in_vault",
)
@@ -388,7 +388,7 @@ def test_inspect_metadata_all(test_application, mocker, capsys) -> None:
assert captured == Regex(r"type +│ article", re.DOTALL)
def test_rename_inline_tag(test_application, mocker, capsys) -> None:
def test_rename_tag(test_application, mocker, capsys) -> None:
"""Test renaming an inline tag."""
app = test_application
app._load_vault()
@@ -398,10 +398,10 @@ def test_rename_inline_tag(test_application, mocker, capsys) -> None:
)
mocker.patch(
"obsidian_metadata.models.application.Questions.ask_selection",
side_effect=["rename_inline_tag", "back"],
side_effect=["rename_tag", "back"],
)
mocker.patch(
"obsidian_metadata.models.application.Questions.ask_existing_inline_tag",
"obsidian_metadata.models.application.Questions.ask_existing_tag",
return_value="not_a_tag",
)
mocker.patch(
@@ -420,10 +420,10 @@ def test_rename_inline_tag(test_application, mocker, capsys) -> None:
)
mocker.patch(
"obsidian_metadata.models.application.Questions.ask_selection",
side_effect=["rename_inline_tag", "back"],
side_effect=["rename_tag", "back"],
)
mocker.patch(
"obsidian_metadata.models.application.Questions.ask_existing_inline_tag",
"obsidian_metadata.models.application.Questions.ask_existing_tag",
return_value="breakfast",
)
mocker.patch(

View File

@@ -324,7 +324,7 @@ def test_delete_3():
THEN return False
"""
frontmatter = Frontmatter(FRONTMATTER_CONTENT)
assert frontmatter.delete(r"\d{3}") is False
assert frontmatter.delete(r"\d{3}", is_regex=True) is False
def test_delete_4():
@@ -335,7 +335,7 @@ def test_delete_4():
THEN return False
"""
frontmatter = Frontmatter(FRONTMATTER_CONTENT)
assert frontmatter.delete("tags", r"\d{5}") is False
assert frontmatter.delete("tags", r"\d{5}", is_regex=True) is False
def test_delete_5():
@@ -371,7 +371,7 @@ def test_delete_7():
THEN return True and delete the matching keys from the dict
"""
frontmatter = Frontmatter(FRONTMATTER_CONTENT)
assert frontmatter.delete(r"front\w+") is True
assert frontmatter.delete(r"front\w+", is_regex=True) is True
assert "frontmatter_Key1" not in frontmatter.dict
assert "frontmatter_Key2" not in frontmatter.dict
@@ -384,7 +384,7 @@ def test_delete_8():
THEN return True and delete the matching values
"""
frontmatter = Frontmatter(FRONTMATTER_CONTENT)
assert frontmatter.delete("tags", r"\w+_[23]") is True
assert frontmatter.delete("tags", r"\w+_[23]", is_regex=True) is True
assert "tag_2" not in frontmatter.dict["tags"]
assert "📅/tag_3" not in frontmatter.dict["tags"]
assert "tag_1" in frontmatter.dict["tags"]

View File

@@ -289,7 +289,7 @@ def test_delete_3():
THEN return False
"""
inline = InlineMetadata(INLINE_CONTENT)
assert inline.delete(r"\d{3}") is False
assert inline.delete(r"\d{3}", is_regex=True) is False
def test_delete_4():
@@ -300,7 +300,7 @@ def test_delete_4():
THEN return False
"""
inline = InlineMetadata(INLINE_CONTENT)
assert inline.delete("key1", r"\d{5}") is False
assert inline.delete("key1", r"\d{5}", is_regex=True) is False
def test_delete_5():
@@ -336,7 +336,7 @@ def test_delete_7():
THEN return True and delete the matching keys from the dict
"""
inline = InlineMetadata(INLINE_CONTENT)
assert inline.delete(r"key\w+") is True
assert inline.delete(r"key\w+", is_regex=True) is True
assert "key1" not in inline.dict
assert "key2" not in inline.dict
@@ -349,7 +349,7 @@ def test_delete_8():
THEN return True and delete the matching values
"""
inline = InlineMetadata(INLINE_CONTENT)
assert inline.delete("key1", r"\w+\d") is True
assert inline.delete("key1", r"\w+\d", is_regex=True) is True
assert "value1" not in inline.dict["key1"]
assert "value2" not in inline.dict["key1"]
assert "value3" not in inline.dict["key1"]

View File

@@ -48,7 +48,7 @@ def test_create_note_1(sample_note):
],
}
assert note.inline_tags.list == [
assert note.tags.list == [
"inline_tag_bottom1",
"inline_tag_bottom2",
"inline_tag_top1",
@@ -233,12 +233,12 @@ def test_add_metadata_method_10(sample_note):
THEN the tag is added to the InlineTags object and the file content
"""
note = Note(note_path=sample_note)
assert "new_tag2" not in note.inline_tags.list
assert "new_tag2" not in note.tags.list
assert (
note.add_metadata(MetadataType.TAGS, value="new_tag2", location=InsertLocation.BOTTOM)
is True
)
assert "new_tag2" in note.inline_tags.list
assert "new_tag2" in note.tags.list
assert "#new_tag2" in note.file_content
@@ -279,19 +279,19 @@ def test_commit_2(sample_note) -> None:
assert "Heading 1" in note.file_content
def test_contains_inline_tag(sample_note) -> None:
"""Test contains_inline_tag method.
def test_contains_tag(sample_note) -> None:
"""Test contains_tag method.
GIVEN a note object
WHEN contains_inline_tag() is called
WHEN contains_tag() is called
THEN the method returns True if the tag is found and False if not
"""
note = Note(note_path=sample_note)
assert note.contains_inline_tag("intext_tag1") is True
assert note.contains_inline_tag("nonexistent_tag") is False
assert note.contains_inline_tag(r"\d$", is_regex=True) is True
assert note.contains_inline_tag(r"^\d", is_regex=True) is False
assert note.contains_tag("intext_tag1") is True
assert note.contains_tag("nonexistent_tag") is False
assert note.contains_tag(r"\d$", is_regex=True) is True
assert note.contains_tag(r"^\d", is_regex=True) is False
def test_contains_metadata(sample_note) -> None:
@@ -323,7 +323,7 @@ def test_delete_all_metadata(sample_note):
"""
note = Note(note_path=sample_note)
note.delete_all_metadata()
assert note.inline_tags.list == []
assert note.tags.list == []
assert note.frontmatter.dict == {}
assert note.inline_metadata.dict == {}
assert note.file_content == Regex("consequat. Duis")
@@ -332,17 +332,17 @@ def test_delete_all_metadata(sample_note):
assert "---" not in note.file_content
def test_delete_inline_tag(sample_note) -> None:
"""Test delete_inline_tag method.
def test_delete_tag(sample_note) -> None:
"""Test delete_tag method.
GIVEN a note object
WHEN delete_inline_tag() is called
WHEN delete_tag() is called
THEN the method returns True if the tag is found and deleted and False if not
"""
note = Note(note_path=sample_note)
assert note.delete_inline_tag("not_a_tag") is False
assert note.delete_inline_tag("intext_tag[1]") is True
assert "intext_tag1" not in note.inline_tags.list
assert note.delete_tag("not_a_tag") is False
assert note.delete_tag("intext_tag[1]") is True
assert "intext_tag1" not in note.tags.list
assert note.file_content == Regex("consequat. Duis")
@@ -454,7 +454,7 @@ def test_has_changes(sample_note) -> None:
note = Note(note_path=sample_note)
assert note.has_changes() is False
note.delete_inline_tag("intext_tag1")
note.delete_tag("intext_tag1")
assert note.has_changes() is True
@@ -494,29 +494,29 @@ def test_print_note(sample_note, capsys) -> None:
assert "#shared_tag" in captured.out
def test_rename_inline_tag_1(sample_note) -> None:
"""Test rename_inline_tag() method.
def test_rename_tag_1(sample_note) -> None:
"""Test rename_tag() method.
GIVEN a note object
WHEN rename_inline_tag() is called with a tag that does not exist
WHEN rename_tag() is called with a tag that does not exist
THEN the method returns False
"""
note = Note(note_path=sample_note)
assert note.rename_inline_tag("no_note_tag", "intext_tag2") is False
assert note.rename_tag("no_note_tag", "intext_tag2") is False
def test_rename_inline_tag_2(sample_note) -> None:
"""Test rename_inline_tag() method.
def test_rename_tag_2(sample_note) -> None:
"""Test rename_tag() method.
GIVEN a note object
WHEN rename_inline_tag() is called with a tag exists
THEN the tag is renamed in the InlineTags object and the file content
WHEN rename_tag() is called with a tag exists
THEN the tag is renamed in the InlineTag object and the file content
"""
note = Note(note_path=sample_note)
assert "intext_tag1" in note.inline_tags.list
assert note.rename_inline_tag("intext_tag1", "intext_tag26") is True
assert "intext_tag1" not in note.inline_tags.list
assert "intext_tag26" in note.inline_tags.list
assert "intext_tag1" in note.tags.list
assert note.rename_tag("intext_tag1", "intext_tag26") is True
assert "intext_tag1" not in note.tags.list
assert "intext_tag26" in note.tags.list
assert note.file_content == Regex(r"#intext_tag26")
assert note.file_content != Regex(r"#intext_tag1")
@@ -846,7 +846,7 @@ def test_write_delete_inline_metadata_2(sample_note) -> None:
"""
note = Note(note_path=sample_note)
note.write_delete_inline_metadata("intext_key")
note.write_delete_inline_metadata("intext_key", is_regex=False)
assert note.file_content == Regex(r"dolore eu fugiat", re.DOTALL)
@@ -858,7 +858,7 @@ def test_write_delete_inline_metadata_3(sample_note) -> None:
THEN the key/value is removed from the note content
"""
note = Note(note_path=sample_note)
note.write_delete_inline_metadata("bottom_key2", "bottom_key2_value")
note.write_delete_inline_metadata("bottom_key2", "bottom_key2_value", is_regex=False)
assert note.file_content != Regex(r"bottom_key2_value")
assert note.file_content == Regex(r"bottom_key2::")
note.write_delete_inline_metadata("bottom_key1")

View File

@@ -68,12 +68,12 @@ def test_validate_number() -> None:
assert questions._validate_number("1") is True
def test_validate_existing_inline_tag() -> None:
def test_validate_existing_tag() -> None:
"""Test existing tag validation."""
questions = Questions(vault=VAULT)
assert "Tag cannot be empty" in questions._validate_existing_inline_tag("")
assert "'test' does not exist" in questions._validate_existing_inline_tag("test")
assert questions._validate_existing_inline_tag("shared_tag") is True
assert "Tag cannot be empty" in questions._validate_existing_tag("")
assert "'test' does not exist" in questions._validate_existing_tag("test")
assert questions._validate_existing_tag("shared_tag") is True
def test_validate_key_exists_regex() -> None:

View File

@@ -315,16 +315,16 @@ def test_delete_backup_2(test_vault, capsys):
assert vault.backup_path.exists() is True
def test_delete_inline_tag_1(test_vault) -> None:
"""Test delete_inline_tag() method.
def test_delete_tag_1(test_vault) -> None:
"""Test delete_tag() method.
GIVEN a vault object
WHEN the delete_inline_tag method is called
WHEN the delete_tag method is called
THEN the inline tag is deleted
"""
vault = Vault(config=test_vault)
assert vault.delete_inline_tag("intext_tag2") == 1
assert vault.delete_tag("intext_tag2") == 1
assert vault.metadata.tags == [
"inline_tag_bottom1",
"inline_tag_bottom2",
@@ -335,16 +335,16 @@ def test_delete_inline_tag_1(test_vault) -> None:
]
def test_delete_inline_tag_2(test_vault) -> None:
"""Test delete_inline_tag() method.
def test_delete_tag_2(test_vault) -> None:
"""Test delete_tag() method.
GIVEN a vault object
WHEN the delete_inline_tag method is called with a tag that does not exist
WHEN the delete_tag method is called with a tag that does not exist
THEN no changes are made
"""
vault = Vault(config=test_vault)
assert vault.delete_inline_tag("no tag") == 0
assert vault.delete_tag("no tag") == 0
def test_delete_metadata_1(test_vault) -> None:
@@ -594,16 +594,16 @@ def test_move_inline_metadata_1(test_vault) -> None:
assert vault.move_inline_metadata(location=InsertLocation.TOP) == 1
def test_rename_inline_tag_1(test_vault) -> None:
"""Test rename_inline_tag() method.
def test_rename_tag_1(test_vault) -> None:
"""Test rename_tag() method.
GIVEN a vault object
WHEN the rename_inline_tag() method is called with a tag that is found
WHEN the rename_tag() method is called with a tag that is found
THEN the inline tag is renamed
"""
vault = Vault(config=test_vault)
assert vault.rename_inline_tag("intext_tag2", "new_tag") == 1
assert vault.rename_tag("intext_tag2", "new_tag") == 1
assert vault.metadata.tags == [
"inline_tag_bottom1",
"inline_tag_bottom2",
@@ -615,16 +615,16 @@ def test_rename_inline_tag_1(test_vault) -> None:
]
def test_rename_inline_tag_2(test_vault) -> None:
"""Test rename_inline_tag() method.
def test_rename_tag_2(test_vault) -> None:
"""Test rename_tag() method.
GIVEN a vault object
WHEN the rename_inline_tag() method is called with a tag that is not found
WHEN the rename_tag() method is called with a tag that is not found
THEN the inline tag is not renamed
"""
vault = Vault(config=test_vault)
assert vault.rename_inline_tag("no tag", "new_tag") == 0
assert vault.rename_tag("no tag", "new_tag") == 0
def test_rename_metadata_1(test_vault) -> None:
@@ -773,7 +773,7 @@ def test_update_from_dict_3(test_vault):
assert vault.get_changed_notes()[0].note_path.name == "test1.md"
assert vault.get_changed_notes()[0].frontmatter.dict == {"new_key": ["new_value"]}
assert vault.get_changed_notes()[0].inline_metadata.dict == {"new_key2": ["new_value"]}
assert vault.get_changed_notes()[0].inline_tags.list == ["new_tag"]
assert vault.get_changed_notes()[0].tags.list == ["new_tag"]
assert vault.metadata.frontmatter == {"new_key": ["new_value"]}
assert vault.metadata.inline_metadata == {"new_key2": ["new_value"]}
assert vault.metadata.tags == ["new_tag"]