-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_server.py
More file actions
119 lines (109 loc) · 3.86 KB
/
test_server.py
File metadata and controls
119 lines (109 loc) · 3.86 KB
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
import unittest
from polyapi.utils import to_func_namespace
from .test_api import TWILIO
from polyapi.server import render_server_function
GET_PRODUCTS_COUNT = {
"id": "8f7d24b0-4a29-40c0-9091",
"type": "serverFunction",
"context": "test",
"name": "getProductsCount111",
"description": "An API call to retrieve the count of products in the product list.",
"requirements": ["snabbdom"],
"function": {
"arguments": [
{
"name": "products",
"required": False,
"type": {
"kind": "array",
"items": {"kind": "primitive", "type": "string"},
},
}
],
"returnType": {"kind": "plain", "value": "number"},
"synchronous": True,
},
"code": "",
"language": "javascript",
"visibilityMetadata": {"visibility": "ENVIRONMENT"},
}
LIST_RECOMMENDATIONS = {
"id": "1234-5678-90ab-cdef",
"type": "serverFunction",
"context": "foo",
"name": "listRecommendations",
"contextName": "foo.listRecommendations",
"description": "",
"requirements": [],
"serverSideAsync": False,
"function": {
"arguments": [],
"returnType": {
"kind": "object",
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {"type": "string"},
"stay_date": {"type": "string"},
},
"additionalProperties": False,
"required": ["id", "stay_date"],
},
},
},
"synchronous": False,
},
"sourceCode": '',
"language": "javascript",
"state": "ALPHA",
"visibilityMetadata": {"visibility": "ENVIRONMENT"},
}
class T(unittest.TestCase):
def test_render_function_twilio_server(self):
# same test but try it as a serverFunction rather than an apiFunction
name = TWILIO["name"]
func_str, _ = render_server_function(
"serverFunction",
TWILIO["name"],
TWILIO["id"],
TWILIO["description"],
TWILIO["function"]["arguments"],
TWILIO["function"]["returnType"],
)
self.assertIn(TWILIO["id"], func_str)
self.assertIn("conversationSID: str", func_str)
self.assertIn("authToken: str", func_str)
self.assertIn(f"-> {to_func_namespace(name)}.ResponseType", func_str)
def test_render_function_get_products_count(self):
return_type = GET_PRODUCTS_COUNT["function"]["returnType"]
func_str, _ = render_server_function(
GET_PRODUCTS_COUNT["type"],
GET_PRODUCTS_COUNT["name"],
GET_PRODUCTS_COUNT["id"],
GET_PRODUCTS_COUNT["description"],
GET_PRODUCTS_COUNT["function"]["arguments"],
return_type,
)
self.assertIn(GET_PRODUCTS_COUNT["id"], func_str)
self.assertIn("products: List[str]", func_str)
self.assertIn("-> float", func_str)
def test_render_function_list_recommendations(self):
return_type = LIST_RECOMMENDATIONS["function"]["returnType"]
func_str, func_type_defs = render_server_function(
LIST_RECOMMENDATIONS["type"],
LIST_RECOMMENDATIONS["name"],
LIST_RECOMMENDATIONS["id"],
LIST_RECOMMENDATIONS["description"],
LIST_RECOMMENDATIONS["function"]["arguments"],
return_type,
)
self.assertIn(LIST_RECOMMENDATIONS["id"], func_str)
self.assertIn("-> List", func_str)
# expected_return_type = '''class ReturnType(TypedDict, total=False):
# id: Required[str]
# """ Required property """
# stay_date: Required[str]
# """ Required property """'''
# self.assertIn(expected_return_type, func_str)