Skip to content

Commit

Permalink
Atualizando o código fonte das aulas 4-12 com a fixture de pausar o t…
Browse files Browse the repository at this point in the history
…empo

related #251
  • Loading branch information
dunossauro committed Oct 5, 2024
1 parent e54a859 commit 6e18d94
Show file tree
Hide file tree
Showing 18 changed files with 262 additions and 207 deletions.
26 changes: 25 additions & 1 deletion codigo_das_aulas/04/tests/conftest.py
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 fast_zero.app import app
Expand All @@ -21,3 +24,24 @@ def session():
yield 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
26 changes: 4 additions & 22 deletions codigo_das_aulas/04/tests/test_db.py
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
}
26 changes: 25 additions & 1 deletion codigo_das_aulas/05/tests/conftest.py
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

Expand Down Expand Up @@ -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')
Expand Down
26 changes: 4 additions & 22 deletions codigo_das_aulas/05/tests/test_db.py
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
}
26 changes: 25 additions & 1 deletion codigo_das_aulas/06/tests/conftest.py
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

Expand Down Expand Up @@ -37,6 +40,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(
Expand Down
26 changes: 4 additions & 22 deletions codigo_das_aulas/06/tests/test_db.py
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
}
26 changes: 25 additions & 1 deletion codigo_das_aulas/07/tests/conftest.py
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

Expand Down Expand Up @@ -37,6 +40,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(
Expand Down
26 changes: 4 additions & 22 deletions codigo_das_aulas/07/tests/test_db.py
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
}
26 changes: 25 additions & 1 deletion codigo_das_aulas/08/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from contextlib import contextmanager
from datetime import datetime

import factory
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

Expand Down Expand Up @@ -52,6 +55,27 @@ def user(session):
return 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)


@pytest.fixture
def mock_db_time():
return _mock_db_time


@pytest.fixture
def other_user(session):
password = 'testtest'
Expand Down
Loading

0 comments on commit 6e18d94

Please sign in to comment.