musicmuster/test_models.py
2022-03-02 09:12:55 +00:00

223 lines
5.4 KiB
Python

import time
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_by_id(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_by_id(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_by_id(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
def test_playdates_remove_track(session):
"""Test removing a track from a playdate"""
# 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()
Playdates.add_playdate(session, track)
Playdates.remove_track(session, track.id)
last_played = Playdates.last_played(session, track.id)
assert last_played is None
def test_playlist_add_track(session):
"""Test adding track to playlist"""
# 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()
playlist = Playlists()
playlist.name = "Test playlist"
session.add(playlist)
session.commit()
playlist.add_track(session, track)
tracks = playlist.get_tracks()
assert len(tracks) == 1
assert tracks[0].path == track_path
def test_playlist_close(session):
"""Test closing a playlist"""
playlist = Playlists()
playlist.name = "Test playlist one"
session.add(playlist)
session.commit()
playlist2 = Playlists()
playlist2.name = "Test playlist two"
session.add(playlist2)
session.commit()
apl = Playlists.get_all_playlists(session)
assert len(apl) == 2
cpl = Playlists.get_all_closed_playlists(session)
assert len(cpl) == 0
playlist2.close(session)
cpl = Playlists.get_all_closed_playlists(session)
assert len(cpl) == 1
def test_playlist_get_last_user(session):
"""Test get_last_used function"""
playlist1 = Playlists()
playlist1.name = "Test playlist one"
session.add(playlist1)
session.commit()
playlist2 = Playlists()
playlist2.name = "Test playlist two"
session.add(playlist2)
session.commit()
playlist3 = Playlists()
playlist3.name = "Test playlist three"
session.add(playlist3)
session.commit()
apl = Playlists.get_all_playlists(session)
assert len(apl) == 3
playlist1.open(session)
playlist2.close(session)
time.sleep(1)
playlist3.open(session)
last_used = Playlists.get_last_used(session)
assert len(last_used) == 2
assert last_used[0].name == "Test playlist three"
def test_playlist_new(session):
"""Test new function"""
plname = "This is a test name"
p = Playlists.new(session, plname)
n = Playlists.get_playlist_by_id(session, p.id)
assert p.name == n.name