-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Atualizando o código fonte das aulas 4-12 com a fixture de pausar o t…
…empo related #251
- Loading branch information
1 parent
e54a859
commit 6e18d94
Showing
18 changed files
with
262 additions
and
207 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,25 @@ | ||
from contextlib import contextmanager | ||
from dataclasses import asdict | ||
from datetime import datetime | ||
|
||
from sqlalchemy import event, select | ||
from sqlalchemy import select | ||
|
||
from fast_zero.models import User | ||
|
||
|
||
@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) | ||
|
||
|
||
def test_create_user(session): | ||
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')) | ||
user = session.scalar(select(User).where(User.username == 'alice')) | ||
|
||
assert asdict(user) == { | ||
'id': 1, | ||
'username': 'alice', | ||
'password': 'secret', | ||
'email': 'teste@test', | ||
'created_at': time, | ||
'updated_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
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') | ||
|
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,43 +1,25 @@ | ||
from contextlib import contextmanager | ||
from dataclasses import asdict | ||
from datetime import datetime | ||
|
||
from sqlalchemy import event, select | ||
from sqlalchemy import select | ||
|
||
from fast_zero.models import User | ||
|
||
|
||
@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) | ||
|
||
|
||
def test_create_user(session): | ||
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')) | ||
user = session.scalar(select(User).where(User.username == 'alice')) | ||
|
||
assert asdict(user) == { | ||
'id': 1, | ||
'username': 'alice', | ||
'password': 'secret', | ||
'email': 'teste@test', | ||
'created_at': time, | ||
'updated_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,43 +1,25 @@ | ||
from contextlib import contextmanager | ||
from dataclasses import asdict | ||
from datetime import datetime | ||
|
||
from sqlalchemy import event, select | ||
from sqlalchemy import select | ||
|
||
from fast_zero.models import User | ||
|
||
|
||
@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) | ||
|
||
|
||
def test_create_user(session): | ||
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')) | ||
user = session.scalar(select(User).where(User.username == 'alice')) | ||
|
||
assert asdict(user) == { | ||
'id': 1, | ||
'username': 'alice', | ||
'password': 'secret', | ||
'email': 'teste@test', | ||
'created_at': time, | ||
'updated_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,43 +1,25 @@ | ||
from contextlib import contextmanager | ||
from dataclasses import asdict | ||
from datetime import datetime | ||
|
||
from sqlalchemy import event, select | ||
from sqlalchemy import select | ||
|
||
from fast_zero.models import User | ||
|
||
|
||
@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) | ||
|
||
|
||
def test_create_user(session): | ||
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')) | ||
user = session.scalar(select(User).where(User.username == 'alice')) | ||
|
||
assert asdict(user) == { | ||
'id': 1, | ||
'username': 'alice', | ||
'password': 'secret', | ||
'email': 'teste@test', | ||
'created_at': time, | ||
'updated_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
Oops, something went wrong.