diff --git a/modules/database/db_manager.py b/modules/database/db_manager.py index 1375582..4925682 100644 --- a/modules/database/db_manager.py +++ b/modules/database/db_manager.py @@ -4,7 +4,7 @@ from modules.song_loader.song_data import SongData from utils import string_utils -from utils.collection_utils import set_of_the_tuples_from_the_first_position +from utils.collection_utils import get_tuples_from_the_first_position_of from utils.string_utils import remove_special_chars log = logging.getLogger() @@ -44,7 +44,7 @@ def __get_songs_from_db(self, artist, title): cur = self.db.cursor() songs = cur.execute("SELECT distinct colFileName FROM songs where colArtist like ? and colTitle like ?", ("%" + artist + "%", "%" + title + "%")).fetchall() - return set_of_the_tuples_from_the_first_position(songs) + return get_tuples_from_the_first_position_of(songs) def search_song_by_artist_and_title(self, artist, title): songs_from_db_without_special_chars = self.__get_songs_from_db(remove_special_chars(artist), @@ -87,8 +87,8 @@ def insert_song(self, song_data): def all_song_filenames(self): cur = self.db.cursor() - all_elements_tuple = cur.execute("SELECT distinct colFileName FROM songs").fetchall() - return set_of_the_tuples_from_the_first_position(all_elements_tuple) + all_songs = cur.execute("SELECT distinct colFileName FROM songs").fetchall() + return get_tuples_from_the_first_position_of(all_songs) def delete_song_by_filename(self, to_delete): cur = self.db.cursor() diff --git a/tests/utils/test_collection_utils.py b/tests/utils/test_collection_utils.py index dbaa449..e3cc92d 100644 --- a/tests/utils/test_collection_utils.py +++ b/tests/utils/test_collection_utils.py @@ -31,41 +31,50 @@ def test_is_not_empty(test_input, expected): assert collection_utils.is_not_empty(test_input) == expected -def test_set_of_the_tuples_from_the_first_position__with_none(): +def test_get_tuples_from_the_first_position_of__with_none(): tuple_to_test = None - actual = collection_utils.set_of_the_tuples_from_the_first_position(tuple_to_test) + # noinspection PyTypeChecker + actual = collection_utils.get_tuples_from_the_first_position_of(tuple_to_test) - assert actual is None + assert len(actual) == 0 -def test_set_of_the_tuples_from_the_first_position__with_an_empty_list_input(): +def test_get_tuples_from_the_first_position_of__with_an_empty_list_input(): tuple_to_test = [] - actual = collection_utils.set_of_the_tuples_from_the_first_position(tuple_to_test) + actual = collection_utils.get_tuples_from_the_first_position_of(tuple_to_test) assert len(actual) == 0 -def test_set_of_the_tuples_from_the_first_position__with_1_tuple_and_string_in_the_first_position(): +def test_get_tuples_from_the_first_position_of__with_1_tuple_and_string_in_the_first_position__type_is_set(): + test_input = [('first tuple 1st', 'first tuple 2nd', 'first tuple 3rd')] + + actual = collection_utils.get_tuples_from_the_first_position_of(test_input) + + assert isinstance(actual, set) + + +def test_get_tuples_from_the_first_position_of__with_1_tuple_and_string_in_the_first_position(): test_input = [('first tuple 1st', 'first tuple 2nd', 'first tuple 3rd')] expected = {'first tuple 1st'} - actual = collection_utils.set_of_the_tuples_from_the_first_position(test_input) + actual = collection_utils.get_tuples_from_the_first_position_of(test_input) assert actual == expected -def test_set_of_the_tuples_from_the_first_position__with_1_tuple_and_int_in_the_first_position(): +def test_get_tuples_from_the_first_position_of__with_1_tuple_and_int_in_the_first_position(): test_input = [(1, 'first tuple 2nd', 'first tuple 3rd')] expected = {1} - actual = collection_utils.set_of_the_tuples_from_the_first_position(test_input) + actual = collection_utils.get_tuples_from_the_first_position_of(test_input) assert actual == expected -def test_set_of_the_tuples_from_the_first_position__with_2_tuples_and_string_in_the_first_position(): +def test_get_tuples_from_the_first_position_of__with_2_tuples_and_string_in_the_first_position(): test_input = [ ('first tuple 1st', 'first tuple 2nd', 'first tuple 3rd'), ('second tuple 1st', 'second tuple 2nd', 'second tuple 3rd'), @@ -75,19 +84,19 @@ def test_set_of_the_tuples_from_the_first_position__with_2_tuples_and_string_in_ 'second tuple 1st', } - actual = collection_utils.set_of_the_tuples_from_the_first_position(test_input) + actual = collection_utils.get_tuples_from_the_first_position_of(test_input) assert actual == expected -def test_set_of_the_tuples_from_the_first_position__with_2_tuples_and_int_in_the_first_position(): +def test_get_tuples_from_the_first_position_of__with_2_tuples_and_int_in_the_first_position(): test_input = [ (1, 'first tuple 2nd', 'first tuple 3rd'), (2, 'second tuple 2nd', 'second tuple 3rd'), ] expected = {1, 2} - actual = collection_utils.set_of_the_tuples_from_the_first_position(test_input) + actual = collection_utils.get_tuples_from_the_first_position_of(test_input) assert actual == expected diff --git a/utils/collection_utils.py b/utils/collection_utils.py index c249b3c..87daf6a 100644 --- a/utils/collection_utils.py +++ b/utils/collection_utils.py @@ -9,10 +9,10 @@ def is_not_empty(collection): return bool(collection) -def set_of_the_tuples_from_the_first_position(tuples): - if tuples is None: - return None - return {item[0] for item in tuples} +def get_tuples_from_the_first_position_of(list_of_tuples: list[tuple]) -> set: + if list_of_tuples is None: + return set() + return {item[0] for item in list_of_tuples} def repr_in_multi_line(collection):