Skip to content

Commit

Permalink
Move more tests over to hypothesis
Browse files Browse the repository at this point in the history
  • Loading branch information
shezadkhan137 committed Jun 9, 2017
1 parent d36ac64 commit 56dd9f9
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions tests/test_requires.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@

from required import Requires, R, RequirementError
from hypothesis import given
from hypothesis.strategies import data, integers
from hypothesis.strategies import (
data, integers, one_of, booleans,
floats, complex_numbers, tuples,
none, lists, sets, fixed_dictionaries,
characters, text, binary,
)

all_types = (
integers(), none(),
floats(), complex_numbers(), tuples(),
characters(), text(), binary(), booleans(),
)


class TestRequires(object):
Expand All @@ -17,15 +28,17 @@ def test_value_no_dependency_does_not_raise_error(self, draw):
data = {"x": x, "y": y}
requires.validate(data)

def test_exists_dependency_raises_requirements_error(self):
@given(one_of(*all_types))
def test_exists_dependency_raises_requirements_error(self, item):
requires = Requires("x", "y")
data = {"x": 1}
data = {"x": item}
with pytest.raises(RequirementError):
requires._validate("x", data)

def test_triple_dependency_raises_correct_error(self):
@given(one_of(*all_types))
def test_triple_dependency_raises_correct_error(self, item):
requires = Requires("x", "y") + Requires("y", "z")
data = {"x": 1}
data = {"x": item}

with pytest.raises(RequirementError):
requires._validate("x", data)
Expand All @@ -35,42 +48,45 @@ def test_triple_dependency_raises_correct_error(self):

requires._validate("z", data)

def test_multiple_dependencies_requires_all(self):
@given(one_of(*all_types), one_of(*all_types), one_of(*all_types))
def test_multiple_dependencies_requires_all(self, x, y, z):
requires = Requires("x", "y") + Requires("x", "z")
data = {"x": 1, "y": 2}
data = {"x": x, "y": y}

with pytest.raises(RequirementError):
requires._validate("x", data)

data = {"x": 1, "z": 2}
data = {"x": x, "z": z}
with pytest.raises(RequirementError):
requires._validate("x", data)

data = {"x": 1, "z": 2, "y": 2}
data = {"x": x, "z": y, "y": z}
requires._validate("x", data)

def test_circular_dependency_requires_both(self):
@given(one_of(*all_types), one_of(*all_types))
def test_circular_dependency_requires_both(self, x, y):
requires = Requires("x", "y") + Requires("y", "x")

data = {"x": 1}
data = {"x": x}
with pytest.raises(RequirementError):
requires._validate("x", data)

data = {"y": 1}
data = {"y": y}
with pytest.raises(RequirementError):
requires._validate("y", data)

data = {"x": 1, "y": 2}
data = {"x": x, "y": y}
requires._validate("y", data)
requires._validate("x", data)

def test_simple_expression_dependency_equal(self):
requires = Requires("x", R("y") == 1)
data = {"x": 1, "y": 2}
@given(one_of(*all_types), integers())
def test_simple_expression_dependency_equal(self, x, y):
requires = Requires("x", R("y") == y)
data = {"x": x, "y": y + 1}
with pytest.raises(RequirementError):
requires._validate("x", data)

data = {"x": 1, "y": 1}
data = {"x": x, "y": y}
requires._validate("x", data)

@given(data())
Expand All @@ -93,18 +109,19 @@ def test_simple_expression_dependency_greater_or_equal(self, draw):
data = {"y": x}
requires._validate("y", data)

def test_multi_expression_dependency_correctly_validates(self):
requires = Requires("x", R("y") >= 1) + Requires("z", R("y") <= 1)
@given(integers(), integers(), integers(), integers())
def test_multi_expression_dependency_correctly_validates(self, x, y, z, gte):
requires = Requires("x", R("y") >= gte) + Requires("z", R("y") <= gte)

data = {"x": 1, "y": 2, "z": 1}
data = {"x": x, "y": gte + 1, "z": z}

with pytest.raises(RequirementError):
requires._validate("z", data)

requires._validate("x", data)
requires._validate("y", data)

data = {"x": 1, "y": 1, "z": 1}
data = {"x": gte, "y": gte, "z": gte}
requires._validate("z", data)

def test_multi_r_in_expression_correctly_validates(self):
Expand Down

0 comments on commit 56dd9f9

Please sign in to comment.