Skip to content

Commit

Permalink
E2E: test_configuration_downloads_limit_option
Browse files Browse the repository at this point in the history
  • Loading branch information
dplocki committed Jan 29, 2024
1 parent 01c3241 commit 2298a09
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 12 deletions.
76 changes: 65 additions & 11 deletions e2e/fixures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
import random
import subprocess
import sys
from e2e.random import generate_random_sentence, generate_random_mp3_file
from e2e.random import (
generate_random_sentence,
generate_random_mp3_file,
generate_random_string,
)
from feedgen.feed import FeedGenerator
from pathlib import Path
from pytest_httpserver import HTTPServer
from typing import Dict, List, Set
from typing import Dict, Generator, List, Set


def print_set_content(content: Set):
Expand All @@ -18,9 +22,10 @@ def print_set_content(content: Set):
class FeedBuilder:
FEED_RSS_FILE_NAME = "/rss_feed.xml"

def __init__(self, httpserver: HTTPServer) -> None:
def __init__(self, httpserver: HTTPServer, url_prefix: str = None) -> None:
self.metadata = []
self.httpserver = httpserver
self.url_prefix = url_prefix or ""

def add_entry(
self,
Expand Down Expand Up @@ -75,25 +80,27 @@ def __build_rss(self):
fg.link(href="http://example.com", rel="alternate")

for file_name, title, description, published_date, file_type in self.metadata:
self.httpserver.expect_request("/" + file_name).respond_with_data(
"mp3_content"
)
self.httpserver.expect_request(
self.url_prefix + "/" + file_name
).respond_with_data("mp3_content")

fe = fg.add_entry()
fe.title(title)
fe.description(description)
fe.enclosure(self.httpserver.url_for(file_name), 0, file_type)
fe.enclosure(
self.httpserver.url_for(self.url_prefix + "/" + file_name), 0, file_type
)
fe.published(published_date)

self.httpserver.expect_request(self.FEED_RSS_FILE_NAME).respond_with_data(
fg.rss_str()
)
self.httpserver.expect_request(
self.url_prefix + self.FEED_RSS_FILE_NAME
).respond_with_data(fg.rss_str())

def get_feed_url(self) -> str:
self.__fill_up_dates()
self.__build_rss()

return self.httpserver.url_for(self.FEED_RSS_FILE_NAME)
return self.httpserver.url_for(self.url_prefix + self.FEED_RSS_FILE_NAME)


class PodcastDirectory:
Expand Down Expand Up @@ -128,6 +135,43 @@ def path(self):
return str(self.download_destination_directory)


class MultiplePodcastDirectory:
def __init__(self, tmp_path: Path) -> None:
self.tmp_path = tmp_path
self.directories = {}

def __get_directory(self, name) -> Path:
if name not in self.directories:
feed_destination_path = self.tmp_path / "destination"
if not feed_destination_path.exists():
feed_destination_path.mkdir()

output_directory = feed_destination_path / name
output_directory.mkdir()

self.directories[name] = output_directory

return self.directories[name]

def get_first_directory(self) -> str:
return str(self.__get_directory("first"))

def get_second_directory(self) -> str:
return str(self.__get_directory("second"))

def get_first_directory_files(self) -> Generator[str, None, None]:
return self.__get_directory("first").iterdir()

def get_second_directory_files(self) -> Generator[str, None, None]:
return self.__get_directory("second").iterdir()


class MultipleFeedBuilder:
def __init__(self, httpserver) -> None:
self.first_feed = FeedBuilder(httpserver, "/" + generate_random_string())
self.second_feed = FeedBuilder(httpserver, "/" + generate_random_string())


@pytest.fixture()
def download_destination_directory(tmp_path) -> Path:
feed_destination_path = tmp_path / "destination"
Expand All @@ -146,6 +190,16 @@ def podcast_directory(download_destination_directory):
yield PodcastDirectory(download_destination_directory)


@pytest.fixture()
def podcast_directory_manager(tmp_path):
yield MultiplePodcastDirectory(tmp_path)


@pytest.fixture()
def feed_builder_manager(httpserver):
return MultipleFeedBuilder(httpserver)


@pytest.fixture
def use_config():
def internal(config_object: Dict):
Expand Down
53 changes: 52 additions & 1 deletion e2e/test_config_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
from typing import Callable, Dict
from e2e.fixures import (
FeedBuilder,
MultipleFeedBuilder,
MultiplePodcastDirectory,
PodcastDirectory,
run_podcast_downloader,
# fixures:
download_destination_directory,
feed,
feed_builder_manager,
use_config,
podcast_directory,
download_destination_directory,
podcast_directory_manager,
)
from e2e.random import (
generate_random_file,
Expand Down Expand Up @@ -88,6 +92,7 @@ def test_configuration_file_name_template_option(
use_config: Callable[[Dict], None],
podcast_directory: PodcastDirectory,
):
# Arrange
mp3_files_date = [generate_random_mp3_file() for _ in range(generate_random_int())]

for file_name in mp3_files_date:
Expand Down Expand Up @@ -117,3 +122,49 @@ def test_configuration_file_name_template_option(
for file_name in mp3_files_date
]
)


def test_configuration_downloads_limit_option(
feed_builder_manager: MultipleFeedBuilder,
use_config: Callable[[Dict], None],
podcast_directory_manager: MultiplePodcastDirectory,
):
# Arrange
limit = random.randrange(9, 13)

for _ in range(generate_random_int()):
feed_builder_manager.first_feed.add_entry(file_name=generate_random_mp3_file())

for _ in range(generate_random_int()):
feed_builder_manager.second_feed.add_entry(file_name=generate_random_mp3_file())

use_config(
{
"if_directory_empty": "download_all_from_feed",
"downloads_limit": limit,
"podcasts": [
{
"name": generate_random_string(),
"path": podcast_directory_manager.get_first_directory(),
"rss_link": feed_builder_manager.first_feed.get_feed_url(),
},
{
"name": generate_random_string(),
"path": podcast_directory_manager.get_second_directory(),
"rss_link": feed_builder_manager.second_feed.get_feed_url(),
},
],
}
)

# Act
run_podcast_downloader()

# Assert
downloaded_files_count = sum(
1 for _ in podcast_directory_manager.get_first_directory_files()
) + sum(1 for _ in podcast_directory_manager.get_second_directory_files())

assert (
downloaded_files_count == limit
), f"Excepted the download files number ({downloaded_files_count}) to equal to limit ({limit})"

0 comments on commit 2298a09

Please sign in to comment.