Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions audiostats/db/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ def __init__(self, session_factory : sessionmaker[Session]):
self._session_factory = session_factory

def upsert_albums(self, albums : Iterator[AlbumDTO]):
t_start = time.time()
with UnitOfWork(self._session_factory) as uof:
total, success = 0, 0
for album in albums:
uof.albums.upsert(album)
total += 1
success += 1
logger.info(f'Albums updated/inserted in {(time.time() - t_start) * 1000:.3f} ms. Total: {total}, success: {success}')

def get_all_albums(self):
t_start = time.time()
with UnitOfWork(self._session_factory) as uof:
return uof.albums.all()

4 changes: 3 additions & 1 deletion audiostats/db/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ def upsert(self, album_data : AlbumDTO):
update_track_orm_f_dto(old_track, dto)
else:
track = create_track_orm_f_dto(dto)
track.album = album
album.tracks.append(track)

for track in old_tracks_by_title.values():
album.tracks.remove(track)
#album.tracks.remove(track)
self._session.delete(track)

def find_by_title_performer(self, title : str, performer : str | None) -> Album | None:
return self._session.query(Album).filter(Album.title == title and Album.performer == performer if performer else Album.performer.is_(None)).first()
Expand Down
17 changes: 14 additions & 3 deletions tests/test_db_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging

from audiostats.db.api import DBApi
from audiostats.handlers import TrackDTO
from tests import test_sessionmaker, test_engine

def test_upsert_albums(test_sessionmaker, processed_album_dtos):
Expand All @@ -10,7 +9,19 @@ def test_upsert_albums(test_sessionmaker, processed_album_dtos):

albums = api.get_all_albums()

assert albums.sort(key=lambda x: x.title) == processed_album_dtos.sort(key=lambda x: x.title)
assert albums.sort(key=lambda x: x.title) == processed_album_dtos.sort(key=lambda x: x.title), 'Insert some albums to db'

processed_album_dtos[0].tracks.pop(-1)
processed_album_dtos[0].tracks.pop(2)
processed_album_dtos[1].tracks.append(TrackDTO(title='New_Track_1', number=6, path='music/new_track_1.flac', offset=None, duration=None))
processed_album_dtos[1].year = None

api.upsert_albums(processed_album_dtos)

albums=api.get_all_albums()

assert albums.sort(key=lambda x: x.title) == processed_album_dtos.sort(key=lambda x: x.title), 'Update some albums in db'




Expand Down