-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
148 lines (127 loc) · 5.24 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
138
139
140
141
142
143
144
145
146
147
148
import json
from textwrap import dedent
import unittest
from app import app
class TestTranslateSql(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
def test_translate_sql(self):
# Define the JSON data to send in the POST request
data = {
"sql": "SELECT * FROM my_table",
"from": "mysql",
"to": "postgres",
}
# Send a POST request to the /translate endpoint with the JSON data
response = self.app.post(
"/translate",
data=json.dumps(data),
content_type="application/json",
)
# Check that the response status code is 200 OK
self.assertEqual(response.status_code, 200)
# Check that the response JSON contains the expected 'sql' key
response_data = json.loads(response.data)
self.assertIn("sql", response_data)
# Check that the response JSON contains the expected SQL translation
self.assertEqual(response_data["sql"], "SELECT * FROM my_table;")
def test_translate_sql_with_stub_input_dialect(self):
# Define the JSON data to send in the POST request
data = {
"sql": "SELECT * FROM my_table",
"from": "stub",
"to": "postgres"
}
# Send a POST request to the /translate endpoint with the JSON data
response = self.app.post('/translate', data=json.dumps(data), content_type='application/json')
# Check that the response status code is 400 Bad Request
self.assertEqual(response.status_code, 400)
# Check that the response JSON contains the expected error message
response_data = json.loads(response.data)
self.assertIn('error', response_data)
self.assertEqual(response_data['error'], 'Unsupported input dialect: stub')
def test_valid_translation(self):
data = {
"sql": "SELECT * FROM my_table",
"from": "postgres",
"to": "mysql",
"pretty": True
}
response = self.app.post('/translate', data=json.dumps(data), content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertIn('sql', json.loads(response.data))
def test_missing_sql(self):
data = {
"from": "postgres",
"to": "mysql",
"pretty": True
}
response = self.app.post('/translate', data=json.dumps(data), content_type='application/json')
self.assertEqual(response.status_code, 400)
self.assertIn('error', json.loads(response.data))
def test_missing_from(self):
data = {
"sql": "SELECT * FROM my_table",
"to": "mysql",
"pretty": True
}
response = self.app.post('/translate', data=json.dumps(data), content_type='application/json')
self.assertEqual(response.status_code, 400)
self.assertIn('error', json.loads(response.data))
def test_missing_to(self):
data = {
"sql": "SELECT * FROM my_table",
"from": "postgres",
"pretty": True
}
response = self.app.post('/translate', data=json.dumps(data), content_type='application/json')
self.assertEqual(response.status_code, 400)
self.assertIn('error', json.loads(response.data))
def test_invalid_pretty(self):
data = {
"sql": "SELECT * FROM my_table",
"from": "postgres",
"to": "mysql",
"pretty": "not_a_boolean"
}
response = self.app.post('/translate', data=json.dumps(data), content_type='application/json')
self.assertEqual(response.status_code, 400)
self.assertIn('error', json.loads(response.data))
def test_unsupported_from_dialect(self):
data = {
"sql": "SELECT * FROM my_table",
"from": "invalid_dialect",
"to": "mysql",
"pretty": True
}
response = self.app.post('/translate', data=json.dumps(data), content_type='application/json')
self.assertEqual(response.status_code, 400)
self.assertIn('error', json.loads(response.data))
def test_unsupported_to_dialect(self):
data = {
"sql": "SELECT * FROM my_table",
"from": "postgres",
"to": "invalid_dialect",
"pretty": True
}
response = self.app.post('/translate', data=json.dumps(data), content_type='application/json')
self.assertEqual(response.status_code, 400)
self.assertIn('error', json.loads(response.data))
def test_supported_dialect_and_pretty(self):
data = {
"sql": "SELECT * FROM my_table",
"from": "postgres",
"to": "mysql",
"pretty": True
}
response = self.app.post('/translate', data=json.dumps(data), content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertIn('sql', json.loads(response.data))
# assert sql is pretty
expected_sql = dedent("""\
SELECT
*
FROM my_table;""")
self.assertEqual(expected_sql, json.loads(response.data)['sql'])
if __name__ == "__main__":
unittest.main()