Skip to content

Commit

Permalink
Implement of logic for the only_n_last_entities
Browse files Browse the repository at this point in the history
  • Loading branch information
dplocki committed Feb 7, 2024
1 parent e76aaa7 commit ea947b7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
9 changes: 7 additions & 2 deletions podcast_downloader/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
flatten_rss_links_data,
get_raw_rss_entries_from_web,
limit_file_name,
only_last_entity,
only_entities_from_date,
only_last_n_entities,
)


Expand Down Expand Up @@ -78,7 +78,7 @@ def configuration_to_function_on_empty_directory(
configuration_value: str,
) -> Callable[[Iterable[RSSEntity]], Iterable[RSSEntity]]:
if configuration_value == "download_last":
return only_last_entity
return partial(only_last_n_entities, 1)

if configuration_value == "download_all_from_feed":
return lambda source: source
Expand All @@ -90,6 +90,11 @@ def configuration_to_function_on_empty_directory(
from_date = get_n_age_date(int(from_n_day_match[1]), local_time)
return only_entities_from_date(from_date)

last_n_episodes = re.match(r"^download_last_(\d+)_episodes", configuration_value)
if last_n_episodes:
download_limit = int(last_n_episodes[1])
return partial(only_last_n_entities, download_limit)

from_nth_day_match = re.match(r"^download_from_(.*)", configuration_value)
if from_nth_day_match:
day_label = parse_day_label(from_nth_day_match[1])
Expand Down
6 changes: 4 additions & 2 deletions podcast_downloader/rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ def build_only_new_entities(
)


def only_last_entity(raw_rss_entries: Iterator[RSSEntity]) -> Iterator[RSSEntity]:
return islice(raw_rss_entries, 1)
def only_last_n_entities(
n: int, raw_rss_entries: Iterator[RSSEntity]
) -> Iterator[RSSEntity]:
return islice(raw_rss_entries, n)


def is_entity_newer(from_date: time.struct_time, entity: RSSEntity) -> bool:
Expand Down
21 changes: 0 additions & 21 deletions tests/only_last_rss_entity_test.py

This file was deleted.

36 changes: 36 additions & 0 deletions tests/only_n_last_entities_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import unittest

from podcast_downloader.rss import only_last_n_entities
from commons import rss_entity_generator


class TestOnlyNLastRSSEntity(unittest.TestCase):
def test_get_only_last_n_rss_entities_happy_path(self):
# Assign
entity_1, entity_2, entity_3, entity_4, entity_5 = rss_entity_generator(limit=5)

rss_entities = [entity_1, entity_2, entity_3, entity_4, entity_5]
expected = [entity_1, entity_2]

# Act
result = list(only_last_n_entities(2, rss_entities))

# Assert
self.assertSequenceEqual(
result, expected, "Only the last entity should be return as result"
)

def test_get_only_last_n_rss_entities_less_than_required(self):
# Assign
entity_1 = rss_entity_generator(limit=1)

rss_entities = [entity_1]
expected = [entity_1]

# Act
result = list(only_last_n_entities(2, rss_entities))

# Assert
self.assertSequenceEqual(
result, expected, "Only the last entity should be return as result"
)

0 comments on commit ea947b7

Please sign in to comment.