Skip to content

Commit

Permalink
Rewrite 'update_expiration_time' from JS to Python
Browse files Browse the repository at this point in the history
  • Loading branch information
istride committed Sep 6, 2023
1 parent 5109418 commit cddff65
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/parenttext_pipeline/pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from rpft.converters import create_flows
from rapidpro_abtesting.main import apply_abtests

from parenttext_pipeline.steps import update_expiration_time


@dataclass(kw_only=True)
class Config:
Expand Down Expand Up @@ -143,14 +145,12 @@ def run_pipeline(
output_file_name_1_2 = source_file_name + "_1_2_modified_expiration_times"
output_path_1_2 = os.path.join(outputpath, output_file_name_1_2 + ".json")

subprocess.run([
"node",
"./node_modules/@idems/idems-chatbot-tools/update_expiration_time.js",
update_expiration_time(
input_path_1_2,
special_expiration,
default_expiration,
output_path_1_2,
])
special_expiration,
output_path_1_2
)

print("Step 1 complete, created " + source_file_name + " and modified expiration times")

Expand Down
26 changes: 26 additions & 0 deletions src/parenttext_pipeline/steps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json


def update_expiration_time(in_fp, default, specifics_fp, out_fp):
with open(specifics_fp, 'r') as specifics_json:
specifics = json.load(specifics_json)

with open(in_fp, "r") as in_json:
org = json.load(in_json)

for flow in org.get("flows", []):
set_expiration(flow, default, specifics)

with open(out_fp, "w") as out_json:
json.dump(org, out_json)


def set_expiration(flow, default, specifics={}):
expiration = specifics.get(flow["name"], default)

flow["expire_after_minutes"] = expiration

if "expires" in flow.get("metadata", {}):
flow["metadata"]["expires"] = expiration

return flow
56 changes: 56 additions & 0 deletions tests/test_set_expiration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from unittest import TestCase

from parenttext_pipeline.steps import set_expiration


class TestUpdateExpirationTime(TestCase):

def test_must_set_expiration_to_default(self):
default = 1440
flow = {
"name": "flow_1",
"expire_after_minutes": 0,
}
updated = set_expiration(flow, default)
self.assertEqual(updated["expire_after_minutes"], 1440)
self.assertFalse("metadata" in updated)

def test_must_set_metadata_expires_if_exists(self):
default = 360
flow = {
"name": "flow_1",
"expire_after_minutes": 0,
"metadata": {
"expires": 0
}
}
updated = set_expiration(flow, default)
self.assertEqual(updated["metadata"]["expires"], 360)
self.assertEqual(updated["expire_after_minutes"], 360)

def test_must_not_create_metadata_expires(self):
default = 480
flow = {
"name": "flow_1",
"expire_after_minutes": 0,
"metadata": {}
}
updated = set_expiration(flow, default)
self.assertFalse("expires" in updated["metadata"])
self.assertEqual(updated["expire_after_minutes"], 480)

def test_must_allow_per_flow_expiration_times(self):
default = 120
specifics = {
"flow_1": 240,
}
flow = {
"name": "flow_1",
"expire_after_minutes": 0,
"metadata": {
"expires": 0
},
}
updated = set_expiration(flow, default, specifics)
self.assertEqual(updated["expire_after_minutes"], 240)
self.assertEqual(updated["metadata"]["expires"], 240)

0 comments on commit cddff65

Please sign in to comment.