|
3 | 3 | from ignite.base import Serializable |
4 | 4 |
|
5 | 5 |
|
| 6 | +class ExampleSerializable(Serializable): |
| 7 | + _state_dict_all_req_keys = ("a", "b") |
| 8 | + _state_dict_one_of_opt_keys = (("c", "d"), ("e", "f")) |
| 9 | + |
| 10 | + |
6 | 11 | def test_state_dict(): |
7 | 12 | s = Serializable() |
8 | 13 | with pytest.raises(NotImplementedError): |
9 | 14 | s.state_dict() |
10 | 15 |
|
11 | | - |
12 | 16 | def test_load_state_dict(): |
13 | | - s = Serializable() |
14 | | - s.load_state_dict({}) |
| 17 | + |
| 18 | + s = ExampleSerializable() |
| 19 | + with pytest.raises(TypeError, match=r"Argument state_dict should be a dictionary"): |
| 20 | + s.load_state_dict("abc") |
| 21 | + |
| 22 | + with pytest.raises(ValueError, match=r"is absent in provided state_dict"): |
| 23 | + s.load_state_dict({}) |
| 24 | + |
| 25 | + with pytest.raises(ValueError, match=r"is absent in provided state_dict"): |
| 26 | + s.load_state_dict({"a": 1}) |
| 27 | + |
| 28 | + with pytest.raises(ValueError, match=r"state_dict should contain only one of"): |
| 29 | + s.load_state_dict({"a": 1, "b": 2}) |
| 30 | + |
| 31 | + with pytest.raises(ValueError, match=r"state_dict should contain only one of"): |
| 32 | + s.load_state_dict({"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}) |
| 33 | + |
| 34 | + with pytest.raises(ValueError, match=r"state_dict should contain only one of"): |
| 35 | + s.load_state_dict({"a": 1, "b": 2, "c": 3, "e": 5, "f": 5}) |
| 36 | + |
| 37 | + s.state_dict_user_keys.append("alpha") |
| 38 | + with pytest.raises(ValueError, match=r"Required user state attribute"): |
| 39 | + s.load_state_dict({"a": 1, "b": 2, "c": 3, "e": 4}) |
0 commit comments