# Copyright (c) 2011-2020 Eric Froemling # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # ----------------------------------------------------------------------------- """Testing dataclassutils functionality.""" from __future__ import annotations from dataclasses import dataclass from typing import TYPE_CHECKING import pytest from efro.dataclassutils import dataclass_assign, dataclass_validate if TYPE_CHECKING: from typing import Optional def test_assign() -> None: """Testing various assignments.""" @dataclass class _TestClass: ival: int = 0 sval: str = '' bval: bool = True fval: float = 1.0 oival: Optional[int] = None osval: Optional[str] = None obval: Optional[bool] = None ofval: Optional[float] = None tclass = _TestClass() class _TestClass2: pass tclass2 = _TestClass2() # Arg types: with pytest.raises(TypeError): dataclass_assign(tclass2, {}) with pytest.raises(TypeError): dataclass_assign(tclass, []) # type: ignore # Invalid attrs. with pytest.raises(AttributeError): dataclass_assign(tclass, {'nonexistent': 'foo'}) # Correct types. dataclass_assign(tclass, { 'ival': 1, 'sval': 'foo', 'bval': True, 'fval': 2.0, }) dataclass_assign(tclass, { 'oival': None, 'osval': None, 'obval': None, 'ofval': None, }) dataclass_assign(tclass, { 'oival': 1, 'osval': 'foo', 'obval': True, 'ofval': 2.0, }) # Type mismatches. with pytest.raises(TypeError): dataclass_assign(tclass, {'ival': 'foo'}) with pytest.raises(TypeError): dataclass_assign(tclass, {'sval': 1}) with pytest.raises(TypeError): dataclass_assign(tclass, {'bval': 2}) with pytest.raises(TypeError): dataclass_assign(tclass, {'oival': 'foo'}) with pytest.raises(TypeError): dataclass_assign(tclass, {'osval': 1}) with pytest.raises(TypeError): dataclass_assign(tclass, {'obval': 2}) with pytest.raises(TypeError): dataclass_assign(tclass, {'ofval': 'blah'}) # More subtle ones (we currently require EXACT type matches) with pytest.raises(TypeError): dataclass_assign(tclass, {'ival': True}) with pytest.raises(TypeError): dataclass_assign(tclass, {'fval': 2}) with pytest.raises(TypeError): dataclass_assign(tclass, {'bval': 1}) with pytest.raises(TypeError): dataclass_assign(tclass, {'ofval': 1}) def test_validate() -> None: """Testing validation.""" @dataclass class _TestClass: ival: int = 0 sval: str = '' bval: bool = True fval: float = 1.0 oival: Optional[int] = None osval: Optional[str] = None obval: Optional[bool] = None ofval: Optional[float] = None # Should pass by default. tclass = _TestClass() dataclass_validate(tclass) # No longer valid. tclass.fval = 1 with pytest.raises(TypeError): dataclass_validate(tclass) # Should pass by default. tclass = _TestClass() dataclass_validate(tclass) # No longer valid. # noinspection PyTypeHints tclass.ival = None # type: ignore with pytest.raises(TypeError): dataclass_validate(tclass)