-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1/17] Improve commons module doctests
Merge pull request #1033 from openfisca/doctests/commons
- Loading branch information
Showing
16 changed files
with
278 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import pytest | ||
|
||
from openfisca_core.commons import Dummy | ||
|
||
|
||
def test_dummy_deprecation(): | ||
"""Dummy throws a deprecation warning when instantiated.""" | ||
|
||
with pytest.warns(DeprecationWarning): | ||
assert Dummy() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import numpy | ||
import pytest | ||
from numpy.testing import assert_array_equal | ||
|
||
from openfisca_core import commons | ||
|
||
|
||
def test_apply_thresholds_when_several_inputs(): | ||
"""Makes a choice for any given input.""" | ||
|
||
input_ = numpy.array([4, 5, 6, 7, 8, 9, 10]) | ||
thresholds = [5, 7, 9] | ||
choices = [10, 15, 20, 25] | ||
|
||
result = commons.apply_thresholds(input_, thresholds, choices) | ||
|
||
assert_array_equal(result, [10, 10, 15, 15, 20, 20, 25]) | ||
|
||
|
||
def test_apply_thresholds_when_too_many_thresholds(): | ||
"""Raises an AssertionError when thresholds > choices.""" | ||
|
||
input_ = numpy.array([6]) | ||
thresholds = [5, 7, 9, 11] | ||
choices = [10, 15, 20] | ||
|
||
with pytest.raises(AssertionError): | ||
assert commons.apply_thresholds(input_, thresholds, choices) | ||
|
||
|
||
def test_apply_thresholds_when_too_many_choices(): | ||
"""Raises an AssertionError when thresholds < choices - 1.""" | ||
|
||
input_ = numpy.array([6]) | ||
thresholds = [5, 7] | ||
choices = [10, 15, 20, 25] | ||
|
||
with pytest.raises(AssertionError): | ||
assert commons.apply_thresholds(input_, thresholds, choices) | ||
|
||
|
||
def test_concat_when_this_is_array_not_str(): | ||
"""Casts ``this`` to ``str`` when it is a numpy array other than string.""" | ||
|
||
this = numpy.array([1, 2]) | ||
that = numpy.array(["la", "o"]) | ||
|
||
result = commons.concat(this, that) | ||
|
||
assert_array_equal(result, ["1la", "2o"]) | ||
|
||
|
||
def test_concat_when_that_is_array_not_str(): | ||
"""Casts ``that`` to ``str`` when it is a numpy array other than string.""" | ||
|
||
this = numpy.array(["ho", "cha"]) | ||
that = numpy.array([1, 2]) | ||
|
||
result = commons.concat(this, that) | ||
|
||
assert_array_equal(result, ["ho1", "cha2"]) | ||
|
||
|
||
def test_concat_when_args_not_str_array_like(): | ||
"""Raises a TypeError when args are not a string array-like object.""" | ||
|
||
this = (1, 2) | ||
that = (3, 4) | ||
|
||
with pytest.raises(TypeError): | ||
commons.concat(this, that) | ||
|
||
|
||
def test_switch_when_values_are_empty(): | ||
"""Raises an AssertionError when the values are empty.""" | ||
|
||
conditions = [1, 1, 1, 2] | ||
value_by_condition = {} | ||
|
||
with pytest.raises(AssertionError): | ||
assert commons.switch(conditions, value_by_condition) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import numpy | ||
from numpy.testing import assert_array_equal | ||
|
||
from openfisca_core import commons | ||
|
||
|
||
def test_average_rate_when_varying_is_zero(): | ||
"""Yields infinity when the varying gross income crosses zero.""" | ||
|
||
target = numpy.array([1, 2, 3]) | ||
varying = [0, 0, 0] | ||
|
||
result = commons.average_rate(target, varying) | ||
|
||
assert_array_equal(result, [- numpy.inf, - numpy.inf, - numpy.inf]) | ||
|
||
|
||
def test_marginal_rate_when_varying_is_zero(): | ||
"""Yields infinity when the varying gross income crosses zero.""" | ||
|
||
target = numpy.array([1, 2, 3]) | ||
varying = numpy.array([0, 0, 0]) | ||
|
||
result = commons.marginal_rate(target, varying) | ||
|
||
assert_array_equal(result, [numpy.inf, numpy.inf]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.