Skip to content

Commit

Permalink
update test_zklend_loan_entity.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingsuite committed Nov 3, 2024
1 parent cec1722 commit 1ce8302
Showing 1 changed file with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
from unittest.mock import MagicMock, patch
import pandas as pd
from decimal import Decimal
from data_handler.handlers.loan_states.zklend.events import (
ZkLendState
)
from data_handler.handlers.loan_states.zklend.events import ZkLendState

class TestZkLendState(unittest.TestCase):

"""
Test ZkLendState class
"""
def setUp(self):
"""Set up the ZkLendState instance and mock dependencies."""
self.verbose_user = "user_123"
self.zk_lend_state = ZkLendState(verbose_user=self.verbose_user)
self.zk_lend_state.db_connector = MagicMock()

# Mock loan entity and interest rate model structures
self.zk_lend_state.loan_entities = {
self.verbose_user: MagicMock(),
"user_456": MagicMock()
Expand All @@ -26,7 +25,7 @@ def setUp(self):
self.zk_lend_state.interest_rate_models.collateral = {"TOKEN": Decimal("1.1")}
self.zk_lend_state.interest_rate_models.debt = {"TOKEN": Decimal("0.9")}

@patch("ZklendDataParser.parse_accumulators_sync_event")
@patch("data_handler.handlers.loan_states.zklend.events.ZklendDataParser.parse_accumulators_sync_event")
def test_process_accumulators_sync_event(self, mock_parse):
"""Test that process_accumulators_sync_event updates the correct interest rates."""
mock_parse.return_value = MagicMock(
Expand All @@ -39,7 +38,7 @@ def test_process_accumulators_sync_event(self, mock_parse):
self.assertEqual(self.zk_lend_state.interest_rate_models.collateral["TOKEN"], Decimal("1.1"))
self.assertEqual(self.zk_lend_state.interest_rate_models.debt["TOKEN"], Decimal("0.9"))

@patch("ZklendDataParser.parse_deposit_event")
@patch("data_handler.handlers.loan_states.zklend.events.ZklendDataParser.parse_deposit_event")
def test_process_deposit_event(self, mock_parse):
"""Test that process_deposit_event correctly updates user deposits and collateral."""
mock_parse.return_value = MagicMock(user=self.verbose_user, token="TOKEN", face_amount=Decimal("110"))
Expand All @@ -56,7 +55,7 @@ def test_process_deposit_event(self, mock_parse):
self.zk_lend_state.loan_entities[self.verbose_user].extra_info.block = 1001
self.zk_lend_state.loan_entities[self.verbose_user].extra_info.timestamp = 163827

@patch("ZklendDataParser.parse_collateral_enabled_event")
@patch("data_handler.handlers.loan_states.zklend.events.ZklendDataParser.parse_collateral_enabled_event")
def test_process_collateral_enabled_event(self, mock_parse):
"""Test that process_collateral_enabled_event enables collateral for the correct token."""
mock_parse.return_value = MagicMock(user=self.verbose_user, token="TOKEN")
Expand All @@ -68,7 +67,7 @@ def test_process_collateral_enabled_event(self, mock_parse):
self.assertTrue(self.zk_lend_state.loan_entities[self.verbose_user].collateral_enabled["TOKEN"])
self.zk_lend_state.db_connector.save_collateral_enabled_by_user.assert_called_once()

@patch("ZklendDataParser.parse_withdrawal_event")
@patch("data_handler.handlers.loan_states.zklend.events.ZklendDataParser.parse_withdrawal_event")
def test_process_withdrawal_event(self, mock_parse):
"""Test that process_withdrawal_event correctly decreases deposit and collateral values."""
mock_parse.return_value = MagicMock(user=self.verbose_user, token="TOKEN", face_amount=Decimal("55"))
Expand All @@ -86,7 +85,7 @@ def test_process_withdrawal_event(self, mock_parse):
token="TOKEN", value=-raw_amount
)

@patch("ZklendDataParser.parse_borrowing_event")
@patch("data_handler.handlers.loan_states.zklend.events.ZklendDataParser.parse_borrowing_event")
def test_process_borrowing_event(self, mock_parse):
"""Test that process_borrowing_event increases the user's debt by the borrowed amount."""
mock_parse.return_value = MagicMock(user=self.verbose_user, token="TOKEN", raw_amount=Decimal("200"))
Expand All @@ -98,7 +97,7 @@ def test_process_borrowing_event(self, mock_parse):
token="TOKEN", value=Decimal("200")
)

@patch("ZklendDataParser.parse_repayment_event")
@patch("data_handler.handlers.loan_states.zklend.events.ZklendDataParser.parse_repayment_event")
def test_process_repayment_event(self, mock_parse):
"""Test that process_repayment_event decreases the user's debt by the repaid amount."""
mock_parse.return_value = MagicMock(beneficiary=self.verbose_user, token="TOKEN", raw_amount=Decimal("100"))
Expand All @@ -110,7 +109,7 @@ def test_process_repayment_event(self, mock_parse):
token="TOKEN", value=-Decimal("100")
)

@patch("ZklendDataParser.parse_liquidation_event")
@patch("data_handler.handlers.loan_states.zklend.events.ZklendDataParser.parse_liquidation_event")
def test_process_liquidation_event(self, mock_parse):
"""Test that process_liquidation_event updates debt and collateral during liquidation."""
mock_parse.return_value = MagicMock(
Expand All @@ -132,4 +131,5 @@ def test_process_liquidation_event(self, mock_parse):
)
self.zk_lend_state.loan_entities[self.verbose_user].collateral.increase_value.assert_any_call(
token="COLLATERAL_TOKEN", value=-collateral_raw_amount
)
)

0 comments on commit 1ce8302

Please sign in to comment.