diff --git a/app/helpers.py b/app/helpers.py index 2657dfc..892c7d0 100644 --- a/app/helpers.py +++ b/app/helpers.py @@ -7,6 +7,7 @@ from pydub import AudioSegment from mutagen.flac import FLAC from mutagen.mp3 import MP3 from PyQt5.QtWidgets import QMessageBox +from tinytag import TinyTag def fade_point(audio_segment, fade_threshold=0, @@ -47,7 +48,7 @@ def get_audio_segment(path): return None -def get_tag_data(path): +def get_tags(path): """ Return a dictionary of title, artist, duration-in-milliseconds and path. """ @@ -191,5 +192,3 @@ def show_warning(title, msg): def trailing_silence(audio_segment, silence_threshold=-50.0, chunk_size=Config.AUDIO_SEGMENT_CHUNK_SIZE): return fade_point(audio_segment, silence_threshold, chunk_size) - - diff --git a/test_helpers.py b/test_helpers.py new file mode 100644 index 0000000..a8ffc6c --- /dev/null +++ b/test_helpers.py @@ -0,0 +1,72 @@ +from config import Config +from datetime import datetime, timedelta +from helpers import * +from models import Tracks + + +def test_fade_point(): + test_track_path = "../testdata/isa.mp3" + test_track_data = "../testdata/isa.py" + + audio_segment = get_audio_segment(test_track_path) + assert audio_segment + + fade_at = fade_point(audio_segment) + + # Get test data + with open(test_track_data) as f: + testdata = eval(f.read()) + + # Volume detection can vary, so ± 1 second is OK + assert fade_at < testdata['fade_at'] + 1000 + assert fade_at > testdata['fade_at'] - 1000 + + +def test_get_tags(): + test_track_path = "../testdata/mom.mp3" + test_track_data = "../testdata/mom.py" + + tags = get_tags(test_track_path) + + # Get test data + with open(test_track_data) as f: + testdata = eval(f.read()) + + assert tags['artist'] == testdata['artist'] + assert tags['title'] == testdata['title'] + + +def test_get_relative_date(): + assert get_relative_date(None) == "Never" + today_at_10 = datetime.now().replace(hour=10, minute=0) + today_at_11 = datetime.now().replace(hour=11, minute=0) + assert get_relative_date(today_at_10, today_at_11) == "10:00" + eight_days_ago = today_at_10 - timedelta(days=8) + assert get_relative_date(eight_days_ago, today_at_11) == "1 week, 1 day ago" + sixteen_days_ago = today_at_10 - timedelta(days=16) + assert get_relative_date( + sixteen_days_ago, today_at_11) == "2 weeks, 2 days ago" + + +def test_leading_silence(): + test_track_path = "../testdata/isa.mp3" + test_track_data = "../testdata/isa.py" + + audio_segment = get_audio_segment(test_track_path) + assert audio_segment + + silence_at = leading_silence(audio_segment) + + # Get test data + with open(test_track_data) as f: + testdata = eval(f.read()) + + # Volume detection can vary, so ± 1 second is OK + assert silence_at < testdata['leading_silence'] + 1000 + assert silence_at > testdata['leading_silence'] - 1000 + + +def test_ms_to_mmss(): + assert ms_to_mmss(None) == "-" + assert ms_to_mmss(59600) == "0:59" + assert ms_to_mmss((5 * 60 * 1000) + 23000) == "5:23" \ No newline at end of file