Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
fix: TT-274 fix error on archive a customer removes the project
  • Loading branch information
PieritoAlva95 committed Jul 13, 2021
commit 954775423a8454cbfa0f701c2d769606a6bd787a
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ migration_status.csv
env.*

# SSL certificate for cosmos emulator
emulatorcert.crt
emulatorcert.crt

seed_database.json
61 changes: 61 additions & 0 deletions tests/time_tracker_api/projects/projects_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
CustomerCosmosDBModel,
CustomerCosmosDBDao,
)
from time_tracker_api.project_types.project_types_model import (
ProjectTypeCosmosDBModel,
ProjectTypeCosmosDBDao,
)
from time_tracker_api.projects.projects_model import (
ProjectCosmosDBRepository,
ProjectCosmosDBModel,
create_dao,
)
from faker import Faker

fake = Faker()


@patch(
Expand Down Expand Up @@ -77,3 +84,57 @@ def test_get_project_with_their_customer(

assert isinstance(project, ProjectCosmosDBModel)
assert project.__dict__['customer_name'] == customer_data['name']


def test_get_all_projects_with_customers(
mocker,
):
customer_id = fake.uuid4()
project_type_id = fake.uuid4()

customer_data = {
'id': customer_id,
'name': fake.company(),
'description': fake.paragraph(),
'tenant_id': fake.uuid4(),
}

project_data = {
'customer_id': customer_id,
'id': fake.uuid4(),
'name': fake.company(),
'description': fake.paragraph(),
'project_type_id': project_type_id,
'tenant_id': fake.uuid4(),
}

project_type_dao = {
'id': project_type_id,
'name': fake.name(),
'description': fake.paragraph(),
'tenant_id': fake.uuid4(),
}

expected_customer = CustomerCosmosDBModel(customer_data)
expected_project = ProjectCosmosDBModel(project_data)
expected_project_type = ProjectTypeCosmosDBModel(project_type_dao)

customer_dao_get_all_mock = mocker.patch.object(
CustomerCosmosDBDao, 'get_all'
)
customer_dao_get_all_mock.return_value = [expected_customer]

projects_repository_find_all_mock = mocker.patch.object(
ProjectCosmosDBRepository, 'find_all'
)
projects_repository_find_all_mock.return_value = [expected_project]

project_type_dao_get_all_mock = mocker.patch.object(
ProjectTypeCosmosDBDao, 'get_all'
)
project_type_dao_get_all_mock.return_value = [expected_project_type]
projects = create_dao().get_all()

assert isinstance(projects[0], ProjectCosmosDBModel)
assert projects[0].__dict__['customer_name'] == customer_data['name']
assert len(projects) == 1
27 changes: 17 additions & 10 deletions time_tracker_api/project_types/project_types_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

from azure.cosmos import PartitionKey

from commons.data_access_layer.cosmos_db import CosmosDBModel, CosmosDBDao, CosmosDBRepository
from commons.data_access_layer.cosmos_db import (
CosmosDBModel,
CosmosDBDao,
CosmosDBRepository,
)
from time_tracker_api.database import CRUDDao, APICosmosDBDao


Expand All @@ -17,7 +21,7 @@ class ProjectTypeDao(CRUDDao):
'uniqueKeys': [
{'paths': ['/name', '/customer_id', '/deleted']},
]
}
},
}


Expand All @@ -32,7 +36,9 @@ class ProjectTypeCosmosDBModel(CosmosDBModel):
tenant_id: str

def __init__(self, data):
super(ProjectTypeCosmosDBModel, self).__init__(data) # pragma: no cover
super(ProjectTypeCosmosDBModel, self).__init__(
data
) # pragma: no cover

def __repr__(self):
return '<ProjectType %r>' % self.name # pragma: no cover
Expand All @@ -41,12 +47,13 @@ def __str___(self):
return "the project type \"%s\"" % self.name # pragma: no cover


def create_dao() -> ProjectTypeDao:
repository = CosmosDBRepository.from_definition(container_definition,
mapper=ProjectTypeCosmosDBModel)
class ProjectTypeCosmosDBDao(APICosmosDBDao, ProjectTypeDao):
def __init__(self, repository):
CosmosDBDao.__init__(self, repository)

class ProjectTypeCosmosDBDao(APICosmosDBDao, ProjectTypeDao):
def __init__(self):
CosmosDBDao.__init__(self, repository)

return ProjectTypeCosmosDBDao()
def create_dao() -> ProjectTypeDao:
repository = CosmosDBRepository.from_definition(
container_definition, mapper=ProjectTypeCosmosDBModel
)
return ProjectTypeCosmosDBDao(repository)
14 changes: 8 additions & 6 deletions time_tracker_api/projects/projects_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,17 @@ def get_all(
"""
event_ctx = self.create_event_context("read-many")
customer_dao = customers_create_dao()
customer_status = kwargs.get('customer_status', None)
customer_conditions = (
{'status': customer_status} if customer_status else None
)

customers = customer_dao.get_all(
max_count=kwargs.get('max_count', None)
conditions=customer_conditions,
max_count=kwargs.get('max_count', None),
)

customers_id = [
customer.id
for customer in customers
if customer.status == 'active'
]
customers_id = [customer.id for customer in customers]

conditions = conditions if conditions else {}

Expand Down
74 changes: 41 additions & 33 deletions time_tracker_api/projects/projects_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,39 +70,45 @@
},
)

project_type_nested_field = ns.model('ProjectType', {
'name': fields.String(
title='Name',
required=True,
max_length=50,
description='Name of the project type',
example=faker.random_element(["Customer", "Training", "Internal"]),
),
'description': NullableString(
title='Description',
required=False,
max_length=250,
description='Comments about the project type',
example=faker.paragraph(),
)
})
project_type_nested_field = ns.model(
'ProjectType',
{
'name': fields.String(
title='Name',
required=True,
max_length=50,
description='Name of the project type',
example=faker.random_element(["Customer", "Training", "Internal"]),
),
'description': NullableString(
title='Description',
required=False,
max_length=250,
description='Comments about the project type',
example=faker.paragraph(),
),
},
)

customer_nested_field = ns.model('Customer', {
'name': fields.String(
title='Name',
required=True,
max_length=50,
description='Name of the customer',
example=faker.company(),
),
'description': NullableString(
title='Description',
required=False,
max_length=250,
description='Description about the customer',
example=faker.paragraph(),
)
})
customer_nested_field = ns.model(
'Customer',
{
'name': fields.String(
title='Name',
required=True,
max_length=50,
description='Name of the customer',
example=faker.company(),
),
'description': NullableString(
title='Description',
required=False,
max_length=250,
description='Description about the customer',
example=faker.paragraph(),
),
},
)

project_response_fields = {
# TODO: Remove this DEAD CODE
Expand Down Expand Up @@ -135,7 +141,9 @@ class Projects(Resource):
def get(self):
"""List all projects"""
conditions = attributes_filter.parse_args()
return project_dao.get_all(conditions=conditions)
return project_dao.get_all(
conditions=conditions, customer_status='active'
)

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