Skip to content

Commit

Permalink
feat: init factories and write first test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel-Dijoux committed Dec 31, 2023
1 parent 921e424 commit 7354578
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pytest-cov = "*"
pytest-mock = "*"
pytest-socket = "*"
pytest-sugar = "*"
factory-boy = "*"

[dev-packages]

Expand Down
26 changes: 25 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions src/tests/factories/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import factory
from src import models


class UserFactory(factory.Factory):
class Meta:
model = models.User

username = factory.Faker("uuidv4")
email = factory.Faker("uuidv4")


class UserFavorisFactory(factory.Factory):
class Meta:
model = models.UserFavori


class FormationFactory(factory.Factory):
class Meta:
model = models.Formation

code_nsf = 334
type = "baccalauréat technologique"
libelle = (
"bac techno STHR Sciences et technologies de l'hôtellerie et de la restauration"
)
tutelle = "Ministère chargé de l'Éducation nationale et de la Jeunesse"
url = "http://www.onisep.fr/http/redirection/formation/slug/FOR.494"
domain = "hôtellerie-restauration, tourisme/hôtellerie | hôtellerie-restauration, tourisme/restauration"
niveau_de_sortie = "Bac ou équivalent"
duree = "1 an"
20 changes: 20 additions & 0 deletions src/tests/factories/test_factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import uuid
from sqlalchemy import func
from src.models.formation import Formation
from src.tests.factories.factories import FormationFactory


def test_check_if_formation_table_is_empty_should_return_0(db_session):
formation_count = db_session.query(func.count(Formation.id)).scalar()
assert formation_count == 0


def test_create_one_formation_should_return_1(db_session):
formation: Formation = FormationFactory()
db_session.add(formation)
db_session.commit()

assert formation.id is not None
assert isinstance(formation.id, uuid.UUID)
formation_count = db_session.query(func.count(Formation.id)).scalar()
assert formation_count == 1
7 changes: 4 additions & 3 deletions src/tests/fixtures/service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Generator
import pytest
from flask import Flask
from sqlalchemy.orm import Session
from flask.testing import FlaskClient
from flask_sqlalchemy import SQLAlchemy

Expand All @@ -9,21 +10,21 @@


@pytest.fixture(autouse=True, scope="session")
def app():
def app() -> Flask:
app = create_app("testing")
return app


@pytest.fixture(scope="module")
def db(app):
def db(app: Flask) -> SQLAlchemy:
with app.app_context():
_db.create_all()
yield _db
_db.drop_all()


@pytest.fixture(scope="module")
def db_session(db):
def db_session(db: SQLAlchemy) -> Session:
yield db.session
db.session.rollback()
db.session.close()
Expand Down

0 comments on commit 7354578

Please sign in to comment.