Spaces:
Runtime error
Runtime error
Commit ·
c8a050e
1
Parent(s): bb4b25f
test_post.py added
Browse filesFiles changed (4)
- app/routers/post.py+1-1
- tests/database.py → tests/conftest.pyRenamed+44-1
- tests/test_posts.pyAdded+48
- tests/test_users.py+13-13
24 unmodified lines2526272829303124 unmodified linespost.votes = vote_countposts.append(post)return posts@router.post("/", response_model=PostResponse)def create_post(post: PostCreate, db: Session = Depends(get_db), current_user: User= Depends(Get_Current_User)):new_post = Post(owner_id=current_user.id,**post.model_dump())db.add(new_post)24 unmodified lines2526272829303124 unmodified linespost.votes = vote_countposts.append(post)return posts@router.post("/", response_model=PostResponse,status_code=status.HTTP_201_CREATED)def create_post(post: PostCreate, db: Session = Depends(get_db), current_user: User= Depends(Get_Current_User)):new_post = Post(owner_id=current_user.id,**post.model_dump())db.add(new_post)
4 unmodified lines567891019 unmodified lines303132334 unmodified linesfrom sqlalchemy import create_enginefrom sqlalchemy.orm import sessionmakerfrom app.config import settingSQLALCHEMY_DATABASE_URL = f"postgresql://{setting.database_username}:{setting.database_password}@{setting.database_hostname}:{setting.database_port}/{setting.database_name}_Test"19 unmodified linesfinally:session.close()app.dependency_overrides[get_db] = override_get_dbyield TestClient(app)4 unmodified lines5678910111219 unmodified lines3233343536373839404142434445464748495051525354555657585960616263646566676869707172737475764 unmodified linesfrom sqlalchemy import create_enginefrom sqlalchemy.orm import sessionmakerfrom app.config import settingfrom app.oauth2 import Create_Access_Tokenfrom app.models import PostSQLALCHEMY_DATABASE_URL = f"postgresql://{setting.database_username}:{setting.database_password}@{setting.database_hostname}:{setting.database_port}/{setting.database_name}_Test"19 unmodified linesfinally:session.close()app.dependency_overrides[get_db] = override_get_dbyield TestClient(app)@pytest.fixturedef create_test_user(client):user_data={"email": "abdullah@gmail.com","password": "abdullah1234"}response = client.post("/users/",json=user_data)assert response.status_code == 201new_user=response.json()new_user["password"]="abdullah1234"return new_user@pytest.fixturedef token(create_test_user):return Create_Access_Token({"id": create_test_user["id"]})@pytest.fixturedef authorize_client(client,token):client.headers = {**client.headers,"Authorization": f"Bearer {token}"}return client@pytest.fixturedef test_posts(create_test_user,session):posts_data = [{"title": "first title", "content": "first content", "owner_id": create_test_user['id']},{"title": "2nd title", "content": "2nd content", "owner_id": create_test_user['id']},{"title": "3rd title", "content": "3rd content", "owner_id": create_test_user['id']},]def create_post(post):return Post(**post)post=map(create_post,posts_data)post=list(post)session.add_all(post)session.commit()posts=session.query(Post).all()return posts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748from urllib import response
import pytestfrom typing import Listfrom pydantic import TypeAdapterfrom app.schemas import PostResponsefrom app.schemas.post import PostVotefrom tests.conftest import authorize_client
def test_get_all_posts(authorize_client,test_posts): response = authorize_client.get("/posts/") posts = TypeAdapter(List[PostResponse]).validate_python(response.json()) assert response.status_code == 200 assert len(posts) == len(test_posts)
def test_unauthorized_get_all_post(client,test_posts): response=client.get("/posts/") assert response.status_code == 401
def test_unauthorized_get_one_post(client,test_posts): response=client.get(f"/posts/{test_posts[0].id}") assert response.status_code == 401
def test_get_one_post_not_exit(authorize_client): response=authorize_client.get(f"/posts/-9999") assert response.status_code == 404
def test_get_one_post(authorize_client,test_posts): response=authorize_client.get(f"/posts/{test_posts[0].id}") post=PostVote(**response.json()) assert post.id == test_posts[0].id @pytest.mark.parametrize("title, content, published", [ ("MHA","Anime of the Year",True), ("To be Hero X","Can be Considerd to be Anime of the Year",True), ("Wind Breaker","Watched",False)])def test_create_post(authorize_client,title,content,published,create_test_user): response =authorize_client.post("/posts/",json={"title": title, "content": content, "published": published}) created_post = PostResponse(**response.json()) assert response.status_code == 201 assert created_post.title == title assert created_post.content == content assert created_post.published == published assert created_post.owner_id == create_test_user['id'] def test_unauthorized_user_create_post(client,test_posts): response =client.post("/posts/",json={"title": "Anime", "content": "Worth Watching", "published": True}) assert response.status_code == 4011 unmodified line2345678910111213141516171819209 unmodified lines3031321 unmodified linefrom app.config import settingfrom app.schemas import user, tokenfrom jose import jwtfrom tests.database import client, session@pytest.fixturedef create_test_user(client):user_data={"email": "abdullah@gmail.com","password": "abdullah1234"}response = client.post("/users/",json=user_data)assert response.status_code == 201new_user=response.json()new_user["password"]="abdullah1234"return new_userdef test_create_user(client):response = client.post("/users/", json={"email": "abdullah@gmail.com", "password": "abdullah1234"})9 unmodified linesassert id == create_test_user["id"]assert login_response.token_type == "bearer"assert response.status_code==2001 unmodified line2345679 unmodified lines171819202122232425262728293031321 unmodified linefrom app.config import settingfrom app.schemas import user, tokenfrom jose import jwtdef test_create_user(client):response = client.post("/users/", json={"email": "abdullah@gmail.com", "password": "abdullah1234"})9 unmodified linesassert id == create_test_user["id"]assert login_response.token_type == "bearer"assert response.status_code==200@pytest.mark.parametrize("email, password, status_code", [("abdullah@gmail.com", "wrongpassword", 403),("wrong@gmail.com", "abdullah1234", 403),("wrong@gmail.com", "wrongpassword", 403),(None, None, 422),("abdullah@gmail.com", None, 422),(None, "abdullah1234", 422),("abdullah@gmail.com", "abdullah1234", 200),])def test_login_parametrized(client, create_test_user, email, password, status_code):response = client.post("/login", data={"username": email, "password": password})assert response.status_code == status_code
app/routers/post.py+1-1
tests/database.py → tests/conftest.pyRenamed+44-1
tests/test_posts.pyAdded+48
tests/test_users.py+13-13