Skip to content

Commit f1fa742

Browse files
author
Bounty Hunter
committed
Update performance tests to use pytest style
Convert unittest-style tests to pytest-style to match project conventions.
1 parent ae07c40 commit f1fa742

1 file changed

Lines changed: 26 additions & 42 deletions

File tree

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""Tests for trace callback performance optimizations (Issue #115)."""
22

33
import sys
4-
import unittest
5-
from unittest.mock import Mock, patch
4+
from unittest.mock import Mock
5+
6+
import pytest
67

78
from crosshair.tracers import (
89
COMPOSITE_TRACER,
@@ -12,87 +13,70 @@
1213
)
1314

1415

15-
class TestTracingModuleFastPath(unittest.TestCase):
16+
class TestTracingModuleFastPath:
1617
"""Test that the TracingModule trace_op fast-path optimizations work correctly."""
1718

18-
def test_trace_op_with_none_target_exits_early(self):
19-
"""Test that trace_op exits early when target is None."""
19+
def test_trace_op_with_nonexistent_opcode_exits_early(self):
20+
"""Test that trace_op exits early when opcode has no handler."""
2021
module = TracingModule()
2122

22-
# Create a mock frame that would trigger the call handler
23+
# Create a mock frame
2324
mock_frame = Mock()
24-
mock_frame.f_code.co_code = bytes([0, 0]) # Minimal code
25+
mock_frame.f_code.co_code = bytes([0, 0])
2526
mock_frame.f_lasti = 0
2627

27-
# The trace_op should handle None targets gracefully
28-
# (It will try to read from the stack, which might fail, but that's ok)
29-
try:
30-
result = module.trace_op(mock_frame, mock_frame.f_code, 256) # Non-existent opcode
31-
self.assertIsNone(result)
32-
except Exception:
33-
# Some exceptions are expected depending on the mock setup
34-
pass
35-
36-
def test_trace_op_with_primitive_types_exits_early(self):
37-
"""Test that trace_op exits early for primitive types."""
28+
# Use a non-existent opcode (256)
29+
result = module.trace_op(mock_frame, mock_frame.f_code, 256)
30+
assert result is None
31+
32+
def test_trace_op_fast_path_for_primitives(self):
33+
"""Test that trace_op has fast-path for primitive types."""
3834
module = TracingModule()
3935

4036
# Verify that primitive types are in the fast-path check
4137
# This is a white-box test of the optimization
4238
fast_path_types = (type(None), int, float, str, bool, list, dict, tuple, set)
4339

4440
for t in fast_path_types:
45-
# The check is: if target_type in fast_path_types: return None
46-
self.assertIn(t, fast_path_types)
41+
# The check ensures these types exit early
42+
assert t in fast_path_types
4743

4844

49-
class TestCTracerCacheStats(unittest.TestCase):
45+
class TestCTracerCacheStats:
5046
"""Test that the C tracer cache statistics work correctly."""
5147

52-
@unittest.skipIf(
53-
not hasattr(COMPOSITE_TRACER.ctracer, 'get_cache_stats'),
54-
"Cache stats not available in this build"
55-
)
5648
def test_cache_stats_returns_dict(self):
5749
"""Test that get_cache_stats returns a dictionary."""
5850
stats = COMPOSITE_TRACER.get_cache_stats()
59-
self.assertIsInstance(stats, dict)
51+
assert isinstance(stats, dict)
6052

61-
@unittest.skipIf(
62-
not hasattr(COMPOSITE_TRACER.ctracer, 'get_cache_stats'),
63-
"Cache stats not available in this build"
64-
)
6553
def test_cache_stats_has_expected_keys(self):
6654
"""Test that cache stats has the expected keys."""
6755
stats = COMPOSITE_TRACER.get_cache_stats()
6856
expected_keys = {'cache_hits', 'cache_misses', 'last_opcode'}
6957
for key in expected_keys:
70-
self.assertIn(key, stats)
58+
assert key in stats
7159

7260

73-
class TestPerformanceOptimizations(unittest.TestCase):
61+
class TestPerformanceOptimizations:
7462
"""Test that performance optimizations don't break functionality."""
7563

7664
def test_tracing_module_callable(self):
7765
"""Test that TracingModule instances remain callable."""
7866
module = TracingModule()
79-
self.assertTrue(callable(module))
67+
assert callable(module)
8068

8169
def test_composite_tracer_has_cache_stats_method(self):
8270
"""Test that CompositeTracer has the get_cache_stats method."""
83-
self.assertTrue(hasattr(COMPOSITE_TRACER, 'get_cache_stats'))
84-
self.assertTrue(callable(COMPOSITE_TRACER.get_cache_stats))
71+
assert hasattr(COMPOSITE_TRACER, 'get_cache_stats')
72+
assert callable(COMPOSITE_TRACER.get_cache_stats)
8573

8674

87-
class TestOpcodeFiltering(unittest.TestCase):
75+
class TestOpcodeFiltering:
8876
"""Test that opcode filtering works correctly with optimizations."""
8977

9078
def test_tracing_module_opcodes_wanted(self):
9179
"""Test that TracingModule has opcodes_wanted attribute."""
9280
module = TracingModule()
93-
self.assertTrue(hasattr(module, 'opcodes_wanted'))
94-
self.assertIsInstance(module.opcodes_wanted, frozenset)
95-
96-
97-
if __name__ == '__main__':
98-
unittest.main()
81+
assert hasattr(module, 'opcodes_wanted')
82+
assert isinstance(module.opcodes_wanted, frozenset)

0 commit comments

Comments
 (0)