55import pytest
66from aiohttp import ClientResponseError
77
8- from aleph_client .commands .credit import show
8+ from aleph_client .commands .credit import history , show
99
1010
1111@pytest .fixture
@@ -49,6 +49,47 @@ def mock_credits_list_response():
4949 return mock_response
5050
5151
52+ @pytest .fixture
53+ def mock_credit_history_response ():
54+ """Create a mock response for credit history API call."""
55+ mock_response = AsyncMock ()
56+ mock_response .__aenter__ .return_value = mock_response
57+ mock_response .status = 200
58+ mock_response .json = AsyncMock (
59+ return_value = {
60+ "address" : "0x1234567890123456789012345678901234567890" ,
61+ "credit_balances" : [
62+ {
63+ "amount" : 1000000000 ,
64+ "message_timestamp" : "2023-06-15T12:30:45Z" ,
65+ "payment_method" : "credit_card" ,
66+ "origin" : "purchase" ,
67+ "origin_ref" : "txn_123456" ,
68+ "expiration_date" : "2024-06-15T12:30:45Z" ,
69+ "credit_ref" : "credit_ref_1" ,
70+ "credit_index" : 1 ,
71+ },
72+ {
73+ "amount" : 500000000 ,
74+ "message_timestamp" : "2023-07-20T15:45:30Z" ,
75+ "payment_method" : "wire_transfer" ,
76+ "origin" : "purchase" ,
77+ "origin_ref" : "txn_789012" ,
78+ "expiration_date" : None ,
79+ "credit_ref" : "credit_ref_2" ,
80+ "credit_index" : 2 ,
81+ },
82+ ],
83+ "pagination_page" : 1 ,
84+ "pagination_total" : 1 ,
85+ "pagination_per_page" : 100 ,
86+ "pagination_total_items" : 2 ,
87+ "pagination_item" : "credit_history" ,
88+ }
89+ )
90+ return mock_response
91+
92+
5293@pytest .fixture
5394def mock_credit_error_response ():
5495 """Create a mock error response for credit API calls."""
@@ -89,6 +130,7 @@ async def run(mock_get):
89130@pytest .mark .asyncio
90131async def test_show_json_output (mock_credit_balance_response , capsys ):
91132 """Test the show command with JSON output."""
133+ import json
92134
93135 @patch ("aiohttp.ClientSession.get" )
94136 async def run (mock_get ):
@@ -105,8 +147,13 @@ async def run(mock_get):
105147
106148 await run ()
107149 captured = capsys .readouterr ()
108- assert "0x1234567890123456789012345678901234567890" in captured .out
109- assert "1000000000" in captured .out
150+
151+ # Try to parse the output as JSON to validate it's properly formatted
152+ parsed_json = json .loads (captured .out )
153+
154+ # Verify expected data is in the parsed JSON
155+ assert parsed_json ["address" ] == "0x1234567890123456789012345678901234567890"
156+ assert parsed_json ["credits" ] == 1000000000
110157
111158
112159@pytest .mark .asyncio
@@ -181,3 +228,86 @@ async def run(mock_get):
181228 )
182229
183230 await run ()
231+
232+
233+ @pytest .mark .asyncio
234+ async def test_history_command (mock_credit_history_response , capsys ):
235+ """Test the history command with an explicit address."""
236+
237+ @patch ("aiohttp.ClientSession.get" )
238+ async def run (mock_get ):
239+ mock_get .return_value = mock_credit_history_response
240+
241+ # Run the history command with an explicit address
242+ await history (
243+ address = "0x1234567890123456789012345678901234567890" ,
244+ private_key = None ,
245+ private_key_file = None ,
246+ page_size = 100 ,
247+ page = 1 ,
248+ json = False ,
249+ debug = False ,
250+ )
251+
252+ await run ()
253+ captured = capsys .readouterr ()
254+ assert "Credits History" in captured .out
255+ assert "0x1234567890123456789012345678901234567890" in captured .out
256+ assert "credit_card" in captured .out
257+ assert "Page: 1" in captured .out
258+
259+
260+ @pytest .mark .asyncio
261+ async def test_history_json_output (mock_credit_history_response , capsys ):
262+ """Test the history command with JSON output."""
263+ import json
264+
265+ @patch ("aiohttp.ClientSession.get" )
266+ async def run (mock_get ):
267+ mock_get .return_value = mock_credit_history_response
268+
269+ # Run the history command with JSON output
270+ await history (
271+ address = "0x1234567890123456789012345678901234567890" ,
272+ private_key = None ,
273+ private_key_file = None ,
274+ page_size = 100 ,
275+ page = 1 ,
276+ json = True ,
277+ debug = False ,
278+ )
279+
280+ await run ()
281+ captured = capsys .readouterr ()
282+
283+ # Try to parse the output as JSON to validate it's properly formatted
284+ parsed_json = json .loads (captured .out )
285+
286+ # Verify expected data is in the parsed JSON
287+ assert parsed_json ["address" ] == "0x1234567890123456789012345678901234567890"
288+ assert parsed_json ["credit_balances" ][0 ]["amount" ] == 1000000000
289+ assert parsed_json ["credit_balances" ][0 ]["payment_method" ] == "credit_card"
290+ assert len (parsed_json ["credit_balances" ]) == 2
291+
292+
293+ @pytest .mark .asyncio
294+ async def test_history_api_error (mock_credit_error_response ):
295+ """Test the history command handling API errors."""
296+
297+ @patch ("aiohttp.ClientSession.get" )
298+ async def run (mock_get ):
299+ mock_get .return_value = mock_credit_error_response
300+
301+ # Run the history command and expect an exception
302+ with pytest .raises (ClientResponseError ):
303+ await history (
304+ address = "0x1234567890123456789012345678901234567890" ,
305+ private_key = None ,
306+ private_key_file = None ,
307+ page_size = 100 ,
308+ page = 1 ,
309+ json = False ,
310+ debug = False ,
311+ )
312+
313+ await run ()
0 commit comments