def notebooks(nsx):
    with patch('zip_file_reader.read_json_data',
               autospec=True,
               return_value={'title': 'notebook 1'}):
        test_notebook1 = sn_notebook.Notebook(nsx, 'note_book1')
    with patch('zip_file_reader.read_json_data',
               autospec=True,
               return_value={'title': 'recycle bin'}):
        test_notebook2 = sn_notebook.Notebook(nsx, 'recycle-bin')
    return {'note_book1': test_notebook1, 'recycle-bin': test_notebook2}
def test_remove_notebooks_to_be_skipped(conv_setting, nsx):
    nsx_fc = nsx_file_converter.NSXFile('fake_file', conv_setting,
                                        'fake_pandoc_converter')
    with patch('zip_file_reader.read_json_data',
               autospec=True,
               return_value={'title': 'Notebook Title'}):
        test_notebook1 = sn_notebook.Notebook(nsx, '1234')
    with patch('zip_file_reader.read_json_data',
               autospec=True,
               return_value={'title': 'Notebook Title2'}):
        test_notebook2 = sn_notebook.Notebook(nsx, '7890')

    nsx_fc._notebooks = {'1234': test_notebook1, '7890': test_notebook2}
    notebooks_to_skip = ['1234']
    nsx_fc.remove_notebooks_to_be_skipped(notebooks_to_skip)
    assert nsx_fc._notebooks == {'7890': test_notebook2}
def test_process_notebook_pages_json_null_attachments(nsx):
    config.yanom_globals.is_silent = True
    note_page_1_json = {
        'parent_id': 'note_book1',
        'title': 'Page 1 title',
        'mtime': 1619298559,
        'ctime': 1619298539,
        'content': 'content',
        'tag': ['1'],
        'attachment': None
    }
    note_page_1 = sn_note_page.NotePage(nsx, 1, note_page_1_json)
    note_page_1.notebook_folder_name = 'note_book1'
    note_page_1._file_name = 'page-1-title.md'
    note_page_1._raw_content = """<div>Below is a hyperlink to the internet</div><div><a href=\"https://github.com/kevindurston21/YANOM-Note-O-Matic\">https://github.com/kevindurston21/YANOM-Note-O-Matic</a></div>"""
    notebook_title = 'notebook1'
    with patch('zip_file_reader.read_json_data',
               autospec=True,
               return_value={'title': notebook_title}):
        notebook = sn_notebook.Notebook(nsx, 'notebook_id_abcd')
        notebook.folder_name = 'notebook_folder'
        notebook.note_pages = [note_page_1]

        with patch('sn_note_page.NotePage.process_note',
                   spec=True) as mock_process_note:
            notebook.process_notebook_pages()

        mock_process_note.assert_called()

    assert len(notebook._null_attachment_list) == 1
def test_create_notebook_folders_force_fail_to_create_attachment_folder(
        conv_setting, caplog, nsx, monkeypatch):
    config.yanom_globals.logger_level = logging.DEBUG

    nsx_fc = nsx_file_converter.NSXFile('fake_file', conv_setting,
                                        'fake_pandoc_converter')

    with patch('zip_file_reader.read_json_data',
               autospec=True,
               return_value=None):
        test_notebook = sn_notebook.Notebook(nsx, '1234')
        nsx_fc._notebooks = {'1234': test_notebook}

        monkeypatch.setattr(sn_notebook.Notebook, 'full_path_to_notebook',
                            None)
        result = nsx_fc.create_notebook_and_attachment_folders()

    assert nsx_fc._notebooks['1234'].full_path_to_notebook is None

    assert "Creating folders for notebooks" in caplog.messages
    # confirm notebook folder was created
    assert Path(conv_setting.working_directory, config.yanom_globals.data_dir,
                nsx_fc.conversion_settings.export_folder,
                nsx_fc._notebooks['1234'].folder_name).exists()
    # confirm the attachment folder was not created
    assert not Path(
        conv_setting.working_directory, config.yanom_globals.data_dir,
        nsx_fc.conversion_settings.export_folder,
        nsx_fc._notebooks['1234'].folder_name,
        nsx_fc.conversion_settings.attachment_folder_name).exists()
    # confirm the notebook is listed to be skipped
    assert result == ['1234']
