Skip to content

Commit 075a504

Browse files
committed
flake8 + mypy + unittest
1 parent 5b05932 commit 075a504

File tree

10 files changed

+104
-0
lines changed

10 files changed

+104
-0
lines changed

python_ci/Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM python:3.8-slim
2+
3+
COPY requirements.txt .
4+
COPY requirements-dev.txt .
5+
6+
RUN pip install --no-cache-dir --upgrade pip setuptools wheel \
7+
&& pip install --no-cache-dir -r requirements.txt \
8+
&& pip install --no-cache-dir -r requirements-dev.txt

python_ci/Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
image = python-ci
2+
tag = 0.1.0
3+
curdir := `pwd`
4+
5+
.PHONY: dbuild
6+
dbuild:
7+
docker build -t $(image):$(tag) .
8+
9+
.PHONY: drun
10+
drun:
11+
docker run --rm -it -v $(curdir):/opt -w /opt $(image):$(tag) bash
12+
13+
.PHONY: drmi
14+
drmi:
15+
docker rmi -f $(image):$(tag)
16+
17+
.PHONY: flake8
18+
flake8:
19+
flake8 --config=config/tox.ini src
20+
21+
.PHONY: mypy
22+
mypy:
23+
mypy --config=config/mypy.ini src
24+
25+
.PHONY: test
26+
test:
27+
python3 -m unittest discover --verbose src

python_ci/config/mypy.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[mypy]
2+
python_version = 3.8
3+
disallow_untyped_defs = True
4+
ignore_missing_imports = True
5+
warn_redundant_casts = True
6+
no_implicit_optional = True
7+
8+
[mypy-src.tests.*]
9+
disallow_untyped_defs = False

python_ci/config/tox.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[flake8]
2+
max-line-length = 79
3+
per-file-ignores =
4+
src/tests/*:E501

python_ci/requirements-dev.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
flake8==3.8.3
2+
mypy==0.782

python_ci/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests==2.24.0

python_ci/src/__init__.py

Whitespace-only changes.

python_ci/src/sample.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import requests
2+
3+
4+
class Sample:
5+
def fetch_json(self, url: str) -> dict:
6+
response = requests.get(url)
7+
return response.json()

python_ci/src/tests/__init__.py

Whitespace-only changes.

python_ci/src/tests/test_sample.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from unittest import TestCase, main
2+
from unittest.mock import MagicMock, call, patch
3+
4+
from src.sample import Sample
5+
6+
7+
class TestSample(TestCase):
8+
def mocked_requests_get(*args, **kwargs):
9+
class MockResponse:
10+
def __init__(self, json_data, status_code):
11+
self.json_data = json_data
12+
self.status_code = status_code
13+
14+
def json(self):
15+
return self.json_data
16+
17+
if args[0] == 'http://example.com/test.json':
18+
return MockResponse({'key1': 'value1'}, 200)
19+
elif args[0] == 'http://example.com/another_test.json':
20+
return MockResponse({'key2': 'value2'}, 200)
21+
22+
return MockResponse(None, 404)
23+
24+
@patch('requests.get', side_effect=mocked_requests_get)
25+
def test_fetch_json(self, mock_get: MagicMock):
26+
sample = Sample()
27+
28+
json_data = sample.fetch_json('http://example.com/test.json')
29+
self.assertEqual(json_data, {'key1': 'value1'})
30+
json_data = sample.fetch_json('http://example.com/another_test.json')
31+
self.assertEqual(json_data, {'key2': 'value2'})
32+
json_data = sample.fetch_json('http://no_example.com/test.json')
33+
self.assertIsNone(json_data)
34+
35+
self.assertIn(
36+
call('http://example.com/test.json'), mock_get.call_args_list
37+
)
38+
self.assertIn(
39+
call('http://example.com/another_test.json'), mock_get.call_args_list
40+
)
41+
42+
self.assertEqual(len(mock_get.call_args_list), 3)
43+
44+
45+
if __name__ == '__main__':
46+
main()

0 commit comments

Comments
 (0)