def test_insert_track_inserts_all_rows_expected(): from cozy.model.database_importer import DatabaseImporter, TrackInsertRequest from cozy.db.book import Book from cozy.db.file import File from cozy.db.track_to_file import TrackToFile database_importer = DatabaseImporter() file = File.create(path="New File", modified=1234567) track_data = { "name": "Test", "number": 2, "disk": 2, "book": Book.select().where(Book.name == "Test Book").get(), "length": 123, "position": 0 } track = TrackInsertRequest(track_data, file, 1234) database_importer._insert_tracks([track]) track_to_file_query = TrackToFile.select().join(File).where( TrackToFile.file == file.id) assert track_to_file_query.count() == 1 track_to_file: TrackToFile = track_to_file_query.get() assert track_to_file.track.name == track_data["name"] assert track_to_file.track.number == track_data["number"] assert track_to_file.track.disk == track_data["disk"] assert track_to_file.track.book.id == Book.select().where( Book.name == "Test Book").get().id assert track_to_file.track.length == track_data["length"] assert track_to_file.track.position == track_data["position"]
def test_prepare_db_objects_creates_new_book(mocker): from cozy.model.database_importer import DatabaseImporter from cozy.media.media_file import MediaFile from cozy.media.chapter import Chapter from cozy.db.file import File database_importer = DatabaseImporter() spy = mocker.spy(database_importer, "_create_book_db_object") File.create(path="New test File", modified=1234567) chapter = Chapter("New Chapter", 0, 1234567, 999) media_file = MediaFile(book_name="Test Book New", author="New Author2", reader="New Reader", disk=999, cover=b"cover", path="New test File", modified=1234567, chapters=[chapter]) res_dict = database_importer._prepare_track_db_objects([media_file]) assert len(list(res_dict)) == 1 spy.assert_called_once()
def test_prepare_db_objects_updates_existing_book_regardless_of_spelling( mocker): from cozy.model.database_importer import DatabaseImporter from cozy.media.media_file import MediaFile from cozy.media.chapter import Chapter from cozy.db.file import File database_importer = DatabaseImporter() spy = mocker.spy(database_importer, "_update_book_db_object") File.create(path="New test File", modified=1234567) File.create(path="Another test File", modified=1234568) chapter = Chapter("New Chapter", 0, 1234567, 999) another_chapter = Chapter("Another Chapter", 0, 1234567, 999) media_file = MediaFile(book_name="TeSt bOOk", author="New Author2", reader="New Reader", disk=999, cover=b"cover", path="New test File", modified=1234567, chapters=[chapter]) another_media_file = MediaFile(book_name="TEST BOOK", author="New Author2", reader="New Reader", disk=999, cover=b"cover", path="Another test File", modified=1234568, chapters=[another_chapter]) res_dict = database_importer._prepare_track_db_objects( [media_file, another_media_file]) assert len(list(res_dict)) == 2 spy.assert_called_once()