def test_create_notebook_folder_folder_already_exist(tmp_path, nsx, caplog):
    config.yanom_globals.logger_level = logging.DEBUG
    notebook_title = 'notebook1'
    with patch('zip_file_reader.read_json_data',
               autospec=True,
               return_value={'title': notebook_title}):
        notebook = sn_notebook.Notebook(nsx, 'abcd')

        notebook.conversion_settings.export_folder = 'export-folder'
        notebook.conversion_settings.renaming = 'rename'
        notebook.folder_name = 'notebook1'

        Path(tmp_path, config.yanom_globals.data_dir,
             notebook.conversion_settings.export_folder,
             notebook.folder_name).mkdir(parents=True, exist_ok=True)
        expected_folder_name = Path('notebook1-1')

        notebook.create_notebook_folder()

    assert Path(tmp_path, config.yanom_globals.data_dir,
                notebook.conversion_settings.export_folder,
                expected_folder_name).exists()

    assert notebook.folder_name == expected_folder_name
    assert notebook._full_path_to_notebook == Path(
        tmp_path, config.yanom_globals.data_dir,
        notebook.conversion_settings.export_folder, expected_folder_name)

    assert f'Creating notebook folder for {notebook_title}' in caplog.messages
def test_pair_up_note_pages_and_notebooks_note_title_already_exists(nsx):
    note_jason = {
        'parent_id': 'note_book2',
        'title': 'Page 8 title',
        'mtime': 1619298559,
        'ctime': 1619298539,
        'attachment': {},
        'content': 'content',
        'tag': [9]
    }
    note_page = sn_note_page.NotePage(nsx, '1234', note_jason)
    notebook_title = 'notebook1'
    with patch('zip_file_reader.read_json_data',
               autospec=True,
               return_value={'title': notebook_title}):
        notebook = sn_notebook.Notebook(nsx, 'notebook_id_abcd')
        notebook.folder_name = Path('notebook_folder')

        notebook.note_titles = ['abcd', 'Page 8 title', 'Page 8 title-1']

        notebook.pair_up_note_pages_and_notebooks(note_page)

    assert note_page.notebook_folder_name == Path('notebook_folder')
    assert note_page.parent_notebook_id == 'notebook_id_abcd'

    assert note_page.title == 'Page 8 title-2'
    assert note_page.title in notebook.note_titles
    assert note_page in notebook.note_pages
def test_fetch_notebook_json(nsx, json, expected):
    with patch('zip_file_reader.read_json_data',
               autospec=True,
               return_value=json):
        notebook = sn_notebook.Notebook(nsx, 'id-1234')
        result = notebook.fetch_notebook_json('id-1234')

    assert result == expected
def test_create_attachment_folder_when_note_book_folder_not_created(
        tmp_path, nsx, caplog):
    config.yanom_globals.logger_level = logging.DEBUG
    notebook_title = 'notebook1'
    with patch('zip_file_reader.read_json_data',
               autospec=True,
               return_value={'title': notebook_title}):
        notebook = sn_notebook.Notebook(nsx, 'abcd')
        notebook._full_path_to_notebook = ''
        notebook.conversion_settings.attachment_folder_name = 'attachments'

        notebook.create_attachment_folder()

    assert not Path(tmp_path, config.yanom_globals.data_dir,
                    notebook.conversion_settings.export_folder, 'notebook1',
                    'attachments').exists()

    assert f"Attachment folder for '{notebook_title}' was not created as the notebook folder has not been created" in caplog.messages
    assert f'Creating attachment folder' not in caplog.messages
