template_flask/tests/test_app.py

50 lines
No EOL
1.5 KiB
Python

import unittest
import os
import tempfile
from app import create_app, db
from app.models import User
class BasicTestCase(unittest.TestCase):
def setUp(self):
"""Set up test client and database."""
self.app = create_app("testing")
self.client = self.app.test_client()
with self.app.app_context():
db.create_all()
def tearDown(self):
"""Clean up database after tests."""
with self.app.app_context():
db.session.remove()
db.drop_all()
def test_index_route(self):
"""Test home page route."""
response = self.client.get("/")
self.assertEqual(response.status_code, 200)
data = response.get_json()
self.assertEqual(data["message"], "Flask App is running")
def test_health_route(self):
"""Test health check endpoint."""
response = self.client.get("/health")
self.assertEqual(response.status_code, 200)
data = response.get_json()
self.assertEqual(data["status"], "healthy")
def test_create_user(self):
"""Test creating a new user."""
with self.app.app_context():
user = User(username="testuser", email="test@example.com")
db.session.add(user)
db.session.commit()
# Verify user was created
found_user = User.query.first()
self.assertIsNotNone(found_user)
self.assertEqual(found_user.username, "testuser")
if __name__ == "__main__":
unittest.main()