|
| 1 | +""" |
| 2 | +Integration tests for freezegun compatibility. |
| 3 | +
|
| 4 | +These tests verify that Qase reporters work correctly when tests use freezegun |
| 5 | +to mock time. The issue is that freezegun mocks time.time(), which affects |
| 6 | +execution timestamps, but Qase API requires real timestamps. |
| 7 | +""" |
| 8 | + |
| 9 | +import time |
| 10 | +import pytest |
| 11 | + |
| 12 | +try: |
| 13 | + from freezegun import freeze_time |
| 14 | + FREEZEGUN_AVAILABLE = True |
| 15 | +except ImportError: |
| 16 | + FREEZEGUN_AVAILABLE = False |
| 17 | + |
| 18 | +from qase.commons.models.result import Execution, Result |
| 19 | +from qase.commons.models.step import Step, StepType, StepTextData |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.skipif(not FREEZEGUN_AVAILABLE, reason="freezegun not installed") |
| 23 | +class TestFreezegunIntegration: |
| 24 | + """Test that Qase works correctly with freezegun""" |
| 25 | + |
| 26 | + def test_execution_timestamps_with_freezegun(self): |
| 27 | + """ |
| 28 | + Test that Execution timestamps are real time even when freezegun is active. |
| 29 | + |
| 30 | + This reproduces the issue from GitHub issue #415 where users get |
| 31 | + "Data is invalid" errors when using freezegun with Qase steps. |
| 32 | + """ |
| 33 | + # Get current real time |
| 34 | + real_time_before = time.time() |
| 35 | + |
| 36 | + # Freeze time to 2023-02-01 |
| 37 | + with freeze_time("2023-02-01"): |
| 38 | + # time.time() returns frozen time |
| 39 | + frozen_time = time.time() |
| 40 | + assert frozen_time < 1700000000, "Time should be frozen in the past" |
| 41 | + |
| 42 | + # Create execution (this is what happens when test starts) |
| 43 | + execution = Execution() |
| 44 | + |
| 45 | + # Small delay to ensure end_time > start_time |
| 46 | + time.sleep(0.1) # This is also mocked by freezegun |
| 47 | + |
| 48 | + # Complete execution |
| 49 | + execution.complete() |
| 50 | + |
| 51 | + # Verify that execution times are real (not frozen) |
| 52 | + # They should be close to current time, not 2023-02-01 |
| 53 | + assert execution.start_time > frozen_time + 60000000, \ |
| 54 | + f"start_time {execution.start_time} should be real time, not frozen time {frozen_time}" |
| 55 | + |
| 56 | + assert execution.end_time > frozen_time + 60000000, \ |
| 57 | + f"end_time {execution.end_time} should be real time, not frozen time {frozen_time}" |
| 58 | + |
| 59 | + assert execution.start_time >= real_time_before, \ |
| 60 | + f"start_time {execution.start_time} should be >= {real_time_before}" |
| 61 | + |
| 62 | + # Duration should be positive |
| 63 | + assert execution.duration >= 0, \ |
| 64 | + f"duration {execution.duration} should be non-negative" |
| 65 | + |
| 66 | + # Verify timestamps would be accepted by Qase API |
| 67 | + # (API requires timestamps >= current time - some tolerance) |
| 68 | + current_timestamp = 1760383058 # Example from error message |
| 69 | + # Our timestamps should be much greater than this example from Oct 2025 |
| 70 | + # Since we're running tests later |
| 71 | + assert execution.start_time > 1700000000, \ |
| 72 | + f"Timestamp {execution.start_time} should be valid for Qase API" |
| 73 | + |
| 74 | + def test_step_execution_with_freezegun(self): |
| 75 | + """ |
| 76 | + Test that Step execution timestamps are real time when freezegun is active. |
| 77 | + """ |
| 78 | + real_time_before = time.time() |
| 79 | + |
| 80 | + with freeze_time("2023-02-01"): |
| 81 | + frozen_time = time.time() |
| 82 | + |
| 83 | + # Create a step (this is what happens with qase.step()) |
| 84 | + step = Step( |
| 85 | + step_type=StepType.TEXT, |
| 86 | + id="test-step-1", |
| 87 | + data=StepTextData(action="Test action") |
| 88 | + ) |
| 89 | + |
| 90 | + # Simulate some work |
| 91 | + time.sleep(0.01) |
| 92 | + |
| 93 | + # Complete the step |
| 94 | + step.execution.complete() |
| 95 | + |
| 96 | + # Verify timestamps are real |
| 97 | + assert step.execution.start_time > frozen_time + 60000000, \ |
| 98 | + "step start_time should be real time" |
| 99 | + |
| 100 | + assert step.execution.end_time > frozen_time + 60000000, \ |
| 101 | + "step end_time should be real time" |
| 102 | + |
| 103 | + assert step.execution.start_time >= real_time_before, \ |
| 104 | + "step start_time should be >= real time before test" |
| 105 | + |
| 106 | + assert step.execution.duration >= 0, \ |
| 107 | + "step duration should be non-negative" |
| 108 | + |
| 109 | + def test_result_with_steps_freezegun(self): |
| 110 | + """ |
| 111 | + Test complete Result with steps when freezegun is active. |
| 112 | + |
| 113 | + This simulates the exact scenario from the user's issue. |
| 114 | + """ |
| 115 | + with freeze_time("2023-02-01"): |
| 116 | + frozen_time = time.time() |
| 117 | + |
| 118 | + # Create result |
| 119 | + result = Result(title="Test with freezegun", signature="test-sig") |
| 120 | + |
| 121 | + # Simulate test execution with step |
| 122 | + step = Step( |
| 123 | + step_type=StepType.TEXT, |
| 124 | + id="step-1", |
| 125 | + data=StepTextData(action="Doing something") |
| 126 | + ) |
| 127 | + |
| 128 | + time.sleep(0.01) |
| 129 | + step.execution.complete() |
| 130 | + |
| 131 | + result.steps.append(step) |
| 132 | + result.execution.set_status("passed") |
| 133 | + result.execution.complete() |
| 134 | + |
| 135 | + # Verify all timestamps are real (not frozen) |
| 136 | + assert result.execution.start_time > frozen_time + 60000000, \ |
| 137 | + "result start_time should not be frozen" |
| 138 | + |
| 139 | + assert result.execution.end_time > frozen_time + 60000000, \ |
| 140 | + "result end_time should not be frozen" |
| 141 | + |
| 142 | + assert step.execution.start_time > frozen_time + 60000000, \ |
| 143 | + "step start_time should not be frozen" |
| 144 | + |
| 145 | + assert step.execution.end_time > frozen_time + 60000000, \ |
| 146 | + "step end_time should not be frozen" |
| 147 | + |
| 148 | + # All durations should be valid |
| 149 | + assert result.execution.duration >= 0 |
| 150 | + assert step.execution.duration >= 0 |
| 151 | + |
| 152 | + def test_multiple_freezegun_contexts(self): |
| 153 | + """ |
| 154 | + Test that timestamps work correctly across multiple freezegun contexts. |
| 155 | + """ |
| 156 | + executions = [] |
| 157 | + |
| 158 | + # First frozen context |
| 159 | + with freeze_time("2023-01-01"): |
| 160 | + exec1 = Execution() |
| 161 | + time.sleep(0.01) |
| 162 | + exec1.complete() |
| 163 | + executions.append(exec1) |
| 164 | + |
| 165 | + # Second frozen context (different time) |
| 166 | + with freeze_time("2023-06-01"): |
| 167 | + exec2 = Execution() |
| 168 | + time.sleep(0.01) |
| 169 | + exec2.complete() |
| 170 | + executions.append(exec2) |
| 171 | + |
| 172 | + # Outside frozen context |
| 173 | + exec3 = Execution() |
| 174 | + time.sleep(0.01) |
| 175 | + exec3.complete() |
| 176 | + executions.append(exec3) |
| 177 | + |
| 178 | + # All executions should have real timestamps |
| 179 | + # and should be close to each other (not separated by months) |
| 180 | + for i, exec in enumerate(executions): |
| 181 | + assert exec.start_time > 1700000000, \ |
| 182 | + f"Execution {i} should have real timestamp" |
| 183 | + assert exec.duration >= 0, \ |
| 184 | + f"Execution {i} should have valid duration" |
| 185 | + |
| 186 | + # Timestamps should be sequential (each one after the previous) |
| 187 | + for i in range(len(executions) - 1): |
| 188 | + assert executions[i].end_time <= executions[i + 1].start_time + 1, \ |
| 189 | + f"Execution {i+1} should start after execution {i} ends" |
| 190 | + |
0 commit comments