From cec17228d16567309e11b33374f3c19ca702dd29 Mon Sep 17 00:00:00 2001 From: Kingsuite Date: Sat, 2 Nov 2024 19:13:21 +0100 Subject: [PATCH 1/6] create test_zklend_loan_entity.py --- .../loan_states/test_zklend_loan_entity.py | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 apps/data_handler/tests/data_handlers/loan_states/test_zklend_loan_entity.py diff --git a/apps/data_handler/tests/data_handlers/loan_states/test_zklend_loan_entity.py b/apps/data_handler/tests/data_handlers/loan_states/test_zklend_loan_entity.py new file mode 100644 index 00000000..632b63d7 --- /dev/null +++ b/apps/data_handler/tests/data_handlers/loan_states/test_zklend_loan_entity.py @@ -0,0 +1,135 @@ +""" +Tests for zklend state +""" +import unittest +from unittest.mock import MagicMock, patch +import pandas as pd +from decimal import Decimal +from data_handler.handlers.loan_states.zklend.events import ( + ZkLendState + ) + +class TestZkLendState(unittest.TestCase): + + 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() + } + self.zk_lend_state.interest_rate_models = MagicMock() + 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") + 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( + token="TOKEN", lending_accumulator=Decimal("1.1"), debt_accumulator=Decimal("0.9") + ) + event_data = pd.Series({"data": "mock_data"}) + + self.zk_lend_state.process_accumulators_sync_event(event_data) + + 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") + 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")) + event_data = pd.Series({"data": "mock_data", "block_number": 1001, "timestamp": 163827}) + self.zk_lend_state.loan_entities[self.verbose_user].deposit.increase_value = MagicMock() + self.zk_lend_state.loan_entities[self.verbose_user].collateral.increase_value = MagicMock() + + self.zk_lend_state.process_deposit_event(event_data) + + raw_amount = Decimal("110") / Decimal("1.1") + self.zk_lend_state.loan_entities[self.verbose_user].deposit.increase_value.assert_called_once_with( + token="TOKEN", value=raw_amount + ) + 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") + 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") + event_data = pd.Series({"data": "mock_data", "block_number": 1002, "timestamp": 163828}) + self.zk_lend_state.loan_entities[self.verbose_user].collateral_enabled = {} + + self.zk_lend_state.process_collateral_enabled_event(event_data) + + 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") + 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")) + event_data = pd.Series({"data": "mock_data", "block_number": 1003, "timestamp": 163829}) + self.zk_lend_state.loan_entities[self.verbose_user].deposit.increase_value = MagicMock() + self.zk_lend_state.loan_entities[self.verbose_user].collateral.increase_value = MagicMock() + + self.zk_lend_state.process_withdrawal_event(event_data) + + raw_amount = Decimal("55") / Decimal("1.1") + self.zk_lend_state.loan_entities[self.verbose_user].deposit.increase_value.assert_any_call( + token="TOKEN", value=-raw_amount + ) + self.zk_lend_state.loan_entities[self.verbose_user].collateral.increase_value.assert_any_call( + token="TOKEN", value=-raw_amount + ) + + @patch("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")) + event_data = pd.Series({"data": "mock_data", "block_number": 1004, "timestamp": 163830}) + + self.zk_lend_state.process_borrowing_event(event_data) + + self.zk_lend_state.loan_entities[self.verbose_user].debt.increase_value.assert_called_once_with( + token="TOKEN", value=Decimal("200") + ) + + @patch("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")) + event_data = pd.Series({"data": "mock_data", "block_number": 1005, "timestamp": 163831}) + + self.zk_lend_state.process_repayment_event(event_data) + + self.zk_lend_state.loan_entities[self.verbose_user].debt.increase_value.assert_called_once_with( + token="TOKEN", value=-Decimal("100") + ) + + @patch("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( + user=self.verbose_user, + debt_token="DEBT_TOKEN", + debt_raw_amount=Decimal("150"), + collateral_token="COLLATERAL_TOKEN", + collateral_amount=Decimal("60") + ) + event_data = pd.Series({"data": "mock_data", "block_number": 1006, "timestamp": 163832}) + self.zk_lend_state.loan_entities[self.verbose_user].debt.increase_value = MagicMock() + self.zk_lend_state.loan_entities[self.verbose_user].collateral.increase_value = MagicMock() + + self.zk_lend_state.process_liquidation_event(event_data) + + collateral_raw_amount = Decimal("60") / Decimal("1.1") + self.zk_lend_state.loan_entities[self.verbose_user].debt.increase_value.assert_any_call( + token="DEBT_TOKEN", value=-Decimal("150") + ) + self.zk_lend_state.loan_entities[self.verbose_user].collateral.increase_value.assert_any_call( + token="COLLATERAL_TOKEN", value=-collateral_raw_amount + ) \ No newline at end of file From 1ce8302057c5743dbdd894bf9d3397efeecb51d1 Mon Sep 17 00:00:00 2001 From: Kingsuite Date: Sun, 3 Nov 2024 15:37:56 +0100 Subject: [PATCH 2/6] update test_zklend_loan_entity.py --- .../loan_states/test_zklend_loan_entity.py | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/apps/data_handler/tests/data_handlers/loan_states/test_zklend_loan_entity.py b/apps/data_handler/tests/data_handlers/loan_states/test_zklend_loan_entity.py index 632b63d7..a622edf8 100644 --- a/apps/data_handler/tests/data_handlers/loan_states/test_zklend_loan_entity.py +++ b/apps/data_handler/tests/data_handlers/loan_states/test_zklend_loan_entity.py @@ -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() @@ -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( @@ -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")) @@ -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") @@ -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")) @@ -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")) @@ -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")) @@ -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( @@ -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 - ) \ No newline at end of file + ) + From 62b589c72307cda2c79ca7219b6ce87df86030be Mon Sep 17 00:00:00 2001 From: Kingsuite Date: Sun, 3 Nov 2024 20:54:12 +0100 Subject: [PATCH 3/6] update test_zklend_state --- .../loan_states/test_zklend_loan_entity.py | 135 ------------ .../loan_state/test_zklend_state_entity.py | 195 ++++++++++++++++++ 2 files changed, 195 insertions(+), 135 deletions(-) delete mode 100644 apps/data_handler/tests/data_handlers/loan_states/test_zklend_loan_entity.py create mode 100644 apps/data_handler/tests/loan_state/test_zklend_state_entity.py diff --git a/apps/data_handler/tests/data_handlers/loan_states/test_zklend_loan_entity.py b/apps/data_handler/tests/data_handlers/loan_states/test_zklend_loan_entity.py deleted file mode 100644 index a622edf8..00000000 --- a/apps/data_handler/tests/data_handlers/loan_states/test_zklend_loan_entity.py +++ /dev/null @@ -1,135 +0,0 @@ -""" -Tests for zklend state -""" -import unittest -from unittest.mock import MagicMock, patch -import pandas as pd -from decimal import Decimal -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() - - self.zk_lend_state.loan_entities = { - self.verbose_user: MagicMock(), - "user_456": MagicMock() - } - self.zk_lend_state.interest_rate_models = MagicMock() - 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("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( - token="TOKEN", lending_accumulator=Decimal("1.1"), debt_accumulator=Decimal("0.9") - ) - event_data = pd.Series({"data": "mock_data"}) - - self.zk_lend_state.process_accumulators_sync_event(event_data) - - 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("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")) - event_data = pd.Series({"data": "mock_data", "block_number": 1001, "timestamp": 163827}) - self.zk_lend_state.loan_entities[self.verbose_user].deposit.increase_value = MagicMock() - self.zk_lend_state.loan_entities[self.verbose_user].collateral.increase_value = MagicMock() - - self.zk_lend_state.process_deposit_event(event_data) - - raw_amount = Decimal("110") / Decimal("1.1") - self.zk_lend_state.loan_entities[self.verbose_user].deposit.increase_value.assert_called_once_with( - token="TOKEN", value=raw_amount - ) - 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("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") - event_data = pd.Series({"data": "mock_data", "block_number": 1002, "timestamp": 163828}) - self.zk_lend_state.loan_entities[self.verbose_user].collateral_enabled = {} - - self.zk_lend_state.process_collateral_enabled_event(event_data) - - 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("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")) - event_data = pd.Series({"data": "mock_data", "block_number": 1003, "timestamp": 163829}) - self.zk_lend_state.loan_entities[self.verbose_user].deposit.increase_value = MagicMock() - self.zk_lend_state.loan_entities[self.verbose_user].collateral.increase_value = MagicMock() - - self.zk_lend_state.process_withdrawal_event(event_data) - - raw_amount = Decimal("55") / Decimal("1.1") - self.zk_lend_state.loan_entities[self.verbose_user].deposit.increase_value.assert_any_call( - token="TOKEN", value=-raw_amount - ) - self.zk_lend_state.loan_entities[self.verbose_user].collateral.increase_value.assert_any_call( - token="TOKEN", value=-raw_amount - ) - - @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")) - event_data = pd.Series({"data": "mock_data", "block_number": 1004, "timestamp": 163830}) - - self.zk_lend_state.process_borrowing_event(event_data) - - self.zk_lend_state.loan_entities[self.verbose_user].debt.increase_value.assert_called_once_with( - token="TOKEN", value=Decimal("200") - ) - - @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")) - event_data = pd.Series({"data": "mock_data", "block_number": 1005, "timestamp": 163831}) - - self.zk_lend_state.process_repayment_event(event_data) - - self.zk_lend_state.loan_entities[self.verbose_user].debt.increase_value.assert_called_once_with( - token="TOKEN", value=-Decimal("100") - ) - - @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( - user=self.verbose_user, - debt_token="DEBT_TOKEN", - debt_raw_amount=Decimal("150"), - collateral_token="COLLATERAL_TOKEN", - collateral_amount=Decimal("60") - ) - event_data = pd.Series({"data": "mock_data", "block_number": 1006, "timestamp": 163832}) - self.zk_lend_state.loan_entities[self.verbose_user].debt.increase_value = MagicMock() - self.zk_lend_state.loan_entities[self.verbose_user].collateral.increase_value = MagicMock() - - self.zk_lend_state.process_liquidation_event(event_data) - - collateral_raw_amount = Decimal("60") / Decimal("1.1") - self.zk_lend_state.loan_entities[self.verbose_user].debt.increase_value.assert_any_call( - token="DEBT_TOKEN", value=-Decimal("150") - ) - self.zk_lend_state.loan_entities[self.verbose_user].collateral.increase_value.assert_any_call( - token="COLLATERAL_TOKEN", value=-collateral_raw_amount - ) - diff --git a/apps/data_handler/tests/loan_state/test_zklend_state_entity.py b/apps/data_handler/tests/loan_state/test_zklend_state_entity.py new file mode 100644 index 00000000..5f773ea5 --- /dev/null +++ b/apps/data_handler/tests/loan_state/test_zklend_state_entity.py @@ -0,0 +1,195 @@ +"""Test module for ZkLendState.""" +import decimal +from unittest.mock import MagicMock, patch + +import pandas as pd +import pytest + +from data_handler.handlers.loan_states.zklend.events import ZkLendState +from shared.loan_entity import LoanEntity +from shared.types import InterestRateModels, Portfolio, ZkLendCollateralEnabled + + +@pytest.fixture +def mock_db_connector(): + """Create a mocked database connector""" + connector = MagicMock() + connector.write_loan_states_to_db = MagicMock() + connector.save_collateral_enabled_by_user = MagicMock() + + connector.engine = MagicMock() + connector.engine.begin = MagicMock() + connector.engine.begin.return_value._enter_ = MagicMock() + connector.engine.begin.return_value._exit_ = MagicMock() + + return connector + + +@pytest.fixture +def mock_portfolio(): + """Creates a mock portfolio""" + portfolio = MagicMock(spec=Portfolio) + portfolio.increase_value = MagicMock() + portfolio.set_value = MagicMock() + portfolio._getitem_ = MagicMock(return_value=decimal.Decimal("1.0")) + return portfolio + + +@pytest.fixture +def zklend_state(mock_db_connector): + """Create a ZkLendState instance with mocked dependencies.""" + with patch( + "data_handler.handlers.loan_states.zklend.events.InitializerDBConnector" + ) as mock_db_class, patch("sqlalchemy.orm.declarative_base") as mock_base: + + mock_metadata = MagicMock() + mock_metadata.create_all = MagicMock() + mock_base_instance = MagicMock() + mock_base_instance.metadata = mock_metadata + mock_base.return_value = mock_base_instance + + mock_db_class.return_value = mock_db_connector + mock_db_connector.engine = MagicMock() + + state = ZkLendState() + state.interest_rate_models = MagicMock() + state.interest_rate_models.collateral = { + "0x123": decimal.Decimal("1.1"), + "0x456": decimal.Decimal("1.05"), + "0x789": decimal.Decimal("1.2"), + } + state.interest_rate_models.debt = { + "0x123": decimal.Decimal("1.2"), + "0x456": decimal.Decimal("1.15"), + "0x789": decimal.Decimal("1.3"), + } + return state + + +@pytest.fixture +def sample_event(): + return pd.Series( + {"block_number": 12345, "timestamp": 1234567890, "data": ["0x123", "0x456", int(1e18)]} + ) + + +class TestZkLendState: + def test_init(self, zklend_state): + """Test ZkLendState initialization""" + assert hasattr(zklend_state, "loan_entities") + assert hasattr(zklend_state, "db_connector") + assert isinstance(zklend_state.loan_entities, dict) + + def test_process_deposit_event(self, zklend_state, sample_event, mock_portfolio): + """Test deposit event processing""" + mock_parsed_data = MagicMock() + mock_parsed_data.user = "0x123" + mock_parsed_data.token = "0x456" + mock_parsed_data.face_amount = int(1e18) + + with patch( + "data_handler.handler_tools.data_parser.zklend.ZklendDataParser.parse_deposit_event", + return_value=mock_parsed_data, + ): + user = mock_parsed_data.user + loan_entity = MagicMock() + loan_entity.deposit = mock_portfolio + loan_entity.collateral = mock_portfolio + loan_entity.collateral_enabled = ZkLendCollateralEnabled() + loan_entity.collateral_enabled.get = MagicMock(return_value=False) + loan_entity.extra_info = MagicMock() + + zklend_state.loan_entities[user] = loan_entity + + zklend_state.process_deposit_event(sample_event) + + mock_portfolio.increase_value.assert_called_once_with( + token=mock_parsed_data.token, + value=mock_parsed_data.face_amount + / zklend_state.interest_rate_models.collateral[mock_parsed_data.token], + ) + assert loan_entity.extra_info.block == 12345 + assert loan_entity.extra_info.timestamp == 1234567890 + + def test_process_withdrawal_event(self, zklend_state, sample_event, mock_portfolio): + """Test withdrawal event processing""" + mock_parsed_data = MagicMock() + mock_parsed_data.user = "0x123" + mock_parsed_data.token = "0x456" + mock_parsed_data.face_amount = int(1e18) + + with patch( + "data_handler.handler_tools.data_parser.zklend.ZklendDataParser.parse_withdrawal_event", + return_value=mock_parsed_data, + ): + user = mock_parsed_data.user + loan_entity = MagicMock() + loan_entity.deposit = mock_portfolio + loan_entity.collateral = mock_portfolio + loan_entity.collateral_enabled = ZkLendCollateralEnabled() + loan_entity.collateral_enabled.get = MagicMock(return_value=True) + loan_entity.extra_info = MagicMock() + + zklend_state.loan_entities[user] = loan_entity + + zklend_state.process_withdrawal_event(sample_event) + + mock_portfolio.increase_value.assert_called() + assert loan_entity.extra_info.block == 12345 + assert loan_entity.extra_info.timestamp == 1234567890 + + def test_process_borrowing_event(self, zklend_state, sample_event, mock_portfolio): + """Test borrowing event processing""" + mock_parsed_data = MagicMock() + mock_parsed_data.user = "0x123" + mock_parsed_data.token = "0x456" + mock_parsed_data.raw_amount = decimal.Decimal("1.0") + + with patch( + "data_handler.handler_tools.data_parser.zklend.ZklendDataParser.parse_borrowing_event", + return_value=mock_parsed_data, + ): + user = mock_parsed_data.user + loan_entity = MagicMock() + loan_entity.debt = mock_portfolio + loan_entity.extra_info = MagicMock() + + zklend_state.loan_entities[user] = loan_entity + + zklend_state.process_borrowing_event(sample_event) + + mock_portfolio.increase_value.assert_called_once_with( + token=mock_parsed_data.token, value=mock_parsed_data.raw_amount + ) + assert loan_entity.extra_info.block == 12345 + assert loan_entity.extra_info.timestamp == 1234567890 + + def test_process_liquidation_event(self, zklend_state, sample_event, mock_portfolio): + """Test liquidation event processing""" + mock_parsed_data = MagicMock() + mock_parsed_data.user = "0x123" + mock_parsed_data.debt_token = "0x456" + mock_parsed_data.debt_raw_amount = decimal.Decimal("1.0") + mock_parsed_data.collateral_token = "0x789" + mock_parsed_data.collateral_amount = int(1e18) + + with patch( + "data_handler.handler_tools.data_parser.zklend.ZklendDataParser.parse_liquidation_event", + return_value=mock_parsed_data, + ): + user = mock_parsed_data.user + loan_entity = MagicMock() + loan_entity.debt = mock_portfolio + loan_entity.deposit = mock_portfolio + loan_entity.collateral = mock_portfolio + loan_entity.collateral_enabled = ZkLendCollateralEnabled() + loan_entity.collateral_enabled._getitem_ = MagicMock(return_value=True) + loan_entity.extra_info = MagicMock() + + zklend_state.loan_entities[user] = loan_entity + + zklend_state.process_liquidation_event(sample_event) + + mock_portfolio.increase_value.assert_called() + assert loan_entity.extra_info.block == 12345 + assert loan_entity.extra_info.timestamp == 1234567890 From 6623cc8a822d6b0ddaa39ff560fd554194a40c44 Mon Sep 17 00:00:00 2001 From: djeck1432 Date: Mon, 4 Nov 2024 08:33:32 +0100 Subject: [PATCH 4/6] rework pipeline --- .github/workflows/pylint.yml | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index d1c27b7d..d7108918 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -37,12 +37,5 @@ jobs: git ls-files './apps/data_handler/*.py' - name: Run Pylint - run: | - PYTHON_FILES=$(find ./apps/data_handler -name "*.py") - if [ -n "$PYTHON_FILES" ]; then - echo "Files to lint: $PYTHON_FILES" - python -m pylint $PYTHON_FILES --disable=all --enable=C0114,C0115,C0116,C0301 --max-line-length=150 - else - echo "No Python files found in apps/data_handler/" - exit 1 - fi \ No newline at end of file + run: + pylint $(git ls-files 'data_handler/*.py') --disable=all --enable=C0114,C0115,C0116,C0301 \ No newline at end of file From ad731c7979eacee9fe286ad82927b101757bf0d2 Mon Sep 17 00:00:00 2001 From: djeck1432 Date: Mon, 4 Nov 2024 08:35:00 +0100 Subject: [PATCH 5/6] chnage[ --- .github/workflows/pylint.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index d7108918..598f8a6b 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -37,5 +37,8 @@ jobs: git ls-files './apps/data_handler/*.py' - name: Run Pylint - run: - pylint $(git ls-files 'data_handler/*.py') --disable=all --enable=C0114,C0115,C0116,C0301 \ No newline at end of file + run: | + files=$(git ls-files 'data_handler/*.py') + if [ -n "$files" ]; then + pylint $files --disable=all --enable=C0114,C0115,C0116,C0301 + fi \ No newline at end of file From 1697e620e25264089ffe30bc4c915e53fc45c88b Mon Sep 17 00:00:00 2001 From: djeck1432 Date: Mon, 4 Nov 2024 08:35:35 +0100 Subject: [PATCH 6/6] change to only push --- .github/workflows/pylint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index 598f8a6b..4beaddbe 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -1,6 +1,6 @@ name: Pylint Check -on: [push, pull_request] +on: [push] jobs: lint: