Skip to content

Commit

Permalink
Create tests for generate_field_key
Browse files Browse the repository at this point in the history
  • Loading branch information
istride committed Dec 17, 2024
1 parent c7ae679 commit ec6efa6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
15 changes: 9 additions & 6 deletions src/rpft/rapidpro/models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rpft.rapidpro.utils import generate_new_uuid


FIELD_KEY_MAX_LENGTH = 36
RESERVED_FIELD_KEYS = [
"created_by",
"created_on",
Expand Down Expand Up @@ -91,19 +92,21 @@ def render(self):


def generate_field_key(field_name):
field_key = field_name.strip().lower().replace(" ", "_")
key = field_name.strip().lower().replace(" ", "_")

if not len(field_key) <= 36:
if len(key) > FIELD_KEY_MAX_LENGTH:
raise RapidProActionError(
"Contact field keys should be no longer than 36 characters."
"Contact field key length limit exceeded",
{"length": len(key), "limit": FIELD_KEY_MAX_LENGTH, "key": key}
)

if not re.search("[A-Za-z]", field_key):
if not re.search("[A-Za-z]", key):
raise RapidProActionError(
"Contact field keys should contain at least one letter."
"Contact field key without letter characters detected",
{"key": key, "name": field_name}
)

return field_key
return key


def generate_field_name(field_name):
Expand Down
38 changes: 29 additions & 9 deletions tests/test_actions.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
import unittest
from unittest import TestCase

from rpft.rapidpro.models.actions import EnterFlowAction
from rpft.rapidpro.models.common import generate_field_key
from rpft.rapidpro.models.exceptions import RapidProActionError


class TestActions(unittest.TestCase):
def setUp(self) -> None:
pass
class TestActions(TestCase):

def test_enter_flow_node(self):
enter_flow_node = EnterFlowAction(flow_name="test_flow", flow_uuid="fake-uuid")
render_output = enter_flow_node.render()
self.assertEqual(render_output["type"], "enter_flow")
self.assertEqual(render_output["flow"]["name"], "test_flow")
self.assertEqual(render_output["flow"]["uuid"], "fake-uuid")
out = EnterFlowAction(flow_name="test_flow", flow_uuid="fake-uuid").render()

self.assertEqual(out["type"], "enter_flow")
self.assertEqual(out["flow"]["name"], "test_flow")
self.assertEqual(out["flow"]["uuid"], "fake-uuid")


class TestFieldKeyGenerator(TestCase):

def test_generate_key_from_field_name(self):
self.assertEqual(
generate_field_key(" Field Name "),
"field_name",
)

def test_generate_keys_up_to_max_length_limit(self):
self.assertIsNotNone(generate_field_key("x" * 36))

def test_fail_if_max_key_length_exceeded(self):
with self.assertRaises(RapidProActionError):
generate_field_key("z" * 37)

def test_fail_if_key_does_not_contain_letters(self):
with self.assertRaises(RapidProActionError):
generate_field_key("123")

0 comments on commit ec6efa6

Please sign in to comment.