def test_retry_failed_logs_in_archive(self, admin_client):
        failed_logs = TriggerLogArchive.objects.bulk_create([
            make_trigger_log(
                is_archived=True,
                state=TRIGGER_LOG_STATE['FAILED'],
                record_id=i,
                action=action,
            ) for i, action in enumerate(TRIGGER_LOG_ACTION.values())
        ])
        succeeded = make_trigger_log(is_archived=True,
                                     state=TRIGGER_LOG_STATE['SUCCESS'])
        succeeded.save()

        admin_client.post(
            self.admin_changelist_url(TriggerLogArchive),
            data=self.action_post_data(
                admin.TriggerLogAdmin.retry_failed_logs_action,
                TriggerLogArchive.objects.all()),
        )

        for failed_log in failed_logs:
            failed_log.refresh_from_db()
            assert failed_log.state == TRIGGER_LOG_STATE['REQUEUED']
            new_trigger_log = TriggerLog.objects.get(
                table_name=failed_log.table_name,
                record_id=failed_log.record_id,
                action=failed_log.action,
            )
            assert new_trigger_log.state == TRIGGER_LOG_STATE['NEW']
        succeeded.refresh_from_db()
        assert succeeded.state == TRIGGER_LOG_STATE['SUCCESS']
    def test_ignore_failed_logs(self, admin_client):
        failed_logs = TriggerLog.objects.bulk_create([
            make_trigger_log(
                state=TRIGGER_LOG_STATE['FAILED'],
                record_id=i,
                action=action,
            ) for i, action in enumerate(TRIGGER_LOG_ACTION.values())
        ])
        succeeded = make_trigger_log(state=TRIGGER_LOG_STATE['SUCCESS'])
        succeeded.save()

        admin_client.post(
            self.admin_changelist_url(TriggerLog),
            data=self.action_post_data(
                admin.TriggerLogAdmin.ignore_failed_logs_action,
                TriggerLog.objects.all()),
        )

        for failed_log in failed_logs:
            failed_log.refresh_from_db()
            assert failed_log.state == TRIGGER_LOG_STATE['IGNORED']
        succeeded.refresh_from_db()
        assert succeeded.state == TRIGGER_LOG_STATE['SUCCESS']
Exemplo n.º 3
0
    def test_retry_failed_logs(self, admin_client, set_write_mode_merge):
        failed_logs = TriggerLog.objects.bulk_create([
            make_trigger_log(
                state=TRIGGER_LOG_STATE["FAILED"],
                record_id=i,
                action=action,
            ) for i, action in enumerate(TRIGGER_LOG_ACTION.values())
        ])
        succeeded = make_trigger_log(state=TRIGGER_LOG_STATE["SUCCESS"])
        succeeded.save()

        admin_client.post(
            self.admin_changelist_url(TriggerLog),
            data=self.action_post_data(
                admin.TriggerLogAdmin.retry_failed_logs_action,
                TriggerLog.objects.all()),
        )

        for failed_log in failed_logs:
            failed_log.refresh_from_db()
            assert failed_log.state == TRIGGER_LOG_STATE["NEW"]
        succeeded.refresh_from_db()
        assert succeeded.state == TRIGGER_LOG_STATE["SUCCESS"]
