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
12
12
Faker==4.0.2
13
13
14
14
# 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 1
1
import pytest
2
+ from _pytest .fixtures import FixtureRequest
2
3
from flask import Flask
3
4
from flask .testing import FlaskClient
4
5
5
6
from time_tracker_api import create_app
6
7
8
+ CONFIGURATIONS = ['AzureSQLDatabaseDevelopTestConfig' ]
7
9
8
- @pytest .fixture (scope = 'session' )
9
- def app () -> Flask :
10
+
11
+ @pytest .fixture (scope = 'session' , params = CONFIGURATIONS )
12
+ def app (request : FixtureRequest ) -> Flask :
10
13
"""An instance of the app for tests"""
11
- return create_app ()
14
+ return create_app ("time_tracker_api.config.%s" % request . param )
12
15
13
16
14
17
@pytest .fixture
15
18
def client (app : Flask ) -> FlaskClient :
16
19
"""A test client for the app."""
17
20
with app .test_client () as c :
18
21
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
+
1
4
class Config :
2
5
DEBUG = False
3
6
4
7
5
- class WhateverDevelopConfig (Config ):
8
+ class DevelopConfig (Config ):
6
9
DEBUG = True
7
10
FLASK_DEBUG = True
8
11
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
10
25
11
26
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:
13
13
"""Make the app ready to use the database"""
14
14
database_strategy_name = app .config ['DATABASE' ]
15
15
with app .app_context ():
16
- module = globals ()["use_%s" % database_strategy_name ]()
16
+ module = globals ()["use_%s" % database_strategy_name ](app )
17
17
global RepositoryModel
18
18
RepositoryModel = module .repository_model
19
19
@@ -23,6 +23,7 @@ def create(model_name: str):
23
23
return RepositoryModel (model_name )
24
24
25
25
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