|
| 1 | +""" |
| 2 | +Test the _deprecation module. |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from pvlib import _deprecation |
| 8 | +from .conftest import fail_on_pvlib_version |
| 9 | + |
| 10 | +import warnings |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.xfail(strict=True, |
| 14 | + reason='fail_on_pvlib_version should cause test to fail') |
| 15 | +@fail_on_pvlib_version('0.0') |
| 16 | +def test_fail_on_pvlib_version(): |
| 17 | + pass # pragma: no cover |
| 18 | + |
| 19 | + |
| 20 | +@fail_on_pvlib_version('100000.0') |
| 21 | +def test_fail_on_pvlib_version_pass(): |
| 22 | + pass |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.xfail(strict=True, reason='ensure that the test is called') |
| 26 | +@fail_on_pvlib_version('100000.0') |
| 27 | +def test_fail_on_pvlib_version_fail_in_test(): |
| 28 | + raise Exception |
| 29 | + |
| 30 | + |
| 31 | +# set up to test using fixtures with function decorated with |
| 32 | +# conftest.fail_on_pvlib_version |
| 33 | +@pytest.fixture |
| 34 | +def some_data(): |
| 35 | + return "some data" |
| 36 | + |
| 37 | + |
| 38 | +def alt_func(*args): |
| 39 | + return args |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def deprec_func(): |
| 44 | + return _deprecation.deprecated( |
| 45 | + "350.8", alternative="alt_func", name="deprec_func", removal="350.9" |
| 46 | + )(alt_func) |
| 47 | + |
| 48 | + |
| 49 | +@fail_on_pvlib_version('350.9') |
| 50 | +def test_use_fixture_with_decorator(some_data, deprec_func): |
| 51 | + # test that the correct data is returned by the some_data fixture |
| 52 | + assert some_data == "some data" |
| 53 | + with pytest.warns(_deprecation.pvlibDeprecationWarning): |
| 54 | + # test for custom deprecation warning provided by pvlib |
| 55 | + deprec_func(some_data) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture |
| 59 | +def renamed_kwarg_func(): |
| 60 | + """Returns a function decorated by renamed_kwarg_warning. |
| 61 | + This function is called 'func' and has a docstring equal to 'docstring'. |
| 62 | + """ |
| 63 | + |
| 64 | + @_deprecation.renamed_kwarg_warning( |
| 65 | + "0.1.0", "old_kwarg", "new_kwarg", "0.2.0" |
| 66 | + ) |
| 67 | + def func(new_kwarg): |
| 68 | + """docstring""" |
| 69 | + return new_kwarg |
| 70 | + |
| 71 | + return func |
| 72 | + |
| 73 | + |
| 74 | +def test_renamed_kwarg_warning(renamed_kwarg_func): |
| 75 | + # assert decorated function name and docstring are unchanged |
| 76 | + assert renamed_kwarg_func.__name__ == "func" |
| 77 | + assert renamed_kwarg_func.__doc__ == "docstring" |
| 78 | + |
| 79 | + # assert no warning is raised when using the new kwarg |
| 80 | + with warnings.catch_warnings(): |
| 81 | + warnings.simplefilter("error") |
| 82 | + assert renamed_kwarg_func(new_kwarg=1) == 1 # as keyword argument |
| 83 | + assert renamed_kwarg_func(1) == 1 # as positional argument |
| 84 | + |
| 85 | + # assert a warning is raised when using the old kwarg |
| 86 | + with pytest.warns(Warning, match="Parameter 'old_kwarg' has been renamed"): |
| 87 | + assert renamed_kwarg_func(old_kwarg=1) == 1 |
| 88 | + |
| 89 | + # assert an error is raised when using both the old and new kwarg |
| 90 | + with pytest.raises(ValueError, match="they refer to the same parameter."): |
| 91 | + renamed_kwarg_func(old_kwarg=1, new_kwarg=2) |
| 92 | + |
| 93 | + # assert when not providing any of them |
| 94 | + with pytest.raises( |
| 95 | + TypeError, match="missing 1 required positional argument" |
| 96 | + ): |
| 97 | + renamed_kwarg_func() |
0 commit comments