Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content dispatcher #67

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
7 changes: 7 additions & 0 deletions tests/input/content-dispatcher/content-cycle-data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"ID","properties.1","properties.2","content.1","content.2","content.3","content.4"
"a","male","happy","a1","a2","a3",
"b","female","happy","b2","b3","b3","b4"
"c",,"happy","c1","c2",,"c4"
"d","male","sad",,"d2","d3","d4"
"e","female","sad","e1","e2","e3","e4"
"f",,"sad","f1","f2",,
6 changes: 6 additions & 0 deletions tests/input/content-dispatcher/content-index.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"type","sheet_name","data_sheet","data_row_id","new_name","data_model","template_arguments","status"
"data_sheet","content-cycle-data",,,"content_cycles","ContentModel",,
"template_definition","dispatcher_main",,,,,"property_names|cycles;sheet",
"template_definition","dispatcher_branch",,,,,"property_names|cycles|pidx",
"template_definition","dispatcher_cycle",,,,,,"draft"
"create_flow","dispatcher_main",,,,,"@fields.gender;@fields.mood|content_cycles",
7 changes: 7 additions & 0 deletions tests/input/content-dispatcher/contentmodel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from parsers.creation.datarowmodel import DataRowModel
from parsers.common.rowparser import ParserModel
from typing import List

class ContentModel(DataRowModel):
properties: List[str]
content: List[str]
10 changes: 10 additions & 0 deletions tests/input/content-dispatcher/dispatcher_branch.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"row_id","type","from","condition","condition_var","condition_type","condition_name","save_name","include_if","loop_variable","message_text","template_arguments"
,"send_message","start",,,,,,,,"Field depth: {{pidx}}",
,"send_message",,,,,,,FALSE,,"{{cycles|groupby(""properties.""+pidx|string)}}",
,"begin_block",,,,,,,"{@pidx<property_names|length@}",,,
"1","split_by_value",,,,,,,,,"{{property_names[pidx]}}",
,"begin_for",,,,,,,,"group","{@cycles|groupby(""properties.""+pidx|string)@}",
,"insert_as_block",1,"{{group.0}}",,,,,,,"dispatcher_branch","{@property_names,group.1,pidx+1@}"
,"end_for",,,,,,,,,,
,"end_block",,,,,,,,,,
,"send_message",,,,,,,"{@pidx>=property_names|length@}",,"Content cycle: {{cycles.0.content}}",
3 changes: 3 additions & 0 deletions tests/input/content-dispatcher/dispatcher_main.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"row_id","type","from","condition","condition_var","condition_type","condition_name","save_name","include_if","loop_variable","message_text","template_arguments"
,"send_message","start",,,,,,,,"Main dispatcher",
,"insert_as_block",,,,,,,,,"dispatcher_branch","{@[property_names,cycles.values()|list,0]@}"
30 changes: 30 additions & 0 deletions tests/test_dispatcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import unittest
import json

from parsers.creation.contentindexparser import ContentIndexParser
from parsers.sheets.csv_sheet_reader import CSVSheetReader
from tests.utils import traverse_flow, Context

class TestParsing(unittest.TestCase):

def compare_messages(self, render_output, flow_name, messages_exp, context=None):
flow_found = False
for flow in render_output["flows"]:
if flow["name"] == flow_name:
flow_found = True
actions = traverse_flow(flow, context or Context())
actions_exp = list(zip(['send_msg']*len(messages_exp), messages_exp))
self.assertEqual(actions, actions_exp)
if not flow_found:
self.assertTrue(False, msg=f'Flow with name "{flow_name}" does not exist in output.')

def test_parsing(self):
self.maxDiff = None
sheet_reader = CSVSheetReader('tests/input/content-dispatcher/content-index.csv')
ci_parser = ContentIndexParser(sheet_reader, user_data_model_module_name='tests.input.content-dispatcher.contentmodel')
container = ci_parser.parse_all_flows()
render_output = container.render()
messages_exp = [
'Nice to see you :)Bye :)',
]
self.compare_messages(render_output, 'dispatcher_main', messages_exp, Context(variables={'@fields.mood':'happy'}))