1818import unittest
1919from unittest .mock import MagicMock , patch
2020
21+ from google .adk_community .tools .spraay import spraay_tools as spraay_module
2122from google .adk_community .tools .spraay .constants import (
2223 BASE_CHAIN_ID ,
2324 MAX_RECIPIENTS ,
@@ -47,6 +48,14 @@ def _make_mock_w3():
4748 return mock_w3
4849
4950
51+ def _make_mock_web3_module ():
52+ """Create a mock web3 module with Web3 class."""
53+ mock_web3_mod = MagicMock ()
54+ mock_web3_mod .Web3 .is_address .return_value = True
55+ mock_web3_mod .Web3 .to_checksum_address .side_effect = lambda x : x
56+ return mock_web3_mod
57+
58+
5059class TestValidateRecipients (unittest .TestCase ):
5160 """Tests for recipient address validation."""
5261
@@ -61,19 +70,24 @@ def test_too_many_recipients(self):
6170 with self .assertRaises (ValueError ):
6271 _validate_recipients (addresses )
6372
64- @patch ("google.adk_community.tools.spraay.spraay_tools._validate_recipients" )
65- def test_valid_addresses (self , mock_validate ):
73+ def test_valid_addresses (self ):
6674 """Valid addresses should be checksummed and returned."""
67- mock_validate .return_value = [ADDR_1 , ADDR_2 ]
68- result = _validate_recipients ([ADDR_1 , ADDR_2 ])
69- self .assertEqual (len (result ), 2 )
75+ import sys
7076
71- @patch ("google.adk_community.tools.spraay.spraay_tools._validate_recipients" )
72- def test_invalid_address (self , mock_validate ):
77+ mock_web3_mod = _make_mock_web3_module ()
78+ with patch .dict (sys .modules , {"web3" : mock_web3_mod }):
79+ result = _validate_recipients ([ADDR_1 , ADDR_2 ])
80+ self .assertEqual (len (result ), 2 )
81+
82+ def test_invalid_address (self ):
7383 """Invalid address should raise ValueError."""
74- mock_validate .side_effect = ValueError ("Invalid Ethereum address" )
75- with self .assertRaises (ValueError ):
76- _validate_recipients (["not_an_address" ])
84+ import sys
85+
86+ mock_web3_mod = MagicMock ()
87+ mock_web3_mod .Web3 .is_address .return_value = False
88+ with patch .dict (sys .modules , {"web3" : mock_web3_mod }):
89+ with self .assertRaises (ValueError ):
90+ _validate_recipients (["not_an_address" ])
7791
7892
7993class TestCalculateFee (unittest .TestCase ):
@@ -99,8 +113,8 @@ def test_small_amount(self):
99113class TestSpraayBatchEth (unittest .TestCase ):
100114 """Tests for spraay_batch_eth function."""
101115
102- @patch ( "google.adk_community.tools.spraay.spraay_tools. _get_account" )
103- @patch ( "google.adk_community.tools.spraay.spraay_tools. _get_web3" )
116+ @patch . object ( spraay_module , " _get_account" )
117+ @patch . object ( spraay_module , " _get_web3" )
104118 def test_missing_private_key (self , mock_web3 , mock_account ):
105119 """Should return error if SPRAAY_PRIVATE_KEY is not set."""
106120 mock_w3 = _make_mock_w3 ()
@@ -113,9 +127,9 @@ def test_missing_private_key(self, mock_web3, mock_account):
113127 self .assertEqual (result ["status" ], "error" )
114128 self .assertIn ("SPRAAY_PRIVATE_KEY" , result ["error" ])
115129
116- @patch ( "google.adk_community.tools.spraay.spraay_tools. _validate_recipients" )
117- @patch ( "google.adk_community.tools.spraay.spraay_tools. _get_account" )
118- @patch ( "google.adk_community.tools.spraay.spraay_tools. _get_web3" )
130+ @patch . object ( spraay_module , " _validate_recipients" )
131+ @patch . object ( spraay_module , " _get_account" )
132+ @patch . object ( spraay_module , " _get_web3" )
119133 def test_zero_amount_returns_error (self , mock_web3 , mock_account , mock_validate ):
120134 """Zero ETH amount should return error."""
121135 mock_w3 = _make_mock_w3 ()
@@ -132,9 +146,9 @@ def test_zero_amount_returns_error(self, mock_web3, mock_account, mock_validate)
132146class TestSpraayBatchEthVariable (unittest .TestCase ):
133147 """Tests for spraay_batch_eth_variable function."""
134148
135- @patch ( "google.adk_community.tools.spraay.spraay_tools. _validate_recipients" )
136- @patch ( "google.adk_community.tools.spraay.spraay_tools. _get_account" )
137- @patch ( "google.adk_community.tools.spraay.spraay_tools. _get_web3" )
149+ @patch . object ( spraay_module , " _validate_recipients" )
150+ @patch . object ( spraay_module , " _get_account" )
151+ @patch . object ( spraay_module , " _get_web3" )
138152 def test_mismatched_lengths (self , mock_web3 , mock_account , mock_validate ):
139153 """Recipients and amounts must have same length."""
140154 mock_w3 = _make_mock_w3 ()
@@ -153,8 +167,8 @@ def test_mismatched_lengths(self, mock_web3, mock_account, mock_validate):
153167class TestSpraayBatchToken (unittest .TestCase ):
154168 """Tests for spraay_batch_token function."""
155169
156- @patch ( "google.adk_community.tools.spraay.spraay_tools. _get_account" )
157- @patch ( "google.adk_community.tools.spraay.spraay_tools. _get_web3" )
170+ @patch . object ( spraay_module , " _get_account" )
171+ @patch . object ( spraay_module , " _get_web3" )
158172 def test_missing_private_key (self , mock_web3 , mock_account ):
159173 """Should return error if SPRAAY_PRIVATE_KEY is not set."""
160174 mock_w3 = _make_mock_w3 ()
@@ -171,8 +185,8 @@ def test_missing_private_key(self, mock_web3, mock_account):
171185class TestSpraayBatchTokenVariable (unittest .TestCase ):
172186 """Tests for spraay_batch_token_variable function."""
173187
174- @patch ( "google.adk_community.tools.spraay.spraay_tools. _get_account" )
175- @patch ( "google.adk_community.tools.spraay.spraay_tools. _get_web3" )
188+ @patch . object ( spraay_module , " _get_account" )
189+ @patch . object ( spraay_module , " _get_web3" )
176190 def test_missing_private_key (self , mock_web3 , mock_account ):
177191 """Should return error if SPRAAY_PRIVATE_KEY is not set."""
178192 mock_w3 = _make_mock_w3 ()
0 commit comments