def setUp(self) -> None:
     super().setUp()
     self.user_id = 5968705978
     self.action = TopicDelete(
         "topic.delete",
         PermissionTestAdapter(superuser=self.user_id),
         DatabaseTestAdapter(old_style_testing=True),
     )
 def setUp(self) -> None:
     super().setUp()
     self.action = TopicDelete(PermissionTestAdapter(), DatabaseTestAdapter())
     self.action.user_id = (
         5968705978  # This user has perm TOPIC_CAN_MANAGE for some meetings.
     )
class TopicDeleteActionUnitTester(BaseTopicDeleteActionTester):
    def setUp(self) -> None:
        super().setUp()
        self.action = TopicDelete(PermissionTestAdapter(), DatabaseTestAdapter())
        self.action.user_id = (
            5968705978  # This user has perm TOPIC_CAN_MANAGE for some meetings.
        )

    def test_validation_empty(self) -> None:
        payload: ActionPayload = []
        with self.assertRaises(ActionException):
            self.action.validate(payload)

    def test_validation_empty_2(self) -> None:
        payload: ActionPayload = [{}]
        with self.assertRaises(ActionException):
            self.action.validate(payload)

    def test_validation_fuzzy(self) -> None:
        payload = [{"wrong_field": "text_Eichielao7"}]
        with self.assertRaises(ActionException):
            self.action.validate(payload)

    def test_validation_correct_1(self) -> None:
        self.action.validate(self.valid_payload_1)

    def test_validation_correct_2(self) -> None:
        self.action.validate(self.valid_payload_2)

    def test_validation_correct_3(self) -> None:
        self.action.validate(self.pseudo_valid_payload_3)

    def test_prepare_dataset_1(self) -> None:
        dataset = self.action.prepare_dataset(self.valid_payload_1)
        self.assertEqual(dataset["position"], 1)
        self.assertEqual(
            dataset["data"],
            [
                {
                    "instance": {
                        "id": self.valid_payload_1[0]["id"],
                        "meeting_id": None,
                        "agenda_item_id": None,
                        "attachment_ids": None,
                        "tag_ids": None,
                    },
                    "relations": {
                        get_fqfield("meeting/7816466305/topic_ids"): {
                            "type": "remove",
                            "value": [],
                        },
                    },
                }
            ],
        )

    def test_prepare_dataset_2(self) -> None:
        dataset = self.action.prepare_dataset(self.valid_payload_2)
        self.assertEqual(dataset["position"], 1)
        self.assertEqual(
            dataset["data"],
            [
                {
                    "instance": {
                        "id": self.valid_payload_2[0]["id"],
                        "meeting_id": None,
                        "agenda_item_id": None,
                        "attachment_ids": None,
                        "tag_ids": None,
                    },
                    "relations": {
                        get_fqfield("meeting/7816466305/topic_ids"): {
                            "type": "remove",
                            "value": [],
                        },
                    },
                },
                {
                    "instance": {
                        "id": self.valid_payload_2[1]["id"],
                        "meeting_id": None,
                        "agenda_item_id": None,
                        "attachment_ids": None,
                        "tag_ids": None,
                    },
                    "relations": {
                        get_fqfield("meeting/3611987967/topic_ids"): {
                            "type": "remove",
                            "value": [6375863023],
                        },
                        get_fqfield("mediafile/3549387598/attachment_ids"): {
                            "type": "remove",
                            "value": [],
                        },
                    },
                },
            ],
        )

    def test_prepare_dataset_3(self) -> None:
        dataset = self.action.prepare_dataset(self.pseudo_valid_payload_3)
        self.assertEqual(dataset["position"], 1)
        self.assertEqual(
            dataset["data"],
            [
                {
                    "instance": {
                        "id": self.pseudo_valid_payload_3[0]["id"],
                        "meeting_id": None,
                        "agenda_item_id": None,
                        "attachment_ids": None,
                        "tag_ids": None,
                    },
                    "relations": {
                        get_fqfield("meeting/9079236097/topic_ids"): {
                            "type": "remove",
                            "value": [],
                        },
                        get_fqfield("agenda_item/3393211712/content_object_id"): {
                            "type": "remove",
                            "value": None,
                        },
                    },
                }
            ],
        )
