Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor songs so its unit testable #2

Merged
merged 3 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
python-version: ["3.11", "3.12", "3.13"]

steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
> Given a song and artist, fetches lyrics from [Genius](https://genius.com), translates them to English using [Azure AI Translator](https://learn.microsoft.com/en-us/azure/ai-services/translator/), and displays original and translated lyrics side-by-side in the console.

[![Python package](https://github.com/patkub/console-songs/actions/workflows/python-app.yml/badge.svg)](https://github.com/patkub/console-songs/actions/workflows/python-app.yml)
![Python 3.x Required](https://img.shields.io/badge/python-3.x-brightgreen.svg)
![Python 3.11+ Required](https://img.shields.io/badge/python-3.11+-brightgreen.svg)

### Idea
1. Fetch song lyrics
Expand All @@ -13,6 +13,8 @@
Enjoy listening to music and learning a new language!

### Setup
Requires Python 3.11+

Create an `.env` file with your API keys:
```
GENIUS_ACCESS_TOKEN=...
Expand Down
82 changes: 51 additions & 31 deletions songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# - Translate to English
# - Display original and English translated lyrics side-by-side

import argparse, os, sys
import argparse, os
from dotenv import load_dotenv

# Class to get song lyrics from Genius
Expand All @@ -20,42 +20,62 @@
# loading variables from .env file
load_dotenv()

#
# Parse arguments
#

parser = argparse.ArgumentParser()
parser.add_argument("song", nargs="+")
parser.add_argument('--experimental',
action=argparse.BooleanOptionalAction,
help='Enable experimental features.\nUses a patched version of Genius API')
args = parser.parse_args()
def process_song(song, artist, access_keys, experimental):
"""
Fetch song lyrics, translate to English, and display original and English side-by-side lyrics.
@param song: the name of the song
@param artist: the name of the artist
@param access_keys: dictionary of API access keys
@param experimental: use experimental features
"""
#
# Fetch song lyrics from Genius
#
patched_genius = PatchedGenius if experimental else None
lyrics_fetcher = FetchLyrics(access_keys["GENIUS_ACCESS_TOKEN"], patched_genius)
song_info = lyrics_fetcher.fetch_lyrics(song, artist)
if song_info is None:
# song not found, end
return

song = args.song[0]
artist = args.song[1] if len(args.song) > 1 else None
song_lyrics = song_info.lyrics

#
# Fetch song lyrics from Genius
#
#
# Translate lyrics to English using Microsoft Azure AI Translator
#
lyrics_translator = TranslateLyrics(access_keys["MS_TRANSLATOR_KEY"], access_keys["MS_TRANSLATOR_REGION"])
english_translation = lyrics_translator.translate_lyrics(song_lyrics)

patched_genius = PatchedGenius if args.experimental else None
lyrics_fetcher = FetchLyrics(os.getenv("GENIUS_ACCESS_TOKEN"), patched_genius)
song_info = lyrics_fetcher.fetch_lyrics(song, artist)
if song_info is None:
# song not found
sys.exit()
#
# Display original and English translated lyrics side-by-side
#
DisplayLyrics.display_lyrics(song_info, song_lyrics, english_translation)

song_lyrics = song_info.lyrics

#
# Translate lyrics to English using Microsoft Azure AI Translator
#
if __name__ == '__main__':
#
# Parse arguments
#

lyrics_translator = TranslateLyrics(os.getenv("MS_TRANSLATOR_KEY"), os.getenv("MS_TRANSLATOR_REGION"))
english_translation = lyrics_translator.translate_lyrics(song_lyrics)
parser = argparse.ArgumentParser()
parser.add_argument("song", nargs="+")
parser.add_argument('--experimental',
action=argparse.BooleanOptionalAction,
help='Enable experimental features.\nUses a patched version of Genius API')
args = parser.parse_args()

#
# Display original and English translated lyrics side-by-side
#
song = args.song[0]
artist = args.song[1] if len(args.song) > 1 else None

DisplayLyrics.display_lyrics(song_info, song_lyrics, english_translation)
access_keys = {
'GENIUS_ACCESS_TOKEN': os.getenv("GENIUS_ACCESS_TOKEN"),
'MS_TRANSLATOR_KEY': os.getenv("MS_TRANSLATOR_KEY"),
'MS_TRANSLATOR_REGION': os.getenv("MS_TRANSLATOR_REGION")
}

#
# Fetch song lyrics, translate to English, and display original and English side-by-side lyrics.
#

process_song(song, artist, access_keys, args.experimental)
65 changes: 65 additions & 0 deletions test_songs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from songs import process_song
from unittest.mock import Mock, patch, ANY

# mock object for reply from fetched lyrics
mockedFetchLyrics = Mock()
mockedFetchLyrics.lyrics = "mocked_lyrics"


@patch('fetch_lyrics.FetchLyrics.FetchLyrics.fetch_lyrics', return_value=mockedFetchLyrics)
@patch('translate_lyrics.TranslateLyrics.TranslateLyrics.translate_lyrics', return_value="english translation")
@patch('display_lyrics.DisplayLyrics.DisplayLyrics.display_lyrics')
def test_process_song(mocked_display_lyrics, mocked_translate_lyrics, mocked_fetch_lyrics):
"""
Fetch song lyrics, translate to English, and display original and English side-by-side lyrics.
"""

# Prepare
song = "test_song_name"
artist = "test_artist_name"
access_keys = {
'GENIUS_ACCESS_TOKEN': "fake_token_1",
'MS_TRANSLATOR_KEY': "fake_token_2",
'MS_TRANSLATOR_REGION': "fake_token_3"
}
experimental = False

# Act
process_song(song, artist, access_keys, experimental)

# Assert
# song searched by name and artist
mocked_fetch_lyrics.assert_called_with(song, artist)
# mocked lyrics get translated
mocked_translate_lyrics.assert_called_with("mocked_lyrics")
# display is called with song info, original and translated lyrics
mocked_display_lyrics.assert_called_with(ANY, "mocked_lyrics", "english translation")


@patch('fetch_lyrics.FetchLyrics.FetchLyrics.fetch_lyrics', return_value=None)
@patch('translate_lyrics.TranslateLyrics.TranslateLyrics.translate_lyrics', return_value="english translation")
@patch('display_lyrics.DisplayLyrics.DisplayLyrics.display_lyrics')
def test_process_song_null(mocked_display_lyrics, mocked_translate_lyrics, mocked_fetch_lyrics):
"""
Null song fetched, translation and display are not called
"""

# Prepare
song = "test_song_name"
artist = "test_artist_name"
access_keys = {
'GENIUS_ACCESS_TOKEN': "fake_token_1",
'MS_TRANSLATOR_KEY': "fake_token_2",
'MS_TRANSLATOR_REGION': "fake_token_3"
}
experimental = False

# Act
process_song(song, artist, access_keys, experimental)

# Assert
# song searched by name and artist
mocked_fetch_lyrics.assert_called_with(song, artist)
# fetched lyrics are null, so translation and display are not called
mocked_translate_lyrics.assert_not_called()
mocked_display_lyrics.assert_not_called()