-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_app.py
More file actions
258 lines (223 loc) · 8.09 KB
/
test_app.py
File metadata and controls
258 lines (223 loc) · 8.09 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
"""
Testes unitários para Tasks API
Execução: python app.py
"""
import sys
import json
def test_imports():
"""Teste 1: Importações"""
print("🧪 Teste 1: Importando módulos...")
try:
from flask import Flask
from flask_cors import CORS
import sqlite3
print("✓ Todas as dependências importadas com sucesso")
return True
except ImportError as e:
print(f"✗ Erro ao importar: {e}")
return False
def test_app_creation():
"""Teste 2: Criação da aplicação"""
print("\n🧪 Teste 2: Criando aplicação Flask...")
try:
import app as app_module
app = app_module.app
print(f"✓ Aplicação criada: {app}")
print(f"✓ Nome da aplicação: {app.name}")
return True
except Exception as e:
print(f"✗ Erro ao criar aplicação: {e}")
return False
def test_database():
"""Teste 3: Banco de dados"""
print("\n🧪 Teste 3: Testando banco de dados...")
try:
import app as app_module
# Testa inicialização
result = app_module.init_db()
if not result:
print("✗ Falha ao inicializar banco")
return False
print("✓ Banco inicializado")
# Testa conexão
db = app_module.get_db()
cursor = db.execute('SELECT name FROM sqlite_master WHERE type="table"')
tables = cursor.fetchall()
db.close()
if len(tables) > 0:
print(f"✓ Tabelas encontradas: {[t[0] for t in tables]}")
return True
else:
print("✗ Nenhuma tabela criada")
return False
except Exception as e:
print(f"✗ Erro no banco: {e}")
return False
def test_routes():
"""Teste 4: Rotas da API"""
print("\n🧪 Teste 4: Testando rotas...")
try:
import app as app_module
app = app_module.app
routes = []
for rule in app.url_map.iter_rules():
routes.append(f"{','.join(rule.methods)} {rule.rule}")
print(f"✓ {len(routes)} rotas registradas:")
for route in routes:
if 'GET' in route or 'POST' in route or 'PUT' in route or 'DELETE' in route:
print(f" - {route}")
return True
except Exception as e:
print(f"✗ Erro ao listar rotas: {e}")
return False
def test_endpoints():
"""Teste 5: Endpoints funcionais"""
print("\n🧪 Teste 5: Testando endpoints...")
try:
import app as app_module
app = app_module.app
client = app.test_client()
# Teste GET /
print(" Testando GET /...")
response = client.get('/')
assert response.status_code == 200
data = json.loads(response.data)
assert 'message' in data
print(f" ✓ GET / retornou: {data['message']}")
# Teste GET /health
print(" Testando GET /health...")
response = client.get('/health')
assert response.status_code == 200
data = json.loads(response.data)
assert data['status'] == 'healthy'
print(f" ✓ GET /health: {data['status']}")
# Teste GET /tasks (lista vazia)
print(" Testando GET /tasks...")
response = client.get('/tasks')
assert response.status_code == 200
data = json.loads(response.data)
print(f" ✓ GET /tasks retornou {data['count']} tarefas")
# Teste POST /tasks
print(" Testando POST /tasks...")
response = client.post('/tasks',
data=json.dumps({'title': 'Teste', 'description': 'Descrição teste'}),
content_type='application/json'
)
assert response.status_code == 201
data = json.loads(response.data)
assert data['success'] == True
task_id = data['task']['id']
print(f" ✓ POST /tasks criou tarefa ID: {task_id}")
# Teste GET /tasks/<id>
print(f" Testando GET /tasks/{task_id}...")
response = client.get(f'/tasks/{task_id}')
assert response.status_code == 200
data = json.loads(response.data)
assert data['task']['title'] == 'Teste'
print(f" ✓ GET /tasks/{task_id} retornou: {data['task']['title']}")
# Teste PUT /tasks/<id>
print(f" Testando PUT /tasks/{task_id}...")
response = client.put(f'/tasks/{task_id}',
data=json.dumps({'completed': True}),
content_type='application/json'
)
assert response.status_code == 200
data = json.loads(response.data)
assert data['task']['completed'] == True
print(f" ✓ PUT /tasks/{task_id} atualizou para completed=True")
# Teste DELETE /tasks/<id>
print(f" Testando DELETE /tasks/{task_id}...")
response = client.delete(f'/tasks/{task_id}')
assert response.status_code == 200
data = json.loads(response.data)
assert data['success'] == True
print(f" ✓ DELETE /tasks/{task_id} removeu a tarefa")
print("\n✓ Todos os endpoints funcionando corretamente!")
return True
except AssertionError as e:
print(f"✗ Falha em assertion: {e}")
return False
except Exception as e:
print(f"✗ Erro ao testar endpoints: {e}")
import traceback
traceback.print_exc()
return False
def test_validations():
"""Teste 6: Validações"""
print("\n🧪 Teste 6: Testando validações...")
try:
import app as app_module
app = app_module.app
client = app.test_client()
# POST sem title
print(" Testando POST sem title...")
response = client.post('/tasks',
data=json.dumps({'description': 'Sem título'}),
content_type='application/json'
)
assert response.status_code == 400
print(" ✓ Rejeitou POST sem title (400)")
# POST com title vazio
print(" Testando POST com title vazio...")
response = client.post('/tasks',
data=json.dumps({'title': ' '}),
content_type='application/json'
)
assert response.status_code == 400
print(" ✓ Rejeitou POST com title vazio (400)")
# GET de tarefa inexistente
print(" Testando GET de tarefa inexistente...")
response = client.get('/tasks/99999')
assert response.status_code == 404
print(" ✓ Retornou 404 para tarefa inexistente")
# DELETE de tarefa inexistente
print(" Testando DELETE de tarefa inexistente...")
response = client.delete('/tasks/99999')
assert response.status_code == 404
print(" ✓ Retornou 404 para DELETE inexistente")
print("\n✓ Todas as validações funcionando!")
return True
except AssertionError as e:
print(f"✗ Falha em validation: {e}")
return False
except Exception as e:
print(f"✗ Erro ao testar validações: {e}")
return False
def run_all_tests():
"""Executa todos os testes"""
print("="*70)
print("TESTE UNITÁRIO - TASKS API")
print("="*70)
tests = [
test_imports,
test_app_creation,
test_database,
test_routes,
test_endpoints,
test_validations
]
passed = 0
failed = 0
for test in tests:
try:
if test():
passed += 1
else:
failed += 1
except Exception as e:
print(f"✗ Exceção no teste: {e}")
failed += 1
print("\n" + "="*70)
print("RESULTADO FINAL")
print("="*70)
print(f"✓ Passou: {passed}/{len(tests)}")
print(f"✗ Falhou: {failed}/{len(tests)}")
if failed == 0:
print("\n🎉 TODOS OS TESTES PASSARAM! API PRONTA PARA DEPLOY!")
return True
else:
print("\n⚠️ ALGUNS TESTES FALHARAM. CORRIJA ANTES DO DEPLOY.")
return False
if __name__ == '__main__':
success = run_all_tests()
sys.exit(0 if success else 1)