class TestSetEnumCustomField(TestCase): enum_option = f.enum_option(name="My enum option") custom_field = f.custom_field(name="My custom field", enum_options=[enum_option]) task = f.task(custom_fields=[custom_field]) task_with_field_set = f.task( custom_fields=[ f.custom_field( name="My custom field", enum_options=[enum_option], enum_value=enum_option, ) ] ) def setUp(self) -> None: self.client = create_autospec(Client) def test_success(self) -> None: action = SetEnumCustomField("My custom field", "My enum option") action(self.task, self.client) self.client.set_enum_custom_field.assert_called_once_with( self.task, self.custom_field, self.enum_option ) def test_already_set(self) -> None: action = SetEnumCustomField("My custom field", "My enum option") action(self.task_with_field_set, self.client) self.client.set_enum_custom_field.assert_not_called() def test_missing_enum_option(self) -> None: action = SetEnumCustomField("My custom field", "Other enum option") with self.assertLogs(_logger, logging.WARNING) as logs: action(self.task_with_field_set, self.client) self.assertListEqual( logs.output, [ "WARNING:archie.actions:CustomField(custom-field-gid) " "has no enum option 'Other enum option'" ], ) self.client.set_enum_custom_field.assert_not_called() def test_missing_custom_field(self) -> None: action = SetEnumCustomField("Other custom field", "My enum option") with self.assertLogs(_logger, logging.WARNING) as logs: action(self.task, self.client) self.assertListEqual( logs.output, [ "WARNING:archie.actions:Task(task-gid) " "has no custom field 'Other custom field'" ], ) self.client.set_enum_custom_field.assert_not_called()
def test_wrong_custom_field_type(self) -> None: self.expect_no_context( custom_fields=[ f.custom_field(name="Custom Field", enum_options=None) ], expected_warning="Unable to find enum custom field 'Custom Field'", )
class TestHasUnsetEnum(TestCase): predicate = HasUnsetEnum("My custom field") client = create_autospec(Client) custom_field = f.custom_field(name="My custom field", resource_subtype="enum", enum_value=None) task = f.task(custom_fields=[custom_field]) def test_has_no_value(self) -> None: self.assertTrue(self.predicate(self.task, self.client)) def test_has_any_value(self) -> None: task = f.task(custom_fields=[ f.custom_field( name="My custom field", resource_subtype="enum", enum_value=f.enum_option(name="My enum option"), ) ]) self.assertFalse(self.predicate(task, self.client)) def test_wrong_custom_field(self) -> None: predicate = HasUnsetEnum("Other custom field") self.assertFalse(predicate(self.task, self.client)) def test_missing_custom_field(self) -> None: task = f.task(custom_fields=[]) self.assertFalse(self.predicate(task, self.client)) @patch("archie.predicates._for_at_least") def test_call(self, for_at_least_mock: Mock) -> None: predicate = HasUnsetEnum("My custom field", for_at_least="2d") for_at_least_mock.return_value = return_sentinel = object() self.client.stories_by_task.return_value = story_sentinel = object() result = predicate(self.task, self.client) self.assertIs(result, return_sentinel) self.client.stories_by_task.assert_called_once_with(self.task) for_at_least_mock.assert_called_once_with(self.task, story_sentinel, predicate._story_matcher, timedelta(days=2)) def test_story_matcher(self) -> None: matcher = HasUnsetEnum("My custom field")._story_matcher self.assertTrue( matcher( f.story( resource_subtype="enum_custom_field_changed", custom_field=self.custom_field, ))) self.assertFalse( matcher( f.story( resource_subtype="enum_custom_field_changed", custom_field=f.custom_field(name="Other custom field"), ))) self.assertFalse(matcher(f.story(resource_subtype="unknown")))
def test_unset_enum_value(self) -> None: task = f.task(custom_fields=[ f.custom_field(name="My custom field", resource_subtype="enum", enum_value=None) ]) predicate = HasEnumValue("My custom field", "My enum option") self.assertFalse(predicate(task, self.client))
def test_missing_custom_field(self) -> None: tasks = [ f.task(gid="1", custom_fields=[f.custom_field(name="Other custom field")]), f.task(gid="2", custom_fields=[self.custom_field(3)]), ] sorted_tasks = self.sorter.sort(tasks) ids = [t.gid for t in sorted_tasks] self.assertListEqual(ids, ["2", "1"])
def test_has_any_value(self) -> None: task = f.task(custom_fields=[ f.custom_field( name="My custom field", resource_subtype="enum", enum_value=f.enum_option(name="My enum option"), ) ]) self.assertFalse(self.predicate(task, self.client))
def test_matching_stage(self) -> None: set_custom_field = f.custom_field(name="Custom Field", enum_options=enum_options, enum_value=enum_options[0]) task = f.task(custom_fields=[set_custom_field]) stage, context = self.manager.get_current_stage(task) expected_context = _EnumCustomFieldWorkflowGetStageContext( set_custom_field, enum_options) self.assertIs(stage, stages[0]) self.assertEqual(expected_context, context)
def test_set_enum_custom_field(self) -> None: custom_field = f.custom_field() enum_option = f.enum_option() self.inner_mock.tasks.update.return_value = None self.client.set_enum_custom_field(self.task, custom_field, enum_option) self.inner_mock.tasks.update.assert_called_once_with( self.task.gid, {"custom_fields": { custom_field.gid: enum_option.gid }})
def test_missing_stage(self) -> None: self.expect_no_context( custom_fields=[ f.custom_field( name="Custom Field", enum_options=enum_options, enum_value=f.enum_option(name="C"), ) ], expected_warning="Unable to find stage 'C'", )
def test_unset_enum_option(self) -> None: tasks = [ f.task( gid="1", custom_fields=[f.custom_field(name="My custom field", enum_value=None)], ), f.task(gid="2", custom_fields=[self.custom_field("B")]), ] sorted_tasks = self.sorter.sort(tasks) ids = [t.gid for t in sorted_tasks] self.assertListEqual(ids, ["2", "1"])
def test_story_matcher(self) -> None: matcher = HasUnsetEnum("My custom field")._story_matcher self.assertTrue( matcher( f.story( resource_subtype="enum_custom_field_changed", custom_field=self.custom_field, ))) self.assertFalse( matcher( f.story( resource_subtype="enum_custom_field_changed", custom_field=f.custom_field(name="Other custom field"), ))) self.assertFalse(matcher(f.story(resource_subtype="unknown")))
from archie.asana.client import Client from archie.asana.models import CustomField from archie.predicates import Predicate from archie.workflows import EnumCustomFieldWorkflow, WorkflowStage from archie.workflows.enum import ( _EnumCustomFieldWorkflowGetStageContext, _EnumCustomFieldWorkflowSetStageContext, _EnumCustomFieldWorkflowStageManager, ) predicate_a = create_autospec(Predicate) predicate_b = create_autospec(Predicate) stages = [WorkflowStage("A", predicate_a), WorkflowStage("B", predicate_b)] enum_options = [f.enum_option(name="A"), f.enum_option(name="B")] custom_field = f.custom_field(name="Custom Field", enum_options=enum_options) class TestEnumCustomFieldWorkflow(TestCase): def setUp(self) -> None: self.manager = _EnumCustomFieldWorkflowStageManager( "Custom Field", stages) def expect_no_context(self, custom_fields: List[CustomField], expected_warning: str) -> None: task = f.task(custom_fields=custom_fields) warning = self.manager.get_current_stage(task) self.assertEqual(expected_warning, warning) def test_missing_custom_field(self) -> None: self.expect_no_context(
def custom_field(value: Optional[float]) -> CustomField: return f.custom_field(name="My custom field", number_value=value)
def custom_field(value: str) -> CustomField: return f.custom_field( name="My custom field", enum_value=f.enum_option(name=value) )