-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: integration test
sync_deck
(#330)
- Loading branch information
Showing
10 changed files
with
138 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
personal_mnemonic_medium/v2/domain/utils/hash_cleaned_str.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from personal_mnemonic_medium.v2.domain.utils.clean_str import ( | ||
clean_str, | ||
) | ||
from personal_mnemonic_medium.v2.domain.utils.int_hash_str import ( | ||
int_hash_str, | ||
) | ||
|
||
|
||
def hash_cleaned_str(input_str: str) -> int: | ||
"""Hash a string after cleaning it.""" | ||
cleaned = clean_str(input_str) | ||
hashed = int_hash_str(cleaned) | ||
return hashed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from pathlib import Path | ||
|
||
from personal_mnemonic_medium.data_access.exporters.anki.anki_css import ( | ||
CARD_MODEL_CSS, | ||
) | ||
|
||
from ..data_access.exporters.anki.globals import ANKICONNECT_URL | ||
from .data_access.ankiconnect_gateway import AnkiConnectGateway | ||
from .domain.diff_determiner.base_diff_determiner import ( | ||
PromptDiffDeterminer, | ||
) | ||
from .domain.prompt_destination.anki_connect.ankiconnect_destination import ( | ||
AnkiConnectDestination, | ||
) | ||
from .domain.prompt_destination.anki_connect.prompt_converter.anki_prompt_converter import ( | ||
AnkiPromptConverter, | ||
) | ||
from .domain.prompt_source.document_ingesters.markdown_document_ingester import ( | ||
MarkdownDocumentIngester, | ||
) | ||
from .domain.prompt_source.document_prompt_source import ( | ||
DocumentPromptSource, | ||
) | ||
from .domain.prompt_source.prompt_extractors.cloze_prompt_extractor import ( | ||
ClozePromptExtractor, | ||
) | ||
from .domain.prompt_source.prompt_extractors.qa_prompt_extractor import ( | ||
QAPromptExtractor, | ||
) | ||
|
||
|
||
def sync_deck( | ||
base_deck: str, | ||
input_dir: Path, | ||
apkg_output_dir: Path, | ||
ankiconnect_read_apkg_from_dir: Path, | ||
max_deletions_per_run: int, | ||
): | ||
source_prompts = DocumentPromptSource( | ||
document_ingester=MarkdownDocumentIngester( | ||
directory=input_dir | ||
), | ||
prompt_extractors=[ | ||
QAPromptExtractor( | ||
question_prefix="Q.", answer_prefix="A." | ||
), | ||
ClozePromptExtractor(), | ||
], | ||
).get_prompts() | ||
|
||
destination = AnkiConnectDestination( | ||
gateway=AnkiConnectGateway( | ||
ankiconnect_url=ANKICONNECT_URL, | ||
base_deck=base_deck, | ||
tmp_read_dir=ankiconnect_read_apkg_from_dir, | ||
tmp_write_dir=apkg_output_dir, | ||
max_deletions_per_run=max_deletions_per_run, | ||
), | ||
prompt_converter=AnkiPromptConverter( | ||
base_deck=base_deck, card_css=CARD_MODEL_CSS | ||
), | ||
) | ||
destination_prompts = destination.get_all_prompts() | ||
|
||
update_commands = PromptDiffDeterminer().sync( | ||
source_prompts=source_prompts, | ||
destination_prompts=destination_prompts, | ||
) | ||
|
||
destination.update(commands=update_commands) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from personal_mnemonic_medium.v2.sync_deck import sync_deck | ||
|
||
from ..data_access.exporters.anki.sync.gateway_utils import ( | ||
anki_connect_is_live, | ||
) | ||
|
||
|
||
@pytest.mark.skipif( | ||
not anki_connect_is_live(), | ||
reason="Tests require a running AnkiConnect server", | ||
) | ||
def test_sync_deck(tmp_path: Path): | ||
with (tmp_path / "test.md").open("w") as f: | ||
f.write( | ||
"""# Test note | ||
Q. Test question? | ||
A. Test answer! | ||
""" | ||
) | ||
|
||
sync_deck( | ||
base_deck="Tests::Integration Test deck", | ||
input_dir=tmp_path, | ||
apkg_output_dir=Path("/output"), | ||
ankiconnect_read_apkg_from_dir=Path( | ||
"/Users/Leisure/ankidecks" | ||
), | ||
max_deletions_per_run=1, | ||
) |