Skip to content

Commit

Permalink
finalización de pruebas e instalación de coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Julio Sarango committed Nov 15, 2024
1 parent 3c50e94 commit fedab56
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[report]
fail_under = 90
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,32 @@ python -m unittest tests.test_bank_account.BankAccoutTest.test_deposit_multiple_

# Doctest

Doctest es una librería que está incluida en Python y que permite crear pruebas en los comentarios del código.
Doctest es una librería que está incluida en Python y que permite crear pruebas en los comentarios del código.

# Faker
```
pip install Faker
pip freeze | grep Kaker
```

# Coverage
```
coverage run --source src -m unittest
coverage report
```
![Coverage report](images/report.png)

Si queremos ver un reporte más detallado y dinámico sacamos el reporte en formato html.
```
coverage html
```
![Coverage html](images/report_html.png)
Podemos hacer clic en cada uno de los nombres donde se mostrará la cobertura de las pruebas.
A modo de pruebas, al hacer clic en el archivo que está con 98% de convertura, se mostrará la parte no cubierta
![Report Detail](images/report_detail.png)

Si corregimos la parte que no tiene cobertura y ejecutamos nuevamente el comando ```coverage html``` cambiará el resultado.
![Coverage html full](images/report_html_full.png)

![Report Detail](images/report_detail_full.png)
Binary file added images/report.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/report_detail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/report_detail_full.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/report_html.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/report_html_full.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 3 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
exceptiongroup==1.2.2
iniconfig==2.0.0
packaging==24.1
pluggy==1.5.0
pytest==8.3.3
tomli==2.0.2
requests==2.32.3
Faker==33.0.0
coverage==7.6.5
4 changes: 0 additions & 4 deletions src/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,3 @@ def get_location(ip):
"city": data["cityName"],
"code": data["countryCode"],
}


if __name__ == "__main__":
get_location("8.8.0")
11 changes: 11 additions & 0 deletions src/bank_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ def _log_transaction_fail(self, message):
self.grabar_log(self.log_file_withdraw, message)

def deposit(self, amount):
"""Deposit method
Args:
amount (number): Amount that we will deposit
Returns:
number: New balance after deposit
>>> account = BankAccount(100)
>>> account.deposit(50)
150
"""
if amount > 0:
self.balance += amount
self._log_transaction(f"Deposited {amount}- New balance: {self.balance}")
Expand Down
12 changes: 12 additions & 0 deletions src/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class User:

def __init__(self, name, email):
self.name = name
self.email = email
self.accounts = []

def add_account(self, account):
self.accounts.append(account)

def get_total_balance(self):
return sum(account.get_balance() for account in self.accounts)
8 changes: 6 additions & 2 deletions tests/test_bank_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def test_withdraw(self):
new_balance = self.account.withdraw(200)
self.assertEqual(new_balance, 800)

def test_withdraw_with_negative_amount_not_decrease(self):
@patch("src.bank_account.datetime")
def test_withdraw_with_negative_amount_not_decrease(self, mock_datetime):
mock_datetime.now.return_value.hour = 10
self.assertEqual(self.account.withdraw(-10), 1000)

def test_get_balance(self):
Expand All @@ -51,7 +53,9 @@ def test_count_transaction(self):
self.account.deposit(500)
self.assertEqual(self._count_lines(self.account.log_file), 2)

def test_withdraw_without_saldo(self):
@patch("src.bank_account.datetime")
def test_withdraw_without_saldo(self, mock_datetime):
mock_datetime.now.return_value.hour = 10
self.account.withdraw(1500)
self.assertTrue(os.path.exists("withdraw_log.txt"))
self.assertEqual(self._count_lines(self.account.log_file_withdraw), 1)
Expand Down
38 changes: 38 additions & 0 deletions tests/test_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import unittest, os
from faker import Faker
import time


from src.user import User
from src.bank_account import BankAccount


class UserTest(unittest.TestCase):

def setUp(self) -> None:
self.faker = Faker(locale="es")
self.user = User(name=self.faker.name(), email=self.faker.email())

def test_user_creation(self):
name_generated = self.faker.name()
email_generated = self.faker.email()

user = User(name=name_generated, email=email_generated)
self.assertEqual(user.name, name_generated)
self.assertEqual(user.email, email_generated)

def test_user_with_multiple_accounts(self):
for _ in range(3):
bank_account = BankAccount(
balance=self.faker.random_int(min=100, max=200, step=50),
log_file=self.faker.file_name(extension=".txt"),
)
self.user.add_account(account=bank_account)

expected_balance = self.user.get_total_balance()
value_valance = sum(account.get_balance() for account in self.user.accounts)
self.assertEqual(value_valance, expected_balance)

def tearDown(self):
for account in self.user.accounts:
os.remove(account.log_file)

0 comments on commit fedab56

Please sign in to comment.