-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite 'update_expiration_time' from JS to Python
- Loading branch information
Showing
3 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |