def test_process_nsx_file_no_notebook_ids(conv_setting, caplog): pc = pandoc_converter.PandocConverter(conv_setting) nsx_fc = nsx_file_converter.NSXFile('fake_file', conv_setting, pc) with patch('nsx_file_converter.NSXFile.fetch_json_data', autospec=True, return_value={'tag': 'tag1'}): nsx_fc.process_nsx_file() assert f"No notebook ids found in nsx file '{nsx_fc._nsx_file_name}'. Skipping nsx file" in caplog.messages
def test_find_pandoc_version_invalid_path_check_sys_exit(caplog): config.yanom_globals.logger_level = logging.DEBUG cs = conversion_settings.ConversionSettings() pandoc_processor = pandoc_converter.PandocConverter(cs) pandoc_processor._pandoc_path = 'invalid-path' caplog.clear() with pytest.raises(FileNotFoundError): pandoc_processor.find_pandoc_version() for record in caplog.records: assert record.levelname == "WARNING"
def test_convert_using_strings_forcing_pandoc_not_installed(): cs = conversion_settings.ConversionSettings() pandoc_processor = pandoc_converter.PandocConverter(cs) pandoc_processor.pandoc_options = [ 'fake_path', '-fqwe', 'html', '-s', '-t', 'gfm' ] with pytest.raises(FileNotFoundError): result = pandoc_processor.convert_using_strings( 'hello world', 'my_note') assert result == 'Error converting data'
def test_generate_pandoc_options_check_pandoc_version_conversion_settings( caplog, version, conversion_input, input_file_format, output_file_format, expected): cs = conversion_settings.ConversionSettings() cs.conversion_input = conversion_input pandoc_processor = pandoc_converter.PandocConverter(cs) pandoc_processor._pandoc_version = version pandoc_processor.output_file_format = output_file_format caplog.clear() pandoc_processor.generate_pandoc_options() assert pandoc_processor.pandoc_options[-2:] == expected
def test_check_and_set_up_pandoc_if_required(): cs = conversion_settings.ConversionSettings() cs.conversion_input = 'nsx' cs.export_format = 'gfm' pandoc_processor = pandoc_converter.PandocConverter(cs) with patch('pandoc_converter.PandocConverter.generate_pandoc_options', spec=True) as mock_generate_pandoc_options: with patch('pandoc_converter.PandocConverter.find_pandoc_version', spec=True) as mock_find_pandoc_version: pandoc_processor.check_and_set_pandoc_options_if_required() mock_generate_pandoc_options.assert_called_once() mock_find_pandoc_version.assert_called_once()
def test_store_attachments(notebooks, all_notes_dict, conv_setting, silent_mode): config.yanom_globals.is_silent = silent_mode pc = pandoc_converter.PandocConverter(conv_setting) nsx_fc = nsx_file_converter.NSXFile('fake_file', conv_setting, pc) nsx_fc._notebooks = notebooks nsx_fc._note_pages = all_notes_dict nsx_fc.add_note_pages_to_notebooks() with patch('nsx_file_converter.NSXFile.fetch_attachment_file', autospec=True): with patch('file_writer.store_file', spec=True) as mock_store_file: nsx_fc.process_notebooks() assert mock_store_file.call_count == 4
def test_check_and_set_up_pandoc_if_required_nsx_and_html( conversion_input, markdown_conversion_input, export_format): cs = conversion_settings.ConversionSettings() cs.conversion_input = conversion_input cs.export_format = export_format cs.markdown_conversion_input = markdown_conversion_input pandoc_processor = pandoc_converter.PandocConverter(cs) with patch('pandoc_converter.PandocConverter.generate_pandoc_options', spec=True) as mock_generate_pandoc_options: with patch('pandoc_converter.PandocConverter.find_pandoc_version', spec=True) as mock_find_pandoc_version: pandoc_processor.check_and_set_pandoc_options_if_required() mock_generate_pandoc_options.assert_not_called() mock_find_pandoc_version.assert_not_called()
def test_convert_using_strings_html_to_markdown(caplog, silent_mode, expected_capture_out): config.yanom_globals.is_silent = silent_mode cs = conversion_settings.ConversionSettings() pandoc_processor = pandoc_converter.PandocConverter(cs) pandoc_processor.pandoc_options = [ pandoc_processor._pandoc_path, '-f', 'html', '-s', '-t', 'gfm', '--wrap=none', '--markdown-headings=atx' ] caplog.clear() result = pandoc_processor.convert_using_strings( '<p>hello world</p><p><img src="image.png" width="600"/></p><p>Goodbye world</p>', 'my_note') assert result == 'hello world\n\n<img src="image.png" width="600" />\n\nGoodbye world\n'
def test_convert_using_strings_forcing_non_zero_return_code_from_pandoc( caplog): cs = conversion_settings.ConversionSettings() pandoc_processor = pandoc_converter.PandocConverter(cs) pandoc_processor.pandoc_options = [ pandoc_processor._pandoc_path, '-fqwe', 'html', '-s', '-t', 'gfm' ] caplog.clear() result = pandoc_processor.convert_using_strings('hello world', 'my_note') assert result == '' assert len(caplog.records) == 1 for record in caplog.records: assert 'Pandoc Return code=' in record.message
def test_post_process_note_page_no_front_matter_and_no_iframe_included_in_result(note_1, attachments): note_1.conversion_settings.quick_set_obsidian_settings() note_1.conversion_settings.front_matter_format = 'none' note_1.conversion_settings.export_format = 'pandoc_markdown_strict' note_1._pandoc_converter = pandoc_converter.PandocConverter(note_1.conversion_settings) note_1._attachments = {'an_attachment': attachments} expected = """- [x] Check 1 Pie Chart ![](attachments/replaced_id_number.png) [Chart data file](attachments/replaced_id_number.csv) <table class="dataframe" data-border="1"><thead><tr class="header" style="text-align: right;"><th><strong></strong></th><th><strong>cost</strong></th><th><strong>price</strong></th><th><strong>value</strong></th><th><strong>total value</strong></th><th><strong>sum</strong></th><th><strong>percent</strong></th></tr></thead><tbody><tr class="odd"><th><strong>something</strong></th><td>500</td><td>520</td><td>540</td><td>520</td><td>2080</td><td>32.10</td></tr><tr class="even"><th><strong>something else</strong></th><td>520</td><td>540</td><td>560</td><td>540</td><td>2160</td><td>33.33</td></tr><tr class="odd"><th><strong>another thing</strong></th><td>540</td><td>560</td><td>580</td><td>560</td><td>2240</td><td>34.57</td></tr></tbody></table> Below is a hyperlink to the internet [github.com/kevindurston21/YANOM-Note-O-Matic](https://github.com/kevindurston21/YANOM-Note-O-Matic) Below is a 3x3 Table <table data-border="1" style="width: 240px; height: 90px;"><thead><tr class="header"><th><strong>cell R1C1</strong></th><th><strong>cell R1C2</strong></th><th><strong>cell R1C3</strong></th></tr></thead><tbody><tr class="odd"><td><strong>cell R2C1</strong></td><td>cell R1C2</td><td>cell R1C3</td></tr><tr class="even"><td><strong>cell R3C1</strong></td><td>cell R1C2</td><td>cell R1C3</td></tr></tbody></table> Below is an image of the design of the line chart as seen in note-station <img src="image.png" width="600" /> """ note_1.pre_process_content() note_1.convert_data() note_1.post_process_content() # replace the generated 15 digit id-numbers with placeholder text to allow comparison if os.name == 'nt': regex = r"\d{13}" else: regex = r"\d{15}" test_string = note_1.converted_content substitute_text = 'replaced_id_number' result = re.sub(regex, substitute_text, test_string, 0, re.MULTILINE) assert result == expected
def test_checklist_post_processing_nsx_to_md_with_nsx_html_markdown_output( html, expected): checklist_processor = checklist_processing.NSXInputMDOutputChecklistProcessor( html) cs = conversion_settings.ConversionSettings() pandoc_processor = pandoc_converter.PandocConverter(cs) pre_processed = checklist_processor.processed_html # clean excessive divs as yanom does pre_processed = pre_processed.replace('<div></div>', '<p></p>') pre_processed = pre_processed.replace('<div', '<p') pre_processed = pre_processed.replace('</div', '</p') converted_content = pandoc_processor.convert_using_strings( pre_processed, 'fake_note') result = checklist_processor.checklist_post_processing(converted_content) assert result == expected
def test_find_pandoc_version(caplog, capsys, silent_mode, expected_capture_out): config.yanom_globals.logger_level = logging.DEBUG config.yanom_globals.is_silent = silent_mode cs = conversion_settings.ConversionSettings() pandoc_processor = pandoc_converter.PandocConverter(cs) caplog.clear() pandoc_processor.find_pandoc_version() assert len(caplog.records) == 1 for record in caplog.records: assert record.levelname == "DEBUG" assert 'Found pandoc version' in record.message captured = capsys.readouterr() assert expected_capture_out in captured.out
def test_find_pandoc_version_force_subprocess_error(caplog, capsys, monkeypatch, silent_mode, expected_capture_out): def mock_run(ignored, capture_output, text, timeout): raise subprocess.CalledProcessError(3, "a_command'") config.yanom_globals.is_silent = silent_mode cs = conversion_settings.ConversionSettings() pandoc_processor = pandoc_converter.PandocConverter(cs) caplog.clear() with pytest.raises(SystemExit): monkeypatch.setattr(subprocess, 'run', mock_run) pandoc_processor.find_pandoc_version() assert 'Exiting as unable to get pandoc version' in caplog.messages captured = capsys.readouterr() assert expected_capture_out in captured.out
def test_set_pandoc_path(monkeypatch, is_linux, if_frozen, expected_ends_with): def mock_is_system_linux(_ignored): return is_linux def mock_is_this_a_frozen_package(_ignored): return if_frozen cs = conversion_settings.ConversionSettings() pandoc_processor = pandoc_converter.PandocConverter(cs) monkeypatch.setattr(pandoc_converter.PandocConverter, 'is_system_linux', mock_is_system_linux) monkeypatch.setattr(pandoc_converter.PandocConverter, 'is_this_a_frozen_package', mock_is_this_a_frozen_package) pandoc_processor.set_pandoc_path() found_path_as_posix_string = helper_functions.path_to_posix_str( Path(pandoc_processor._pandoc_path)) assert found_path_as_posix_string.endswith(expected_ends_with)
def test_convert_using_strings_forcing_subprocess_error( caplog, capsys, monkeypatch, silent_mode, expected_capture_out): def mock_run(ingored1, input, capture_output, encoding, text, timeout): raise subprocess.CalledProcessError(3, "a_command'") config.yanom_globals.is_silent = silent_mode cs = conversion_settings.ConversionSettings() pandoc_processor = pandoc_converter.PandocConverter(cs) pandoc_processor.pandoc_options = [ pandoc_processor._pandoc_path, '-fqwe', 'html', '-s', '-t', 'gfm' ] caplog.clear() monkeypatch.setattr(subprocess, 'run', mock_run) result = pandoc_processor.convert_using_strings('hello world', 'my_note') assert result == 'Error converting data' assert 'Unable to convert note "my_note".' == caplog.messages[0] captured = capsys.readouterr() assert expected_capture_out in captured.out
def test_post_process_note_page_gfm(note_1, attachments): note_1.conversion_settings.quick_set_obsidian_settings() note_1._pandoc_converter = pandoc_converter.PandocConverter(note_1.conversion_settings) note_1.conversion_settings.export_format = 'gfm' note_1._attachments = {'an_attachment': attachments} note_1.pre_process_content() note_1.convert_data() note_1.post_process_content() expected = """--- created: 2021-04-24 22:08:59BST tag: - a_tag title: Page 1 title updated: 2021-04-24 22:09:19BST --- - [x] Check 1 Pie Chart ![](attachments/replaced_id_number.png) [Chart data file](attachments/replaced_id_number.csv) | | **cost** | **price** | **value** | **total value** | **sum** | **percent** | |--------------------|----------|-----------|-----------|-----------------|---------|-------------| | **something** | 500 | 520 | 540 | 520 | 2080 | 32.10 | | **something else** | 520 | 540 | 560 | 540 | 2160 | 33.33 | | **another thing** | 540 | 560 | 580 | 560 | 2240 | 34.57 | <iframe allowfullscreen="" anchorhref="https://www.youtube.com/watch?v=SqdxNUMO2cg" frameborder="0" height="315" src="https://www.youtube.com/embed/SqdxNUMO2cg" width="420" youtube="true"> </iframe> Below is a hyperlink to the internet [github.com/kevindurston21/YANOM-Note-O-Matic](https://github.com/kevindurston21/YANOM-Note-O-Matic) Below is a 3x3 Table | **cell R1C1** | **cell R1C2** | **cell R1C3** | |---------------|---------------|---------------| | **cell R2C1** | cell R1C2 | cell R1C3 | | **cell R3C1** | cell R1C2 | cell R1C3 | Below is an image of the design of the line chart as seen in note-station <img src="image.png" width="600" /> """ # replace the generated 15 digit id-numbers with placeholder text to allow comparison if os.name == 'nt': regex = r"\d{13}" else: regex = r"\d{15}" test_string = note_1.converted_content substitute_text = 'replaced_id_number' result = re.sub(regex, substitute_text, test_string, 0, re.MULTILINE) assert result == expected
def nsx(conv_setting): nsx = nsx_file_converter.NSXFile( 'fake_file', conv_setting, pandoc_converter.PandocConverter(conv_setting)) return nsx