-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #252 from dunossauro/251_sqlalchemy_event_datetime
Adicionando evento do sqlalchemy para testes de datetime
- Loading branch information
Showing
28 changed files
with
644 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,25 @@ | ||
from dataclasses import asdict | ||
|
||
from sqlalchemy import select | ||
|
||
from fast_zero.models import User | ||
|
||
|
||
def test_create_user(session): | ||
new_user = User(username='alice', password='secret', email='teste@test') | ||
session.add(new_user) | ||
session.commit() | ||
def test_create_user(session, mock_db_time): | ||
with mock_db_time(model=User) as time: | ||
new_user = User( | ||
username='alice', password='secret', email='teste@test' | ||
) | ||
session.add(new_user) | ||
session.commit() | ||
|
||
user = session.scalar(select(User).where(User.username == 'alice')) | ||
|
||
assert user.username == 'alice' | ||
assert asdict(user) == { | ||
'id': 1, | ||
'username': 'alice', | ||
'password': 'secret', | ||
'email': 'teste@test', | ||
'created_at': time, | ||
'updated_at': time, # Exercício | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
from contextlib import contextmanager | ||
from datetime import datetime | ||
|
||
import pytest | ||
from fastapi.testclient import TestClient | ||
from sqlalchemy import create_engine | ||
from sqlalchemy import create_engine, event | ||
from sqlalchemy.orm import Session | ||
from sqlalchemy.pool import StaticPool | ||
|
||
|
@@ -36,6 +39,27 @@ def session(): | |
table_registry.metadata.drop_all(engine) | ||
|
||
|
||
@contextmanager | ||
def _mock_db_time(*, model, time=datetime(2024, 1, 1)): | ||
|
||
def fake_time_handler(mapper, connection, target): | ||
if hasattr(target, 'created_at'): | ||
target.created_at = time | ||
if hasattr(target, 'updated_at'): | ||
target.updated_at = time | ||
|
||
event.listen(model, 'before_insert', fake_time_handler) | ||
|
||
yield time | ||
|
||
event.remove(model, 'before_insert', fake_time_handler) | ||
|
||
|
||
@pytest.fixture | ||
def mock_db_time(): | ||
return _mock_db_time | ||
|
||
|
||
@pytest.fixture | ||
def user(session): | ||
user = User(username='Teste', email='[email protected]', password='testtest') | ||
|
Oops, something went wrong.