251 lines
6.6 KiB
Python
251 lines
6.6 KiB
Python
import datetime as dt
|
|
|
|
from app.models import (
|
|
Carts,
|
|
NoteColours,
|
|
Playdates,
|
|
Playlists,
|
|
PlaylistRows,
|
|
Tracks,
|
|
)
|
|
|
|
|
|
def test_notecolours_get_colour(session):
|
|
"""Create a colour record and retrieve all colours"""
|
|
|
|
print(">>>text_notcolours_get_colour")
|
|
note_colour = "#0bcdef"
|
|
NoteColours(session, substring="substring", colour=note_colour)
|
|
|
|
records = NoteColours.get_all(session)
|
|
assert len(records) == 1
|
|
record = records[0]
|
|
assert record.colour == note_colour
|
|
|
|
|
|
def test_notecolours_get_all(session):
|
|
"""Create two colour records and retrieve them all"""
|
|
|
|
print(">>>text_notcolours_get_all")
|
|
note1_colour = "#1bcdef"
|
|
note2_colour = "#20ff00"
|
|
NoteColours(session, substring="note1", colour=note1_colour)
|
|
NoteColours(session, substring="note2", colour=note2_colour)
|
|
|
|
records = NoteColours.get_all(session)
|
|
assert len(records) == 2
|
|
assert note1_colour in [n.colour for n in records]
|
|
assert note2_colour in [n.colour for n in records]
|
|
|
|
|
|
def test_notecolours_get_colour_none(session):
|
|
note_colour = "#3bcdef"
|
|
NoteColours(session, substring="substring", colour=note_colour)
|
|
|
|
result = NoteColours.get_colour(session, "xyz")
|
|
assert result is None
|
|
|
|
|
|
def test_notecolours_get_colour_match(session):
|
|
note_colour = "#4bcdef"
|
|
nc = NoteColours(session, substring="sub", colour=note_colour)
|
|
assert nc
|
|
|
|
result = NoteColours.get_colour(session, "The substring")
|
|
assert result == note_colour
|
|
|
|
|
|
def test_playdates_add_playdate(session, track1):
|
|
"""Test playdate and last_played retrieval"""
|
|
|
|
playdate = Playdates(session, track1.id)
|
|
assert playdate
|
|
print(playdate)
|
|
|
|
last_played = Playdates.last_played(session, track1.id)
|
|
assert abs((playdate.lastplayed - last_played).total_seconds()) < 2
|
|
|
|
|
|
def test_playdates_played_after(session, track1):
|
|
playdate = Playdates(session, track1.id)
|
|
yesterday = dt.datetime.now() - dt.timedelta(days=1)
|
|
played = Playdates.played_after(session, yesterday)
|
|
|
|
assert len(played) == 1
|
|
assert played[0] == playdate
|
|
|
|
|
|
def test_playlist_create(session):
|
|
TEMPLATE_NAME = "my template"
|
|
|
|
playlist = Playlists(session, "my playlist")
|
|
assert playlist
|
|
print(playlist)
|
|
|
|
# test clear tabs
|
|
Playlists.clear_tabs(session, [playlist.id])
|
|
|
|
# create template
|
|
Playlists.save_as_template(session, playlist.id, TEMPLATE_NAME)
|
|
|
|
# test create template
|
|
_ = Playlists.create_playlist_from_template(session, playlist, "my new name")
|
|
|
|
# get all templates
|
|
all_templates = Playlists.get_all_templates(session)
|
|
assert len(all_templates) == 1
|
|
# Save as template creates new playlist
|
|
assert all_templates[0] != playlist
|
|
# test delete playlist
|
|
playlist.delete(session)
|
|
|
|
|
|
def test_playlist_open_and_close(session):
|
|
# We need a playlist
|
|
playlist = Playlists(session, "my playlist")
|
|
|
|
assert len(Playlists.get_open(session)) == 0
|
|
assert len(Playlists.get_closed(session)) == 1
|
|
|
|
playlist.mark_open()
|
|
|
|
assert len(Playlists.get_open(session)) == 1
|
|
assert len(Playlists.get_closed(session)) == 0
|
|
|
|
playlist.close()
|
|
|
|
assert len(Playlists.get_open(session)) == 0
|
|
assert len(Playlists.get_closed(session)) == 1
|
|
|
|
|
|
def test_playlist_get_all_and_by_id(session):
|
|
# We need two playlists
|
|
p1_name = "playlist one"
|
|
p2_name = "playlist two"
|
|
playlist1 = Playlists(session, p1_name)
|
|
_ = Playlists(session, p2_name)
|
|
|
|
all_playlists = Playlists.get_all(session)
|
|
assert len(all_playlists) == 2
|
|
assert p1_name in [p.name for p in all_playlists]
|
|
assert p2_name in [p.name for p in all_playlists]
|
|
assert session.get(Playlists, playlist1.id).name == p1_name
|
|
|
|
|
|
def test_tracks_get_all_tracks(session, track1, track2):
|
|
# Need two tracks
|
|
|
|
result = [a.path for a in Tracks.get_all(session)]
|
|
assert track1.path in result
|
|
assert track2.path in result
|
|
|
|
|
|
def test_tracks_by_path(session, track1):
|
|
|
|
assert Tracks.get_by_path(session, track1.path) is track1
|
|
|
|
|
|
def test_tracks_by_id(session, track1):
|
|
|
|
assert session.get(Tracks, track1.id) is track1
|
|
|
|
|
|
def test_tracks_search_artists(session, track1):
|
|
track1_artist = "Fleetwood Mac"
|
|
|
|
assert len(Tracks.search_artists(session, track1_artist)) == 1
|
|
|
|
|
|
def test_tracks_search_titles(session, track1):
|
|
track1_title = "I'm So Afraid"
|
|
|
|
assert len(Tracks.search_titles(session, track1_title)) == 1
|
|
|
|
def test_repr(session):
|
|
"""Just check for error retrieving reprs"""
|
|
|
|
nc = NoteColours(session, substring="x", colour="x")
|
|
print(nc)
|
|
|
|
|
|
def test_get_colour(session):
|
|
"""Test for errors in execution"""
|
|
|
|
GOOD_STRING = "cantelope"
|
|
BAD_STRING = "ericTheBee"
|
|
SUBSTR = "ant"
|
|
COLOUR = "blue"
|
|
|
|
nc1 = NoteColours(session, substring=SUBSTR, colour=COLOUR, is_casesensitive=True)
|
|
|
|
_ = nc1.get_colour(session, "")
|
|
colour = nc1.get_colour(session, GOOD_STRING)
|
|
assert colour == COLOUR
|
|
|
|
colour = nc1.get_colour(session, BAD_STRING)
|
|
assert colour is None
|
|
|
|
nc2 = NoteColours(session, substring=".*" + SUBSTR, colour=COLOUR, is_regex=True)
|
|
colour = nc2.get_colour(session, GOOD_STRING)
|
|
assert colour == COLOUR
|
|
|
|
colour = nc2.get_colour(session, BAD_STRING)
|
|
assert colour is None
|
|
|
|
nc3 = NoteColours(
|
|
session, substring=".*" + SUBSTR, colour=COLOUR, is_regex=True, is_casesensitive=True
|
|
)
|
|
|
|
colour = nc3.get_colour(session, GOOD_STRING)
|
|
assert colour == COLOUR
|
|
|
|
colour = nc3.get_colour(session, BAD_STRING)
|
|
assert colour is None
|
|
|
|
|
|
def test_create_cart(session):
|
|
cart = Carts(session, 1, "name")
|
|
assert cart
|
|
print(cart)
|
|
|
|
|
|
def test_name_available(session):
|
|
PLAYLIST_NAME = "a name"
|
|
RENAME = "new name"
|
|
|
|
if Playlists.name_is_available(session, PLAYLIST_NAME):
|
|
playlist = Playlists(session, PLAYLIST_NAME)
|
|
assert playlist
|
|
|
|
assert Playlists.name_is_available(session, PLAYLIST_NAME) is False
|
|
|
|
playlist.rename(session, RENAME)
|
|
|
|
|
|
def test_create_playlist_row(session):
|
|
PLAYLIST_NAME = "a name"
|
|
|
|
if Playlists.name_is_available(session, PLAYLIST_NAME):
|
|
playlist = Playlists(session, PLAYLIST_NAME)
|
|
|
|
plr = PlaylistRows(session, playlist.id, 1)
|
|
assert plr
|
|
print(plr)
|
|
plr.append_note("a note")
|
|
plr.append_note("another note")
|
|
|
|
|
|
def test_delete_plr(session):
|
|
PLAYLIST_NAME = "a name"
|
|
|
|
if Playlists.name_is_available(session, PLAYLIST_NAME):
|
|
playlist = Playlists(session, PLAYLIST_NAME)
|
|
|
|
plr = PlaylistRows(session, playlist.id, 1)
|
|
assert plr
|
|
PlaylistRows.delete_higher_rows(session, plr.playlist_id, 10)
|
|
|
|
assert PlaylistRows.get_track_plr(session, 12, plr.playlist_id) is None
|
|
|
|
|