Added first few tests in test_models
This commit is contained in:
parent
a164f4c962
commit
9aa6941fca
@ -68,17 +68,21 @@ class NoteColours(Base):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_all(self):
|
def get_all(cls, session):
|
||||||
"""Return all records"""
|
"""Return all records"""
|
||||||
|
|
||||||
return session.query(cls).all()
|
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
|
@staticmethod
|
||||||
def get_colour(session, text):
|
def get_colour(session, text):
|
||||||
"""
|
"""
|
||||||
Parse text and return colour string if match, else None
|
Parse text and return colour string if match, else None
|
||||||
|
|
||||||
Currently ignore is_regex and is_casesensitive
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
for rec in (
|
for rec in (
|
||||||
@ -119,8 +123,8 @@ class Notes(Base):
|
|||||||
f"<Note(id={self.id}, row={self.row}, note={self.note}>"
|
f"<Note(id={self.id}, row={self.row}, note={self.note}>"
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def add_note(session, playlist_id, row, text):
|
def add_note(cls, session, playlist_id, row, text):
|
||||||
"Add note"
|
"Add note"
|
||||||
|
|
||||||
DEBUG(f"add_note(playlist_id={playlist_id}, row={row}, text={text})")
|
DEBUG(f"add_note(playlist_id={playlist_id}, row={row}, text={text})")
|
||||||
@ -130,6 +134,7 @@ class Notes(Base):
|
|||||||
note.note = text
|
note.note = text
|
||||||
session.add(note)
|
session.add(note)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
return note
|
return note
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -149,12 +154,14 @@ class Notes(Base):
|
|||||||
|
|
||||||
DEBUG(f"Notes.update_note(id={id}, row={row}, text={text})")
|
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
|
note.row = row
|
||||||
if text:
|
if text:
|
||||||
note.note = text
|
note.note = text
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
return note
|
||||||
|
|
||||||
|
|
||||||
class Playdates(Base):
|
class Playdates(Base):
|
||||||
__tablename__ = 'playdates'
|
__tablename__ = 'playdates'
|
||||||
@ -164,8 +171,8 @@ class Playdates(Base):
|
|||||||
track_id = Column(Integer, ForeignKey('tracks.id'))
|
track_id = Column(Integer, ForeignKey('tracks.id'))
|
||||||
tracks = relationship("Tracks", back_populates="playdates")
|
tracks = relationship("Tracks", back_populates="playdates")
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def add_playdate(session, track):
|
def add_playdate(cls, session, track):
|
||||||
"Record that track was played"
|
"Record that track was played"
|
||||||
|
|
||||||
DEBUG(f"add_playdate(track={track})")
|
DEBUG(f"add_playdate(track={track})")
|
||||||
@ -176,6 +183,8 @@ class Playdates(Base):
|
|||||||
track.update_lastplayed(session, track.id)
|
track.update_lastplayed(session, track.id)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
return pd
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def last_played(session, track_id):
|
def last_played(session, track_id):
|
||||||
"Return datetime track last played or None"
|
"Return datetime track last played or None"
|
||||||
|
|||||||
@ -44,11 +44,11 @@ def setup_database(connection):
|
|||||||
|
|
||||||
yield
|
yield
|
||||||
|
|
||||||
models.Base.metadata.drop_all()
|
Base.metadata.drop_all()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def db_session(setup_database, connection):
|
def session(setup_database, connection):
|
||||||
transaction = connection.begin()
|
transaction = connection.begin()
|
||||||
yield scoped_session(
|
yield scoped_session(
|
||||||
sessionmaker(autocommit=False, autoflush=False, bind=connection)
|
sessionmaker(autocommit=False, autoflush=False, bind=connection)
|
||||||
|
|||||||
@ -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
|
|
||||||
119
test_models.py
Normal file
119
test_models.py
Normal file
@ -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
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user