|
| 1 | +import re |
| 2 | +import pytest |
| 3 | +from anki.errors import NotFoundError # noqa |
| 4 | +from anki.collection import Collection |
| 5 | +from anki.collection import SearchNode |
| 6 | + |
| 7 | +# from conftest import col |
| 8 | + |
| 9 | +test_name = "ignore_setting" |
| 10 | +col_path = "tests/test_outputs/{}/Anki2/User 1/collection.anki2".format(test_name) |
| 11 | + |
| 12 | +test_file_paths = [ |
| 13 | + "tests/test_outputs/{}/Obsidian/{}/scan_dir/included_file.md".format( |
| 14 | + test_name, test_name |
| 15 | + ), |
| 16 | + "tests/test_outputs/{}/Obsidian/{}/scan_dir/some/other/subdir/also_included_file.md".format( |
| 17 | + test_name, test_name |
| 18 | + ), |
| 19 | +] |
| 20 | + |
| 21 | +test_file_no_cards_paths = [ |
| 22 | + "tests/test_outputs/{}/Obsidian/{}/outside_of_scandir/not_supposed_to_be_scanned.md".format( |
| 23 | + test_name, test_name |
| 24 | + ), |
| 25 | + "tests/test_outputs/{}/Obsidian/{}/scan_dir/ignored_by_setting_ignored/not_supposed_to_be_scanned.md".format( |
| 26 | + test_name, test_name |
| 27 | + ), |
| 28 | + "tests/test_outputs/{}/Obsidian/{}/scan_dir/ignored_by_setting_ignored/some/other/subdir/not_supposed_to_be_scanned.md".format( |
| 29 | + test_name, test_name |
| 30 | + ), |
| 31 | +] |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture() |
| 35 | +def col(): |
| 36 | + col = Collection(col_path) |
| 37 | + yield col |
| 38 | + col.close() |
| 39 | + |
| 40 | + |
| 41 | +def test_col_exists(col): |
| 42 | + assert not col.is_empty() |
| 43 | + |
| 44 | + |
| 45 | +def test_deck_default_exists(col: Collection): |
| 46 | + assert col.decks.id_for_name("Default") is not None |
| 47 | + |
| 48 | + |
| 49 | +def test_cards_count(col: Collection): |
| 50 | + assert len(col.find_cards(col.build_search_string(SearchNode(deck="Default")))) == 6 |
| 51 | + |
| 52 | + |
| 53 | +def test_cards_ids_from_obsidian(col: Collection): |
| 54 | + ID_REGEXP_STR = r"\n?(?:<!--)?(?:ID: (\d+).*)" |
| 55 | + |
| 56 | + obs_IDs = [] |
| 57 | + for obsidian_test_md in test_file_paths: |
| 58 | + with open(obsidian_test_md) as file: |
| 59 | + for line in file: |
| 60 | + output = re.search(ID_REGEXP_STR, line.rstrip()) |
| 61 | + if output is not None: |
| 62 | + output = output.group(1) |
| 63 | + obs_IDs.append(output) |
| 64 | + |
| 65 | + anki_IDs = col.find_notes(col.build_search_string(SearchNode(deck="Default"))) |
| 66 | + for aid, oid in zip(anki_IDs, obs_IDs): |
| 67 | + assert str(aid) == oid |
| 68 | + |
| 69 | + |
| 70 | +def test_no_cards_added_from_ignored_paths(col: Collection): |
| 71 | + ID_REGEXP_STR = r"\n?(?:<!--)?(?:ID: (\d+).*)" |
| 72 | + |
| 73 | + for obsidian_test_md in test_file_no_cards_paths: |
| 74 | + obs_IDs = [] |
| 75 | + with open(obsidian_test_md) as file: |
| 76 | + for line in file: |
| 77 | + output = re.search(ID_REGEXP_STR, line.rstrip()) |
| 78 | + if output is not None: |
| 79 | + output = output.group(1) |
| 80 | + obs_IDs.append(output) |
| 81 | + |
| 82 | + assert len(obs_IDs) == 0 |
0 commit comments