def test_process_file_return_ElementTree_modified_existing(self): """Modify the ElementTree in the mapping function""" f2f.AnkiConnectWrapper.add_note = MagicMock() f2f.AnkiConnectWrapper.add_note.return_value = "1234" f2f.AnkiConnectWrapper.update_note = MagicMock() tmp_dir_o = tempfile.TemporaryDirectory() tmp_dir = tmp_dir_o.name shutil.copyfile("tests/test_existing_note.tid", tmp_dir + "/" + "test.tid") def edit_ElementTree(root): root.attrib["data-dummy"] = "test" return abbreviation.extract(root) f2f.add_format( tag="abbr", class_name="h-fcard", note_type="Abbreviation", mapping_function=edit_ElementTree) f2f.process_file(tmp_dir + "/" + "test.tid") with open(tmp_dir + "/" + "test.tid", encoding='utf-8') as f: content = f.read() self.assertIn('data-dummy="test"', content)
def test_process_file(self): """Register formats and process a file""" f2f.AnkiConnectWrapper.add_note = MagicMock() f2f.AnkiConnectWrapper.add_note.return_value = "1234" f2f.AnkiConnectWrapper.update_note = MagicMock() f2f.add_format(**abbreviation.definition) tmp_dir_o = tempfile.TemporaryDirectory() tmp_dir = tmp_dir_o.name shutil.copyfile("tests/test.tid", tmp_dir + "/" + "test.tid") f2f.process_file(tmp_dir + "/" + "test.tid") with open(tmp_dir + "/" + "test.tid", encoding='utf-8') as f: content = f.read() fragments = f2f.find_fragments(content, "abbr") root = ET.fromstring(fragments[0]) data = abbreviation.extract(root) self.assertEqual(data, {"Full": "Bit error rate", "Context": "Communication", "Abbreviation": "BER"})
def test_new_note(self): f2f.AnkiConnectWrapper.deck_name = "Test" f2f.AnkiConnectWrapper.invoke("createDeck", {"deck": "Test"}) f2f.add_format(tag="abbr", class_name="h-fcard", note_type="Basic", mapping_function=f2f.extract_abbreviation_basic) f2f.add_format(**cloze.definition) tmp_dir_o = tempfile.TemporaryDirectory() tmp_dir = tmp_dir_o.name shutil.copyfile("tests/test.tid", tmp_dir + "/" + "test.tid") f2f.process_file(tmp_dir + "/" + "test.tid") with open(tmp_dir + "/" + "test.tid", "r+", encoding='utf-8') as f: content = f.read() content = content.replace("BER", "SER") content = content.replace("Bit", "Symbol") content = content.replace("replace-me", "<em>replace-me</em>") f.seek(0) f.write(content) f.seek(0) content = f.read() f2f.process_file(tmp_dir + "/" + "test.tid") f2f.AnkiConnectWrapper.invoke("deleteDecks", { "decks": ["Test"], "cardsToo": True })
def test_process_file_call_Anki_existing_note(self): """Existing notes should be requested to be updated""" f2f.add_format(**abbreviation.definition) tmp_dir_o = tempfile.TemporaryDirectory() tmp_dir = tmp_dir_o.name shutil.copyfile("tests/test_existing_note.tid", tmp_dir + "/" + "test.tid") f2f.AnkiConnectWrapper.update_note = MagicMock() f2f.process_file(tmp_dir + "/" + "test.tid") f2f.AnkiConnectWrapper.update_note.assert_called_with("654321", {"Full": "Bit error rate", "Context": "Communication", "Abbreviation":"BER"})
def test_process_file_call_Anki(self): """New notes should be requested to be added""" f2f.add_format(**abbreviation.definition) tmp_dir_o = tempfile.TemporaryDirectory() tmp_dir = tmp_dir_o.name shutil.copyfile("tests/test.tid", tmp_dir + "/" + "test.tid") f2f.AnkiConnectWrapper.add_note = MagicMock() f2f.AnkiConnectWrapper.add_note.return_value = "123456" f2f.process_file(tmp_dir + "/" + "test.tid") f2f.AnkiConnectWrapper.add_note.assert_called_with("Abbreviation", {"Full": "Bit error rate", "Context": "Communication", "Abbreviation":"BER"}) with open(tmp_dir + "/" + "test.tid", encoding='utf-8') as f: content = f.read() self.assertIn("123456", content)
def test_process_file_no_flashcards(self): """There can be things which looks like flashcards, but aren't Here we check that when the h-fcard class is not present, then flashcards shouldn't be processed""" f2f.add_format(**abbreviation.definition) tmp_dir_o = tempfile.TemporaryDirectory() tmp_dir = tmp_dir_o.name shutil.copyfile("tests/test_no_flashcard.tid", tmp_dir + "/" + "test.tid") content_before = "" with open(tmp_dir + "/" + "test.tid", encoding='utf-8') as f: content_before = f.read() f2f.process_file(tmp_dir + "/" + "test.tid") with open(tmp_dir + "/" + "test.tid", encoding='utf-8') as f: content = f.read() self.assertEqual(content, content_before)
def test_process_file_taboo_word(self): """Ignore files with the taboo word""" f2f.AnkiConnectWrapper.add_note = MagicMock() f2f.AnkiConnectWrapper.add_note.return_value = "1234" f2f.AnkiConnectWrapper.update_note = MagicMock() f2f.taboo_word = "Taboo!" f2f.add_format(**abbreviation.definition) tmp_dir_o = tempfile.TemporaryDirectory() tmp_dir = tmp_dir_o.name shutil.copyfile("tests/test_taboo.tid", tmp_dir + "/" + "test.tid") content_before = "" with open(tmp_dir + "/" + "test.tid", encoding='utf-8') as f: content_before = f.read() f2f.process_file(tmp_dir + "/" + "test.tid") with open(tmp_dir + "/" + "test.tid", encoding='utf-8') as f: self.assertEqual(content_before, f.read())