-
Notifications
You must be signed in to change notification settings - Fork 0
Create configuration and database routing #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Angeluz-07
merged 3 commits into
master
from
feature/Create-configuration-and-database-routing#23
Mar 16, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,10 @@ | |
# For development | ||
|
||
# Tests | ||
pytest==4.1.1 | ||
pytest==5.2.0 | ||
|
||
# Mocking | ||
pytest-mock==2.0.0 | ||
|
||
# Coverage | ||
coverage==4.5.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
|
||
import pytest | ||
from flask import Flask | ||
from flask.testing import FlaskClient | ||
|
||
from time_tracker_api import create_app | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def app(): | ||
def app() -> Flask: | ||
"""An instance of the app for tests""" | ||
return create_app() | ||
|
||
|
||
@pytest.fixture | ||
def client(app): | ||
def client(app: Flask) -> FlaskClient: | ||
"""A test client for the app.""" | ||
with app.test_client() as c: | ||
return c | ||
return c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from flask import json | ||
from flask.testing import FlaskClient | ||
from pytest_mock import MockFixture | ||
|
||
|
||
def test_list_should_return_empty_array(mocker: MockFixture, client: FlaskClient): | ||
from time_tracker_api.time_entries.time_entries_namespace import model | ||
"""Should return an empty array""" | ||
model_mock = mocker.patch.object(model, 'find_all', return_value=[]) | ||
|
||
response = client.get("/time-entries", follow_redirects=True) | ||
|
||
assert 200 == response.status_code | ||
|
||
json_data = json.loads(response.data) | ||
assert [] == json_data | ||
model_mock.assert_called_once() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,38 @@ | ||
import os | ||
|
||
from flask import Flask | ||
|
||
|
||
def create_app(): | ||
def create_app(config_path='time_tracker_api.config.DefaultConfig', | ||
config_data=None): | ||
flask_app = Flask(__name__) | ||
|
||
init_app_config(flask_app, config_path, config_data) | ||
init_app(flask_app) | ||
|
||
return flask_app | ||
|
||
|
||
def init_app_config(app: Flask, config_path: str, config_data: dict = None): | ||
if config_path: | ||
app.config.from_object(config_path) | ||
else: | ||
# ensure the instance folder exists | ||
try: | ||
os.makedirs(app.instance_path) | ||
except OSError: | ||
pass | ||
|
||
# Located in `/instance` | ||
app.config.from_pyfile('config.py', silent=True) | ||
|
||
if config_data: | ||
app.config.update(config_data) | ||
|
||
|
||
def init_app(app: Flask): | ||
from .database import init_app as init_database | ||
init_database(app) | ||
|
||
from .api import api | ||
api.init_app(app) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Config: | ||
DEBUG = False | ||
|
||
|
||
class WhateverDevelopConfig(Config): | ||
DEBUG = True | ||
FLASK_DEBUG = True | ||
FLASK_ENV = "develop" | ||
DATABASE = "whatever" | ||
|
||
|
||
DefaultConfig = WhateverDevelopConfig |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
""" | ||
Agnostic database assets | ||
|
||
Put here your utils and class independent of | ||
the database solution | ||
""" | ||
from flask import Flask | ||
|
||
RepositoryModel = None | ||
|
||
|
||
def init_app(app: Flask) -> None: | ||
"""Make the app ready to use the database""" | ||
database_strategy_name = app.config['DATABASE'] | ||
with app.app_context(): | ||
module = globals()["use_%s" % database_strategy_name]() | ||
global RepositoryModel | ||
RepositoryModel = module.repository_model | ||
|
||
|
||
def create(model_name: str): | ||
"""Creates the repository instance for the chosen database""" | ||
return RepositoryModel(model_name) | ||
|
||
|
||
def use_whatever(): | ||
from time_tracker_api import whatever_repository | ||
return whatever_repository |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not call this function 'init_database' directly ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is some sort of standard that some objects like the APIs in flask Restplus have a function called init_app so they take the Flask app as a parameter and they do whatever initialization logic they need to be ready to be used by that app. So whatever module or package you add if you want to make it like pluggable to Flask this is pretty much the way to go