def test_create_notebook_folders(conv_setting, caplog, tmp_path, nsx):
    config.yanom_globals.logger_level = logging.DEBUG

    nsx_fc = nsx_file_converter.NSXFile('fake_file', conv_setting,
                                        'fake_pandoc_converter')
    with patch('zip_file_reader.read_json_data',
               autospec=True,
               return_value=None):
        test_notebook = sn_notebook.Notebook(nsx, '1234')
        nsx_fc._notebooks = {'1234': test_notebook}

        nsx_fc.create_notebook_and_attachment_folders()

    assert "Creating folders for notebooks" in caplog.messages
    assert nsx_fc.notebooks['1234'].folder_name == Path('Unknown Notebook')
    assert Path(tmp_path, config.yanom_globals.data_dir,
                conv_setting.export_folder, 'Unknown Notebook').exists()
    assert Path(tmp_path, config.yanom_globals.data_dir,
                conv_setting.export_folder, 'Unknown Notebook',
                conv_setting.attachment_folder_name).exists()
def test_create_attachment_folder(tmp_path, nsx, caplog):
    config.yanom_globals.logger_level = logging.DEBUG
    notebook_title = 'notebook1'
    with patch('zip_file_reader.read_json_data',
               autospec=True,
               return_value={'title': notebook_title}):
        notebook = sn_notebook.Notebook(nsx, 'abcd')
        notebook._full_path_to_notebook = Path(
            tmp_path, config.yanom_globals.data_dir,
            notebook.conversion_settings.export_folder, 'notebook1')
        notebook._full_path_to_notebook.mkdir(parents=True, exist_ok=True)
        notebook.conversion_settings.attachment_folder_name = 'attachments'

        notebook.create_attachment_folder()

    assert Path(tmp_path, config.yanom_globals.data_dir,
                notebook.conversion_settings.export_folder, 'notebook1',
                'attachments').exists()

    assert f'Creating attachment folder' in caplog.messages
def test_create_notebook_folder_folder_unable_to_create_folder(
        tmp_path, nsx, caplog):
    config.yanom_globals.logger_level = logging.DEBUG
    notebook_title = 'notebook1'
    with patch('zip_file_reader.read_json_data',
               autospec=True,
               return_value={'title': notebook_title}):
        notebook = sn_notebook.Notebook(nsx, 'abcd')

        notebook.conversion_settings.export_folder = 'export-folder'
        notebook.folder_name = 'notebook1'

        expected_folder_name = None

        notebook.create_notebook_folder(parents=False)

    assert notebook.folder_name == 'notebook1'
    assert notebook._full_path_to_notebook is None

    assert f"Unable to create notebook folder there is a problem with the path.\n[Errno 2] No such file or directory: '{Path(tmp_path, config.yanom_globals.data_dir, 'export-folder', 'notebook1')}'" in caplog.messages
def test_process_notebook_pages(all_notes, nsx, silent_mode, expected, capsys,
                                caplog):
    config.yanom_globals.is_silent = silent_mode
    notebook_title = 'notebook1'
    with patch('zip_file_reader.read_json_data',
               autospec=True,
               return_value={'title': notebook_title}):
        notebook = sn_notebook.Notebook(nsx, 'notebook_id_abcd')
        notebook.folder_name = 'notebook_folder'
        notebook.note_pages = all_notes

        with patch('sn_note_page.NotePage.process_note',
                   spec=True) as mock_process_note:
            notebook.process_notebook_pages()

        mock_process_note.assert_called()

    assert f"Processing note book {notebook.title} - {notebook.notebook_id}" in caplog.messages

    captured = capsys.readouterr()
    assert expected in captured.out
def test_init_notebook_for_recycle_bin(nsx):
    notebook = sn_notebook.Notebook(nsx, 'recycle-bin')

    assert notebook.title == 'recycle-bin'