Skip to content

Commit accdc7c

Browse files
authored
TEST: Rename converter/ to prompt_converter/ and add 19 converter tests (#1594)
1 parent 7d8219d commit accdc7c

73 files changed

Lines changed: 1084 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tests/unit/converter/test_add_image_text_converter.py renamed to tests/unit/prompt_converter/test_add_image_text_converter.py

File renamed without changes.

tests/unit/converter/test_add_image_video_converter.py renamed to tests/unit/prompt_converter/test_add_image_video_converter.py

File renamed without changes.

tests/unit/converter/test_add_text_image_converter.py renamed to tests/unit/prompt_converter/test_add_text_image_converter.py

File renamed without changes.

tests/unit/converter/test_ansi_attack_converter.py renamed to tests/unit/prompt_converter/test_ansi_attack_converter.py

File renamed without changes.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT license.
3+
4+
import pytest
5+
6+
pytest.importorskip("art")
7+
8+
from pyrit.prompt_converter import AsciiArtConverter, ConverterResult
9+
10+
11+
@pytest.mark.asyncio
12+
async def test_ascii_art_converter_basic():
13+
converter = AsciiArtConverter(font="block")
14+
result = await converter.convert_async(prompt="hi", input_type="text")
15+
assert isinstance(result, ConverterResult)
16+
assert result.output_type == "text"
17+
assert len(result.output_text) > 0
18+
assert "\n" in result.output_text
19+
20+
21+
@pytest.mark.asyncio
22+
async def test_ascii_art_converter_default_random_font():
23+
converter = AsciiArtConverter()
24+
result = await converter.convert_async(prompt="test", input_type="text")
25+
assert isinstance(result, ConverterResult)
26+
assert len(result.output_text) > 0
27+
28+
29+
@pytest.mark.asyncio
30+
async def test_ascii_art_converter_empty():
31+
converter = AsciiArtConverter(font="block")
32+
result = await converter.convert_async(prompt="", input_type="text")
33+
assert isinstance(result, ConverterResult)
34+
assert result.output_type == "text"
35+
36+
37+
@pytest.mark.asyncio
38+
async def test_ascii_art_converter_input_not_supported():
39+
converter = AsciiArtConverter()
40+
with pytest.raises(ValueError, match="Input type not supported"):
41+
await converter.convert_async(prompt="test", input_type="image_path")
42+
43+
44+
def test_ascii_art_converter_input_supported():
45+
converter = AsciiArtConverter()
46+
assert converter.input_supported("text") is True
47+
assert converter.input_supported("image_path") is False
48+
49+
50+
def test_ascii_art_converter_output_supported():
51+
converter = AsciiArtConverter()
52+
assert converter.output_supported("text") is True
53+
assert converter.output_supported("image_path") is False
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT license.
3+
4+
import pytest
5+
6+
from pyrit.prompt_converter import AsciiSmugglerConverter, ConverterResult
7+
8+
9+
@pytest.mark.asyncio
10+
async def test_ascii_smuggler_encode_basic():
11+
converter = AsciiSmugglerConverter(action="encode")
12+
result = await converter.convert_async(prompt="hi", input_type="text")
13+
assert isinstance(result, ConverterResult)
14+
assert result.output_type == "text"
15+
for char in result.output_text:
16+
assert ord(char) > 0xE0000
17+
18+
19+
@pytest.mark.asyncio
20+
async def test_ascii_smuggler_decode_roundtrip():
21+
encoder = AsciiSmugglerConverter(action="encode")
22+
encoded = await encoder.convert_async(prompt="hello", input_type="text")
23+
24+
decoder = AsciiSmugglerConverter(action="decode")
25+
decoded = await decoder.convert_async(prompt=encoded.output_text, input_type="text")
26+
assert decoded.output_text == "hello"
27+
28+
29+
@pytest.mark.asyncio
30+
async def test_ascii_smuggler_with_unicode_tags():
31+
converter = AsciiSmugglerConverter(action="encode", unicode_tags=True)
32+
result = await converter.convert_async(prompt="hi", input_type="text")
33+
assert result.output_text.startswith(chr(0xE0001))
34+
assert result.output_text.endswith(chr(0xE007F))
35+
36+
37+
@pytest.mark.asyncio
38+
async def test_ascii_smuggler_empty():
39+
converter = AsciiSmugglerConverter(action="encode")
40+
result = await converter.convert_async(prompt="", input_type="text")
41+
assert result.output_text == ""
42+
43+
44+
def test_ascii_smuggler_invalid_action():
45+
with pytest.raises(ValueError):
46+
AsciiSmugglerConverter(action="invalid")
47+
48+
49+
@pytest.mark.asyncio
50+
async def test_ascii_smuggler_input_not_supported():
51+
converter = AsciiSmugglerConverter(action="encode")
52+
with pytest.raises(ValueError, match="Input type not supported"):
53+
await converter.convert_async(prompt="test", input_type="image_path")

tests/unit/converter/test_ask_to_decode_converter.py renamed to tests/unit/prompt_converter/test_ask_to_decode_converter.py

File renamed without changes.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT license.
3+
4+
import pytest
5+
6+
from pyrit.prompt_converter import AtbashConverter, ConverterResult
7+
8+
9+
@pytest.mark.asyncio
10+
async def test_atbash_converter_basic():
11+
converter = AtbashConverter()
12+
result = await converter.convert_async(prompt="abc", input_type="text")
13+
assert isinstance(result, ConverterResult)
14+
assert result.output_text == "zyx"
15+
assert result.output_type == "text"
16+
17+
18+
@pytest.mark.asyncio
19+
async def test_atbash_converter_uppercase():
20+
converter = AtbashConverter()
21+
result = await converter.convert_async(prompt="ABC", input_type="text")
22+
assert isinstance(result, ConverterResult)
23+
assert result.output_text == "ZYX"
24+
assert result.output_type == "text"
25+
26+
27+
@pytest.mark.asyncio
28+
async def test_atbash_converter_mixed():
29+
converter = AtbashConverter()
30+
result = await converter.convert_async(prompt="Hello", input_type="text")
31+
assert isinstance(result, ConverterResult)
32+
assert result.output_text == "Svool"
33+
assert result.output_type == "text"
34+
35+
36+
@pytest.mark.asyncio
37+
async def test_atbash_converter_with_description():
38+
converter = AtbashConverter(append_description=True)
39+
result = await converter.convert_async(prompt="hello", input_type="text")
40+
assert isinstance(result, ConverterResult)
41+
assert result.output_type == "text"
42+
# The encoded prompt should be present in the output
43+
assert "svool" in result.output_text
44+
45+
46+
@pytest.mark.asyncio
47+
async def test_atbash_converter_empty_string():
48+
converter = AtbashConverter()
49+
result = await converter.convert_async(prompt="", input_type="text")
50+
assert isinstance(result, ConverterResult)
51+
assert result.output_text == ""
52+
assert result.output_type == "text"
53+
54+
55+
@pytest.mark.asyncio
56+
async def test_atbash_converter_numbers():
57+
converter = AtbashConverter()
58+
result = await converter.convert_async(prompt="012", input_type="text")
59+
assert isinstance(result, ConverterResult)
60+
assert result.output_text == "987"
61+
assert result.output_type == "text"
62+
63+
64+
@pytest.mark.asyncio
65+
async def test_atbash_converter_input_not_supported():
66+
converter = AtbashConverter()
67+
with pytest.raises(ValueError):
68+
await converter.convert_async(prompt="hello", input_type="image_path")
69+
70+
71+
def test_atbash_converter_input_supported():
72+
converter = AtbashConverter()
73+
assert converter.input_supported("text") is True
74+
assert converter.input_supported("image_path") is False
75+
76+
77+
def test_atbash_converter_output_supported():
78+
converter = AtbashConverter()
79+
assert converter.output_supported("text") is True
80+
assert converter.output_supported("image_path") is False
File renamed without changes.

tests/unit/converter/test_audio_frequency_converter.py renamed to tests/unit/prompt_converter/test_audio_frequency_converter.py

File renamed without changes.

0 commit comments

Comments
 (0)