Exemplo n.º 4
0
class TestAdminActions:
    @staticmethod
    def admin_changelist_url(model):
        return reverse(
            'admin:{meta.app_label}_{meta.model_name}_changelist'.format(
                meta=model._meta))

    @staticmethod
    def action_post_data(action, queryset):
        return {
            'action': action.__name__,
            '_selected_action': queryset.values_list('pk', flat=True)
        }

    def test_ignore_failed_logs(self, admin_client):
        failed_logs = TriggerLog.objects.bulk_create([
            make_trigger_log(
                state=TRIGGER_LOG_STATE['FAILED'],
                record_id=i,
                action=action,
            ) for i, action in enumerate(TRIGGER_LOG_ACTION.values())
        ])
        succeeded = make_trigger_log(state=TRIGGER_LOG_STATE['SUCCESS'])
        succeeded.save()

        admin_client.post(
            self.admin_changelist_url(TriggerLog),
            data=self.action_post_data(
                admin.TriggerLogAdmin.ignore_failed_logs_action,
                TriggerLog.objects.all()),
        )

        for failed_log in failed_logs:
            failed_log.refresh_from_db()
            assert failed_log.state == TRIGGER_LOG_STATE['IGNORED']
        succeeded.refresh_from_db()
        assert succeeded.state == TRIGGER_LOG_STATE['SUCCESS']

    def test_retry_failed_logs(self, admin_client, set_write_mode_merge):
        failed_logs = TriggerLog.objects.bulk_create([
            make_trigger_log(
                state=TRIGGER_LOG_STATE['FAILED'],
                record_id=i,
                action=action,
            ) for i, action in enumerate(TRIGGER_LOG_ACTION.values())
        ])
        succeeded = make_trigger_log(state=TRIGGER_LOG_STATE['SUCCESS'])
        succeeded.save()

        admin_client.post(
            self.admin_changelist_url(TriggerLog),
            data=self.action_post_data(
                admin.TriggerLogAdmin.retry_failed_logs_action,
                TriggerLog.objects.all()),
        )

        for failed_log in failed_logs:
            failed_log.refresh_from_db()
            assert failed_log.state == TRIGGER_LOG_STATE['NEW']
        succeeded.refresh_from_db()
        assert succeeded.state == TRIGGER_LOG_STATE['SUCCESS']

    @pytest.mark.parametrize('log_action', TRIGGER_LOG_ACTION.values())
    def test_retry_failed_logs_ordered_write(self, log_action, admin_client,
                                             set_write_mode_ordered,
                                             hc_capture_stored_procedures):

        assert get_unique_connection_write_mode(
        ) == WriteAlgorithm.ORDERED_WRITES

        testrecord = NumberModel.objects.create()

        failed_log = make_trigger_log(
            state=TRIGGER_LOG_STATE['FAILED'],
            table_name='number_object__c',
            record_id=testrecord.id,
            action=log_action,
        )
        failed_log.save()

        succeeded = make_trigger_log(state=TRIGGER_LOG_STATE['SUCCESS'])
        succeeded.save()

        qs = TriggerLog.objects.all()
        assert qs.count() == 2
        assert set(qs.values_list('state', flat=True)) == {
            TRIGGER_LOG_STATE['SUCCESS'],
            TRIGGER_LOG_STATE['FAILED'],
        }

        admin_client.post(
            self.admin_changelist_url(TriggerLog),
            data=self.action_post_data(
                admin.TriggerLogAdmin.retry_failed_logs_action,
                TriggerLog.objects.all()),
        )

        if log_action == 'DELETE':
            assert qs.count() == 2
            assert set(qs.values_list('state', flat=True)) == {
                TRIGGER_LOG_STATE['SUCCESS'],
                TRIGGER_LOG_STATE['NEW'],
            }
        else:
            assert qs.count() == 3
            assert set(qs.values_list('state', flat=True)) == {
                TRIGGER_LOG_STATE['SUCCESS'],
                TRIGGER_LOG_STATE['REQUEUED'],
                TRIGGER_LOG_STATE['NEW'],
            }

        succeeded.refresh_from_db()
        assert succeeded.state == TRIGGER_LOG_STATE['SUCCESS']

    def test_retry_failed_logs_in_archive(self, admin_client,
                                          set_write_mode_merge):
        failed_logs = TriggerLogArchive.objects.bulk_create([
            make_trigger_log(
                is_archived=True,
                state=TRIGGER_LOG_STATE['FAILED'],
                record_id=i,
                action=action,
            ) for i, action in enumerate(TRIGGER_LOG_ACTION.values())
        ])
        succeeded = make_trigger_log(is_archived=True,
                                     state=TRIGGER_LOG_STATE['SUCCESS'])
        succeeded.save()

        admin_client.post(
            self.admin_changelist_url(TriggerLogArchive),
            data=self.action_post_data(
                admin.TriggerLogAdmin.retry_failed_logs_action,
                TriggerLogArchive.objects.all()),
        )

        for failed_log in failed_logs:
            failed_log.refresh_from_db()
            assert failed_log.state == TRIGGER_LOG_STATE['REQUEUED']
            new_trigger_log = TriggerLog.objects.get(
                table_name=failed_log.table_name,
                record_id=failed_log.record_id,
                action=failed_log.action,
            )
            assert new_trigger_log.state == TRIGGER_LOG_STATE['NEW']
        succeeded.refresh_from_db()
        assert succeeded.state == TRIGGER_LOG_STATE['SUCCESS']

    @pytest.mark.parametrize('log_action', TRIGGER_LOG_ACTION.values())
    def test_retry_failed_logs_in_archive_ordered_write(
            self, log_action, admin_client, set_write_mode_ordered,
            hc_capture_stored_procedures):

        assert get_unique_connection_write_mode(
        ) == WriteAlgorithm.ORDERED_WRITES

        testrecord = NumberModel.objects.create()

        failed_log = make_trigger_log(
            is_archived=True,
            state=TRIGGER_LOG_STATE['FAILED'],
            table_name='number_object__c',
            record_id=testrecord.id,
            action=log_action,
        )
        failed_log.save()

        succeeded = make_trigger_log(is_archived=True,
                                     state=TRIGGER_LOG_STATE['SUCCESS'])
        succeeded.save()

        assert TriggerLog.objects.count() == 0
        assert TriggerLogArchive.objects.count() == 2
        assert set(TriggerLogArchive.objects.values_list(
            'state', flat=True)) == {
                TRIGGER_LOG_STATE['SUCCESS'],
                TRIGGER_LOG_STATE['FAILED'],
            }

        admin_client.post(
            self.admin_changelist_url(TriggerLogArchive),
            data=self.action_post_data(
                admin.TriggerLogAdmin.retry_failed_logs_action,
                TriggerLogArchive.objects.all()),
        )

        assert TriggerLog.objects.get().state == TRIGGER_LOG_STATE['NEW']

        assert TriggerLogArchive.objects.count() == 2
        assert set(TriggerLogArchive.objects.values_list(
            'state', flat=True)) == {
                TRIGGER_LOG_STATE['SUCCESS'],
                TRIGGER_LOG_STATE['REQUEUED'],
            }

        succeeded.refresh_from_db()
        assert succeeded.state == TRIGGER_LOG_STATE['SUCCESS']
