From 735457896692a53b7cfe48dbc380e221246c9047 Mon Sep 17 00:00:00 2001 From: Angel-Dijoux Date: Mon, 1 Jan 2024 00:17:20 +0100 Subject: [PATCH] feat: init factories and write first test. --- Pipfile | 1 + Pipfile.lock | 26 +++++++++++++++++++++- src/tests/factories/factories.py | 31 +++++++++++++++++++++++++++ src/tests/factories/test_factories.py | 20 +++++++++++++++++ src/tests/fixtures/service.py | 7 +++--- 5 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 src/tests/factories/factories.py create mode 100644 src/tests/factories/test_factories.py diff --git a/Pipfile b/Pipfile index 4322bb4..c6dad79 100644 --- a/Pipfile +++ b/Pipfile @@ -27,6 +27,7 @@ pytest-cov = "*" pytest-mock = "*" pytest-socket = "*" pytest-sugar = "*" +factory-boy = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index 228eaa9..b03a8db 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "608c46b9e92eb8aa82ddf798eda06b1a3bfa51067e6f9902a1a3e4a3db08753e" + "sha256": "abe3726e4522db276d215f9a52aaeb15ee4fbaa2c4d416472a48df2cd84de5a7" }, "pipfile-spec": 6, "requires": { @@ -244,6 +244,22 @@ "markers": "python_version >= '3.5'", "version": "==5.1.1" }, + "factory-boy": { + "hashes": [ + "sha256:a2cdbdb63228177aa4f1c52f4b6d83fab2b8623bf602c7dedd7eb83c0f69c04c", + "sha256:bc76d97d1a65bbd9842a6d722882098eb549ec8ee1081f9fb2e8ff29f0c300f1" + ], + "index": "pypi", + "version": "==3.3.0" + }, + "faker": { + "hashes": [ + "sha256:1d5dc0a75da7bc40741ee4c84d99dc087b97bd086d4222ad06ac4dd2219bcf3f", + "sha256:9c22c0a734ca01c6e4f2259eab5dab9081905a9d67b27272aea5c9feeb5a3789" + ], + "markers": "python_version >= '3.8'", + "version": "==22.0.0" + }, "flasgger": { "hashes": [ "sha256:0603941cf4003626b4ee551ca87331f1d17b8eecce500ccf1a1f1d3a332fc94a", @@ -677,6 +693,14 @@ "index": "pypi", "version": "==0.9.7" }, + "python-dateutil": { + "hashes": [ + "sha256:0123cacc1627ae19ddf3c27a5de5bd67ee4586fbdd6440d9748f8abb483d3e86", + "sha256:961d03dc3453ebbc59dbdea9e4e11c5651520a876d0f4db161e8674aae935da9" + ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==2.8.2" + }, "python-dotenv": { "hashes": [ "sha256:b7e3b04a59693c42c36f9ab1cc2acc46fa5df8c78e178fc33a8d4cd05c8d498f", diff --git a/src/tests/factories/factories.py b/src/tests/factories/factories.py new file mode 100644 index 0000000..ed1a1a5 --- /dev/null +++ b/src/tests/factories/factories.py @@ -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" diff --git a/src/tests/factories/test_factories.py b/src/tests/factories/test_factories.py new file mode 100644 index 0000000..9a83f9c --- /dev/null +++ b/src/tests/factories/test_factories.py @@ -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 diff --git a/src/tests/fixtures/service.py b/src/tests/fixtures/service.py index 7b404c7..b38915d 100644 --- a/src/tests/fixtures/service.py +++ b/src/tests/fixtures/service.py @@ -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 @@ -9,13 +10,13 @@ @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 @@ -23,7 +24,7 @@ def db(app): @pytest.fixture(scope="module") -def db_session(db): +def db_session(db: SQLAlchemy) -> Session: yield db.session db.session.rollback() db.session.close()