def test_parse(self, _format, mocker): mocky = mocker.patch.object(BookmarksConverter, "_dispatcher") method = f"_parse_{_format}" instance = BookmarksConverter("filepath") instance.parse(_format) assert instance._format == _format mocky.assert_called_once_with(method)
def test_rename_outputfile(self): file_ = Path("/source/bookmarks.html") instance = BookmarksConverter(file_) for i in range(2, 11): instance._rename_outputfile() assert instance.output_filepath == file_.with_name( f"output_{file_.stem}_{i:03d}{file_.suffix}")
def test_dispatcher(self, _format, method, mocker): lower_format = _format.lower() mocky = mocker.patch.object(BookmarksConverter, method) instance = BookmarksConverter("filepath") instance._export = lower_format instance._format = lower_format instance._dispatcher(method) mocky.assert_called_once()
def test_convert_to_db(self, mocker): mocky = mocker.patch.object(BookmarksConverter, "_iterate_folder_db") instance = BookmarksConverter("filepath") instance._tree = "test" instance._convert_to_db() assert isinstance(instance._stack, list) assert len(instance._stack) == 0 assert mocky.call_count == 1
def test_save(self, _format, mocker): mocky = mocker.patch.object(BookmarksConverter, "_dispatcher") method = f"_save_to_{_format}" instance = BookmarksConverter("filepath") instance._export = _format instance._format = _format instance.save() mocky.assert_called_once_with(method)
def test_prepare_filepaths(self): filename = "/home/user/Downloads/source/bookmarks.html" temp_filepath = Path("/home/user/Downloads/source/temp_bookmarks.html") output_filepath = Path( "/home/user/Downloads/source/output_bookmarks_001.html") bookmarks = BookmarksConverter(filename) assert temp_filepath == bookmarks.temp_filepath assert output_filepath == bookmarks.output_filepath
def test_from_chrome_json_to_html(source_bookmark_files, result_bookmark_files): result_file = Path(result_bookmark_files["from_chrome_json.html"]) bookmarks = BookmarksConverter(source_bookmark_files["bookmarks_chrome.json"]) bookmarks.parse("json") bookmarks.convert("html") bookmarks.save() output_file = bookmarks.output_filepath.with_suffix(".html") assert cmp(result_file, output_file, shallow=False) output_file.unlink()
def test_from_db_to_html(result_file, source_file, result_bookmark_files): result_file = Path(result_bookmark_files[result_file]) source_file = Path(result_bookmark_files[source_file]) bookmarks = BookmarksConverter(source_file) bookmarks.parse("db") bookmarks.convert("html") bookmarks.save() output_file = bookmarks.output_filepath.with_suffix(".html") assert cmp(result_file, output_file, shallow=False) output_file.unlink()
def test_save_to_html(self, result_bookmark_files): result_file = result_bookmark_files["from_firefox_json.html"] instance = BookmarksConverter(result_file) with open(result_file, "r", encoding="utf-8") as file_: instance.bookmarks = file_.read() output_file = Path(result_file).with_name("output_file.html") instance.output_filepath = output_file # setting the _export and _format attributes manually. instance._export = "html" instance._format = "html" instance.save() assert cmp(result_file, output_file, shallow=False) output_file.unlink()
def test_from_firefox_json_to_db( source_bookmark_files, result_bookmark_files, get_data_from_db ): origin = "Firefox" result_file = Path(result_bookmark_files["from_firefox_json.db"]) result_bookmarks, _, _ = get_data_from_db(result_file, origin) bookmarks = BookmarksConverter(source_bookmark_files["bookmarks_firefox.json"]) bookmarks.parse("json") bookmarks.convert("db") bookmarks.save() output_file = bookmarks.output_filepath.with_suffix(".db") output_bookmarks, _, _ = get_data_from_db(output_file, origin) assert result_bookmarks == output_bookmarks output_file.unlink()
def test_from_db_to_json_firefox(result_bookmark_files): result_file = Path(result_bookmark_files["from_firefox_html.json"]) source_file = Path(result_bookmark_files["from_firefox_html.db"]) bookmarks = BookmarksConverter(source_file) bookmarks.parse("db") # change the root, menu and toolber folder dates, as they are generated # when they are created and don't exist in an html file. bookmarks._tree.date_added = 1601886171439 bookmarks._tree.children[0].date_added = 1601886171439 bookmarks.convert("json") bookmarks.save() output_file = bookmarks.output_filepath.with_suffix(".json") assert cmp(result_file, output_file, shallow=False) output_file.unlink()
def test_from_chrome_html_to_db( source_bookmark_files, result_bookmark_files, get_data_from_db ): origin = "Chrome" result_file = Path(result_bookmark_files["from_chrome_html.db"]) result_bookmarks, root_date, other_date = get_data_from_db(result_file, origin) bookmarks = BookmarksConverter(source_bookmark_files["bookmarks_chrome.html"]) bookmarks.parse("html") bookmarks.convert("db") bookmarks.bookmarks[0].date_added = root_date bookmarks.bookmarks[1].date_added = other_date bookmarks.save() output_file = bookmarks.output_filepath.with_suffix(".db") output_bookmarks, _, _ = get_data_from_db(output_file, origin) assert result_bookmarks == output_bookmarks output_file.unlink()
def test_init(self): file_path = Path("/home/user/Downloads/source/bookmarks.html") output_file = file_path.with_name( f"output_{file_path.stem}_001{file_path.suffix}") temp_file = file_path.with_name(f"temp_{file_path.name}") instance = BookmarksConverter(str(file_path)) assert instance.bookmarks is None assert instance._export is None assert instance._format is None assert instance._stack is None assert instance._stack_item is None assert instance._tree is None assert instance.filepath == file_path assert isinstance(instance.filepath, Path) assert instance.output_filepath == output_file assert instance.temp_filepath == temp_file
def test_from_chrome_html_to_json( source_bookmark_files, result_bookmark_files, read_json ): result_file = Path(result_bookmark_files["from_chrome_html.json"]) json_data = read_json(result_file) # date_added of "root" folder root_date = json_data["date_added"] # date_added of "Other Bookmarks" folder other_date = json_data["children"][1]["date_added"] bookmarks = BookmarksConverter(source_bookmark_files["bookmarks_chrome.html"]) bookmarks.parse("html") bookmarks.convert("json") bookmarks.bookmarks["date_added"] = root_date bookmarks.bookmarks["children"][1]["date_added"] = other_date bookmarks.save() output_file = bookmarks.output_filepath.with_suffix(".json") assert cmp(result_file, output_file, shallow=False) output_file.unlink()
def test_save_to_db(self, get_data_from_db): file_path = "temp.db" bookmarks = list() for i in range(1, 11): parent_id = None if i == 0 else 0 bookmarks.append( Folder(date_added=0, index=0, _id=i, parent_id=parent_id, title="Title")) instance = BookmarksConverter(file_path) instance.bookmarks = bookmarks # setting the _export attribute as not to raise an error # setting the _format attribute for the desired output format instance._export = instance._format = "db" instance.save() output_file = instance.output_filepath temp_bookmarks, _, _ = get_data_from_db(output_file, None) assert bookmarks == temp_bookmarks output_file.unlink()
def test_save_error(self): instance = BookmarksConverter("filepath") with pytest.raises(RuntimeError) as e: instance.save()
def test_dispatcher_error(self): instance = BookmarksConverter("filepath") instance._format = "wrong format" with pytest.raises(TypeError) as e: instance._dispatcher("method")
def test_parse_db(self, result_bookmark_files, get_data_from_db): file_path = result_bookmark_files["from_chrome_html.db"] instance = BookmarksConverter(file_path) instance.parse("db") bookmarks, _, _ = get_data_from_db(file_path, "Chrome") assert bookmarks[0] == instance._tree