Exemplo n.º 5
0
class TestAdminActions:
    @staticmethod
    def admin_changelist_url(model):
        return reverse(
            "admin:{meta.app_label}_{meta.model_name}_changelist".format(
                meta=model._meta))

    @staticmethod
    def action_post_data(action, queryset):
        return {
            "action": action.__name__,
            "_selected_action": queryset.values_list("pk", flat=True),
        }

    def test_ignore_failed_logs(self, admin_client):
        failed_logs = TriggerLog.objects.bulk_create([
            make_trigger_log(
                state=TRIGGER_LOG_STATE["FAILED"],
                record_id=i,
                action=action,
            ) for i, action in enumerate(TRIGGER_LOG_ACTION.values())
        ])
        succeeded = make_trigger_log(state=TRIGGER_LOG_STATE["SUCCESS"])
        succeeded.save()

        admin_client.post(
            self.admin_changelist_url(TriggerLog),
            data=self.action_post_data(
                admin.TriggerLogAdmin.ignore_failed_logs_action,
                TriggerLog.objects.all(),
            ),
        )

        for failed_log in failed_logs:
            failed_log.refresh_from_db()
            assert failed_log.state == TRIGGER_LOG_STATE["IGNORED"]
        succeeded.refresh_from_db()
        assert succeeded.state == TRIGGER_LOG_STATE["SUCCESS"]

    def test_retry_failed_logs(self, admin_client, set_write_mode_merge):
        failed_logs = TriggerLog.objects.bulk_create([
            make_trigger_log(
                state=TRIGGER_LOG_STATE["FAILED"],
                record_id=i,
                action=action,
            ) for i, action in enumerate(TRIGGER_LOG_ACTION.values())
        ])
        succeeded = make_trigger_log(state=TRIGGER_LOG_STATE["SUCCESS"])
        succeeded.save()

        admin_client.post(
            self.admin_changelist_url(TriggerLog),
            data=self.action_post_data(
                admin.TriggerLogAdmin.retry_failed_logs_action,
                TriggerLog.objects.all()),
        )

        for failed_log in failed_logs:
            failed_log.refresh_from_db()
            assert failed_log.state == TRIGGER_LOG_STATE["NEW"]
        succeeded.refresh_from_db()
        assert succeeded.state == TRIGGER_LOG_STATE["SUCCESS"]

    @pytest.mark.parametrize("log_action", TRIGGER_LOG_ACTION.values())
    def test_retry_failed_logs_ordered_write(
        self,
        log_action,
        admin_client,
        set_write_mode_ordered,
        hc_capture_stored_procedures,
    ):

        assert get_unique_connection_write_mode(
        ) == WriteAlgorithm.ORDERED_WRITES

        testrecord = NumberModel.objects.create()

        failed_log = make_trigger_log(
            state=TRIGGER_LOG_STATE["FAILED"],
            table_name="number_object__c",
            record_id=testrecord.id,
            action=log_action,
        )
        failed_log.save()

        succeeded = make_trigger_log(state=TRIGGER_LOG_STATE["SUCCESS"])
        succeeded.save()

        qs = TriggerLog.objects.all()
        assert qs.count() == 2
        assert set(qs.values_list("state", flat=True)) == {
            TRIGGER_LOG_STATE["SUCCESS"],
            TRIGGER_LOG_STATE["FAILED"],
        }

        admin_client.post(
            self.admin_changelist_url(TriggerLog),
            data=self.action_post_data(
                admin.TriggerLogAdmin.retry_failed_logs_action,
                TriggerLog.objects.all()),
        )

        if log_action == "DELETE":
            assert qs.count() == 2
            assert set(qs.values_list("state", flat=True)) == {
                TRIGGER_LOG_STATE["SUCCESS"],
                TRIGGER_LOG_STATE["NEW"],
            }
        else:
            assert qs.count() == 3
            assert set(qs.values_list("state", flat=True)) == {
                TRIGGER_LOG_STATE["SUCCESS"],
                TRIGGER_LOG_STATE["REQUEUED"],
                TRIGGER_LOG_STATE["NEW"],
            }

        succeeded.refresh_from_db()
        assert succeeded.state == TRIGGER_LOG_STATE["SUCCESS"]

    def test_retry_failed_logs_in_archive(self, admin_client,
                                          set_write_mode_merge):
        failed_logs = TriggerLogArchive.objects.bulk_create([
            make_trigger_log(
                is_archived=True,
                state=TRIGGER_LOG_STATE["FAILED"],
                record_id=i,
                action=action,
            ) for i, action in enumerate(TRIGGER_LOG_ACTION.values())
        ])
        succeeded = make_trigger_log(is_archived=True,
                                     state=TRIGGER_LOG_STATE["SUCCESS"])
        succeeded.save()

        admin_client.post(
            self.admin_changelist_url(TriggerLogArchive),
            data=self.action_post_data(
                admin.TriggerLogAdmin.retry_failed_logs_action,
                TriggerLogArchive.objects.all(),
            ),
        )

        for failed_log in failed_logs:
            failed_log.refresh_from_db()
            assert failed_log.state == TRIGGER_LOG_STATE["REQUEUED"]
            new_trigger_log = TriggerLog.objects.get(
                table_name=failed_log.table_name,
                record_id=failed_log.record_id,
                action=failed_log.action,
            )
            assert new_trigger_log.state == TRIGGER_LOG_STATE["NEW"]
        succeeded.refresh_from_db()
        assert succeeded.state == TRIGGER_LOG_STATE["SUCCESS"]

    @pytest.mark.parametrize("log_action", TRIGGER_LOG_ACTION.values())
    def test_retry_failed_logs_in_archive_ordered_write(
        self,
        log_action,
        admin_client,
        set_write_mode_ordered,
        hc_capture_stored_procedures,
    ):

        assert get_unique_connection_write_mode(
        ) == WriteAlgorithm.ORDERED_WRITES

        testrecord = NumberModel.objects.create()

        failed_log = make_trigger_log(
            is_archived=True,
            state=TRIGGER_LOG_STATE["FAILED"],
            table_name="number_object__c",
            record_id=testrecord.id,
            action=log_action,
        )
        failed_log.save()

        succeeded = make_trigger_log(is_archived=True,
                                     state=TRIGGER_LOG_STATE["SUCCESS"])
        succeeded.save()

        assert TriggerLog.objects.count() == 0
        assert TriggerLogArchive.objects.count() == 2
        assert set(TriggerLogArchive.objects.values_list(
            "state", flat=True)) == {
                TRIGGER_LOG_STATE["SUCCESS"],
                TRIGGER_LOG_STATE["FAILED"],
            }

        admin_client.post(
            self.admin_changelist_url(TriggerLogArchive),
            data=self.action_post_data(
                admin.TriggerLogAdmin.retry_failed_logs_action,
                TriggerLogArchive.objects.all(),
            ),
        )

        assert TriggerLog.objects.get().state == TRIGGER_LOG_STATE["NEW"]

        assert TriggerLogArchive.objects.count() == 2
        assert set(TriggerLogArchive.objects.values_list(
            "state", flat=True)) == {
                TRIGGER_LOG_STATE["SUCCESS"],
                TRIGGER_LOG_STATE["REQUEUED"],
            }

        succeeded.refresh_from_db()
        assert succeeded.state == TRIGGER_LOG_STATE["SUCCESS"]

    def test_retry_failed_logs_ordered_write_field_subset(
            self, admin_client, set_write_mode_ordered,
            hc_capture_stored_procedures):
        assert get_unique_connection_write_mode(
        ) == WriteAlgorithm.ORDERED_WRITES

        testrecord = NumberModel.objects.create()

        failed_log = make_trigger_log(
            state=TRIGGER_LOG_STATE["FAILED"],
            table_name="number_object__c",
            record_id=testrecord.id,
            action="UPDATE",
            values='"a_number__c"=>"333"',
        )
        failed_log.save()

        admin_client.post(
            self.admin_changelist_url(TriggerLog),
            data=self.action_post_data(
                admin.TriggerLogAdmin.retry_failed_logs_action,
                TriggerLog.objects.all()),
        )

        qs = TriggerLog.objects.all()
        assert qs.count() == 2
        assert set(qs.values_list("state", flat=True)) == {
            TRIGGER_LOG_STATE["REQUEUED"],
            TRIGGER_LOG_STATE["NEW"],
        }

        new_log = qs.exclude(id=failed_log.id).get()

        assert set(new_log.values_as_dict.keys()) == {"a_number__c"}