Skip to content

Commit

Permalink
Merge pull request #264 from kozaka-tv/261-in-collection_utilsset_of_…
Browse files Browse the repository at this point in the history
…the_tuples_from_the_first_position-return-an-empty-set

#261 now on, always a set is returned
  • Loading branch information
kozaka-tv authored Jun 4, 2024
2 parents 6b74fb3 + a54b2b0 commit d5a86b1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
8 changes: 4 additions & 4 deletions modules/database/db_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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()
Expand Down
35 changes: 22 additions & 13 deletions tests/utils/test_collection_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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

Expand Down
8 changes: 4 additions & 4 deletions utils/collection_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit d5a86b1

Please sign in to comment.