Skip to content

Commit

Permalink
+ coverage
Browse files Browse the repository at this point in the history
+ pytest discover speed fixes
+ docker build, pip install thenewboston.tar.gz if available
+ flake8 rules to discuss
+ requests mocker
+ test with request mock
+ some fixes
  • Loading branch information
vosi committed Sep 25, 2020
1 parent f305b69 commit ececd8c
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 42 deletions.
23 changes: 23 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[run]
omit = */tests/*
*/factories/*
*/migrations/*
*/urls.py
branch = True

include =
v1/*
; parallel = true
; concurrency = multiprocessing

[report]
precision = 2
ignore_errors = True
exclude_lines =
pragma: no cover
raise NotImplementedError
except ImportError
def __repr__
def __str__
if self\.logger\.debug
if __name__ == .__main__.:
16 changes: 16 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[flake8]
ignore = D203,D101,D400,D401,D106,W503,F403,F405,E501,D403,D104
exclude =
.git,
__pycache__,
migrations
filename =
*.py
max-complexity = 12
import-order-style = google
; application-import-names =
max-line-length = 120
max-linenumber = 500
accept-encodings = utf-8,utf-16
inline-quotes = single
multiline-quotes = double
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ sdist/
share/python-wheels/
var/
wheels/
thenewboston.tar.gz

# Unit test / coverage reports
*.cover
Expand Down Expand Up @@ -78,4 +79,4 @@ env.bak/
env/
venv
venv.bak/
venv/
venv/
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ FROM python:3.8

WORKDIR /opt/project

COPY requirements/local.txt .
COPY . .

RUN set -x; \
python3 -m pip install pip-tools; \
python3 -m pip install --no-cache-dir -r local.txt; \
python3 -m pip install --no-cache-dir -r requirements/local.txt; \
test -e thenewboston.tar.gz && python3 -m pip install thenewboston.tar.gz; \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /root/.cache
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ docker-compose run app pytest
docker-compose exec app pytest # if docker-compose run is running
```

To run tests with coverage report:
```
docker-compose run app pytest --cov=v1
# or
docker-compose exec app pytest --cov=v1 # if docker-compose run is running
```

To monitor Celery tasks:
```
docker-compose exec celery celery flower -A config.settings --address=127.0.0.1 --port=5555
Expand Down
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[pytest]
DJANGO_SETTINGS_MODULE = config.settings.local
python_files = tests/*.py
norecursedirs = .* .git *.egg build dist tmp* node_modules
5 changes: 3 additions & 2 deletions requirements/base.in
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
celery==4.4.2
django-debug-toolbar==2.2
django-debug-toolbar==3.1.1
django-filter==2.3.0
django-redis==4.11.0
flower==0.9.4
redis==3.5.2
tblib==1.6.0
thenewboston==0.0.19
wheel==0.34.2
wheel==0.35.1
requests-mock==1.8.0
11 changes: 5 additions & 6 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ constantly==15.1.0 # via twisted
cryptography==3.1.1 # via autobahn, pyopenssl, service-identity
daphne==2.5.0 # via channels
django-cors-headers==3.4.0 # via thenewboston
django-debug-toolbar==2.2 # via -r requirements/base.in
django-debug-toolbar==3.1.1 # via -r requirements/base.in
django-filter==2.3.0 # via -r requirements/base.in
django-redis==4.11.0 # via -r requirements/base.in
django==3.1 # via channels, django-cors-headers, django-debug-toolbar, django-filter, django-redis, djangorestframework, thenewboston
Expand All @@ -34,7 +34,6 @@ hiredis==1.1.0 # via aioredis
humanize==0.5.1 # via flower
hyperlink==20.0.1 # via twisted
idna==2.10 # via hyperlink, requests, twisted
importlib-metadata==2.0.0 # via kombu, pluggy, pytest
incremental==17.5.0 # via twisted
iniconfig==1.0.1 # via pytest
kombu==4.6.11 # via celery
Expand All @@ -58,9 +57,10 @@ pytest==6.0.1 # via pytest-asyncio, pytest-django, thenewboston
python-dateutil==2.8.1 # via faker
pytz==2020.1 # via celery, django, flower
redis==3.5.2 # via -r requirements/base.in, django-redis
requests==2.23.0 # via thenewboston
requests-mock==1.8.0 # via -r requirements/base.in
requests==2.23.0 # via requests-mock, thenewboston
service-identity==18.1.0 # via twisted
six==1.15.0 # via automat, cryptography, packaging, pynacl, pyopenssl, python-dateutil
six==1.15.0 # via automat, cryptography, packaging, pynacl, pyopenssl, python-dateutil, requests-mock
sqlparse==0.3.1 # via django, django-debug-toolbar
tblib==1.6.0 # via -r requirements/base.in
text-unidecode==1.3 # via faker
Expand All @@ -71,8 +71,7 @@ twisted[tls]==20.3.0 # via daphne
txaio==20.4.1 # via autobahn
urllib3==1.25.10 # via requests
vine==1.3.0 # via amqp, celery
wheel==0.34.2 # via -r requirements/base.in
zipp==3.2.0 # via importlib-metadata
wheel==0.35.1 # via -r requirements/base.in
zope.interface==5.1.0 # via twisted

# The following packages are considered to be unsafe in a requirements file:
Expand Down
11 changes: 5 additions & 6 deletions requirements/development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ constantly==15.1.0 # via twisted
cryptography==3.1.1 # via autobahn, pyopenssl, service-identity
daphne==2.5.0 # via channels
django-cors-headers==3.4.0 # via thenewboston
django-debug-toolbar==2.2 # via -r requirements/base.in
django-debug-toolbar==3.1.1 # via -r requirements/base.in
django-filter==2.3.0 # via -r requirements/base.in
django-redis==4.11.0 # via -r requirements/base.in
django==3.1 # via channels, django-cors-headers, django-debug-toolbar, django-filter, django-redis, djangorestframework, thenewboston
Expand All @@ -34,7 +34,6 @@ hiredis==1.1.0 # via aioredis
humanize==0.5.1 # via flower
hyperlink==20.0.1 # via twisted
idna==2.10 # via hyperlink, requests, twisted
importlib-metadata==2.0.0 # via kombu, pluggy, pytest
incremental==17.5.0 # via twisted
iniconfig==1.0.1 # via pytest
kombu==4.6.11 # via celery
Expand All @@ -58,9 +57,10 @@ pytest==6.0.1 # via pytest-asyncio, pytest-django, thenewboston
python-dateutil==2.8.1 # via faker
pytz==2020.1 # via celery, django, flower
redis==3.5.2 # via -r requirements/base.in, django-redis
requests==2.23.0 # via thenewboston
requests-mock==1.8.0 # via -r requirements/base.in
requests==2.23.0 # via requests-mock, thenewboston
service-identity==18.1.0 # via twisted
six==1.15.0 # via automat, cryptography, packaging, pynacl, pyopenssl, python-dateutil
six==1.15.0 # via automat, cryptography, packaging, pynacl, pyopenssl, python-dateutil, requests-mock
sqlparse==0.3.1 # via django, django-debug-toolbar
tblib==1.6.0 # via -r requirements/base.in
text-unidecode==1.3 # via faker
Expand All @@ -71,8 +71,7 @@ twisted[tls]==20.3.0 # via daphne
txaio==20.4.1 # via autobahn
urllib3==1.25.10 # via requests
vine==1.3.0 # via amqp, celery
wheel==0.34.2 # via -r requirements/base.in
zipp==3.2.0 # via importlib-metadata
wheel==0.35.1 # via -r requirements/base.in
zope.interface==5.1.0 # via twisted

# The following packages are considered to be unsafe in a requirements file:
Expand Down
11 changes: 5 additions & 6 deletions requirements/local.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ constantly==15.1.0 # via twisted
cryptography==3.1.1 # via autobahn, pyopenssl, service-identity
daphne==2.5.0 # via channels
django-cors-headers==3.4.0 # via thenewboston
django-debug-toolbar==2.2 # via -r requirements/base.in
django-debug-toolbar==3.1.1 # via -r requirements/base.in
django-filter==2.3.0 # via -r requirements/base.in
django-redis==4.11.0 # via -r requirements/base.in
django==3.1 # via channels, django-cors-headers, django-debug-toolbar, django-filter, django-redis, djangorestframework, thenewboston
Expand All @@ -34,7 +34,6 @@ hiredis==1.1.0 # via aioredis
humanize==0.5.1 # via flower
hyperlink==20.0.1 # via twisted
idna==2.10 # via hyperlink, requests, twisted
importlib-metadata==2.0.0 # via kombu, pluggy, pytest
incremental==17.5.0 # via twisted
iniconfig==1.0.1 # via pytest
iptools==0.7.0 # via -r requirements/local.in
Expand All @@ -59,9 +58,10 @@ pytest==6.0.1 # via pytest-asyncio, pytest-django, thenewboston
python-dateutil==2.8.1 # via faker
pytz==2020.1 # via celery, django, flower
redis==3.5.2 # via -r requirements/base.in, django-redis
requests==2.23.0 # via thenewboston
requests-mock==1.8.0 # via -r requirements/base.in
requests==2.23.0 # via requests-mock, thenewboston
service-identity==18.1.0 # via twisted
six==1.15.0 # via automat, cryptography, packaging, pynacl, pyopenssl, python-dateutil
six==1.15.0 # via automat, cryptography, packaging, pynacl, pyopenssl, python-dateutil, requests-mock
sqlparse==0.3.1 # via django, django-debug-toolbar
tblib==1.6.0 # via -r requirements/base.in
text-unidecode==1.3 # via faker
Expand All @@ -72,8 +72,7 @@ twisted[tls]==20.3.0 # via daphne
txaio==20.4.1 # via autobahn
urllib3==1.25.10 # via requests
vine==1.3.0 # via amqp, celery
wheel==0.34.2 # via -r requirements/base.in
zipp==3.2.0 # via importlib-metadata
wheel==0.35.1 # via -r requirements/base.in
zope.interface==5.1.0 # via twisted

# The following packages are considered to be unsafe in a requirements file:
Expand Down
11 changes: 5 additions & 6 deletions requirements/production.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ constantly==15.1.0 # via twisted
cryptography==3.1.1 # via autobahn, pyopenssl, service-identity
daphne==2.5.0 # via channels
django-cors-headers==3.4.0 # via thenewboston
django-debug-toolbar==2.2 # via -r requirements/base.in
django-debug-toolbar==3.1.1 # via -r requirements/base.in
django-filter==2.3.0 # via -r requirements/base.in
django-redis==4.11.0 # via -r requirements/base.in
django==3.1 # via channels, django-cors-headers, django-debug-toolbar, django-filter, django-redis, djangorestframework, thenewboston
Expand All @@ -34,7 +34,6 @@ hiredis==1.1.0 # via aioredis
humanize==0.5.1 # via flower
hyperlink==20.0.1 # via twisted
idna==2.10 # via hyperlink, requests, twisted
importlib-metadata==2.0.0 # via kombu, pluggy, pytest
incremental==17.5.0 # via twisted
iniconfig==1.0.1 # via pytest
kombu==4.6.11 # via celery
Expand All @@ -58,9 +57,10 @@ pytest==6.0.1 # via pytest-asyncio, pytest-django, thenewboston
python-dateutil==2.8.1 # via faker
pytz==2020.1 # via celery, django, flower
redis==3.5.2 # via -r requirements/base.in, django-redis
requests==2.23.0 # via thenewboston
requests-mock==1.8.0 # via -r requirements/base.in
requests==2.23.0 # via requests-mock, thenewboston
service-identity==18.1.0 # via twisted
six==1.15.0 # via automat, cryptography, packaging, pynacl, pyopenssl, python-dateutil
six==1.15.0 # via automat, cryptography, packaging, pynacl, pyopenssl, python-dateutil, requests-mock
sqlparse==0.3.1 # via django, django-debug-toolbar
tblib==1.6.0 # via -r requirements/base.in
text-unidecode==1.3 # via faker
Expand All @@ -71,8 +71,7 @@ twisted[tls]==20.3.0 # via daphne
txaio==20.4.1 # via autobahn
urllib3==1.25.10 # via requests
vine==1.3.0 # via amqp, celery
wheel==0.34.2 # via -r requirements/base.in
zipp==3.2.0 # via importlib-metadata
wheel==0.35.1 # via -r requirements/base.in
zope.interface==5.1.0 # via twisted

# The following packages are considered to be unsafe in a requirements file:
Expand Down
11 changes: 5 additions & 6 deletions requirements/staging.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ constantly==15.1.0 # via twisted
cryptography==3.1.1 # via autobahn, pyopenssl, service-identity
daphne==2.5.0 # via channels
django-cors-headers==3.4.0 # via thenewboston
django-debug-toolbar==2.2 # via -r requirements/base.in
django-debug-toolbar==3.1.1 # via -r requirements/base.in
django-filter==2.3.0 # via -r requirements/base.in
django-redis==4.11.0 # via -r requirements/base.in
django==3.1 # via channels, django-cors-headers, django-debug-toolbar, django-filter, django-redis, djangorestframework, thenewboston
Expand All @@ -34,7 +34,6 @@ hiredis==1.1.0 # via aioredis
humanize==0.5.1 # via flower
hyperlink==20.0.1 # via twisted
idna==2.10 # via hyperlink, requests, twisted
importlib-metadata==2.0.0 # via kombu, pluggy, pytest
incremental==17.5.0 # via twisted
iniconfig==1.0.1 # via pytest
kombu==4.6.11 # via celery
Expand All @@ -58,9 +57,10 @@ pytest==6.0.1 # via pytest-asyncio, pytest-django, thenewboston
python-dateutil==2.8.1 # via faker
pytz==2020.1 # via celery, django, flower
redis==3.5.2 # via -r requirements/base.in, django-redis
requests==2.23.0 # via thenewboston
requests-mock==1.8.0 # via -r requirements/base.in
requests==2.23.0 # via requests-mock, thenewboston
service-identity==18.1.0 # via twisted
six==1.15.0 # via automat, cryptography, packaging, pynacl, pyopenssl, python-dateutil
six==1.15.0 # via automat, cryptography, packaging, pynacl, pyopenssl, python-dateutil, requests-mock
sqlparse==0.3.1 # via django, django-debug-toolbar
tblib==1.6.0 # via -r requirements/base.in
text-unidecode==1.3 # via faker
Expand All @@ -71,8 +71,7 @@ twisted[tls]==20.3.0 # via daphne
txaio==20.4.1 # via autobahn
urllib3==1.25.10 # via requests
vine==1.3.0 # via amqp, celery
wheel==0.34.2 # via -r requirements/base.in
zipp==3.2.0 # via importlib-metadata
wheel==0.35.1 # via -r requirements/base.in
zope.interface==5.1.0 # via twisted

# The following packages are considered to be unsafe in a requirements file:
Expand Down
Empty file modified scripts/compile_requirements.sh
100644 → 100755
Empty file.
21 changes: 17 additions & 4 deletions v1/banks/tests/bank.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework import status
from rest_framework.reverse import reverse
from rest_framework.status import HTTP_200_OK
from thenewboston.third_party.pytest.asserts import assert_objects_vs_dicts
from thenewboston.utils.signed_requests import generate_signed_request

Expand All @@ -11,12 +11,25 @@ def test_banks_list(client, banks, django_assert_max_num_queries):
response = client.get_json(
reverse('bank-list'),
{'limit': 0},
expected=HTTP_200_OK,
expected=status.HTTP_200_OK,
)
assert_objects_vs_dicts(banks, response, key='node_identifier')
assert response


def test_banks_post(client, bank_fake_data, self_configuration):
response = client.post_json(
reverse('bank-list'),
generate_signed_request(
data=bank_fake_data,
nid_signing_key=get_signing_key(),
),
expected=status.HTTP_201_CREATED
)
bank_fake_data['trust'] = f'{bank_fake_data["trust"]:.2f}'
assert response == bank_fake_data


def test_banks_patch(client, bank, bank_fake_data, self_configuration):

response = client.patch_json(
Expand All @@ -28,6 +41,6 @@ def test_banks_patch(client, bank, bank_fake_data, self_configuration):
data=bank_fake_data,
nid_signing_key=get_signing_key(),
),
expected=HTTP_200_OK,
expected=status.HTTP_200_OK,
)
assert float(response['trust']) == bank_fake_data['trust']
assert response['trust'] == f'{bank_fake_data["trust"]:.2f}'
6 changes: 5 additions & 1 deletion v1/banks/views/bank.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from rest_framework import status
from rest_framework.mixins import CreateModelMixin, ListModelMixin, RetrieveModelMixin, UpdateModelMixin
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet
Expand All @@ -24,6 +25,8 @@ class BankViewSet(
parameters:
- name: trust
type: number
create:
description: Create bank
"""

lookup_field = 'node_identifier'
Expand All @@ -43,7 +46,8 @@ def create(self, request, *args, **kwargs):
bank = serializer.save()

return Response(
self.get_serializer(bank).data
self.get_serializer(bank).data,
status=status.HTTP_201_CREATED
)

@is_self_signed_message
Expand Down
2 changes: 1 addition & 1 deletion v1/connection_requests/serializers/connection_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get_node_config(data):
raise serializers.ValidationError('Invalid node_type')
except Exception as e:
logger.exception(e)
raise serializers.ValidationError(e)
raise e

if config_serializer.is_valid():
return config_data
Expand Down
Loading

0 comments on commit ececd8c

Please sign in to comment.