From 9aa6941fca9d607563a9ebf7ec4c47711b0dfece Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Mon, 7 Feb 2022 22:18:54 +0000 Subject: [PATCH] Added first few tests in test_models --- app/models.py | 25 +++++++---- conftest.py | 4 +- test_model.py | 6 --- test_models.py | 119 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+), 16 deletions(-) delete mode 100644 test_model.py create mode 100644 test_models.py diff --git a/app/models.py b/app/models.py index e277472..07f5699 100644 --- a/app/models.py +++ b/app/models.py @@ -68,17 +68,21 @@ class NoteColours(Base): ) @classmethod - def get_all(self): + def get_all(cls, session): """Return all records""" return session.query(cls).all() + @classmethod + def get_by_id(cls, session, id): + """Return record identified by id, or None if not found""" + + return session.query(NoteColours).filter(NoteColours.id == id).first() + @staticmethod def get_colour(session, text): """ Parse text and return colour string if match, else None - - Currently ignore is_regex and is_casesensitive """ for rec in ( @@ -119,8 +123,8 @@ class Notes(Base): f"" ) - @staticmethod - def add_note(session, playlist_id, row, text): + @classmethod + def add_note(cls, session, playlist_id, row, text): "Add note" DEBUG(f"add_note(playlist_id={playlist_id}, row={row}, text={text})") @@ -130,6 +134,7 @@ class Notes(Base): note.note = text session.add(note) session.commit() + return note @staticmethod @@ -149,12 +154,14 @@ class Notes(Base): DEBUG(f"Notes.update_note(id={id}, row={row}, text={text})") - note = session.query(cls).filter(cls.id == id).one() + note = session.query(Notes).filter(Notes.id == id).one() note.row = row if text: note.note = text session.commit() + return note + class Playdates(Base): __tablename__ = 'playdates' @@ -164,8 +171,8 @@ class Playdates(Base): track_id = Column(Integer, ForeignKey('tracks.id')) tracks = relationship("Tracks", back_populates="playdates") - @staticmethod - def add_playdate(session, track): + @classmethod + def add_playdate(cls, session, track): "Record that track was played" DEBUG(f"add_playdate(track={track})") @@ -176,6 +183,8 @@ class Playdates(Base): track.update_lastplayed(session, track.id) session.commit() + return pd + @staticmethod def last_played(session, track_id): "Return datetime track last played or None" diff --git a/conftest.py b/conftest.py index 533dddd..6296bae 100644 --- a/conftest.py +++ b/conftest.py @@ -44,11 +44,11 @@ def setup_database(connection): yield - models.Base.metadata.drop_all() + Base.metadata.drop_all() @pytest.fixture -def db_session(setup_database, connection): +def session(setup_database, connection): transaction = connection.begin() yield scoped_session( sessionmaker(autocommit=False, autoflush=False, bind=connection) diff --git a/test_model.py b/test_model.py deleted file mode 100644 index 855b0eb..0000000 --- a/test_model.py +++ /dev/null @@ -1,6 +0,0 @@ -from app.models import Playlists, Session - - -def test_get_colour(db_session): - x = Playlists.get_all_playlists(db_session) - assert False diff --git a/test_models.py b/test_models.py new file mode 100644 index 0000000..a39d2be --- /dev/null +++ b/test_models.py @@ -0,0 +1,119 @@ +from app.models import ( + NoteColours, + Notes, + Playdates, + Playlists, + # PlaylistTracks, + Tracks, +) + + +def test_notecolours_get_colour(session): + """Create a colour record and retrieve all colours""" + + rec = NoteColours() + rec.colour = "ffffff" + rec.substring = "substring" + rec.is_regex = False + rec.is_casesensitive = True + session.add(rec) + session.commit() + + records = NoteColours.get_all(session) + assert len(records) == 1 + record = records[0] + assert record.colour == "ffffff" + + +def test_notecolours_get_by_id(session): + """Create a colour record and retrieve it by id""" + + rec = NoteColours() + rec.colour = "abcdef" + rec.substring = "substring" + rec.is_regex = False + rec.is_casesensitive = True + session.add(rec) + session.commit() + record_id = rec.id + + record = NoteColours.get_by_id(session, record_id) + assert record.colour == "abcdef" + + +def test_notes_add_note(session): + """Add and retrieve a note""" + + # We need to have a playlist to add the note to + pl = Playlists() + pl.name = "Test playlist" + session.add(pl) + session.commit() + + note_text = "Here we are" + Notes.add_note(session, pl.id, 1, note_text) + + # We retrieve notes via playlist + playlist = Playlists.get_playlist(session, pl.id) + notes = playlist.get_notes() + assert len(notes) == 1 + assert notes[0] == note_text + + +def test_notes_delete_note(session): + """Add and delete a note""" + + # We need to have a playlist to add the note to + pl = Playlists() + pl.name = "Test playlist" + session.add(pl) + session.commit() + + note_text = "Here we are" + rec =Notes.add_note(session, pl.id, 1, note_text) + + Notes.delete_note(session, rec.id) + + # We retrieve notes via playlist + playlist = Playlists.get_playlist(session, pl.id) + notes = playlist.get_notes() + assert len(notes) == 0 + + +def test_notes_update_note(session): + """Add and update a note""" + + # We need to have a playlist to add the note to + pl = Playlists() + pl.name = "Test playlist" + session.add(pl) + session.commit() + + original_text = "Here we are" + replacement_text = "There we go" + rec = Notes.add_note(session, pl.id, 1, original_text) + + Notes.update_note(session, rec.id, 1, text=replacement_text) + + # We retrieve notes via playlist + playlist = Playlists.get_playlist(session, pl.id) + notes = playlist.get_notes() + assert len(notes) == 1 + assert notes[0] == replacement_text + + +def test_playdates_add_playdate(session): + """Test playdate and last_played retrieval""" + + # We need a track + track_path = "/a/b/c" + track = Tracks.get_or_create(session, track_path) + # Need to commit because track record is updated in Playdates.add_playdate() + session.commit() + + playdate = Playdates.add_playdate(session, track) + + last_played = Playdates.last_played(session, track.id) + + assert playdate.lastplayed == last_played +