test_timer.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import asyncio
  2. import time
  3. from unittest.mock import patch
  4. import asyncclick as click
  5. import pytest
  6. from click.testing import CliRunner
  7. from cli.utils.timer import timer
  8. from tests.cli.async_invoke import async_invoke
  9. @click.command()
  10. async def test_command():
  11. with timer():
  12. time.sleep(0.1)
  13. @pytest.mark.asyncio
  14. async def test_timer_measures_time():
  15. runner = CliRunner()
  16. result = await async_invoke(runner, test_command)
  17. output = result.stdout_bytes.decode()
  18. assert "Time taken:" in output
  19. assert "seconds" in output
  20. measured_time = float(output.split(":")[1].split()[0])
  21. assert 0.1 <= measured_time <= 0.2
  22. @click.command()
  23. async def zero_duration_command():
  24. with timer():
  25. pass
  26. @pytest.mark.asyncio
  27. async def test_timer_zero_duration():
  28. runner = CliRunner()
  29. result = await async_invoke(runner, zero_duration_command)
  30. output = result.stdout_bytes.decode()
  31. measured_time = float(output.split(":")[1].split()[0])
  32. assert measured_time >= 0
  33. assert measured_time < 0.1
  34. @click.command()
  35. async def exception_command():
  36. with timer():
  37. raise ValueError("Test exception")
  38. @pytest.mark.asyncio
  39. async def test_timer_with_exception():
  40. runner = CliRunner()
  41. result = await async_invoke(runner, exception_command)
  42. assert result.exit_code != 0
  43. assert isinstance(result.exception, ValueError)
  44. @click.command()
  45. async def async_command():
  46. with timer():
  47. await asyncio.sleep(0.1)
  48. @pytest.mark.asyncio
  49. async def test_timer_with_async_code():
  50. runner = CliRunner()
  51. result = await async_invoke(runner, async_command)
  52. output = result.stdout_bytes.decode()
  53. measured_time = float(output.split(":")[1].split()[0])
  54. assert 0.1 <= measured_time <= 0.2
  55. @click.command()
  56. async def nested_command():
  57. with timer():
  58. time.sleep(0.1)
  59. with timer():
  60. time.sleep(0.1)
  61. @pytest.mark.asyncio
  62. async def test_timer_multiple_nested():
  63. runner = CliRunner()
  64. result = await async_invoke(runner, nested_command)
  65. output = result.stdout_bytes.decode()
  66. assert output.count("Time taken:") == 2
  67. @click.command()
  68. async def mock_time_command():
  69. with timer():
  70. pass
  71. @pytest.mark.asyncio
  72. @patch("time.time")
  73. async def test_timer_with_mock_time(mock_time):
  74. mock_time.side_effect = [0, 1] # Start and end times
  75. runner = CliRunner()
  76. result = await async_invoke(runner, mock_time_command)
  77. output = result.stdout_bytes.decode()
  78. assert "Time taken: 1.00 seconds" in output
  79. @click.command()
  80. async def precision_command():
  81. with timer():
  82. time.sleep(0.1)
  83. @pytest.mark.asyncio
  84. async def test_timer_precision():
  85. runner = CliRunner()
  86. result = await async_invoke(runner, precision_command)
  87. output = result.stdout_bytes.decode()
  88. assert len(output.split(":")[1].split()[0].split(".")[1]) == 2