Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Setup SQL Alchemny and tests for sql repository
  • Loading branch information
EliuX committed Mar 19, 2020
commit f9e6b9d85d3b4e47f22aea3bd789ff07b8007336
5 changes: 4 additions & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ pytest-mock==2.0.0
Faker==4.0.2

# Coverage
coverage==4.5.1
coverage==4.5.1

# SQL database (MS SQL)
flask_sqlalchemy==2.4.1
22 changes: 19 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import pytest
from _pytest.fixtures import FixtureRequest
from flask import Flask
from flask.testing import FlaskClient

from time_tracker_api import create_app

CONFIGURATIONS = ['AzureSQLDatabaseDevelopTestConfig']

@pytest.fixture(scope='session')
def app() -> Flask:

@pytest.fixture(scope='session', params=CONFIGURATIONS)
def app(request: FixtureRequest) -> Flask:
"""An instance of the app for tests"""
return create_app()
return create_app("time_tracker_api.config.%s" % request.param)


@pytest.fixture
def client(app: Flask) -> FlaskClient:
"""A test client for the app."""
with app.test_client() as c:
return c


@pytest.fixture()
def repository(app: Flask):
with app.app_context():
from .resources import TestModel
from time_tracker_api.sql_repository import db

db.create_all()
print("Models for test created!")

from time_tracker_api.sql_repository import SQLRepository
return SQLRepository(TestModel)
11 changes: 11 additions & 0 deletions tests/resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from time_tracker_api.sql_repository import db


class TestModel(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
age = db.Column(db.Integer, nullable=False)

def __repr__(self):
return '<Test Model %r>' % self.name
15 changes: 15 additions & 0 deletions tests/sql_repository_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from faker import Faker
from flask import Flask

fake = Faker()


def test_create_entry(repository, app: Flask):
"""Should create a new Entry"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not naming the function/method name as

test_creates_a_new_entry

Then you can get rid of comments

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function just tests the function create of the repository, this is a simple and unique name. As I explained before the comment in the function is providing a verbose description of the test.

assert repository is not None
from .resources import TestModel
sample_model = TestModel(name=fake.name(),
email=fake.safe_email(),
age = fake.pyint(min_value=10, max_value=80))

repository.create(sample_model)
21 changes: 18 additions & 3 deletions time_tracker_api/config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import os


class Config:
DEBUG = False


class WhateverDevelopConfig(Config):
class DevelopConfig(Config):
DEBUG = True
FLASK_DEBUG = True
FLASK_ENV = "develop"
DATABASE = "whatever"
DATABASE_URI = os.environ.get('DATABASE_URI')


class AzureSQLDatabaseDevelopConfig(DevelopConfig):
DATABASE = 'sql'
TEST_TABLE = 'tests'
SQLALCHEMY_COMMIT_ON_TEARDOWN = True
SQLALCHEMY_TRACK_MODIFICATIONS = False


class AzureSQLDatabaseDevelopTestConfig(AzureSQLDatabaseDevelopConfig):
DEBUG = True
TESTING = True


DefaultConfig = WhateverDevelopConfig
DefaultConfig = AzureSQLDatabaseDevelopConfig
9 changes: 5 additions & 4 deletions time_tracker_api/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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]()
module = globals()["use_%s" % database_strategy_name](app)
global RepositoryModel
RepositoryModel = module.repository_model

Expand All @@ -23,6 +23,7 @@ def create(model_name: str):
return RepositoryModel(model_name)


def use_whatever():
from time_tracker_api import whatever_repository
return whatever_repository
def use_sql(app: Flask) -> None:
from time_tracker_api import sql_repository
sql_repository.init_app(app)
return sql_repository
27 changes: 27 additions & 0 deletions time_tracker_api/sql_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import urllib.parse

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

db = None


def init_app(app: Flask) -> None:
app.config['SQLALCHEMY_DATABASE_URI'] = app.config['DATABASE_URI']
global db
db = SQLAlchemy(app)


class SQLRepository():
def __init__(self, model_type: type):
self.model_type = model_type

def create(self, model: dict) -> dict:
db.session.add(model)
db.session.commit()

def find_all(self):
return self.model_type.query.all()


repository_model = SQLRepository
10 changes: 0 additions & 10 deletions time_tracker_api/whatever_repository.py

This file was deleted.