File: test_blocklist.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (245 lines) | stat: -rw-r--r-- 11,624 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# coding=utf-8
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------

from azure.core.exceptions import HttpResponseError
from devtools_testutils import recorded_by_proxy

from azure.ai.contentsafety.models import (
    TextBlocklist,
    TextBlocklistItem,
    AddOrUpdateTextBlocklistItemsOptions,
    RemoveTextBlocklistItemsOptions,
)
from test_case import ContentSafetyTest, ContentSafetyPreparer


class TestBlocklistCase(ContentSafetyTest):
    @ContentSafetyPreparer()
    @recorded_by_proxy
    def test_create_blocklist(self, content_safety_endpoint, content_safety_key):
        client = self.create_blocklist_client_from_key(content_safety_endpoint, content_safety_key)

        name = "TestBlocklist"
        description = "Test blocklist management."
        response = client.create_or_update_text_blocklist(
            blocklist_name=name, options=TextBlocklist(blocklist_name=name, description=description)
        )

        assert response is not None
        assert response.blocklist_name == name
        assert response.description == description

    @ContentSafetyPreparer()
    @recorded_by_proxy
    def test_list_text_blocklists(self, content_safety_endpoint, content_safety_key):
        client = self.create_blocklist_client_from_key(content_safety_endpoint, content_safety_key)

        # Create blocklist
        blocklist_name = "TestBlocklist"
        blocklist_description = "Test blocklist management."
        create_blocklist_response = client.create_or_update_text_blocklist(
            blocklist_name=blocklist_name,
            options=TextBlocklist(blocklist_name=blocklist_name, description=blocklist_description),
        )
        if not create_blocklist_response or not create_blocklist_response.blocklist_name:
            raise RuntimeError("Failed to create blocklist.")

        # List blocklist
        blocklists = list(client.list_text_blocklists())

        assert blocklists is not None
        assert any(blocklist_name in item["blocklistName"] for item in blocklists) is True
        assert any(blocklist_description in item["description"] for item in blocklists) is True

    @ContentSafetyPreparer()
    @recorded_by_proxy
    def test_get_text_blocklist(self, content_safety_endpoint, content_safety_key):
        client = self.create_blocklist_client_from_key(content_safety_endpoint, content_safety_key)

        # Create blocklist
        blocklist_name = "TestBlocklist"
        blocklist_description = "Test blocklist management."
        create_blocklist_response = client.create_or_update_text_blocklist(
            blocklist_name=blocklist_name,
            options=TextBlocklist(blocklist_name=blocklist_name, description=blocklist_description),
        )
        if not create_blocklist_response or not create_blocklist_response.blocklist_name:
            raise RuntimeError("Failed to create blocklist.")

        # Get blocklist
        blocklist = client.get_text_blocklist(blocklist_name=blocklist_name)
        assert blocklist is not None
        assert blocklist.blocklist_name == blocklist_name
        assert blocklist.description == blocklist_description

    @ContentSafetyPreparer()
    @recorded_by_proxy
    def test_delete_blocklist(self, content_safety_endpoint, content_safety_key):
        client = self.create_blocklist_client_from_key(content_safety_endpoint, content_safety_key)

        # Create blocklist
        blocklist_name = "TestDeleteBlocklist"
        blocklist_description = "Test blocklist management."
        create_blocklist_response = client.create_or_update_text_blocklist(
            blocklist_name=blocklist_name,
            options=TextBlocklist(blocklist_name=blocklist_name, description=blocklist_description),
        )
        if not create_blocklist_response or not create_blocklist_response.blocklist_name:
            raise RuntimeError("Failed to create blocklist.")

        # Delete blocklist
        try:
            client.delete_text_blocklist(blocklist_name=blocklist_name)
        except HttpResponseError:
            raise

    @ContentSafetyPreparer()
    @recorded_by_proxy
    def test_add_blocklist_items(self, content_safety_endpoint, content_safety_key):
        client = self.create_blocklist_client_from_key(content_safety_endpoint, content_safety_key)

        # Create blocklist
        blocklist_name = "TestBlocklist"
        blocklist_description = "Test blocklist management."
        create_blocklist_response = client.create_or_update_text_blocklist(
            blocklist_name=blocklist_name,
            options=TextBlocklist(blocklist_name=blocklist_name, description=blocklist_description),
        )
        if not create_blocklist_response or not create_blocklist_response.blocklist_name:
            raise RuntimeError("Failed to create blocklist.")

        # Add blocklist item
        block_item_text_1 = "k*ll"
        block_item_text_2 = "h*te"
        block_items = [TextBlocklistItem(text=block_item_text_1), TextBlocklistItem(text=block_item_text_2)]
        response = client.add_or_update_blocklist_items(
            blocklist_name=blocklist_name, options=AddOrUpdateTextBlocklistItemsOptions(blocklist_items=block_items)
        )
        assert response is not None
        assert response.blocklist_items is not None
        assert any(block_item_text_1 in item["text"] for item in response.blocklist_items) is True
        assert any(block_item_text_2 in item["text"] for item in response.blocklist_items) is True

    @ContentSafetyPreparer()
    @recorded_by_proxy
    def test_list_blocklist_items(self, content_safety_endpoint, content_safety_key):
        client = self.create_blocklist_client_from_key(content_safety_endpoint, content_safety_key)

        # Create blocklist
        blocklist_name = "TestBlocklist"
        blocklist_description = "Test blocklist management."
        create_blocklist_response = client.create_or_update_text_blocklist(
            blocklist_name=blocklist_name,
            options=TextBlocklist(blocklist_name=blocklist_name, description=blocklist_description),
        )
        if not create_blocklist_response or not create_blocklist_response.blocklist_name:
            raise RuntimeError("Failed to create blocklist.")

        # Add blocklist item
        block_item_text_1 = "This is a test."
        block_item_text_2 = "This is a test 2."
        block_item_text_3 = "This is a test 3."
        block_item_text_4 = "This is a test 4."
        block_item_text_5 = "This is a test 5."
        block_items = [
            TextBlocklistItem(text=block_item_text_1),
            TextBlocklistItem(text=block_item_text_2),
            TextBlocklistItem(text=block_item_text_3),
            TextBlocklistItem(text=block_item_text_4),
            TextBlocklistItem(text=block_item_text_5),
        ]
        add_item_response = client.add_or_update_blocklist_items(
            blocklist_name=blocklist_name, options=AddOrUpdateTextBlocklistItemsOptions(blocklist_items=block_items)
        )
        if not add_item_response or not add_item_response.blocklist_items or len(add_item_response.blocklist_items) < 0:
            raise RuntimeError("Failed to add blocklist item.")

        # List blocklist item
        response = list(client.list_text_blocklist_items(blocklist_name=blocklist_name))
        assert response is not None
        items_count = len(response)
        assert items_count >= 5
        assert any(block_item_text_1 in item["text"] for item in response) is True
        assert any(block_item_text_2 in item["text"] for item in response) is True
        assert any(block_item_text_3 in item["text"] for item in response) is True
        assert any(block_item_text_4 in item["text"] for item in response) is True
        assert any(block_item_text_5 in item["text"] for item in response) is True

        # List blocklist item, test top
        response = list(client.list_text_blocklist_items(blocklist_name=blocklist_name, top=2))
        assert response is not None
        assert len(response) <= 2

        # List blocklist item, test skip
        response = list(client.list_text_blocklist_items(blocklist_name=blocklist_name, skip=2))
        assert response is not None
        assert len(response) <= items_count - 2

    @ContentSafetyPreparer()
    @recorded_by_proxy
    def test_get_blocklist_item(self, content_safety_endpoint, content_safety_key):
        client = self.create_blocklist_client_from_key(content_safety_endpoint, content_safety_key)

        # Create blocklist
        blocklist_name = "TestBlocklist"
        blocklist_description = "Test blocklist management."
        create_blocklist_response = client.create_or_update_text_blocklist(
            blocklist_name=blocklist_name,
            options=TextBlocklist(blocklist_name=blocklist_name, description=blocklist_description),
        )
        if not create_blocklist_response or not create_blocklist_response.blocklist_name:
            raise RuntimeError("Failed to create blocklist.")

        # Add blocklist item
        block_item_text_1 = "k*ll"
        block_items = [TextBlocklistItem(text=block_item_text_1)]
        add_item_response = client.add_or_update_blocklist_items(
            blocklist_name=blocklist_name, options=AddOrUpdateTextBlocklistItemsOptions(blocklist_items=block_items)
        )
        if not add_item_response or not add_item_response.blocklist_items or len(add_item_response.blocklist_items) < 0:
            raise RuntimeError("Failed to add blocklist item.")

        block_item_id = add_item_response.blocklist_items[0].blocklist_item_id

        # Get this blockItem by blockItemId
        blocklist_item = client.get_text_blocklist_item(blocklist_name=blocklist_name, blocklist_item_id=block_item_id)
        assert blocklist_item is not None
        assert blocklist_item.text == block_item_text_1

    @ContentSafetyPreparer()
    @recorded_by_proxy
    def test_remove_blocklist_items(self, content_safety_endpoint, content_safety_key):
        client = self.create_blocklist_client_from_key(content_safety_endpoint, content_safety_key)

        # Create blocklist
        blocklist_name = "TestRemoveBlocklistItem"
        blocklist_description = "Test blocklist management."
        create_blocklist_response = client.create_or_update_text_blocklist(
            blocklist_name=blocklist_name,
            options=TextBlocklist(blocklist_name=blocklist_name, description=blocklist_description),
        )
        if not create_blocklist_response or not create_blocklist_response.blocklist_name:
            raise RuntimeError("Failed to create blocklist.")

        # Add blocklist item
        block_item_text_1 = "k*ll"
        block_items = [TextBlocklistItem(text=block_item_text_1)]
        add_item_response = client.add_or_update_blocklist_items(
            blocklist_name=blocklist_name, options=AddOrUpdateTextBlocklistItemsOptions(blocklist_items=block_items)
        )
        if not add_item_response or not add_item_response.blocklist_items or len(add_item_response.blocklist_items) < 0:
            raise RuntimeError("Failed to add blocklist item.")

        block_item_id = add_item_response.blocklist_items[0].blocklist_item_id

        try:
            # Remove this blockItem by blockItemId
            client.remove_blocklist_items(
                blocklist_name=blocklist_name,
                options=RemoveTextBlocklistItemsOptions(blocklist_item_ids=[block_item_id]),
            )
        except HttpResponseError:
            raise