generated from openai/plugins-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_app.py
137 lines (115 loc) · 4.69 KB
/
test_app.py
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
from fastapi.testclient import TestClient
import pytest
from D2.run_d2 import run_go_script
from mermaid.mermaid import PakoSerde, deserialize_state, generate_diagram_state, generate_mermaid_live_editor_url, serialize_state
from plantuml import PlantUML, PlantUMLHTTPError
from .app import app
client = TestClient(app)
def test_root_endpoint():
response = client.get("/")
assert response.status_code == 200
def test_plugin_manifest():
response = client.get("/.well-known/ai-plugin.json")
assert response.status_code == 200
assert "api" in response.json()
def test_logo_endpoint():
response = client.get("/logo.png")
assert response.status_code == 200
def test_generate_diagram_endpoint():
response = client.post("/generate_diagram", json={
"lang": "plantuml",
"type": "sequence",
"code": "@startuml \n Alice -> Bob: Authentication Request \n @enduml"
})
assert response.status_code == 200
assert "url" in response.json()
def test_generate_mermaid_diagram_endpoint():
response = client.post("/generate_diagram", json={
"lang": "mermaid",
"type": "sequence",
"code": "sequenceDiagram\n Alice->>Bob: Hello Bob, how are you?\n Bob-->>Alice: Not too bad, thanks!"
})
assert response.status_code == 200
assert "url" in response.json()
def test_generate_d2_diagram_endpoint():
response = client.post("/generate_diagram", json={
"lang": "d2lang",
"type": "class",
"code": "class Test{}"
})
assert response.json() == {"error": "An error occurred while generating the diagram."}
assert response.status_code == 200
assert "url" in response.json()
def test_generate_diagram_endpoint_with_empty_text():
response = client.post("/generate_diagram", json={
"lang": "plantuml",
"type": "sequence",
"code": ""
})
assert response.status_code == 422 # Unprocessable Entity
def test_generate_diagram_endpoint_with_no_lang():
response = client.post("/generate_diagram", json={
"type": "sequence",
"code": "@startuml\nAlice -> Bob: Authentication Request\n@enduml"
})
assert response.status_code == 422 # Unprocessable Entity
def test_generate_diagram_endpoint_with_unsupported_lang():
response = client.post("/generate_diagram", json={
"lang": "unsupported",
"type": "sequence",
"code": "@startuml\nAlice -> Bob: Authentication Request\n@enduml"
})
assert response.status_code == 422 # Unprocessable Entity
def test_openapi_spec():
response = client.get("/openapi.yaml")
assert response.status_code == 200
assert "openapi: 3.0.2" in response.text
def test_generate_image_from_string():
plantuml = PlantUML(url="https://www.plantuml.com/plantuml/dpng")
# Test with valid PlantUML text
valid_text = """@startuml \n Alice -> Bob: Authentication Request \n @enduml"""
try:
url, content, playground = plantuml.generate_image_from_string(valid_text)
assert url is not None
assert content is not None
assert playground is not None
except PlantUMLHTTPError:
pytest.fail("PlantUMLHTTPError was raised with valid PlantUML text")
# Test with invalid PlantUML text
invalid_text = "This is not valid PlantUML text"
with pytest.raises(PlantUMLHTTPError):
plantuml.generate_image_from_string(invalid_text)
def test_pako_serde():
serde = PakoSerde()
original = "Hello, world!"
serialized = serde.serialize(original)
assert original == serde.deserialize(serialized)
def test_generate_diagram_state():
diagram_text = "graph TD; A-->B;"
state = generate_diagram_state(diagram_text)
assert state["code"] == diagram_text
assert state["mermaid"]["theme"] == "dark"
assert state["updateEditor"] == True
assert state["autoSync"] == True
assert state["updateDiagram"] == True
def test_generate_mermaid_live_editor_url():
diagram_text = "graph TD; A-->B;"
state = generate_diagram_state(diagram_text)
url, code, playground = generate_mermaid_live_editor_url(state)
assert url.startswith("https://mermaid.ink/svg/")
assert code == diagram_text
assert playground.startswith("https://mermaid.live/edit#")
def test_serialize_deserialize_state():
state = {"key": "value"}
serialized = serialize_state(state)
deserialized = deserialize_state(serialized)
assert state == deserialized
@pytest.mark.asyncio
async def test_run_go_script():
# Define some test input
test_input = "test input"
# Call the function with the test input
url, content, playground = await run_go_script(test_input)
assert url is not None
assert content is not None
assert playground is not None