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-257 Fixed customer name null on get a specific project
  • Loading branch information
jcalarcon98 committed Jun 1, 2021
commit 91135849f5f12596e345203c7981d9899d6c53b1
42 changes: 40 additions & 2 deletions tests/time_tracker_api/projects/projects_model_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from unittest.mock import Mock, patch
import pytest

from commons.data_access_layer.cosmos_db import CosmosDBDao
from commons.data_access_layer.database import EventContext
from time_tracker_api.customers.customers_model import (
CustomerCosmosDBModel,
CustomerCosmosDBDao,
)
from time_tracker_api.projects.projects_model import (
ProjectCosmosDBRepository,
ProjectCosmosDBModel,
create_dao,
)


Expand Down Expand Up @@ -39,3 +43,37 @@ def test_find_all_projects_new_version(
project = result[0]
assert isinstance(project, ProjectCosmosDBModel)
assert project.__dict__ == expected_item


def test_get_project_with_their_customer(
mocker,
):
project_data = {
'customer_id': 'dsakldh12ASD',
'id': 'JDKASDH12837',
'name': 'testing',
'description': 'do some testing',
'project_type_id': "id2",
'tenant_id': 'tenantid1',
}

customer_data = {
"id": "dsakldh12ASD",
"name": 'IOET inc.',
"description": 'nomatter',
"tenant_id": 'nomatter',
}

cosmos_db_get_mock = mocker.patch.object(CosmosDBDao, 'get')
customer_db_get_mock = mocker.patch.object(CustomerCosmosDBDao, 'get')

expected_customer = CustomerCosmosDBModel(customer_data)
expected_project = ProjectCosmosDBModel(project_data)

cosmos_db_get_mock.return_value = expected_project
customer_db_get_mock.return_value = expected_customer

project = create_dao().get('nomatterid')

assert isinstance(project, ProjectCosmosDBModel)
assert project.__dict__['customer_name'] == customer_data['name']
13 changes: 6 additions & 7 deletions tests/time_tracker_api/projects/projects_namespace_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from flask_restplus._http import HTTPStatus
from pytest_mock import MockFixture

from time_tracker_api.projects.projects_model import ProjectCosmosDBDao

fake = Faker()

valid_project_data = {
Expand Down Expand Up @@ -103,20 +105,17 @@ def test_list_all_active_projects(
def test_get_project_should_succeed_with_valid_id(
client: FlaskClient, mocker: MockFixture, valid_header: dict
):
from time_tracker_api.projects.projects_namespace import project_dao

valid_id = fake.random_int(1, 9999)
repository_find_mock = mocker.patch.object(
project_dao.repository, 'find', return_value=fake_project
)

project_dao_get = mocker.patch.object(ProjectCosmosDBDao, 'get')
project_dao_get.return_value = fake_project

response = client.get(
"/projects/%s" % valid_id, headers=valid_header, follow_redirects=True
)

assert HTTPStatus.OK == response.status_code
fake_project == json.loads(response.data)
repository_find_mock.assert_called_once_with(str(valid_id), ANY)
project_dao_get.assert_called_with(str(valid_id))


def test_get_project_should_return_not_found_with_invalid_id(
Expand Down
12 changes: 6 additions & 6 deletions time_tracker_api/customers/customers_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def __str___(self):
return "the customer \"%s\"" % self.name # pragma: no cover


class CustomerCosmosDBDao(APICosmosDBDao, CustomerDao):
def __init__(self, repository):
CosmosDBDao.__init__(self, repository)


def create_dao() -> CustomerDao:
repository = CosmosDBRepository.from_definition(
container_definition, mapper=CustomerCosmosDBModel
)

class CustomerCosmosDBDao(APICosmosDBDao, CustomerDao):
def __init__(self):
CosmosDBDao.__init__(self, repository)

return CustomerCosmosDBDao()
return CustomerCosmosDBDao(repository)
6 changes: 5 additions & 1 deletion time_tracker_api/projects/projects_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ def get(self, id) -> ProjectCosmosDBModel:
Get one project an active client
:param (str) id: project's id
"""
return super().get(id)
project = super().get(id)
customer_dao = customers_create_dao()
customer = customer_dao.get(project.customer_id)
setattr(project, 'customer_name', customer.name)
return project

@add_custom_attribute_in_list('customer', customers_create_dao)
@add_custom_attribute_in_list('project_type', project_types_create_dao)
Expand Down