Skip to content

Commit 9615709

Browse files
committed
fix: TT-43 Fixing decorators errors on tests
1 parent 4f4c6c9 commit 9615709

File tree

3 files changed

+47
-32
lines changed

3 files changed

+47
-32
lines changed

tests/utils/extend_model_test.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,37 @@ def test_add_custom_attribute(customers_create_dao, projects_create_dao):
1515
"technologies": "['python', 'restplus', 'openapi']",
1616
"status": "active",
1717
"customer_name": "Tucker Inc",
18-
"customer": {
19-
"name": None,
20-
"description": None
21-
},
22-
"project_type": {
23-
"name": None,
24-
"description": None
25-
},
18+
"customer": {"name": None, "description": None},
19+
"project_type": {"name": None, "description": None},
2620
"id": "768c924e-4501-457f-99c5-7198440d3c60",
2721
"tenant_id": "e2953984-03e7-4730-be29-1753d24df3b0",
28-
"deleted": "null"
22+
"deleted": "null",
2923
}
3024

31-
@add_custom_attribute('project_type', projects_create_dao())
32-
@add_custom_attribute('customer', customers_create_dao())
25+
@add_custom_attribute('project_type', projects_create_dao)
26+
@add_custom_attribute('customer', customers_create_dao)
3327
@patch('time_tracker_api.projects.projects_model.ProjectCosmosDBModel')
3428
def fn(project):
35-
project.return_value.name = "Franklin, Mcdonald and Morrison",
36-
project.return_value.description = "Include speech feeling court almost country smile economy. True quality mention key. Similar provide yard.",
37-
project.return_value.customer_id = "9afbfa3a-9de4-4b90-a1b7-a53d2c17a178",
38-
project.return_value.project_type_id = "208aadb7-1ec1-4a67-a0b0-e0308d27045b",
39-
project.return_value.technologies = "['python', 'restplus', 'openapi']",
40-
project.return_value.status = "active",
41-
project.return_value.customer_name = "Tucker Inc",
42-
project.return_value.id = "768c924e-4501-457f-99c5-7198440d3c60",
43-
project.return_value.tenant_id = "e2953984-03e7-4730-be29-1753d24df3b0",
44-
project.return_value.deleted = None,
29+
project.return_value.name = ("Franklin, Mcdonald and Morrison",)
30+
project.return_value.description = (
31+
"Include speech feeling court almost country smile economy. True quality mention key. Similar provide yard.",
32+
)
33+
project.return_value.customer_id = (
34+
"9afbfa3a-9de4-4b90-a1b7-a53d2c17a178",
35+
)
36+
project.return_value.project_type_id = (
37+
"208aadb7-1ec1-4a67-a0b0-e0308d27045b",
38+
)
39+
project.return_value.technologies = (
40+
"['python', 'restplus', 'openapi']",
41+
)
42+
project.return_value.status = ("active",)
43+
project.return_value.customer_name = ("Tucker Inc",)
44+
project.return_value.id = ("768c924e-4501-457f-99c5-7198440d3c60",)
45+
project.return_value.tenant_id = (
46+
"e2953984-03e7-4730-be29-1753d24df3b0",
47+
)
48+
project.return_value.deleted = (None,)
4549

4650
return project.return_value
4751

time_tracker_api/projects/projects_model.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from utils.extend_model import (
2020
add_customer_name_to_projects,
2121
add_custom_attribute_in_list,
22-
add_custom_attribute
22+
add_custom_attribute,
2323
)
2424

2525

@@ -108,18 +108,20 @@ class ProjectCosmosDBDao(APICosmosDBDao, ProjectDao):
108108
def __init__(self, repository):
109109
CosmosDBDao.__init__(self, repository)
110110

111-
@add_custom_attribute('customer', customers_create_dao())
112-
@add_custom_attribute('project_type', project_types_create_dao())
111+
@add_custom_attribute('customer', customers_create_dao)
112+
@add_custom_attribute('project_type', project_types_create_dao)
113113
def get(self, id) -> ProjectCosmosDBModel:
114114
"""
115115
Get one project an active client
116116
:param (str) id: project's id
117117
"""
118118
return super().get(id)
119119

120-
@add_custom_attribute_in_list('customer', customers_create_dao())
121-
@add_custom_attribute_in_list('project_type', project_types_create_dao())
122-
def get_all(self, conditions: dict = None, project_ids: List = None, **kwargs) -> list:
120+
@add_custom_attribute_in_list('customer', customers_create_dao)
121+
@add_custom_attribute_in_list('project_type', project_types_create_dao)
122+
def get_all(
123+
self, conditions: dict = None, project_ids: List = None, **kwargs
124+
) -> list:
123125
"""
124126
Get all the projects an active client has
125127
:param (dict) conditions: Conditions for querying the database

utils/extend_model.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,25 @@ def add_custom_attribute(attr, dao):
88
:param (attr) attribute: name of the new attribute
99
:param (dao) dao: related entity to the model
1010
"""
11+
1112
def decorator_for_single_item(func):
1213
@wraps(func)
1314
def wrapper(*args, **kwargs):
15+
current_dao = dao()
1416
entity_model = func(*args, **kwargs)
1517
attribute_id = f"{attr}_id"
16-
value_id = entity_model.__dict__[attribute_id]
1718

1819
try:
19-
related_entity = dao.get(value_id)
20+
value_id = entity_model.__dict__[attribute_id]
21+
related_entity = current_dao.get(value_id)
2022
setattr(entity_model, attr, related_entity)
2123
except:
2224
print(f"This item isn't related a {attribute_id}")
2325

2426
return entity_model
27+
2528
return wrapper
29+
2630
return decorator_for_single_item
2731

2832

@@ -32,22 +36,27 @@ def add_custom_attribute_in_list(attr, dao):
3236
:param (attr) attribute: name of the new attribute
3337
:param (dao) dao: related entity to the model
3438
"""
39+
3540
def decorator_for_list_item(func):
3641
@wraps(func)
3742
def wrapper(*args, **kwargs):
43+
current_dao = dao()
3844
entity_model_list = func(*args, **kwargs)
3945
attribute_id = f"{attr}_id"
4046

41-
related_entity_list = dao.get_all()
47+
related_entity_list = current_dao.get_all()
4248
related_entities_ids_dict = {x.id: x for x in related_entity_list}
4349

4450
for entity_model in entity_model_list:
4551
value_id = entity_model.__dict__[attribute_id]
46-
setattr(entity_model, attr,
47-
related_entities_ids_dict.get(value_id))
52+
setattr(
53+
entity_model, attr, related_entities_ids_dict.get(value_id)
54+
)
4855

4956
return entity_model_list
57+
5058
return wrapper
59+
5160
return decorator_for_list_item
5261

5362

0 commit comments

Comments
 (0)