Skip to content
This repository was archived by the owner on Nov 6, 2022. It is now read-only.

Commit dbdd0a4

Browse files
author
Hugo Osvaldo Barrera
authored
Merge pull request #19 from WhyNotHugo/preference-creation-tests
Add some tests for preference creation And switch to using pytest and github actions.
2 parents 536dabd + 2157468 commit dbdd0a4

File tree

12 files changed

+298
-69
lines changed

12 files changed

+298
-69
lines changed

.github/workflows/checks.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Checks
2+
3+
on: [push]
4+
5+
jobs:
6+
styles:
7+
runs-on: ubuntu-18.04
8+
name: Linting
9+
steps:
10+
- uses: actions/checkout@master
11+
- uses: actions/setup-python@v1
12+
with:
13+
python-version: 3.7
14+
architecture: x64
15+
- uses: WhyNotHugo/python-linting@master

.github/workflows/publish.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Publish
2+
3+
on:
4+
push:
5+
tags:
6+
- v*
7+
8+
jobs:
9+
github-release:
10+
runs-on: ubuntu-18.04
11+
name: Publish GitHub Release
12+
steps:
13+
- uses: actions/checkout@master
14+
- uses: actions/setup-python@v1
15+
with:
16+
python-version: 3.7
17+
architecture: x64
18+
- name: Install build dependencies
19+
run: pip install wheel
20+
- name: Build packages
21+
run: python setup.py sdist bdist_wheel
22+
- name: Release
23+
uses: softprops/action-gh-release@v1
24+
with:
25+
files: dist/*
26+
env:
27+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28+
pypi:
29+
runs-on: ubuntu-18.04
30+
name: Publish package on PyPI
31+
steps:
32+
- uses: actions/checkout@master
33+
- uses: actions/setup-python@v1
34+
with:
35+
python-version: 3.7
36+
architecture: x64
37+
- name: Install build dependencies
38+
run: pip install wheel
39+
- name: Build packages
40+
run: python setup.py sdist bdist_wheel
41+
- name: Publish a Python distribution to PyPI
42+
uses: pypa/gh-action-pypi-publish@master
43+
with:
44+
password: ${{ secrets.PYPI_TOKEN }}

.github/workflows/tests.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Tests
2+
3+
on: [push]
4+
5+
jobs:
6+
tests:
7+
runs-on: ubuntu-18.04
8+
strategy:
9+
matrix:
10+
python: [ '3.5', '3.6', '3.7' ]
11+
name: python${{ matrix.python }}
12+
steps:
13+
- uses: actions/checkout@master
14+
- uses: actions/setup-python@v1
15+
with:
16+
python-version: ${{ matrix.python }}
17+
architecture: x64
18+
- name: Install test dependencies
19+
run: pip install tox codecov
20+
- name: Run tests
21+
run: tox
22+
env:
23+
TOXENV: py
24+
- name: Report coverage
25+
run: codecov
26+
env:
27+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.travis.yml

Lines changed: 0 additions & 58 deletions
This file was deleted.

requirements-dev.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
coverage
22
factory-boy
3+
pytest
4+
pytest-cov
5+
pytest-django
6+
responses

requirements.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

setup.cfg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@ import-order-style=smarkets
66
[coverage:run]
77
branch=true
88
source=django_mercadopago
9+
10+
[tool:pytest]
11+
DJANGO_SETTINGS_MODULE = tests.settings
12+
addopts =
13+
--reuse-db
14+
--cov=django_mercadopago
15+
--cov-report=term-missing:skip-covered
16+
--no-cov-on-fail

setup.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
license='ISC',
1212
packages=find_packages(),
1313
long_description=open('README.rst').read(),
14-
install_requires=open('requirements.txt').readlines(),
14+
install_requires=[
15+
'wheel>=0.26.0',
16+
'Django>=1.8.5',
17+
'mercadopago>=0.3.4',
18+
'setuptools-git>=1.1',
19+
'setuptools-scm>=1.8.0',
20+
],
1521
use_scm_version={'version_scheme': 'post-release'},
1622
setup_requires=['setuptools_scm'],
1723
extras_require={

tests/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
'NAME': ':memory:',
1818
}
1919
}
20-
ROOT_URLCONF = 'django_mercadopago.urls'
20+
ROOT_URLCONF = 'tests.urls'
2121

2222
MERCADOPAGO = {
2323
'autoprocess': False,

tests/test_models.py

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
import json
2+
3+
import pytest
4+
import responses
5+
6+
from django_mercadopago import fixtures, models
7+
8+
9+
preference_response = {
10+
'processing_modes': [],
11+
'metadata': {},
12+
'binary_mode': False,
13+
'payment_methods': {
14+
'excluded_payment_methods': [{'id': ''}],
15+
'excluded_payment_types': [{'id': ''}],
16+
'installments': None,
17+
'default_payment_method_id': None,
18+
'default_installments': None
19+
},
20+
'collector_id': 152658942,
21+
'operation_type': 'regular_payment',
22+
'items': [{
23+
'id': '',
24+
'picture_url': '',
25+
'title': 'Test',
26+
'description': 'Something that a client bought.',
27+
'category_id': 'services',
28+
'currency_id': 'ARS',
29+
'quantity': 1,
30+
'unit_price': 100
31+
}],
32+
'payer': {
33+
'name': '',
34+
'surname': '',
35+
'email': '',
36+
'date_created': '',
37+
'phone': {'area_code': '', 'number': ''},
38+
'identification': {'type': '', 'number': ''},
39+
'address': {
40+
'street_name': '',
41+
'street_number': None,
42+
'zip_code': ''
43+
}
44+
},
45+
'back_urls': {
46+
'success': 'http://localhost:8001/post_payment/ref-123',
47+
'pending': 'http://localhost:8001/payment_pending/ref-123',
48+
'failure': 'http://localhost:8001/payment_failed/ref-123'
49+
},
50+
'auto_return': 'all',
51+
'client_id': '3641931198760523',
52+
'marketplace': 'NONE',
53+
'marketplace_fee': 0,
54+
'shipments': {
55+
'receiver_address': {
56+
'zip_code': '',
57+
'street_number': None,
58+
'street_name': '',
59+
'floor': '',
60+
'apartment': ''
61+
}
62+
},
63+
'notification_url': 'http://localhost:8001/notifications/ref-123',
64+
'external_reference': 'ref-123',
65+
'additional_info': '',
66+
'expires': False,
67+
'expiration_date_from': None,
68+
'expiration_date_to': None,
69+
'date_created': '2019-10-13T15:39:08.981-04:00',
70+
'id': '152658942-f090626e-6d4d-4877-a3d5-292e8877e4cb',
71+
'init_point': 'https://www.mercadopago.com/init_point',
72+
'sandbox_init_point': 'https://sbox.mercadopago.com/init_point'
73+
}
74+
75+
76+
@pytest.mark.django_db
77+
@responses.activate
78+
def test_preference_request():
79+
"""Preference creation runs the expected query."""
80+
responses.add(
81+
responses.POST,
82+
'https://api.mercadopago.com/oauth/token',
83+
json={
84+
"access_token": "APP_USR-3641931198760523",
85+
"refresh_token": "TG-5da379a4705f640006b69d70-152658942",
86+
"live_mode": True,
87+
"user_id": 152658949,
88+
"token_type": "bearer",
89+
"expires_in": 21600,
90+
"scope": "offline_access read write"
91+
},
92+
status=200,
93+
)
94+
responses.add(
95+
responses.POST,
96+
'https://api.mercadopago.com/checkout/preferences',
97+
json=preference_response,
98+
status=200,
99+
)
100+
101+
models.Preference.objects.create(
102+
title="Test",
103+
description="Something that a client bought.",
104+
price=100,
105+
reference="ref-123",
106+
account=fixtures.AccountFactory(),
107+
)
108+
109+
expected_url = (
110+
'https://api.mercadopago.com/checkout/preferences?access_token='
111+
'APP_USR-3641931198760523'
112+
)
113+
expected_request = {
114+
"auto_return":
115+
"all",
116+
"items": [{
117+
"title": "Test",
118+
"currency_id": "ARS",
119+
"description": "Something that a client bought.",
120+
"category_id": "services",
121+
"quantity": 1,
122+
"unit_price": 100.0
123+
}],
124+
"external_reference":
125+
"ref-123",
126+
"back_urls": {
127+
"success": "http://localhost:8001/post_payment/ref-123",
128+
"pending": "http://localhost:8001/payment_pending/ref-123",
129+
"failure": "http://localhost:8001/payment_failed/ref-123"
130+
},
131+
"notification_url":
132+
"http://localhost:8001/notifications/ref-123"
133+
}
134+
135+
assert len(responses.calls) == 2
136+
assert responses.calls[1].request.url == expected_url
137+
assert responses.calls[1].request.body == json.dumps(expected_request)
138+
139+
140+
@pytest.mark.django_db
141+
@responses.activate
142+
def test_preference_creation():
143+
"""Preference creation assigned attributes properly."""
144+
responses.add(
145+
responses.POST,
146+
'https://api.mercadopago.com/oauth/token',
147+
json={
148+
"access_token": "APP_USR-3641931198760523",
149+
"refresh_token": "TG-5da379a4705f640006b69d70-152658942",
150+
"live_mode": True,
151+
"user_id": 152658949,
152+
"token_type": "bearer",
153+
"expires_in": 21600,
154+
"scope": "offline_access read write"
155+
},
156+
status=200,
157+
)
158+
responses.add(
159+
responses.POST,
160+
'https://api.mercadopago.com/checkout/preferences',
161+
json=preference_response,
162+
status=200,
163+
)
164+
165+
account = fixtures.AccountFactory()
166+
preference = models.Preference.objects.create(
167+
title="Test",
168+
description="Something that a client bought.",
169+
price=100,
170+
reference="ref-123",
171+
account=account,
172+
)
173+
174+
assert preference.title == 'Test'
175+
assert preference.price == 100
176+
assert preference.quantity == 1
177+
assert preference.mp_id == '152658942-f090626e-6d4d-4877-a3d5-292e8877e4cb'
178+
assert preference.payment_url == 'https://www.mercadopago.com/init_point'
179+
assert preference.sandbox_url == 'https://sbox.mercadopago.com/init_point'
180+
assert preference.reference == 'ref-123'
181+
assert preference.owner == account

0 commit comments

Comments
 (0)