File size: 985 Bytes
451dece | 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 | import random as pyrandom
from unittest.mock import patch
from server.seed import apply_seed
def test_apply_seed_returns_provided_value():
assert apply_seed(42) == 42
assert apply_seed(0) == 0
def test_apply_seed_negative_draws_random():
s = apply_seed(-1)
assert isinstance(s, int)
assert 0 <= s < 2**31
def test_apply_seed_none_draws_random():
s = apply_seed(None)
assert isinstance(s, int)
assert 0 <= s < 2**31
def test_apply_seed_seeds_pyrandom_so_repeats_match():
s = apply_seed(123)
a = pyrandom.random()
apply_seed(s)
b = pyrandom.random()
assert a == b
def test_apply_seed_calls_torch_manual_seed():
with patch("server.seed.torch.manual_seed") as m:
apply_seed(99)
m.assert_called_once_with(99)
def test_apply_seed_swallows_mps_failure():
with patch("server.seed._maybe_seed_mps", side_effect=RuntimeError("nope")):
# Should not raise
s = apply_seed(7)
assert s == 7
|