99
1010
1111@pytest .fixture
12- def opa_filter_generator ():
12+ def opa_filter_factory ():
1313 """Create an OPA instance for testing."""
1414 return Opa (host = "http://localhost:8181" , decision = "stac/filter" )
1515
@@ -24,72 +24,72 @@ def mock_opa_response():
2424
2525
2626@pytest .mark .asyncio
27- async def test_opa_initialization (opa_filter_generator ):
27+ async def test_opa_initialization (opa_filter_factory ):
2828 """Test OPA initialization."""
29- assert opa_filter_generator .host == "http://localhost:8181"
30- assert opa_filter_generator .decision == "stac/filter"
31- assert opa_filter_generator .cache_key == "req.headers.authorization"
32- assert opa_filter_generator .cache_ttl == 5.0
33- assert isinstance (opa_filter_generator .client , AsyncClient )
34- assert opa_filter_generator .cache is not None
29+ assert opa_filter_factory .host == "http://localhost:8181"
30+ assert opa_filter_factory .decision == "stac/filter"
31+ assert opa_filter_factory .cache_key == "req.headers.authorization"
32+ assert opa_filter_factory .cache_ttl == 5.0
33+ assert isinstance (opa_filter_factory .client , AsyncClient )
34+ assert opa_filter_factory .cache is not None
3535
3636
3737@pytest .mark .asyncio
38- async def test_opa_cache_hit (opa_filter_generator , mock_opa_response ):
38+ async def test_opa_cache_hit (opa_filter_factory , mock_opa_response ):
3939 """Test OPA cache hit behavior."""
4040 context = {"req" : {"headers" : {"authorization" : "test-token" }}}
4141
4242 # Mock the OPA response
4343 with patch .object (
44- opa_filter_generator .client , "post" , new_callable = AsyncMock
44+ opa_filter_factory .client , "post" , new_callable = AsyncMock
4545 ) as mock_post :
4646 mock_post .return_value = mock_opa_response
4747
4848 # First call should hit OPA
49- result = await opa_filter_generator (context )
49+ result = await opa_filter_factory (context )
5050 assert result == "collection = 'test'"
5151 assert mock_post .call_count == 1
5252
5353 # Second call should use cache
54- result = await opa_filter_generator (context )
54+ result = await opa_filter_factory (context )
5555 assert result == "collection = 'test'"
5656 assert mock_post .call_count == 1 # Still 1, no new call made
5757
5858
5959@pytest .mark .asyncio
60- async def test_opa_cache_miss (opa_filter_generator , mock_opa_response ):
60+ async def test_opa_cache_miss (opa_filter_factory , mock_opa_response ):
6161 """Test OPA cache miss behavior."""
6262 context = {"req" : {"headers" : {"authorization" : "test-token" }}}
6363
6464 with patch .object (
65- opa_filter_generator .client , "post" , new_callable = AsyncMock
65+ opa_filter_factory .client , "post" , new_callable = AsyncMock
6666 ) as mock_post :
6767 mock_post .return_value = mock_opa_response
6868
6969 # First call with token1
70- result = await opa_filter_generator (context )
70+ result = await opa_filter_factory (context )
7171 assert result == "collection = 'test'"
7272 assert mock_post .call_count == 1
7373
7474 # Call with different token should miss cache
7575 context ["req" ]["headers" ]["authorization" ] = "different-token"
76- result = await opa_filter_generator (context )
76+ result = await opa_filter_factory (context )
7777 assert result == "collection = 'test'"
7878 assert mock_post .call_count == 2 # New call made
7979
8080
8181@pytest .mark .asyncio
82- async def test_opa_error_handling (opa_filter_generator ):
82+ async def test_opa_error_handling (opa_filter_factory ):
8383 """Test OPA error handling."""
8484 context = {"req" : {"headers" : {"authorization" : "test-token" }}}
8585
8686 with patch .object (
87- opa_filter_generator .client , "post" , new_callable = AsyncMock
87+ opa_filter_factory .client , "post" , new_callable = AsyncMock
8888 ) as mock_post :
8989 # Create a mock response that raises an exception on raise_for_status
9090 error_response = MagicMock (spec = Response )
9191 error_response .raise_for_status .side_effect = Exception ("Internal server error" )
9292 mock_post .return_value = error_response
9393
9494 with pytest .raises (Exception ):
95- await opa_filter_generator (context )
95+ await opa_filter_factory (context )
0 commit comments