Skip to content

Commit

Permalink
linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zstumgoren committed Apr 14, 2024
1 parent 191c7e3 commit 30eb634
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
13 changes: 7 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import csv
import datetime
import os
from pathlib import Path

Expand All @@ -11,11 +10,13 @@
# and pass pytest caplog fixture to
# a test function. More details here:
# https://vcrpy.readthedocs.io/en/latest/debugging.html
# import vcr
# import logging
# logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from vcrpy
# vcr_log = logging.getLogger("vcr")
# vcr_log.setLevel(logging.INFO)
"""
import vcr
import logging
logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from vcrpy
vcr_log = logging.getLogger("vcr")
vcr_log.setLevel(logging.INFO)
"""


@pytest.fixture(scope="module")
Expand Down
18 changes: 14 additions & 4 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import pytest
from pathlib import Path
from unittest.mock import patch

import pytest

from clean.cache import Cache


@pytest.fixture
def cache(tmp_path):
return Cache(tmp_path)


@pytest.mark.vcr
def test_download_existing_file(cache):
# Create a dummy file in the cache
Expand All @@ -20,6 +22,7 @@ def test_download_existing_file(cache):
# Assert that the existing file is returned
assert result == file_path


@pytest.mark.vcr
def test_download_new_file(cache):
# Call the download method with a new file
Expand All @@ -28,6 +31,7 @@ def test_download_new_file(cache):
# Assert that a new file is created in the cache
assert result.exists()


@pytest.mark.vcr
def test_download_existing_file_with_force(cache):
# Create a dummy file in the cache
Expand All @@ -40,6 +44,7 @@ def test_download_existing_file_with_force(cache):
# Assert that a new file is created in the cache
assert result.exists()


@pytest.mark.vcr
def test_download_with_encoding(cache):
# Call the download method with encoding specified
Expand All @@ -48,11 +53,16 @@ def test_download_with_encoding(cache):
# Assert that the file is downloaded with the specified encoding
assert result.exists()


@pytest.mark.vcr
@patch("clean.cache.get_url")
def test_download_with_additional_args(mock_get_url, cache):
# Call the download method with additional arguments
result = cache.download("file.txt", "http://example.com", headers={"User-Agent": "Mozilla/5.0"})
cache.download(
"file.txt", "http://example.com", headers={"User-Agent": "Mozilla/5.0"}
)

# Assert that the additional arguments are passed to get_url
mock_get_url.assert_called_once_with("http://example.com", stream=True, headers={"User-Agent": "Mozilla/5.0"})
mock_get_url.assert_called_once_with(
"http://example.com", stream=True, headers={"User-Agent": "Mozilla/5.0"}
)
25 changes: 15 additions & 10 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,45 @@


@pytest.fixture
def mock_runner():
with patch('clean.cli.Runner') as MockRunner:
def mock_runner():
with patch("clean.cli.Runner") as MockRunner:
mock_runner = MockRunner.return_value
mock_runner.scrape.return_value = "Invoked scrape"
mock_runner.scrape_meta.return_value = "Invoked scrape"
yield mock_runner


@pytest.mark.usefixtures("set_default_env", "create_scraper_dir")
def test_cli_scrape_command(mock_runner):
"Test the 'scrape' command"
"""Test the 'scrape' command."""
runner = CliRunner()
result = runner.invoke(
runner.invoke(
cli,
[
"scrape",
"ca_san_diego_pd",
"--log-level", "DEBUG",
"--throttle", "1",
"--log-level",
"DEBUG",
"--throttle",
"1",
],
)
mock_runner.scrape.assert_called_once_with("ca_san_diego_pd", filter="")


@pytest.mark.usefixtures("set_default_env", "create_scraper_dir")
def test_cli_scrape_meta_command(mock_runner):
"Test the 'scrape' command"
"""Test the 'scrape' command."""
runner = CliRunner()
result = runner.invoke(
runner.invoke(
cli,
[
"scrape-meta",
"ca_san_diego_pd",
"--log-level", "DEBUG",
"--throttle", "1",
"--log-level",
"DEBUG",
"--throttle",
"1",
],
)
mock_runner.scrape_meta.assert_called_once_with("ca_san_diego_pd")
9 changes: 6 additions & 3 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import pytest
from pathlib import Path
from unittest.mock import patch

import pytest

from clean.runner import Runner


@pytest.fixture
def runner(tmp_path):
return Runner(data_dir=tmp_path / "exports", cache_dir=tmp_path / "cache")


def test_scrape_meta(runner):
# Call the scrape_meta method with a valid agency slug
with patch("clean.ca.san_diego_pd.Site.scrape_meta") as mock_scrape_meta:
runner.scrape_meta("ca_san_diego_pd")
# Assert that the scrape_meta method was called
mock_scrape_meta.assert_called_once_with(throttle=0)


def test_scrape(runner):
with patch("clean.ca.san_diego_pd.Site.scrape") as mock_scrape:
runner.scrape("ca_san_diego_pd")
# Assert that the scrape method was called
mock_scrape.assert_called_once_with(throttle=0, filter='')
mock_scrape.assert_called_once_with(throttle=0, filter="")

0 comments on commit 30eb634

Please sign in to comment.