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
fix: #91 adding filter by attributes
  • Loading branch information
enriquezrene committed Apr 23, 2020
commit a1b1a6ab121050f7efb904807b022fd8ca73a6df
4 changes: 2 additions & 2 deletions commons/data_access_layer/cosmos_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ class CosmosDBDao(CRUDDao):
def __init__(self, repository: CosmosDBRepository):
self.repository = repository

def get_all(self) -> list:
return self.repository.find_all(partition_key_value=self.partition_key_value)
def get_all(self, conditions: []) -> list:
return self.repository.find_all(partition_key_value=self.partition_key_value, conditions= conditions)

def get(self, id):
return self.repository.find(id, partition_key_value=self.partition_key_value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,4 @@ def test_delete_activity_should_return_422_for_invalid_id_format(client: FlaskCl

assert HTTPStatus.UNPROCESSABLE_ENTITY == response.status_code
repository_remove_mock.assert_called_once_with(str(invalid_id),
partition_key_value=tenant_id)
partition_key_value=tenant_id)
15 changes: 15 additions & 0 deletions tests/time_tracker_api/customers/customers_namespace_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from flask.testing import FlaskClient
from flask_restplus._http import HTTPStatus
from pytest_mock import MockFixture
from werkzeug.datastructures import ImmutableMultiDict

fake = Faker()

Expand Down Expand Up @@ -68,6 +69,20 @@ def test_list_all_customers(client: FlaskClient,
assert [] == json_data
repository_find_all_mock.assert_called_once()

def test_list_all_customers_with_conditions(client: FlaskClient, mocker: MockFixture):
from time_tracker_api.customers.customers_namespace import customer_dao
repository_find_all_mock = mocker.patch.object(customer_dao.repository,
'find_all',
return_value=[])

response = client.get("/customers?a=b", follow_redirects=True)

assert HTTPStatus.OK == response.status_code
json_data = json.loads(response.data)
assert [] == json_data
repository_find_all_mock.assert_called_once_with(conditions=ImmutableMultiDict({'a': 'b'}),
partition_key_value='ioet')


def test_get_customer_should_succeed_with_valid_id(client: FlaskClient,
mocker: MockFixture,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,4 @@ def test_delete_project_should_return_unprocessable_entity_for_invalid_id_format

assert HTTPStatus.UNPROCESSABLE_ENTITY == response.status_code
repository_remove_mock.assert_called_once_with(str(invalid_id),
partition_key_value=tenant_id)
partition_key_value=tenant_id)
2 changes: 1 addition & 1 deletion tests/time_tracker_api/projects/projects_namespace_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,4 @@ def test_delete_project_should_return_unprocessable_entity_for_invalid_id_format

assert HTTPStatus.UNPROCESSABLE_ENTITY == response.status_code
repository_remove_mock.assert_called_once_with(str(invalid_id),
partition_key_value=tenant_id)
partition_key_value=tenant_id)
3 changes: 2 additions & 1 deletion time_tracker_api/activities/activities_namespace.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from faker import Faker
from flask_restplus import fields, Resource, Namespace
from flask_restplus._http import HTTPStatus
from flask import request

from time_tracker_api.activities.activities_model import create_dao
from time_tracker_api.api import common_fields
Expand Down Expand Up @@ -44,7 +45,7 @@ class Activities(Resource):
@ns.marshal_list_with(activity)
def get(self):
"""List all activities"""
return activity_dao.get_all()
return activity_dao.get_all(conditions=request.args)

@ns.doc('create_activity')
@ns.response(HTTPStatus.CONFLICT, 'This activity already exists')
Expand Down
3 changes: 2 additions & 1 deletion time_tracker_api/customers/customers_namespace.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from faker import Faker
from flask_restplus import Namespace, Resource, fields
from flask_restplus._http import HTTPStatus
from flask import request

from time_tracker_api.api import common_fields
from time_tracker_api.customers.customers_model import create_dao
Expand Down Expand Up @@ -45,7 +46,7 @@ class Customers(Resource):
@ns.marshal_list_with(customer)
def get(self):
"""List all customers"""
return customer_dao.get_all()
return customer_dao.get_all(conditions=request.args)
Copy link
Contributor

@EliuX EliuX Apr 21, 2020

Choose a reason for hiding this comment

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

What if you have some query like this?

/activities?a=1&b=2&pageSize=0&orders["start_date"]="DESC"&limit=1

This is not going to work, because a and b are the attributes but you are filtering for whatever it is specified in the request arguments.

Copy link
Contributor

Choose a reason for hiding this comment

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

In addition, it would be nice to filter per namespace that the attributes you want to filter by are valid.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What if you have some query like this?

/activities?a=1&b=2&pageSize=0&orders["start_date"]="DESC"&limit=1

This is not going to work, because a and b are the attributes but you are filtering for whatever it is specified in the request arguments.

Do we have filtering and sorting arguments right now?

Copy link
Contributor

Choose a reason for hiding this comment

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

The repository has sorting capabilities right now. For instance, The repository for time entries orders the results by start_date. We could do this dynamically in the future.


@ns.doc('create_customer')
@ns.response(HTTPStatus.CONFLICT, 'This customer already exists')
Expand Down
3 changes: 2 additions & 1 deletion time_tracker_api/project_types/project_types_namespace.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from faker import Faker
from flask_restplus import Namespace, Resource, fields
from flask_restplus._http import HTTPStatus
from flask import request

from time_tracker_api.api import common_fields, UUID_REGEX
from time_tracker_api.project_types.project_types_model import create_dao
Expand Down Expand Up @@ -60,7 +61,7 @@ class ProjectTypes(Resource):
@ns.marshal_list_with(project_type)
def get(self):
"""List all project types"""
return project_type_dao.get_all()
return project_type_dao.get_all(conditions=request.args)

@ns.doc('create_project_type')
@ns.response(HTTPStatus.CONFLICT, 'This project type already exists')
Expand Down
3 changes: 2 additions & 1 deletion time_tracker_api/projects/projects_namespace.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from faker import Faker
from flask_restplus import Namespace, Resource, fields
from flask_restplus._http import HTTPStatus
from flask import request

from time_tracker_api.api import common_fields, UUID_REGEX
from time_tracker_api.projects.projects_model import create_dao
Expand Down Expand Up @@ -61,7 +62,7 @@ class Projects(Resource):
@ns.marshal_list_with(project)
def get(self):
"""List all projects"""
return project_dao.get_all()
return project_dao.get_all(conditions=request.args)

@ns.doc('create_project')
@ns.response(HTTPStatus.CONFLICT, 'This project already exists')
Expand Down