Skip to content

Commit

Permalink
Router: Raise error in case of invalid argument set
Browse files Browse the repository at this point in the history
  • Loading branch information
geoo89 committed Feb 19, 2025
1 parent 9c024c4 commit 13bec15
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
15 changes: 7 additions & 8 deletions src/rpft/rapidpro/models/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def from_dict(data, exits):
elif data["type"] == "switch":
return SwitchRouter.from_dict(data, exits)
else:
raise ValueError("Router data has invalid router type.")
raise RapidProRouterError("Router data has invalid router type.")

def _get_result_name_and_categories_from_data(data, exits):
categories = [
Expand Down Expand Up @@ -150,7 +150,7 @@ def from_dict(data, exits):
if category.uuid == data["default_category_uuid"]
]
if not default_categories:
raise ValueError("Default category uuid does not match any category.")
raise RapidProRouterError("Default category uuid does not match any category.")
no_response_category = None
no_response_category_id = None
wait_timeout = None
Expand All @@ -165,7 +165,7 @@ def from_dict(data, exits):
if category.uuid == no_response_category_id
]
if not no_response_categories:
raise ValueError(
raise RapidProRouterError(
"No Response category uuid does not match any category."
)
no_response_category = no_response_categories[0]
Expand Down Expand Up @@ -200,7 +200,6 @@ def create_case(self, comparison_type, arguments, category):

def generate_category_name(self, comparison_arguments):
# Auto-generate a category name that is guaranteed to be unique
# TODO: Write tests for this
category_name = "_".join([str(a).title() for a in comparison_arguments])
category_name = category_name or "Yes"
while self._get_category_or_none(category_name):
Expand Down Expand Up @@ -452,7 +451,7 @@ def from_dict(data, exits):
"""
matching_exits = [exit for exit in exits if exit.uuid == data["exit_uuid"]]
if not matching_exits:
raise ValueError("RouterCategory with no matching exit.")
raise RapidProRouterError("RouterCategory with no matching exit.")
return RouterCategory(
name=data["name"], uuid=data["uuid"], exit=matching_exits[0]
)
Expand Down Expand Up @@ -539,10 +538,10 @@ def from_dict(data):

def validate(self):
if self.type not in RouterCase.TEST_VALIDATIONS:
raise ValueError(f'Invalid router test type: "{self.type}"')
raise RapidProRouterError(f'Invalid router test type: "{self.type}"')
if not RouterCase.TEST_VALIDATIONS[self.type](self.arguments):
print(
f"Warning: Invalid number of arguments {len(self.arguments)} or blank "
raise RapidProRouterError(
f"Invalid number of arguments {len(self.arguments)} or blank "
f'arguments for router test type "{self.type}"'
)

Expand Down
3 changes: 2 additions & 1 deletion tests/test_routers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest

from rpft.rapidpro.models.exceptions import RapidProRouterError
from rpft.rapidpro.models.routers import SwitchRouter, RandomRouter


Expand Down Expand Up @@ -178,7 +179,7 @@ def test_no_args_tests(self):

def test_invalid_test(self):
switch_router = SwitchRouter(operand="@fields.field")
with self.assertRaises(ValueError):
with self.assertRaises(RapidProRouterError):
switch_router.add_choice(
"@fields.field",
"has_junk",
Expand Down

0 comments on commit 13bec15

Please sign in to comment.