Skip to content

Commit

Permalink
checkpoint 2
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewHanasiro committed Jan 7, 2025
1 parent 923b3e8 commit 01eaa58
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/config/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

from src.config.envvar import EnvVars

engine = create_engine(EnvVars.DATABASE_HOST, echo=True)
engine = create_engine(EnvVars.DATABASE_HOST, echo=False)
1 change: 0 additions & 1 deletion src/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def __init__(self):
)
charge_debit = ChargeDebit(
reading_account,
reading_transaction,
billing_updating_invoice,
billing_fetching_invoice,
)
Expand Down
6 changes: 3 additions & 3 deletions src/core/repository/discount.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ def by_account_id(self, account_id: UUID) -> Discount:
select(discount_table)
.where(
discount_table.c.account_id == account_id,
discount_table.c.is_enable,
discount_table.c.deleted_at == None,
)
.limit(1)
.order_by(discount_table.c.created_at.desc())
)
row = self.session.execute(query).first()
if row is None:
raise DiscountNotFoundException("discount not found")
(id_, account_id, reason, is_enable, amount, type_, created_at) = deepcopy(row)
return Discount(id_, account_id, reason, amount, type_, is_enable, created_at)
(id_, account_id, reason, amount, type_, created_at, deleted_at) = deepcopy(row)
return Discount(id_, account_id, reason, amount, type_, created_at, deleted_at)
8 changes: 5 additions & 3 deletions src/core/repository/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ def __init__(self, session: Session):
self.session = session

