Skip to content

Commit

Permalink
Clase 15: Uso de la Biblioteca Faker para Datos de Prueba
Browse files Browse the repository at this point in the history
  • Loading branch information
lcmartinezdev committed Aug 29, 2024
1 parent 336a33c commit 37344a1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests==2.32.3
Faker==28.0.0
11 changes: 11 additions & 0 deletions src/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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)
35 changes: 35 additions & 0 deletions tests/test_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import unittest, os
from faker import Faker

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


class UserTests(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=2000, step=50),
log_file=self.faker.file_name(extension=".txt")
)
self.user.add_account(account=bank_account)

expected_value = self.user.get_total_balance()
value = sum(account.get_balance() for account in self.user.accounts)
self.assertEqual(value, expected_value)

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

0 comments on commit 37344a1

Please sign in to comment.