Skip to content

Feature/#11 #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from contextlib import asynccontextmanager

from fastapi import FastAPI
from app.services.database.database import AsyncSessionLocal, init_db

from app.routers.router import setup_router as setup_router_v2
from app.services.database.database import AsyncSessionLocal, init_db
Expand Down
4 changes: 3 additions & 1 deletion app/services/database/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from app.services.database.models.communities import Community
from app.services.database.models.libraries import Library
from app.services.database.models.news import News
from app.services.database.models.subscriptions import Subscription

__all__ = ["Community", "Library", "Subscription"]

__all__ = ["Community", "Library","News", "Subscription"]
32 changes: 32 additions & 0 deletions app/services/database/models/news.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from datetime import datetime
from typing import Optional

from sqlmodel import Field, SQLModel


class News(SQLModel, table=True):
__tablename__ = "news"

# Campos obrigatórios e suas definições
id: Optional[int] = Field(default=None, primary_key=True)
title: str
content: str
category: str
user_email: str
source_url: str
tags: str
social_media_url: str
likes: int = Field(default=0)

# Chaves estrangeiras
community_id: Optional[int] = Field(
default=None,
foreign_key="communities.id")
# library_id: Optional[int]=Field(default=None, foreign_key="libraries.id")

# Campos de data/hora
created_at: Optional[datetime] = Field(default_factory=datetime.now)
updated_at: Optional[datetime] = Field(
default_factory=datetime.now,
sa_column_kwargs={"onupdate": datetime.now}
)
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ async def get_db_session_test() -> AsyncGenerator[AsyncSession, None]:
async def setup_database():
async with test_engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all)
yield test_engine


@pytest_asyncio.fixture(scope="function")
async def session() -> AsyncGenerator[AsyncSession, None]:
async_session_generator = get_db_session_test()
session = await anext(async_session_generator)
session = await anext(async_session_generator) # noqa: F821
yield session
await session.close()

Expand Down
4 changes: 1 addition & 3 deletions tests/test_communities.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import pytest

from services.database.models import Community
from sqlmodel import select
from sqlmodel.ext.asyncio.session import AsyncSession

from services.database.models import Community


@pytest.mark.asyncio
async def test_insert_communities(session: AsyncSession):
Expand Down
6 changes: 4 additions & 2 deletions tests/test_database.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# Test cases for database SQLite using in memory database fixture from conftest.py
#
"""
Test cases for database SQLite using in memory
database fixture from conftest.py
"""
29 changes: 14 additions & 15 deletions tests/test_libraries.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import pytest
import pytest_asyncio

from services.database.models import Community, Library
from sqlmodel import select
from sqlmodel.ext.asyncio.session import AsyncSession

from services.database.models import Community
from services.database.models import Library

@pytest_asyncio.fixture
async def community(session: AsyncSession):
Expand All @@ -15,25 +13,26 @@ async def community(session: AsyncSession):
await session.refresh(community)
return community


@pytest.mark.asyncio
async def test_insert_libraries(session: AsyncSession, community: Community):
library = Library(
library = Library(
library_name="DevOps",
user_email="teste@teste.com",
releases_url="http://teste.com",
logo="logo",
community_id=community.id,
)
session.add(library)
await session.commit()
session.add(library)
await session.commit()

statement = select(Library).where(Library.library_name == "DevOps")
result = await session.exec(statement)
found = result.first()
statement = select(Library).where(Library.library_name == "DevOps")
result = await session.exec(statement)
found = result.first()

assert found is not None
assert found.library_name == "DevOps"
assert found.user_email == "teste@teste.com"
assert found.releases_url == "http://teste.com"
assert found.logo == "logo"
assert found.community_id == community.id
assert found is not None
assert found.library_name == "DevOps"
assert found.user_email == "teste@teste.com"
assert found.releases_url == "http://teste.com"
assert found.logo == "logo"
assert found.community_id == community.id
54 changes: 54 additions & 0 deletions tests/test_news.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,64 @@
from datetime import datetime
from typing import Mapping

import pytest
import pytest_asyncio
from fastapi import status
from httpx import AsyncClient
from services.database.models import Community, News
from sqlmodel import select
from sqlmodel.ext.asyncio.session import AsyncSession


@pytest_asyncio.fixture
async def community(session: AsyncSession):
community = Community(username="admin", email="a@a.com", password="123")
session.add(community)
await session.commit()
await session.refresh(community)
return community


@pytest.mark.asyncio
async def test_insert_libraries(session: AsyncSession, community: Community):
"""
Testa a inserção de uma notícia no banco de dados.
"""
news = News(
title="Python 3.12 Lançado!",
content="A nova versão do Python traz melhorias ...",
category="release",
user_email="dev@example.com",
source_url="https://python.org/news",
tags="python, release, programming",
social_media_url="https://linkedin.com/pythonista",
community_id=community.id, # Usando o ID da comunidade do fixture
)
session.add(news)
await session.commit()

statement = select(News).where(News.title == "Python 3.12 Lançado!")
result = await session.exec(statement)
found_news = result.first()

assert found_news is not None
assert found_news.title == "Python 3.12 Lançado!"
assert found_news.content == "A nova versão do Python traz melhorias ..."
assert found_news.category == "release"
assert found_news.user_email == "dev@example.com"
assert found_news.source_url == "https://python.org/news"
assert found_news.tags == "python, release, programming"
assert found_news.social_media_url == "https://linkedin.com/pythonista"
assert found_news.likes == 0
assert found_news.community_id == community.id
assert isinstance(found_news.created_at, datetime)
assert isinstance(found_news.updated_at, datetime)
assert found_news.created_at <= datetime.now()
assert found_news.updated_at >= found_news.created_at


# ADD like test case for News model

@pytest.mark.asyncio
async def test_news_endpoint(
async_client: AsyncClient, mock_headers: Mapping[str, str]
Expand Down