示例#1
0
def test_convert_cloze_deletions_to_anki_format_works_with_multiple_notes():
    text1 = (
        r"{Paris} is the capital and most populous city of {2::France},"
        r" with a estimated population of {2,148,271} residents"
    )
    text2 = "Some {{c1::cloze question}}"
    cloze_note1 = ClozeNote(text_md=text1, tags=[], deck_name="")
    cloze_note2 = ClozeNote(text_md=text2, tags=[], deck_name="")
    expected1 = (
        r"{{c1::Paris}} is the capital and most populous city of {{c2::France}},"
        r" with a estimated population of {{c3::2,148,271}} residents"
    )
    expected2 = "Some {{c1::cloze question}}"

    converter.convert_cloze_deletions_to_anki_format([cloze_note1, cloze_note2])

    assert cloze_note1.updated_text_md == expected1
    assert cloze_note2.updated_text_md == expected2
示例#2
0
def test_update_image_links_when_cloze_card_has_image_link():
    image = "![](path/to/img1.png)"
    card = ClozeNote(text_md=f"{{Some}} {image} text",
                     tags=[],
                     deck_name="deck")
    image_links = {image: [card]}
    expected = "{Some} ![](img1.png) text"

    img_handler._update_image_links_in_notes(image_links)

    assert card.updated_text_md == expected
示例#3
0
def test_update_image_links_when_no_image_links():
    card1 = ClozeNote(text_md="Some text", tags=[], deck_name="deck")
    card2 = BasicNote(front_md="More\n\nText",
                      back_md="Answer text",
                      tags=[],
                      deck_name="deck")

    img_handler._update_image_links_in_notes({})

    assert card1.raw_text_md == card1.updated_text_md
    assert card2.raw_front_md == card2.updated_front_md
    assert card2.raw_back_md == card2.updated_back_md
示例#4
0
def test_fetch_image_links_if_cards_have_same_image():
    image = "![](path/to/img1.png)"
    card1 = ClozeNote(text_md=f"long {{sentence}} or not\n\n{image}",
                      tags=[],
                      deck_name="deck")
    card2 = BasicNote(front_md=f"More\n\n{image}",
                      back_md="Answer text",
                      tags=[],
                      deck_name="deck")
    expected = {image: [card1, card2]}

    links = img_handler._fetch_image_links([card1, card2])

    assert expected == links
示例#5
0
def test_fetch_image_links_if_cards_do_not_have_images():
    card1 = BasicNote(front_md="Some text",
                      back_md="Another text",
                      tags=[],
                      deck_name="deck")
    card2 = BasicNote(front_md="More\n\nText",
                      back_md="Answer text",
                      tags=[],
                      deck_name="deck")
    card3 = ClozeNote(text_md="Some {{c1::question}} here",
                      tags=[],
                      deck_name="deck")

    links = img_handler._fetch_image_links([card1, card2, card3])

    assert {} == links
示例#6
0
def test_fetch_image_links_if_cards_have_images():
    image1 = "![](path/to/img1.png)"
    image2 = "![](img2.png)"
    card1 = BasicNote(
        front_md="Some text",
        back_md=f"Another text\n\n{image1}",
        tags=[],
        deck_name="deck",
    )
    card2 = ClozeNote(text_md=f"{{More}}\n\n{image2}",
                      tags=[],
                      deck_name="deck")
    expected = {image1: [card1], image2: [card2]}

    links = img_handler._fetch_image_links([card1, card2])

    assert expected == links
示例#7
0
def test_convert_cards_to_html_works_with_multiple_cards():
    text1 = r"inside $$\sqrt{2}$$ text"
    text2 = "1. Item1\n" "2. Item2\n" "3. Item3\n"
    basic_note1 = BasicNote(front_md=text1, back_md=text2, tags=[], deck_name="")
    basic_note2 = BasicNote(front_md=text2, back_md=text1, tags=[], deck_name="")
    cloze_note = ClozeNote(text_md="Some question {{c1::42}}", tags=[], deck_name="")
    expected1 = r"<p>inside \[\sqrt{2}\] text</p>"
    expected2 = "<ol><li>Item1</li><li>Item2</li><li>Item3</li></ol>"
    expected3 = "<p>Some question {{c1::42}}</p>"

    converter.convert_notes_to_html([basic_note1, basic_note2, cloze_note])

    assert basic_note1.front_html == expected1
    assert basic_note1.back_html == expected2
    assert basic_note2.front_html == expected2
    assert basic_note2.back_html == expected1
    assert cloze_note.text_html == expected3
示例#8
0
def test_update_image_links_when_basic_and_cloze_notes():
    image1 = "![](path/to/img1.png)"
    image2 = "![](images/image2.png)"
    card1 = BasicNote(
        front_md=f"{image2}\n\nSome {image1} text",
        back_md=f"Another text\n\n{image1}\n{image2}",
        tags=[],
        deck_name="deck",
    )
    card2 = ClozeNote(text_md=f"{image2}Some text {{{image1}}}",
                      tags=[],
                      deck_name="deck")
    image_links = {image1: [card1, card2], image2: [card2, card1]}
    expected_front = "![](image2.png)\n\nSome ![](img1.png) text"
    expected_back = "Another text\n\n![](img1.png)\n![](image2.png)"
    expected_cloze = "![](image2.png)Some text {![](img1.png)}"

    img_handler._update_image_links_in_notes(image_links)

    assert expected_front == card1.updated_front_md
    assert expected_back == card1.updated_back_md
    assert expected_cloze == card2.updated_text_md
     ),
     BasicNote(front_md="Q",
               back_md="A",
               tags=["one", "two-three"],
               deck_name="Abraham"),
 ],
 # cloze note
 ("Deck: Abraham\n"
  "\n"
  "Tags: one two-three\n"
  "\n"
  "1. Some {question} here?\n"
  "\n"): [
     ClozeNote(
         text_md="Some {question} here?",
         tags=["one", "two-three"],
         deck_name="Abraham",
     )
 ],
 # cloze note with ID
 ("Deck: Abraham\n"
  "\n"
  "Tags: one two-three\n"
  "\n"
  "<!--ID:123456-->\n"
  "1. Some {question} here?\n"
  "\n"
  "More info on question.\n"
  "\n"
  "And even more!\n"
  "\n"): [
示例#10
0
def test_get_anki_note_type(basic_note, config):
    expected = "my super type"
    config.update_option_value("anki", "basic_type", expected)

    assert basic_note.get_anki_note_type(config) == expected


def test_eq_when_same():
    first_note = BasicNote("front", "back", ["tag1"], "deck")
    second_note = BasicNote("front", "back", ["tag1"], "deck")

    assert first_note == second_note


@pytest.mark.parametrize(
    "second_note",
    (
        BasicNote("oops", "back", ["tag1"], "deck"),
        BasicNote("front", "oops", ["tag1"], "deck"),
        BasicNote("front", "back", ["tag1", "tag2"], "deck"),
        BasicNote("front", "back", ["tag1"], "my deck"),
        None,
        ClozeNote("front", ["tag1"], "my deck"),
        "short string",
    ),
)
def test_eq_when_not_equal(second_note):
    first_note = BasicNote("front", "back", ["tag1"], "deck")

    assert first_note != second_note
示例#11
0
def test_convert_cloze_deletions_to_anki_format(test_input, expected):
    cloze_note = ClozeNote(text_md=test_input, tags=[], deck_name="")

    converter.convert_cloze_deletions_to_anki_format([cloze_note])

    assert cloze_note.updated_text_md == expected