import sqlite3 import unittest from pathlib import Path import change_path import shared class TestChangePath(unittest.TestCase): SAMPLE_TRACK_PATH_1 = '/music/music/Def Leppard/01 - Studio Albums/1980 - On Through The Night/03. Sorrow Is A Woman.mp3' SAMPLE_TRACK_PATH_2 = '/music/music/Def Leppard/01 - Studio Albums/1980 - On Through The Night/05. Satellite.mp3' NOTHING = '/nothing' def setUp(self) -> None: self.conn = sqlite3.connect(':memory:') with open(Path(__file__).parent.resolve() / 'database.sql', 'r', encoding='utf-8') as dump: self.conn.executescript(dump.read()) def test_move_file_into_existing_one(self): """ Make sure we can't move a track to path of another, existing in db, track as it would violate unique constraint on media_file.id (given it is md5(file_path) :return: """ with self.assertRaises(sqlite3.IntegrityError, msg='Expected a unique constraint violation') as context: change_path.main( self.conn, from_path=self.SAMPLE_TRACK_PATH_1, to_path=self.SAMPLE_TRACK_PATH_2 ) self.assertIn('UNIQUE constraint failed: media_file.id', context.exception.args) self.conn.rollback() def test_move_file_into_non_existing_one(self): self.assertEqual(1, len( shared.get_media_by_path(self.conn, self.SAMPLE_TRACK_PATH_1) ), 'expected to find one song in db by path') change_path.main( self.conn, from_path=self.SAMPLE_TRACK_PATH_1, to_path=self.NOTHING ) self.conn.commit() self.assertEqual(0, len( shared.get_media_by_path(self.conn, self.SAMPLE_TRACK_PATH_1) ), 'expected to not find a song by old path') self.assertEqual(1, len( shared.get_media_by_path(self.conn, self.NOTHING) ), 'expected to find moved song by a new path')