def test_it_uses_the_ordered_policy_if_smart_ordering_is_true(self, Policy):
        self.defaults["smart_ordering"] = True
        actions_list = MagicMock()

        with patch.object(TestableCommand, "collect_all_actions", return_value=actions_list):
            sut = TestableCommand(**self.defaults)
            sut.execute()
            Policy.assert_called_with(actions_list)
            Policy.return_value.execute.assert_called_once_with()
    def test_it_uses_the_basic_policy_if_smart_ordering_is_false(self, Policy):
        self.defaults['smart_ordering'] = False
        actions_list = MagicMock()

        with patch.object(TestableCommand,
                          'collect_all_actions',
                          return_value=actions_list):
            sut = TestableCommand(**self.defaults)
            sut.execute()
            Policy.assert_called_with(actions_list)
            Policy.return_value.execute.assert_called_once_with()
    def test_it_wraps_the_basic_policy_in_a_transaction_policy_if_configured(
        self, BasicSyncPolicy, TransactionSyncPolicy
    ):
        self.defaults["smart_ordering"] = False
        self.defaults["as_transaction"] = True
        actions_list = MagicMock()

        with patch.object(TestableCommand, "collect_all_actions", return_value=actions_list):
            sut = TestableCommand(**self.defaults)
            sut.execute()
            TransactionSyncPolicy.assert_called_with(BasicSyncPolicy.return_value)
            TransactionSyncPolicy.return_value.execute.assert_called_once_with()
    def test_command_raises_error_if_not_CSV_file(self, SupportedFileChecker):
        SupportedFileChecker.is_valid.return_value = False
        files_mock = MagicMock()
        sut = TestableCommand(
            **{
                'files': [files_mock],
                'file_name_regex': DEFAULT_FILE_REGEX,
                'create_external_system': MagicMock(),
                'smart_ordering': MagicMock(),
                'as_transaction': MagicMock()
            })

        with self.assertRaises(CommandError):
            sut.execute()
        SupportedFileChecker.is_valid.assert_called_with(files_mock)
    def test_it_wraps_the_basic_policy_in_a_transaction_policy_if_configured(
            self, BasicSyncPolicy, TransactionSyncPolicy):
        self.defaults['smart_ordering'] = False
        self.defaults['as_transaction'] = True
        actions_list = MagicMock()

        with patch.object(TestableCommand,
                          'collect_all_actions',
                          return_value=actions_list):
            sut = TestableCommand(**self.defaults)
            sut.execute()
            TransactionSyncPolicy.assert_called_with(
                BasicSyncPolicy.return_value)
            TransactionSyncPolicy.return_value \
                .execute.assert_called_once_with()
    def test_command_raises_error_if_not_CSV_file(self, SupportedFileChecker):
        SupportedFileChecker.is_valid.return_value = False
        files_mock = MagicMock()
        sut = TestableCommand(
            **{
                "files": [files_mock],
                "file_name_regex": DEFAULT_FILE_REGEX,
                "create_external_system": MagicMock(),
                "smart_ordering": MagicMock(),
                "as_transaction": MagicMock(),
            }
        )

        with self.assertRaises(CommandError):
            sut.execute()
        SupportedFileChecker.is_valid.assert_called_with(files_mock)