|
| 1 | +"""Tests for ArkivModule utility methods.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from unittest.mock import MagicMock |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from arkiv.module import ArkivModule |
| 9 | +from arkiv.module_async import AsyncArkivModule |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class TestToBlocks: |
| 15 | + """Test cases for to_blocks time conversion method.""" |
| 16 | + |
| 17 | + @pytest.fixture |
| 18 | + def mock_module(self): |
| 19 | + """Create a mock ArkivModule for testing.""" |
| 20 | + module = MagicMock(spec=ArkivModule) |
| 21 | + # Mock the get_block_timing response |
| 22 | + module.get_block_timing.return_value = {"duration": 2} |
| 23 | + # Bind the actual to_blocks method to our mock |
| 24 | + module.to_blocks = ArkivModule.to_blocks.__get__(module) |
| 25 | + return module |
| 26 | + |
| 27 | + def test_to_blocks_from_seconds(self, mock_module): |
| 28 | + """Test conversion from seconds to blocks.""" |
| 29 | + # With 2 second block time, 120 seconds = 60 blocks |
| 30 | + result = mock_module.to_blocks(seconds=120) |
| 31 | + assert result == 60 |
| 32 | + mock_module.get_block_timing.assert_called_once() |
| 33 | + |
| 34 | + def test_to_blocks_from_minutes(self, mock_module): |
| 35 | + """Test conversion from minutes to blocks.""" |
| 36 | + # 2 minutes = 120 seconds = 60 blocks |
| 37 | + result = mock_module.to_blocks(minutes=2) |
| 38 | + assert result == 60 |
| 39 | + |
| 40 | + def test_to_blocks_from_hours(self, mock_module): |
| 41 | + """Test conversion from hours to blocks.""" |
| 42 | + # 1 hour = 3600 seconds = 1800 blocks |
| 43 | + result = mock_module.to_blocks(hours=1) |
| 44 | + assert result == 1800 |
| 45 | + |
| 46 | + def test_to_blocks_from_days(self, mock_module): |
| 47 | + """Test conversion from days to blocks.""" |
| 48 | + # 1 day = 86400 seconds = 43200 blocks |
| 49 | + result = mock_module.to_blocks(days=1) |
| 50 | + assert result == 43200 |
| 51 | + |
| 52 | + def test_to_blocks_mixed_units(self, mock_module): |
| 53 | + """Test conversion with mixed time units.""" |
| 54 | + # 1 day + 2 hours + 30 minutes + 60 seconds |
| 55 | + # = 86400 + 7200 + 1800 + 60 = 95460 seconds = 47730 blocks |
| 56 | + result = mock_module.to_blocks(days=1, hours=2, minutes=30, seconds=60) |
| 57 | + assert result == 47730 |
| 58 | + |
| 59 | + def test_to_blocks_caches_block_duration(self, mock_module): |
| 60 | + """Test that block duration is cached after first call.""" |
| 61 | + mock_module.to_blocks(seconds=2) |
| 62 | + mock_module.to_blocks(seconds=4) |
| 63 | + # Should only call get_block_timing once |
| 64 | + assert mock_module.get_block_timing.call_count == 1 |
| 65 | + |
| 66 | + def test_to_blocks_default_values(self, mock_module): |
| 67 | + """Test with all default values (should return 0).""" |
| 68 | + result = mock_module.to_blocks() |
| 69 | + assert result == 0 |
| 70 | + |
| 71 | + def test_to_blocks_rounding_down(self, mock_module): |
| 72 | + """Test that partial blocks are rounded down.""" |
| 73 | + # 125 seconds with 2 second blocks = 62.5 blocks, should round to 62 |
| 74 | + result = mock_module.to_blocks(seconds=125) |
| 75 | + assert result == 62 |
| 76 | + |
| 77 | + |
| 78 | +class TestToBlocksAsync: |
| 79 | + """Test cases for async to_blocks time conversion method.""" |
| 80 | + |
| 81 | + @pytest.fixture |
| 82 | + async def mock_module_async(self): |
| 83 | + """Create a mock AsyncArkivModule for testing.""" |
| 84 | + module = MagicMock(spec=AsyncArkivModule) |
| 85 | + # Mock the async get_block_timing response |
| 86 | + async def mock_get_block_timing(): |
| 87 | + return {"duration": 2} |
| 88 | + |
| 89 | + module.get_block_timing = mock_get_block_timing |
| 90 | + # Bind the actual to_blocks method to our mock |
| 91 | + module.to_blocks = AsyncArkivModule.to_blocks.__get__(module) |
| 92 | + return module |
| 93 | + |
| 94 | + @pytest.mark.asyncio |
| 95 | + async def test_async_to_blocks_from_seconds(self, mock_module_async): |
| 96 | + """Test async conversion from seconds to blocks.""" |
| 97 | + # With 2 second block time, 120 seconds = 60 blocks |
| 98 | + result = await mock_module_async.to_blocks(seconds=120) |
| 99 | + assert result == 60 |
| 100 | + |
| 101 | + @pytest.mark.asyncio |
| 102 | + async def test_async_to_blocks_from_minutes(self, mock_module_async): |
| 103 | + """Test async conversion from minutes to blocks.""" |
| 104 | + # 2 minutes = 120 seconds = 60 blocks |
| 105 | + result = await mock_module_async.to_blocks(minutes=2) |
| 106 | + assert result == 60 |
| 107 | + |
| 108 | + @pytest.mark.asyncio |
| 109 | + async def test_async_to_blocks_from_hours(self, mock_module_async): |
| 110 | + """Test async conversion from hours to blocks.""" |
| 111 | + # 1 hour = 3600 seconds = 1800 blocks |
| 112 | + result = await mock_module_async.to_blocks(hours=1) |
| 113 | + assert result == 1800 |
| 114 | + |
| 115 | + @pytest.mark.asyncio |
| 116 | + async def test_async_to_blocks_from_days(self, mock_module_async): |
| 117 | + """Test async conversion from days to blocks.""" |
| 118 | + # 1 day = 86400 seconds = 43200 blocks |
| 119 | + result = await mock_module_async.to_blocks(days=1) |
| 120 | + assert result == 43200 |
| 121 | + |
| 122 | + @pytest.mark.asyncio |
| 123 | + async def test_async_to_blocks_mixed_units(self, mock_module_async): |
| 124 | + """Test async conversion with mixed time units.""" |
| 125 | + # 1 day + 2 hours + 30 minutes + 60 seconds |
| 126 | + # = 86400 + 7200 + 1800 + 60 = 95460 seconds = 47730 blocks |
| 127 | + result = await mock_module_async.to_blocks( |
| 128 | + days=1, hours=2, minutes=30, seconds=60 |
| 129 | + ) |
| 130 | + assert result == 47730 |
| 131 | + |
| 132 | + @pytest.mark.asyncio |
| 133 | + async def test_async_to_blocks_default_values(self, mock_module_async): |
| 134 | + """Test async with all default values (should return 0).""" |
| 135 | + result = await mock_module_async.to_blocks() |
| 136 | + assert result == 0 |
| 137 | + |
| 138 | + @pytest.mark.asyncio |
| 139 | + async def test_async_to_blocks_rounding_down(self, mock_module_async): |
| 140 | + """Test that partial blocks are rounded down in async version.""" |
| 141 | + # 125 seconds with 2 second blocks = 62.5 blocks, should round to 62 |
| 142 | + result = await mock_module_async.to_blocks(seconds=125) |
| 143 | + assert result == 62 |
0 commit comments