Skip to content

Commit

Permalink
Add tests (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggravlingen authored Mar 9, 2024
1 parent 73e2add commit c61d1e2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
34 changes: 24 additions & 10 deletions pyesef/download/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ class Filing:
file_name: str
path: str

@property
def file_url(self) -> str:
"""Return file URL."""
return f"{BASE_URL}{self.path}/{self.file_name}"

@property
def download_country_folder(self) -> str:
"""Return download path."""
return os.path.join(PATH_ARCHIVES, self.country)

@property
def write_location(self) -> str:
"""Return file write location."""
return os.path.join(self.download_country_folder, self.file_name)


IdentifierType = dict[str, list[Filing]]

Expand Down Expand Up @@ -72,18 +87,17 @@ def _cleanup_package_dict(identifier_map: IdentifierType) -> list[Filing]:

def _download_package(filing: Filing) -> None:
"""Download a package and store it the archive-folder."""
url = f"{BASE_URL}{filing.path}/{filing.file_name}"

download_path = os.path.join(PATH_ARCHIVES, filing.country)

# Create download path if it does not exist
Path(download_path).mkdir(parents=True, exist_ok=True)
Path( # Create download path if it does not exist
filing.download_country_folder
).mkdir(
parents=True,
exist_ok=True,
)

LOGGER.info(f"Downloading {url}")
LOGGER.info(f"Downloading {filing.file_url}")

req = requests.get(url, stream=True, timeout=30)
write_location = os.path.join(download_path, filing.file_name)
with open(write_location, "wb") as _file:
req = requests.get(filing.file_url, stream=True, timeout=30)
with open(filing.write_location, "wb") as _file:
for chunk in req.iter_content(chunk_size=2048):
_file.write(chunk)

Expand Down
18 changes: 18 additions & 0 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,21 @@ def test_cleanup_package_dict(test_data, expected_result) -> None:
"""Test function _cleanup_package_dict."""
function_result = _cleanup_package_dict(test_data)
assert function_result == expected_result


def test_filing_property__file_url() -> None:
"""Return Filing.file_url."""
model = Filing(country="se", file_name="abc", path="a/b/c")
assert model.file_url == "https://filings.xbrl.org/a/b/c/abc"


def test_filing_property__download_country_folder() -> None:
"""Return Filing.download_country_folder."""
model = Filing(country="se", file_name="abc", path="a/b/c")
assert "/pyesef/archives/se" in model.download_country_folder


def test_filing_property__write_location() -> None:
"""Return Filing.write_location."""
model = Filing(country="se", file_name="abc", path="a/b/c")
assert "/pyesef/archives/se/abc" in model.write_location

0 comments on commit c61d1e2

Please sign in to comment.