Skip to content

Commit

Permalink
Add tests to check that invalid module specifications fail
Browse files Browse the repository at this point in the history
  • Loading branch information
jtv8 committed Jun 6, 2020
1 parent 3f4fe5d commit 998ed51
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
6 changes: 6 additions & 0 deletions features/examples/modules/invalid_both_defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from wysdom import UserObject, UserProperty


class Person(UserObject):
first_name: str = UserProperty(str)
last_name: str = UserProperty(str, default="", default_function=lambda x: x.first_name)
6 changes: 6 additions & 0 deletions features/examples/modules/invalid_required_and_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from wysdom import UserObject, UserProperty


class Person(UserObject):
first_name: str = UserProperty(str)
last_name: str = UserProperty(str, default="", optional=False)
17 changes: 17 additions & 0 deletions features/invalid.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Feature: Test that invalid module specifications fail

Scenario: Fail if both default and default_function are set

When we try to load the Python module invalid_both_defaults.py
Then a ValueError is raised with text:
"""
Cannot use both default and default_function.
"""

Scenario: Fail if both default is set and optional is set to False

When we try to load the Python module invalid_required_and_default.py
Then a ValueError is raised with text:
"""
Cannot set optional to False if default or default_function are specified.
"""
27 changes: 25 additions & 2 deletions features/steps/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


@given("the Python module {module}.py")
def step_impl(context, module):
def load_python_module(context, module):
spec = importlib.util.spec_from_file_location(
module,
os.path.join(
Expand All @@ -23,7 +23,30 @@ def step_impl(context, module):
globals()[module] = loaded_module


@step(u"the following string, {variable_name}")
@when("we try to load the Python module {module}.py")
def step_impl(context, module):
try:
load_python_module(context, module)
context.load_python_module_error = None
except Exception as e:
context.load_python_module_error = e


@then("a {exception_type} is raised with text")
def step_impl(context, exception_type):
if context.load_python_module_error is None:
raise Exception("No exception was raised.")
if exception_type != context.load_python_module_error.__class__.__name__:
raise Exception(
f"Expected exception type {exception_type}, got {type(context.load_python_module_error)}."
)
if context.text.strip() != str(context.load_python_module_error).strip():
raise Exception(
f"Expected error message '{context.text}', got '{context.load_python_module_error}'."
)


@given(u"the following string, {variable_name}")
def step_impl(context, variable_name):
globals()[variable_name] = context.text

Expand Down

0 comments on commit 998ed51

Please sign in to comment.