def create_transaction(
self, account_id: UUID, amount: float, description: str, event_id=None | UUID
self, account_id: UUID, amount: float, description: str, price_id=None | UUID
) -> Transaction:
insert_line = (
insert(ledger_table)
.values(
account_id=account_id,
amount=amount,
description=description,
event_id=event_id,
price_id=price_id,
)
.returning(ledger_table.c.id, ledger_table.c.created_at)
)
Expand All @@ -49,17 +49,19 @@ def create_transaction(
if row is None:
raise SystemError("Something on database did not return")
(id_, created_at) = deepcopy(row)
return Transaction(id_, account_id, amount, description, event_id, created_at)
return Transaction(id_, account_id, amount, description, price_id, created_at)

def by_account_id(
self, account_id: UUID, date_start: datetime, date_end=datetime.now()
) -> List[Transaction]:
query = select(ledger_table).where(ledger_table.c.account_id == account_id)
cursor = self.session.execute(query).all()
self.session.commit()
if cursor is None:
return []
else:
transaction_list = deepcopy(cursor)

return list(
map(
lambda t: Transaction(
Expand Down
2 changes: 1 addition & 1 deletion src/core/repository/price.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ def by_event(self, event: EventType) -> Event:
cursor = self.session.execute(query).first()
if cursor is None:
raise EventNotFoundException("event not found")
(id_, event_, price, created_at) = deepcopy(cursor)
(id_, event_, price, created_at, deleted_at) = deepcopy(cursor)
return Event(id_, event_, price, created_at)
2 changes: 0 additions & 2 deletions src/core/usecase/charge_debit.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ class ChargeDebit:
def __init__(
self,
reading_account: ReadingAccount,
reading_transaction: ReadingTransaction,
billing_updating_invoice: BillingUpdatingInvoice,
billing_fetching_invoice: BillingFetchingInvoice,
):
self.reading_account = reading_account
self.reading_transaction = reading_transaction
self.billing_updating_invoice = billing_updating_invoice
self.billing_fetching_invoice = billing_fetching_invoice

Expand Down
1 change: 0 additions & 1 deletion tests/core/repository/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def test_should_create(session: Session):

def test_should_select_by_id(session: Session):
account = create_account(session, uuid4(), AccountType.PRE_PAID)
print(account)
repository = AccountRepository(session)
result = repository.by_id(account.id)
assert result.id == account.id
Expand Down
9 changes: 1 addition & 8 deletions tests/core/repository/test_billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,4 @@ def test_should_fetch_user(session: Session):
repository = BillingService()
repository.fetch_by_account_id(account.external_id)
responses.assert_call_count(f"{EnvVars.BILLING_HOST}/user/{account.external_id}", 1)
delete_account(session, account.id)


def test_should_charge():
with mock.patch("src.presentation.worker.huey.enqueue") as mocked:
repository = BillingService()
repository.charge(uuid4(), [InvoiceItem("descript", 1.0, "BRL", 1.0)])
mocked.assert_called_once()
delete_account(session, account.id)
2 changes: 0 additions & 2 deletions tests/core/repository/test_discount.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def test_should_create(session: Session):
assert isinstance(result.id, UUID)
assert result.account_id == account.id
assert result.reason == reason
assert result.is_enable
assert result.amount == amount
assert result.type == type_
assert isinstance(result.created_at, datetime)
Expand All @@ -42,7 +41,6 @@ def test_should_select_by_account_id(session: Session):
assert result.id == discount.id
assert result.account_id == account.id
assert result.reason == discount.reason
assert result.is_enable
assert result.amount == discount.amount
assert result.type == discount.type
assert result.created_at == discount.created_at
Expand Down
1 change: 1 addition & 0 deletions tests/core/repository/test_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def test_should_create_transaction(session: Session):
def test_should_select_by_account_id(session: Session):
event = get_event(session)
account = create_account(session, uuid4(), AccountType.PRE_PAID)
print(account)
transaction = create_transaction(session, account.id, 123.4, "descript", event.id)
repository = LedgerRepository(session)
result = repository.by_account_id(account.id, datetime.now())
Expand Down
3 changes: 1 addition & 2 deletions tests/core/usecase/test_account_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_should_create(session: Session):
# mock
creating_account: CreatingAccount = AccountRepository(session)
creating_account.create = MagicMock(
return_value=Account(id_, external_id, type_, True, datetime.now())
return_value=Account(id_, external_id, type_, datetime.now(), None)
)
# usecase
usecase = AccountCreate(creating_account)
Expand All @@ -26,6 +26,5 @@ def test_should_create(session: Session):
assert result.id == id_
assert result.external_id == external_id
assert result.type == type_
assert result.is_enable
assert isinstance(result.created_at, datetime)
creating_account.create.assert_called_once_with(external_id, type_)
22 changes: 4 additions & 18 deletions tests/core/usecase/test_charge_debit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from sqlalchemy.orm import Session

from src.core.entity.account import Account, AccountType
from src.core.entity.billing import Invoice, InvoiceItem
from src.core.entity.transaction import Transaction
from src.core.entity.billing import Invoice
from src.core.repository.billing import BillingService
from src.core.repository.ledger import LedgerRepository
from src.core.usecase.charge_debit import ChargeDebit
Expand All @@ -17,43 +16,30 @@
BillingUpdatingInvoice,
)
from src.core.usecase.driven.reading_account import ReadingAccount
from src.core.usecase.driven.reading_transaction import ReadingTransaction


def test_should_charge_debit(session: Session):
account_id = uuid4()
external_id = uuid4()
account = Account(
account_id, external_id, AccountType.PRE_PAID, True, datetime.now()
account_id, external_id, AccountType.POST_PAID, True, datetime.now()
)
date_start = datetime.today() - timedelta(days=1)
date_end = datetime.now()
transaction = Transaction(
uuid4(), account_id, 1.0, "desc", uuid4(), datetime.today()
)
transaction_list = [transaction]
invoice = Invoice(uuid4(), external_id, "draft", datetime.today())
# mock
reading_account: ReadingAccount = LedgerRepository(session)
reading_account.by_subscription_period = MagicMock(return_value=[account])
reading_transaction: ReadingTransaction = LedgerRepository(session)
reading_transaction.by_account_id = MagicMock(return_value=transaction_list)
billing_fetching_invoice: BillingFetchingInvoice = BillingService()
billing_fetching_invoice.get_current = MagicMock(return_value=invoice)
billing_updating_invoice: BillingUpdatingInvoice = BillingService()
billing_updating_invoice.charge = MagicMock(return_value=None)
# usecase
usecase = ChargeDebit(
reading_account,
reading_transaction,
billing_updating_invoice,
billing_fetching_invoice,
)
usecase.charge_debit(external_id, date_start, date_end)
usecase.charge_debit()
# assert
reading_account.by_subscription_period.assert_called_once()
reading_transaction.by_account_id.assert_called_once_with(
account_id, date_start, date_end
)
billing_fetching_invoice.get_current.assert_called_once_with(external_id)
billing_updating_invoice.charge.assert_called_once_with()
billing_updating_invoice.charge.assert_called_once_with(invoice.id)
66 changes: 43 additions & 23 deletions tests/factory/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,36 @@ def create_account(session: Session, external_id: UUID, type_: AccountType):
account_insert_line = (
insert(account_table)
.values(external_id=external_id, type=type_)
.returning(account_table.c.id, account_table.c.created_at, account_table.c.deleted_at)
.returning(
account_table.c.id, account_table.c.created_at, account_table.c.deleted_at
)
)
cursor = session.execute(account_insert_line).first()
if cursor is None:
cursor_user = session.execute(account_insert_line).first()
session.commit()
if cursor_user is None:
raise SystemError("test: create_account something went wrong")
(id_, created_at, deleted_at) = deepcopy(cursor)
(id_, created_at, deleted_at) = deepcopy(cursor_user)
subscription_insert_line = (
insert(subscription_table)
.values(account_id=id_, type=type_)
.returning(subscription_table.c.id, subscription_table.c.created_at, subscription_table.c.deleted_at)
.returning(
subscription_table.c.id,
subscription_table.c.created_at,
subscription_table.c.deleted_at,
)
)
cursor = session.execute(subscription_insert_line).first()
if cursor is None:
cursor_subscription = session.execute(subscription_insert_line).first()
if cursor_subscription is None:
raise SystemError("test: create_account something went wrong")
(id_, created_at, deleted_at) = deepcopy(cursor)
(id_, created_at, deleted_at) = deepcopy(cursor_subscription)
session.commit()
return Account(id_, external_id, type_, created_at, deleted_at)


def delete_account(session: Session, id_: UUID):
subscription_delete_query = delete(subscription_table).where(subscription_table.c.account_id == id_)
subscription_delete_query = delete(subscription_table).where(
subscription_table.c.account_id == id_
)
session.execute(subscription_delete_query)
account_delete_query = delete(account_table).where(account_table.c.id == id_)
session.execute(account_delete_query)
Expand All @@ -56,15 +65,17 @@ def create_discount(
insert(discount_table)
.values(account_id=account_id, reason=reason, amount=amount, type=type_)
.returning(
discount_table.c.id, discount_table.c.is_enable, discount_table.c.created_at
discount_table.c.id,
discount_table.c.deleted_at,
discount_table.c.created_at,
)
)
cursor = session.execute(insert_line).first()
if cursor is None:
raise SystemError("test: create_discount something went wrong")
(id_, is_enable, created_at) = deepcopy(cursor)
(id_, deleted_at, created_at) = deepcopy(cursor)
session.commit()
return Discount(id_, account_id, reason, amount, type_, is_enable, created_at)
return Discount(id_, account_id, reason, amount, type_, created_at, deleted_at)


def delete_discount(session: Session, id_: UUID):
Expand All @@ -76,35 +87,44 @@ def delete_discount(session: Session, id_: UUID):
# EVENT
def get_event(session: Session):
query = (
select(event_table)
.where(event_table.c.event == EventType.EMAIL_AUTH_FACTOR_SENT)
.order_by(event_table.c.created_at.desc())
.limit(1)
)
select(event_table)
.where(event_table.c.event == EventType.EMAIL_AUTH_FACTOR_SENT)
.limit(1)
)
cursor = session.execute(query).first()
session.commit()
if cursor is None:
raise SystemError("test: create_event something went wrong")
(id_, event_, price, created_at) = deepcopy(cursor)
session.commit()
return Event(id_, event_, price, created_at)

(id_, event_, value_, created_at, deleted_at) = deepcopy(cursor)
return Event(id_, event_, value_, created_at)


# Ledger
def create_transaction(
session: Session, account_id: UUID, amount: float, description: str, event_id: UUID
):
print(account_id)
query_user = (
select(account_table)
.where(account_table.c.id == account_id)
.limit(1)
)
cursor_user = session.execute(query_user).first()
print(cursor_user)
insert_line = (
insert(ledger_table)
.values(
account_id=account_id,
amount=amount,
description=description,
event_id=event_id,
price_id=event_id,
)
.returning(ledger_table.c.id, ledger_table.c.created_at)
)
print(insert_line)
cursor = session.execute(insert_line).first()
print(cursor)

if cursor is None:
raise SystemError("test: create_transaction something went wrong")
(id_, created_at) = deepcopy(cursor)
Expand All @@ -115,4 +135,4 @@ def create_transaction(
def delete_transaction(session: Session, id_: UUID):
delete_query = delete(ledger_table).where(ledger_table.c.id == id_)
session.execute(delete_query)
session.commit()
session.commit()

0 comments on commit 01eaa58

Please sign in to comment.