Skip to content

Commit

Permalink
Use pre-commit to lint code on CI (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
glujan authored Nov 8, 2023
1 parent 8827e10 commit a283341
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 49 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/automated-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ jobs:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
- uses: pre-commit/[email protected]
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
- name: Install dependencies
run: |
pip install -r requirements.dev.txt
- name: Lint with flake8
run: |
flake8 . --count --show-source
- name: Test pinned deps with unittest
run: |
coverage erase
Expand Down
17 changes: 6 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
minimum_pre_commit_version: "2.20.0"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: "v4.3.0"
rev: "v4.5.0"
hooks:
- id: check-ast
- id: check-case-conflict
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/asottile/pyupgrade
rev: v2.38.2
rev: v3.15.0
hooks:
- id: pyupgrade
args: [--py38-plus]
- repo: https://github.com/PyCQA/isort
rev: "5.10.1"
rev: "5.12.0"
hooks:
- id: isort
args: ["--profile=black"]
- repo: https://github.com/psf/black
rev: "22.8.0"
rev: "23.11.0"
hooks:
- id: black
args: ["--target-version=py38"]
- repo: https://github.com/PyCQA/flake8
rev: "5.0.4"
rev: "6.1.0"
hooks:
- id: flake8
args: ["--config=setup.cfg"]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v0.991"
rev: "v1.6.1"
hooks:
- id: mypy
args: ["--ignore-missing-imports", "--python-version=3.10"]
- repo: https://github.com/asottile/setup-cfg-fmt
rev: v2.2.0
hooks:
- id: setup-cfg-fmt
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ can turn it on by using `--use-checksums`.
You can change a log level by using `--log-level=<YOUR_LOG_LEVEL>`. Choices are
DEBUG, INFO, WARNING, ERROR, CRITICAL.

You can do a "dry run" of the app by specifying `--dry-run`. This will determine
all the digital content you have purchased, but instead of downloading each file
it will print one line of information to show what file *would* have been downloaded
You can do a "dry run" of the app by specifying `--dry-run`. This will determine
all the digital content you have purchased, but instead of downloading each file
it will print one line of information to show what file *would* have been downloaded
if the `--dry-run` flag wasn't on. Use this if you want to test out the app without
taking the time to download anything.

Expand Down
8 changes: 2 additions & 6 deletions drpg/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,15 @@ def _parse_message(resp):
if resp.is_success:
expected_keys = {"progress", "file_tasks_id", "download_url"}
if isinstance(message, dict) and expected_keys.issubset(message.keys()):
logger.debug(
"Got download url for %s - %s: %s", product_id, item_id, message
)
logger.debug("Got download url for %s - %s: %s", product_id, item_id, message)
else:
logger.debug(
"Got unexpected message when getting download url for %s - %s: %s",
product_id,
item_id,
message,
)
raise self.FileTaskException(
self.FileTaskException.UNEXPECTED_RESPONSE
)
raise self.FileTaskException(self.FileTaskException.UNEXPECTED_RESPONSE)
else:
logger.debug(
"Could not get download link for %s - %s: %s",
Expand Down
4 changes: 1 addition & 3 deletions drpg/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ def _process_item(self, product: Product, item: DownloadItem) -> None:
)
return

file_response = httpx.get(
url_data["download_url"], timeout=30.0, follow_redirects=True
)
file_response = httpx.get(url_data["download_url"], timeout=30.0, follow_redirects=True)

path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(file_response.content)
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ Changelog = "https://github.com/glujan/drpg/blob/master/CHANGELOG.md"
GitHub = "https://github.com/glujan/drpg"
Homepage = "https://github.com/glujan/drpg"

[tool.black]
line-length = 100

[tool.coverage.run]
branch = true
include = ["drpg/*"]
Expand Down
4 changes: 1 addition & 3 deletions tests/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ def complete(cls, file_task_id, checksums_count=1):

@classmethod
def preparing(cls, file_task_id):
instance = cls(
file_task_id, "https://example.com/file.pdf", "Preparing download...", []
)
instance = cls(file_task_id, "https://example.com/file.pdf", "Preparing download...", [])
return dataclasses.asdict(instance)


Expand Down
12 changes: 3 additions & 9 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,12 @@ def test_unsuccesful_response(self, respx_mock):

with self.assertRaises(self.client.FileTaskException) as cm:
self.client.file_task("product_id", "item_id")
self.assertTupleEqual(
cm.exception.args, (self.client.FileTaskException.REQUEST_FAILED,)
)
self.assertTupleEqual(cm.exception.args, (self.client.FileTaskException.REQUEST_FAILED,))

@respx.mock(base_url=api_url)
def test_unexpected_response_with_string_message(self, respx_mock):
dummy_unexpected_response = {"message": "Invalid product id"}
respx_mock.post(self.file_tasks_url).respond(
201, json=dummy_unexpected_response
)
respx_mock.post(self.file_tasks_url).respond(201, json=dummy_unexpected_response)

with self.assertRaises(self.client.FileTaskException) as cm:
self.client.file_task("product_id", "item_id")
Expand All @@ -124,9 +120,7 @@ def test_unexpected_response_with_string_message(self, respx_mock):
@respx.mock(base_url=api_url)
def test_unexpected_response_with_json_message(self, respx_mock):
dummy_unexpected_response = {"message": {"reason": "Invalid product id"}}
respx_mock.post(self.file_tasks_url).respond(
201, json=dummy_unexpected_response
)
respx_mock.post(self.file_tasks_url).respond(201, json=dummy_unexpected_response)

with self.assertRaises(self.client.FileTaskException) as cm:
self.client.file_task("product_id", "item_id")
Expand Down
15 changes: 4 additions & 11 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,10 @@ def test_md5_check(self, _):

def dummy_item(self, date):
file_md5 = md5(self.file_content).hexdigest()
return dataclasses.asdict(
FileResponse("file.pdf", date.isoformat(), [Checksum(file_md5)])
)
return dataclasses.asdict(FileResponse("file.pdf", date.isoformat(), [Checksum(file_md5)]))

def dummy_product(self, *files):
return dataclasses.asdict(
ProductResponse("Test rule book", "Test Publishing", files=files)
)
return dataclasses.asdict(ProductResponse("Test rule book", "Test Publishing", files=files))


class DrpgSyncFilePathTest(TestCase):
Expand Down Expand Up @@ -164,9 +160,7 @@ def setUp(self):
@mock.patch("drpg.api.DrpgApi.file_task", return_value=file_task)
@respx.mock(base_url=DrpgApi.API_URL)
def test_writes_to_file(self, _, m_file_path, respx_mock):
respx_mock.get(self.file_task["download_url"]).respond(
200, content=self.content
)
respx_mock.get(self.file_task["download_url"]).respond(200, content=self.content)

path = m_file_path.return_value
type(path).parent = mock.PropertyMock(return_value=PathMock())
Expand Down Expand Up @@ -217,8 +211,7 @@ def test_processes_each_item(self, process_item_mock, customer_products_mock, *_
files_count = 5
products_count = 3
customer_products_mock.return_value = [
self.dummy_product(f"Rule Book {i}", files_count)
for i in range(products_count)
self.dummy_product(f"Rule Book {i}", files_count) for i in range(products_count)
]
self.sync.sync()
self.assertEqual(process_item_mock.call_count, files_count * products_count)
Expand Down

0 comments on commit a283341

Please sign in to comment.