Skip to content

Commit 773d822

Browse files
author
Julio Sarango
committed
Se agrega pytest para las pruebas unitarias
1 parent aa14560 commit 773d822

9 files changed

Lines changed: 253 additions & 3 deletions

.github/workflows/python-app.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ jobs:
3030
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
3131
- name: Test with unittest
3232
run: |
33-
python -m unittest discover tests
33+
pytest tests

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
requests==2.32.3
22
Faker==33.0.0
3-
coverage==7.6.5
3+
coverage==7.6.5
4+
pytest==8.3.3
5+
pytest-mock==3.14.0

src/bank_account.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ def deposit(self, amount):
3636
>>> account.deposit(50)
3737
150
3838
"""
39+
40+
if amount < 0:
41+
raise ValueError("Can't deposit negative amounts")
42+
3943
if amount > 0:
4044
self.balance += amount
4145
self._log_transaction(f"Deposited {amount}- New balance: {self.balance}")

tests/test_bank_account.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def test_deposit(self):
3030
self.assertEqual(new_balance, 1500)
3131

3232
def test_deposit_with_negative_amount_not_increase(self):
33-
self.assertEqual(self.account.deposit(-50), 1000)
33+
with self.assertRaises(ValueError):
34+
self.assertEqual(self.account.deposit(-50))
3435

3536
def test_withdraw(self):
3637
new_balance = self.account.withdraw(200)

tests/test_pytest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pytest
2+
from src.bank_account import BankAccount
3+
4+
5+
@pytest.mark.parametrize("ammount, expected", [(100, 1100), (3000, 4000), (4500, 5500)])
6+
def test_deposit_multiple_ammounts(ammount, expected):
7+
account = BankAccount(balance=1000, log_file="transaccions.txt")
8+
new_balance = account.deposit(ammount)
9+
assert new_balance == expected

tests/test_pytest_api_client.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import pytest
2+
from unittest.mock import Mock
3+
import requests
4+
5+
from src.api_client import get_location
6+
7+
8+
@pytest.fixture
9+
def mock_data():
10+
yield {
11+
"countryName": "USA",
12+
"regionName": "Florida",
13+
"cityName": "MIAMI",
14+
"countryCode": "US",
15+
}
16+
17+
18+
def test_get_location_return_expected_data(mocker, mock_data):
19+
20+
mocker.patch("src.api_client.requests.get").return_value.json.return_value = (
21+
mock_data
22+
)
23+
24+
result = get_location("8.8.8.8")
25+
26+
assert result.get("country") == "USA"
27+
assert result.get("region") == "Florida"
28+
assert result.get("city") == "MIAMI"
29+
assert result.get("code") == "US"
30+
31+
32+
# create a test with side_effect
33+
def test_get_location_return_side_effect(mocker, mock_data):
34+
mocker.patch("src.api_client.requests.get").side_effect = [
35+
requests.exceptions.RequestException("Service Unavailable"),
36+
Mock(status_code=200, json=lambda: mock_data),
37+
]
38+
39+
with pytest.raises(requests.exceptions.RequestException):
40+
get_location("8.8.8.8")
41+
42+
result = get_location("8.8.8.8")
43+
assert result.get("country") == "USA"
44+
assert result.get("region") == "Florida"
45+
assert result.get("city") == "MIAMI"
46+
assert result.get("code") == "US"
47+
48+
49+
def test_get_location_return_side_effect_with_invalid_ip(mocker):
50+
mocker.patch("src.api_client.requests.get").side_effect = [
51+
ValueError("8.8.0 does not appear to be an IPv4 or IPv6 address"),
52+
Mock(status_code=200, json=lambda: mock_data),
53+
]
54+
55+
with pytest.raises(ValueError):
56+
get_location("8.8.0")

tests/test_pytest_bank_account.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pytest
2+
from src.bank_account import BankAccount
3+
from src.exceptions import WithdrawTimeRestrictionError, WithdrawWeekendError
4+
from unittest.mock import patch
5+
import os
6+
7+
8+
@pytest.fixture
9+
def account():
10+
yield BankAccount(
11+
balance=1000,
12+
log_file="transaction_log.txt",
13+
file_log_withdraw="withdraw_log.txt",
14+
)
15+
16+
17+
def test_deposit(account):
18+
new_balance = account.deposit(500)
19+
20+
assert new_balance == 1500
21+
22+
23+
def test_deposit_with_negative_amount_not_increase(account):
24+
with pytest.raises(ValueError):
25+
account.deposit(-50)
26+
27+
28+
def test_withdraw(account):
29+
new_balance = account.withdraw(200)
30+
assert new_balance == 800
31+
32+
33+
@patch("src.bank_account.datetime")
34+
def test_withdraw_with_negative_amount_not_decrease(mock_datetime, account):
35+
mock_datetime.now.return_value.hour = 10
36+
assert account.withdraw(-10) == 1000
37+
38+
39+
@patch("src.bank_account.datetime")
40+
def test_withdraw_in_available_hours(mock_datetime, account):
41+
mock_datetime.now.return_value.hour = 10
42+
assert account.withdraw(10) == 990
43+
44+
45+
@patch("src.bank_account.datetime")
46+
def test_withdraw_not_in_available_days(mock_datetime, account):
47+
mock_datetime.now.return_value.hour = 10
48+
mock_datetime.now.return_value.weekday.return_value = 6
49+
with pytest.raises(WithdrawWeekendError):
50+
account.withdraw(10)
51+
52+
53+
def teardown_module(module):
54+
if os.path.exists("transaction_log.txt"):
55+
os.remove("transaction_log.txt")
56+
57+
if os.path.exists("withdraw_log.txt"):
58+
os.remove("withdraw_log.txt")

tests/test_pytest_calculator.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pytest
2+
3+
from src.calculator import suma, resta, multiplicacion, division
4+
5+
6+
def test_suma():
7+
assert suma(4, 5) == 9
8+
assert suma(4, 15) == 19
9+
10+
11+
def test_resta():
12+
assert resta(10, 8) == 2
13+
assert resta(20, 8) == 12
14+
15+
16+
def test_multiplicacion():
17+
assert multiplicacion(2, 4) == 8
18+
assert multiplicacion(6, 4) == 24
19+
20+
21+
def test_division():
22+
assert division(40, 10) == 4
23+
with pytest.raises(ZeroDivisionError):
24+
division(10, 0)

transaccions.txt

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
Cuenta creada
2+
Deposited 100- New balance: 1100
3+
Cuenta creada
4+
Deposited 3000- New balance: 4000
5+
Cuenta creada
6+
Deposited 4500- New balance: 5500
7+
Cuenta creada
8+
Deposited 100- New balance: 1100
9+
Cuenta creada
10+
Deposited 3000- New balance: 4000
11+
Cuenta creada
12+
Deposited 4500- New balance: 5500
13+
Cuenta creada
14+
Deposited 100- New balance: 1100
15+
Cuenta creada
16+
Deposited 3000- New balance: 4000
17+
Cuenta creada
18+
Deposited 4500- New balance: 5500
19+
Cuenta creada
20+
Deposited 100- New balance: 1100
21+
Cuenta creada
22+
Deposited 3000- New balance: 4000
23+
Cuenta creada
24+
Deposited 4500- New balance: 5500
25+
Cuenta creada
26+
Deposited 100- New balance: 1100
27+
Cuenta creada
28+
Deposited 3000- New balance: 4000
29+
Cuenta creada
30+
Deposited 4500- New balance: 5500
31+
Cuenta creada
32+
Deposited 100- New balance: 1100
33+
Cuenta creada
34+
Deposited 3000- New balance: 4000
35+
Cuenta creada
36+
Deposited 4500- New balance: 5500
37+
Cuenta creada
38+
Deposited 100- New balance: 1100
39+
Cuenta creada
40+
Deposited 3000- New balance: 4000
41+
Cuenta creada
42+
Deposited 4500- New balance: 5500
43+
Cuenta creada
44+
Deposited 100- New balance: 1100
45+
Cuenta creada
46+
Deposited 3000- New balance: 4000
47+
Cuenta creada
48+
Deposited 4500- New balance: 5500
49+
Cuenta creada
50+
Deposited 100- New balance: 1100
51+
Cuenta creada
52+
Deposited 3000- New balance: 4000
53+
Cuenta creada
54+
Deposited 4500- New balance: 5500
55+
Cuenta creada
56+
Deposited 100- New balance: 1100
57+
Cuenta creada
58+
Deposited 3000- New balance: 4000
59+
Cuenta creada
60+
Deposited 4500- New balance: 5500
61+
Cuenta creada
62+
Deposited 100- New balance: 1100
63+
Cuenta creada
64+
Deposited 3000- New balance: 4000
65+
Cuenta creada
66+
Deposited 4500- New balance: 5500
67+
Cuenta creada
68+
Deposited 100- New balance: 1100
69+
Cuenta creada
70+
Deposited 3000- New balance: 4000
71+
Cuenta creada
72+
Deposited 4500- New balance: 5500
73+
Cuenta creada
74+
Deposited 100- New balance: 1100
75+
Cuenta creada
76+
Deposited 3000- New balance: 4000
77+
Cuenta creada
78+
Deposited 4500- New balance: 5500
79+
Cuenta creada
80+
Deposited 100- New balance: 1100
81+
Cuenta creada
82+
Deposited 3000- New balance: 4000
83+
Cuenta creada
84+
Deposited 4500- New balance: 5500
85+
Cuenta creada
86+
Deposited 100- New balance: 1100
87+
Cuenta creada
88+
Deposited 3000- New balance: 4000
89+
Cuenta creada
90+
Deposited 4500- New balance: 5500
91+
Cuenta creada
92+
Deposited 100- New balance: 1100
93+
Cuenta creada
94+
Deposited 3000- New balance: 4000
95+
Cuenta creada
96+
Deposited 4500- New balance: 5500

0 commit comments

Comments
 (0)