File tree Expand file tree Collapse file tree 8 files changed +99
-21
lines changed Expand file tree Collapse file tree 8 files changed +99
-21
lines changed Original file line number Diff line number Diff line change @@ -12,4 +12,7 @@ pytest-mock==2.0.0
1212Faker==4.0.2
1313
1414# Coverage
15- coverage==4.5.1
15+ coverage==4.5.1
16+
17+ # SQL database (MS SQL)
18+ flask_sqlalchemy==2.4.1
Original file line number Diff line number Diff line change 11import pytest
2+ from _pytest .fixtures import FixtureRequest
23from flask import Flask
34from flask .testing import FlaskClient
45
56from time_tracker_api import create_app
67
8+ CONFIGURATIONS = ['AzureSQLDatabaseDevelopTestConfig' ]
79
8- @pytest .fixture (scope = 'session' )
9- def app () -> Flask :
10+
11+ @pytest .fixture (scope = 'session' , params = CONFIGURATIONS )
12+ def app (request : FixtureRequest ) -> Flask :
1013 """An instance of the app for tests"""
11- return create_app ()
14+ return create_app ("time_tracker_api.config.%s" % request . param )
1215
1316
1417@pytest .fixture
1518def client (app : Flask ) -> FlaskClient :
1619 """A test client for the app."""
1720 with app .test_client () as c :
1821 return c
22+
23+
24+ @pytest .fixture ()
25+ def repository (app : Flask ):
26+ with app .app_context ():
27+ from .resources import TestModel
28+ from time_tracker_api .sql_repository import db
29+
30+ db .create_all ()
31+ print ("Models for test created!" )
32+
33+ from time_tracker_api .sql_repository import SQLRepository
34+ return SQLRepository (TestModel )
Original file line number Diff line number Diff line change 1+ from time_tracker_api .sql_repository import db
2+
3+
4+ class TestModel (db .Model ):
5+ id = db .Column (db .Integer , primary_key = True )
6+ name = db .Column (db .String (80 ), unique = True , nullable = False )
7+ email = db .Column (db .String (120 ), unique = True , nullable = False )
8+ age = db .Column (db .Integer , nullable = False )
9+
10+ def __repr__ (self ):
11+ return '<Test Model %r>' % self .name
Original file line number Diff line number Diff line change 1+ from faker import Faker
2+ from flask import Flask
3+
4+ fake = Faker ()
5+
6+
7+ def test_create_entry (repository , app : Flask ):
8+ """Should create a new Entry"""
9+ assert repository is not None
10+ from .resources import TestModel
11+ sample_model = TestModel (name = fake .name (),
12+ email = fake .safe_email (),
13+ age = fake .pyint (min_value = 10 , max_value = 80 ))
14+
15+ repository .create (sample_model )
Original file line number Diff line number Diff line change 1+ import os
2+
3+
14class Config :
25 DEBUG = False
36
47
5- class WhateverDevelopConfig (Config ):
8+ class DevelopConfig (Config ):
69 DEBUG = True
710 FLASK_DEBUG = True
811 FLASK_ENV = "develop"
9- DATABASE = "whatever"
12+ DATABASE_URI = os .environ .get ('DATABASE_URI' )
13+
14+
15+ class AzureSQLDatabaseDevelopConfig (DevelopConfig ):
16+ DATABASE = 'sql'
17+ TEST_TABLE = 'tests'
18+ SQLALCHEMY_COMMIT_ON_TEARDOWN = True
19+ SQLALCHEMY_TRACK_MODIFICATIONS = False
20+
21+
22+ class AzureSQLDatabaseDevelopTestConfig (AzureSQLDatabaseDevelopConfig ):
23+ DEBUG = True
24+ TESTING = True
1025
1126
12- DefaultConfig = WhateverDevelopConfig
27+ DefaultConfig = AzureSQLDatabaseDevelopConfig
Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ def init_app(app: Flask) -> None:
1313 """Make the app ready to use the database"""
1414 database_strategy_name = app .config ['DATABASE' ]
1515 with app .app_context ():
16- module = globals ()["use_%s" % database_strategy_name ]()
16+ module = globals ()["use_%s" % database_strategy_name ](app )
1717 global RepositoryModel
1818 RepositoryModel = module .repository_model
1919
@@ -23,6 +23,7 @@ def create(model_name: str):
2323 return RepositoryModel (model_name )
2424
2525
26- def use_whatever ():
27- from time_tracker_api import whatever_repository
28- return whatever_repository
26+ def use_sql (app : Flask ) -> None :
27+ from time_tracker_api import sql_repository
28+ sql_repository .init_app (app )
29+ return sql_repository
Original file line number Diff line number Diff line change 1+ import urllib .parse
2+
3+ from flask import Flask
4+ from flask_sqlalchemy import SQLAlchemy
5+
6+ db = None
7+
8+
9+ def init_app (app : Flask ) -> None :
10+ app .config ['SQLALCHEMY_DATABASE_URI' ] = app .config ['DATABASE_URI' ]
11+ global db
12+ db = SQLAlchemy (app )
13+
14+
15+ class SQLRepository ():
16+ def __init__ (self , model_type : type ):
17+ self .model_type = model_type
18+
19+ def create (self , model : dict ) -> dict :
20+ db .session .add (model )
21+ db .session .commit ()
22+
23+ def find_all (self ):
24+ return self .model_type .query .all ()
25+
26+
27+ repository_model = SQLRepository
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments