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
Prev Previous commit
Next Next commit
Apply requested changes
  • Loading branch information
EliuX committed Mar 20, 2020
commit 336eb75db1d621c6b7dcf41f4c178d3950851b4c
2 changes: 0 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@

@pytest.fixture(scope='session', params=CONFIGURATIONS)
def app(request: FixtureRequest) -> Flask:
"""An instance of the app for tests"""
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

Expand Down
1 change: 0 additions & 1 deletion tests/projects/projects_namespace_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


def test_list_all_elements(client: FlaskClient, mocker: MockFixture):
"""Should return all elements in a list"""
from time_tracker_api.projects.projects_namespace import project_dao
repository_find_all_mock = mocker.patch.object(project_dao.repository, 'find_all', return_value=[])

Expand Down
4 changes: 2 additions & 2 deletions tests/resources.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from time_tracker_api.sql_repository import db, SQLAuditedModel
from time_tracker_api.sql_repository import db, AuditedSQLModel


class PersonSQLModel(db.Model, SQLAuditedModel):
class PersonSQLModel(db.Model, AuditedSQLModel):
__tablename__ = 'tests'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=False, nullable=False)
Expand Down
1 change: 0 additions & 1 deletion tests/smoke_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
def test_app_exists(app):
"""Does app exists"""
assert app is not None
5 changes: 0 additions & 5 deletions tests/sql_repository_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


def test_create(sql_repository):
"""Should create a new Entry"""
global sample_element
sample_element = dict(name=fake.name(),
email=fake.safe_email(),
Expand All @@ -26,7 +25,6 @@ def test_create(sql_repository):


def test_find(sql_repository):
"""Should find created element"""
existing_element = existing_elements_registry[0]

found_element = sql_repository.find(existing_element.id)
Expand All @@ -36,7 +34,6 @@ def test_find(sql_repository):


def test_update(sql_repository):
"""Updates an existing element"""
existing_element = existing_elements_registry[0]

updated_element = sql_repository.update(existing_element.id,
Expand All @@ -52,7 +49,6 @@ def test_update(sql_repository):


def test_find_all(sql_repository):
"""Find all existing elements"""
existing_elements = sql_repository.find_all()

assert all(e in existing_elements_registry for e in existing_elements)
Expand All @@ -78,7 +74,6 @@ def test_find_all_that_contains_property_with_string(sql_repository):


def test_delete_existing_element(sql_repository):
"""Should delete created element"""
existing_element = existing_elements_registry[0]

result = sql_repository.remove(existing_element.id)
Expand Down
5 changes: 0 additions & 5 deletions time_tracker_api/activities/activities_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,13 @@ class Activities(Resource):
@ns.doc('list_activities')
@ns.marshal_list_with(activity, code=200)
def get(self):
"""List all available activities"""
return []

@ns.doc('create_activity')
@ns.expect(activity_input)
@ns.marshal_with(activity, code=201)
@ns.response(400, 'Invalid format of the attributes of the activity.')
def post(self):
"""Create a single activity"""
return ns.payload, 201


Expand All @@ -65,19 +63,16 @@ class Activity(Resource):
@ns.doc('get_activity')
@ns.marshal_with(activity)
def get(self, id):
"""Retrieve all the data of a single activity"""
return {}

@ns.doc('delete_activity')
@ns.response(204, 'The activity was deleted successfully (No content is returned)')
def delete(self, id):
"""Deletes a activity"""
return None, 204

@ns.doc('put_activity')
@ns.response(400, 'Invalid format of the attributes of the activity.')
@ns.expect(activity_input)
@ns.marshal_with(activity)
def put(self, id):
"""Updates an activity"""
return ns.payload
13 changes: 0 additions & 13 deletions time_tracker_api/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,18 @@ def delete(self, id):
class Seeder(abc.ABC):
@abc.abstractmethod
def run(self):
"""Provision database"""
raise NotImplementedError

@abc.abstractmethod
def fresh(self):
"""will drop all tables and seed again the database"""
raise NotImplementedError

def __call__(self, *args, **kwargs):
self.run()


class DatabaseModel:
"""
Represents a model of a particular database,
e.g. SQL Model
"""

def to_dto(self):
"""Override this in case you need a DTO instead of a model"""
return self


Expand All @@ -74,10 +66,6 @@ def convert_if_necessary(result):
return result

def to_dto(*args, **kw):
"""
Decorator that converts any result that is a
DatabaseModel into its correspondent dto.
"""
result = f(*args, **kw)
return convert_if_necessary(result)

Expand All @@ -88,7 +76,6 @@ def to_dto(*args, **kw):


def init_app(app: Flask) -> None:
"""Make the app ready to use the database"""
database_strategy = app.config['DATABASE']
with app.app_context():
globals()["use_%s" % database_strategy.name.lower()](app)
Expand Down
19 changes: 0 additions & 19 deletions time_tracker_api/errors.py

This file was deleted.

5 changes: 2 additions & 3 deletions time_tracker_api/projects/projects_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from flask import Flask

from time_tracker_api.database import DATABASE_TYPE, CRUDDao
from time_tracker_api.sql_repository import SQLCRUDDao, SQLAuditedModel, SQLModel
from time_tracker_api.sql_repository import SQLCRUDDao, AuditedSQLModel, SQLModel


class PROJECT_TYPE(enum.Enum):
Expand All @@ -20,11 +20,10 @@ class ProjectDao(CRUDDao):


def create_dao(app: Flask) -> ProjectDao:
"""This will construct the dao based on the chosen DATABASE"""
if app.config['DATABASE'] == DATABASE_TYPE.SQL:
from time_tracker_api.sql_repository import db

class ProjectSQLModel(db.Model, SQLModel, SQLAuditedModel):
class ProjectSQLModel(db.Model, SQLModel, AuditedSQLModel):
__tablename__ = 'projects'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True, nullable=False)
Expand Down
5 changes: 0 additions & 5 deletions time_tracker_api/projects/projects_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class Projects(Resource):
@ns.doc('list_projects')
@ns.marshal_list_with(project, code=200)
def get(self):
"""List all projects"""
return project_dao.get_all(), 200

@ns.doc('create_project')
Expand All @@ -74,7 +73,6 @@ def get(self):
@ns.expect(project_input)
@ns.marshal_with(project, code=201)
def post(self):
"""Create a project"""
return project_dao.create(ns.payload), 201


Expand All @@ -90,7 +88,6 @@ class Project(Resource):
@ns.response(422, 'The id has an invalid format')
@ns.marshal_with(project)
def get(self, id):
"""Retrieve a project"""
return project_dao.get(id)

@ns.doc('update_project')
Expand All @@ -99,13 +96,11 @@ def get(self, id):
@ns.expect(project_input)
@ns.marshal_with(project)
def put(self, id):
"""Updates a project"""
return project_dao.update(id, ns.payload)

@ns.doc('delete_project')
@ns.response(204, 'Project deleted successfully')
@ns.response(422, 'The id has an invalid format')
def delete(self, id):
"""Deletes a project"""
project_dao.delete(id)
return None, 204
14 changes: 4 additions & 10 deletions time_tracker_api/sql_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@

db = None
SQLModel = None
SQLAuditedModel = None
AuditedSQLModel = None


def handle_commit_issues(f):
"""If during a commit an exception happened it should be rolled-back"""

def rollback_if_necessary(*args, **kw):
"""
Decorator that converts any result that is a
DatabaseModel into its correspondent dto.
"""
try:
return f(*args, **kw)
except:
Expand All @@ -40,15 +34,15 @@ def to_dto(self):

SQLModel = SQLModelClass

global SQLAuditedModel
global AuditedSQLModel

class SQLAuditedModelClass():
class AuditedSQLModelClass():
created_at = db.Column(db.DateTime, server_default=db.func.now())
updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow)
created_by = db.Column(db.String, default=current_user_id)
updated_by = db.Column(db.String, onupdate=current_user_id)

SQLAuditedModel = SQLAuditedModelClass
AuditedSQLModel = AuditedSQLModelClass


class SQLRepository():
Expand Down
5 changes: 0 additions & 5 deletions time_tracker_api/technologies/technologies_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ class Technologies(Resource):
@ns.doc('list_technologies')
@ns.marshal_list_with(technology, code=200)
def get(self):
"""List all technologies"""
return [], 200

@ns.doc('create_technology')
@ns.expect(technology_input)
@ns.marshal_with(technology, code=201)
def post(self):
"""Create a technology"""
return ns.payload, 201


Expand All @@ -59,18 +57,15 @@ class Technology(Resource):
@ns.doc('get_technology')
@ns.marshal_with(technology)
def get(self, id):
"""Retrieve a technology"""
return {}

@ns.doc('put_technology')
@ns.expect(technology_input)
@ns.marshal_with(technology)
def put(self, id):
"""Updates a technology"""
return ns.payload()

@ns.doc('delete_technology')
@ns.response(204, 'Technology deleted successfully')
def delete(self, id):
"""Deletes a technology"""
return None, 204
7 changes: 0 additions & 7 deletions time_tracker_api/time_entries/time_entries_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,13 @@ class TimeEntries(Resource):
@ns.doc('list_time_entries')
@ns.marshal_list_with(time_entry, code=200)
def get(self):
"""List all available time entries"""
return [], 200

@ns.doc('create_time_entry')
@ns.expect(time_entry_input)
@ns.marshal_with(time_entry, code=201)
@ns.response(400, 'Invalid format of the attributes of the time entry')
def post(self):
"""Starts a time entry by creating it"""
return ns.payload, 201


Expand All @@ -96,21 +94,18 @@ class TimeEntry(Resource):
@ns.doc('get_time_entry')
@ns.marshal_with(time_entry)
def get(self, id):
"""Retrieve all the data of a single time entry"""
return {}

@ns.doc('delete_time_entry')
@ns.response(204, 'The time entry was deleted successfully (No content is returned)')
def delete(self, id):
"""Deletes a time entry"""
return None, 204

@ns.doc('put_time_entry')
@ns.response(400, 'Invalid format of the attributes of the time entry.')
@ns.expect(time_entry_input)
@ns.marshal_with(time_entry)
def put(self, id):
"""Updates a time entry"""
return ns.payload


Expand All @@ -121,7 +116,6 @@ class StopTimeEntry(Resource):
@ns.doc('stop_time_entry')
@ns.response(204, 'The time entry was stopped successfully (No content is returned)')
def post(self, id):
"""Stops a running time entry"""
return None, 204


Expand All @@ -132,5 +126,4 @@ class ContinueTimeEntry(Resource):
@ns.doc('continue_time_entry')
@ns.response(204, 'The time entry was continued successfully (No content is returned)')
def post(self, id):
"""Restart an stopped time entry"""
return None, 204