Spaces:
Sleeping
Sleeping
File size: 3,004 Bytes
cb922bf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | """
Property-based tests for EnvState validation.
This module validates EnvState field constraints using property-based testing
to ensure robust validation across a wide range of inputs.
"""
import pytest
from hypothesis import given, strategies as st
from pydantic import ValidationError
from env.config import EnvState
class TestEnvStateValidation:
"""Property 2: EnvState Field Validation"""
@given(
cpu=st.floats(min_value=0.0, max_value=100.0, allow_nan=False, allow_infinity=False),
memory=st.floats(min_value=0.0, max_value=100.0, allow_nan=False, allow_infinity=False),
request_rate=st.floats(min_value=0.0, max_value=1000.0, allow_nan=False, allow_infinity=False),
resources=st.integers(min_value=1, max_value=100)
)
def test_valid_values_create_envstate(self, cpu, memory, request_rate, resources):
"""Test that valid values create EnvState without errors."""
state = EnvState(
cpu=cpu,
memory=memory,
request_rate=request_rate,
resources=resources
)
assert state.cpu == cpu
assert state.memory == memory
assert state.request_rate == request_rate
assert state.resources == resources
@given(cpu=st.floats(min_value=100.01, max_value=200.0, allow_nan=False, allow_infinity=False))
def test_invalid_cpu_raises_validation_error(self, cpu):
"""Test that cpu > 100 raises ValidationError."""
with pytest.raises(ValidationError) as exc_info:
EnvState(cpu=cpu, memory=50.0, request_rate=100.0, resources=3)
assert "cpu" in str(exc_info.value).lower()
@given(memory=st.floats(min_value=100.01, max_value=200.0, allow_nan=False, allow_infinity=False))
def test_invalid_memory_raises_validation_error(self, memory):
"""Test that memory > 100 raises ValidationError."""
with pytest.raises(ValidationError) as exc_info:
EnvState(cpu=50.0, memory=memory, request_rate=100.0, resources=3)
assert "memory" in str(exc_info.value).lower()
@given(resources=st.integers(min_value=-10, max_value=0))
def test_invalid_resources_raises_validation_error(self, resources):
"""Test that resources < 1 raises ValidationError."""
with pytest.raises(ValidationError) as exc_info:
EnvState(cpu=50.0, memory=50.0, request_rate=100.0, resources=resources)
assert "resources" in str(exc_info.value).lower()
@given(request_rate=st.floats(min_value=-100.0, max_value=-0.01, allow_nan=False, allow_infinity=False))
def test_negative_request_rate_raises_validation_error(self, request_rate):
"""Test that negative request_rate raises ValidationError."""
with pytest.raises(ValidationError) as exc_info:
EnvState(cpu=50.0, memory=50.0, request_rate=request_rate, resources=3)
assert "request_rate" in str(exc_info.value).lower()
|