class TopicDeleteActionPerformTester(BaseTopicDeleteActionTester):
    def setUp(self) -> None:
        super().setUp()
        self.action = TopicDelete(PermissionTestAdapter(), DatabaseTestAdapter())
        self.user_id = (
            5968705978  # This user has perm TOPIC_CAN_MANAGE for some meetings.
        )

    def test_perform_empty(self) -> None:
        payload: ActionPayload = []
        with self.assertRaises(ActionException):
            self.action.perform(payload, user_id=self.user_id)

    def test_perform_empty_2(self) -> None:
        payload: ActionPayload = [{}]
        with self.assertRaises(ActionException):
            self.action.perform(payload, user_id=self.user_id)

    def test_perform_fuzzy(self) -> None:
        payload = [{"wrong_field": "text_aizaeMai7E"}]
        with self.assertRaises(ActionException):
            self.action.perform(payload, user_id=self.user_id)

    def test_perform_correct_1(self) -> None:
        write_request_elements = self.action.perform(
            self.valid_payload_1, user_id=self.user_id
        )
        expected = [
            {
                "events": [
                    {"type": "delete", "fqid": get_fqid("topic/1312354708")},
                    {
                        "type": "update",
                        "fqid": get_fqid("meeting/7816466305"),
                        "fields": {"topic_ids": []},
                    },
                ],
                "information": {
                    get_fqid("topic/1312354708"): ["Object deleted"],
                    get_fqid("meeting/7816466305"): [
                        "Object attachment to topic reset"
                    ],
                },
                "user_id": self.user_id,
                "locked_fields": {
                    get_fqfield("topic/1312354708/deleted"): 1,
                    get_fqfield("meeting/7816466305/topic_ids"): 1,
                },
            },
        ]
        result = list(write_request_elements)
        self.assertEqual(result, expected)

    def test_perform_correct_2(self) -> None:
        write_request_elements = self.action.perform(
            self.valid_payload_2, user_id=self.user_id
        )
        result = list(write_request_elements)
        expected = [
            {
                "events": [
                    {"type": "delete", "fqid": get_fqid("topic/1312354708")},
                    {
                        "type": "update",
                        "fqid": get_fqid("meeting/7816466305"),
                        "fields": {"topic_ids": []},
                    },
                ],
                "information": {
                    get_fqid("topic/1312354708"): ["Object deleted"],
                    get_fqid("meeting/7816466305"): [
                        "Object attachment to topic reset"
                    ],
                },
                "user_id": self.user_id,
                "locked_fields": {
                    get_fqfield("topic/1312354708/deleted"): 1,
                    get_fqfield("meeting/7816466305/topic_ids"): 1,
                },
            },
            {
                "events": [
                    {"type": "delete", "fqid": get_fqid("topic/6259289755")},
                    {
                        "type": "update",
                        "fqid": get_fqid("mediafile/3549387598"),
                        "fields": {"attachment_ids": []},
                    },
                    {
                        "type": "update",
                        "fqid": get_fqid("meeting/3611987967"),
                        "fields": {"topic_ids": [6375863023]},
                    },
                ],
                "information": {
                    get_fqid("topic/6259289755"): ["Object deleted"],
                    get_fqid("meeting/3611987967"): [
                        "Object attachment to topic reset"
                    ],
                    get_fqid("mediafile/3549387598"): [
                        "Object attachment to topic reset"
                    ],
                },
                "user_id": self.user_id,
                "locked_fields": {
                    get_fqfield("topic/6259289755/deleted"): 1,
                    get_fqfield("meeting/3611987967/topic_ids"): 1,
                    get_fqfield("mediafile/3549387598/attachment_ids"): 1,
                },
            },
        ]
        self.assertEqual(result, expected)

    def test_perform_no_permission_1(self) -> None:
        with self.assertRaises(PermissionDenied):
            self.action.perform(self.valid_payload_1, user_id=4796568680)

    def test_perform_no_permission_2(self) -> None:
        with self.assertRaises(PermissionDenied):
            self.action.perform(self.valid_payload_2, user_id=4796568680)