Pipelinen parannuksia building blockeilla
This commit is contained in:
132
zipit/template_runs/tmpl_blog/test_main.py
Normal file
132
zipit/template_runs/tmpl_blog/test_main.py
Normal file
@@ -0,0 +1,132 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from main import app, get_db
|
||||
from models import Base
|
||||
|
||||
TEST_DB = "sqlite:///./test.db"
|
||||
test_engine = create_engine(TEST_DB, connect_args={"check_same_thread": False})
|
||||
TestSession = sessionmaker(autocommit=False, autoflush=False, bind=test_engine)
|
||||
Base.metadata.create_all(bind=test_engine)
|
||||
|
||||
def override_get_db():
|
||||
db = TestSession()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
client = TestClient(app)
|
||||
|
||||
def test_create_user():
|
||||
response = client.post('/users/', json={"username": "Test username", "email": "Test email", "password_hash": "Test password_hash"})
|
||||
assert response.status_code == 201
|
||||
data = response.json()
|
||||
assert "id" in data
|
||||
|
||||
def test_list_users():
|
||||
client.post('/users/', json={"username": "Test username", "email": "Test email", "password_hash": "Test password_hash"})
|
||||
response = client.get('/users/')
|
||||
assert response.status_code == 200
|
||||
assert len(response.json()) >= 1
|
||||
|
||||
def test_get_user_by_id():
|
||||
created = client.post('/users/', json={"username": "Test username", "email": "Test email", "password_hash": "Test password_hash"}).json()
|
||||
item_id = created['id']
|
||||
response = client.get(f'/users/{item_id}')
|
||||
assert response.status_code == 200
|
||||
assert response.json()['id'] == item_id
|
||||
|
||||
def test_get_user_not_found():
|
||||
response = client.get('/users/99999')
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_update_user():
|
||||
created = client.post('/users/', json={"username": "Test username", "email": "Test email", "password_hash": "Test password_hash"}).json()
|
||||
item_id = created['id']
|
||||
response = client.put(f'/users/{item_id}', json={"username": "Updated username", "email": "Test email", "password_hash": "Test password_hash"})
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_delete_user():
|
||||
created = client.post('/users/', json={"username": "Test username", "email": "Test email", "password_hash": "Test password_hash"}).json()
|
||||
item_id = created['id']
|
||||
response = client.delete(f'/users/{item_id}')
|
||||
assert response.status_code == 204
|
||||
response = client.get(f'/users/{item_id}')
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_create_article():
|
||||
response = client.post('/articles/', json={"title": "Test title", "content": "Test content", "author_id": 1})
|
||||
assert response.status_code == 201
|
||||
data = response.json()
|
||||
assert "id" in data
|
||||
|
||||
def test_list_articles():
|
||||
client.post('/articles/', json={"title": "Test title", "content": "Test content", "author_id": 1})
|
||||
response = client.get('/articles/')
|
||||
assert response.status_code == 200
|
||||
assert len(response.json()) >= 1
|
||||
|
||||
def test_get_article_by_id():
|
||||
created = client.post('/articles/', json={"title": "Test title", "content": "Test content", "author_id": 1}).json()
|
||||
item_id = created['id']
|
||||
response = client.get(f'/articles/{item_id}')
|
||||
assert response.status_code == 200
|
||||
assert response.json()['id'] == item_id
|
||||
|
||||
def test_get_article_not_found():
|
||||
response = client.get('/articles/99999')
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_update_article():
|
||||
created = client.post('/articles/', json={"title": "Test title", "content": "Test content", "author_id": 1}).json()
|
||||
item_id = created['id']
|
||||
response = client.put(f'/articles/{item_id}', json={"title": "Updated title", "content": "Test content", "author_id": 1})
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_delete_article():
|
||||
created = client.post('/articles/', json={"title": "Test title", "content": "Test content", "author_id": 1}).json()
|
||||
item_id = created['id']
|
||||
response = client.delete(f'/articles/{item_id}')
|
||||
assert response.status_code == 204
|
||||
response = client.get(f'/articles/{item_id}')
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_create_comment():
|
||||
response = client.post('/comments/', json={"content": "Test content", "article_id": 1, "author_id": 1})
|
||||
assert response.status_code == 201
|
||||
data = response.json()
|
||||
assert "id" in data
|
||||
|
||||
def test_list_comments():
|
||||
client.post('/comments/', json={"content": "Test content", "article_id": 1, "author_id": 1})
|
||||
response = client.get('/comments/')
|
||||
assert response.status_code == 200
|
||||
assert len(response.json()) >= 1
|
||||
|
||||
def test_get_comment_by_id():
|
||||
created = client.post('/comments/', json={"content": "Test content", "article_id": 1, "author_id": 1}).json()
|
||||
item_id = created['id']
|
||||
response = client.get(f'/comments/{item_id}')
|
||||
assert response.status_code == 200
|
||||
assert response.json()['id'] == item_id
|
||||
|
||||
def test_get_comment_not_found():
|
||||
response = client.get('/comments/99999')
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_update_comment():
|
||||
created = client.post('/comments/', json={"content": "Test content", "article_id": 1, "author_id": 1}).json()
|
||||
item_id = created['id']
|
||||
response = client.put(f'/comments/{item_id}', json={"content": "Updated content", "article_id": 1, "author_id": 1})
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_delete_comment():
|
||||
created = client.post('/comments/', json={"content": "Test content", "article_id": 1, "author_id": 1}).json()
|
||||
item_id = created['id']
|
||||
response = client.delete(f'/comments/{item_id}')
|
||||
assert response.status_code == 204
|
||||
response = client.get(f'/comments/{item_id}')
|
||||
assert response.status_code == 404
|
||||
Reference in New Issue
Block a user