Skip to content

Commit ad3ee0e

Browse files
Feat/reverse many to many (#34)
* feat: Add reverse mn lookup.
1 parent 476ad66 commit ad3ee0e

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ You can also give more than one item. Given that you have a list of relationship
264264
265265
Results in `[2,4]`.
266266
267+
267268
## Configuration
268269
269270
The following parameters can be adjusted to serve testing, development, or particular deployment needs.

data_resource_api/api/v1_0_0/resource_handler.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,17 @@ def get_many_one(self, id: int, parent: str, child: str):
409409
# return {'error': f"relationship '{child}' of '{parent}' not found."}
410410
try:
411411
session = Session()
412-
cols = {f'{parent}_id': id}
412+
parent_col_str = f'{parent}_id'
413+
child_col_str = f'{child}_id'
414+
415+
cols = {parent_col_str: id}
413416
query = session.query(join_table).filter_by(**cols).all()
414417

415-
children = [value[1] for value in query]
418+
children = []
419+
for row in query:
420+
row_dict = row._asdict() # example - {'programs_id': 2, 'credentials_id': 3}
421+
children.append(row_dict[child_col_str])
422+
416423
except Exception:
417424
raise InternalServerError()
418425

data_resource_api/factories/data_resource_factory.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def create_api_from_dict(self, api_schema: dict, endpoint_name: str, table_name:
6161
# f'/{custom_table[1]}/<id>/{custom_table[2]}/<child_id>'
6262
# ) # DELETE route
6363
many_resources.append(f'/{custom_table[1]}/<int:id>/{custom_table[2]}')
64+
many_resources.append(f'/{custom_table[2]}/<int:id>/{custom_table[1]}')
6465

6566
flask_restful_many_resource = type(f'{endpoint_name}Many', (VersionedResourceMany,), {
6667
'data_resource_name': table_name,

tests/test_custom_descriptor.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,19 @@ def check_for_skills_on_framework(c, framework_id: int, skills_list: list):
7070
route = f'/frameworks/{framework_id}/skills'
7171
response = c.get(route)
7272
body = json.loads(response.data)
73-
73+
7474
expect(response.status_code).to(equal(200))
7575
expect(body['skills']).to(equal(skills_list))
7676

77+
@staticmethod
78+
def get_frameworks_on_skill(c, skill_id: int):
79+
route = f'/skills/{skill_id}/frameworks'
80+
response = c.get(route)
81+
body = json.loads(response.data)
82+
83+
expect(response.status_code).to(equal(200))
84+
return body
85+
7786
@staticmethod
7887
def delete_a_framework_skill(c, framework_id: int, skills_list: list):
7988
route = f'/frameworks/{framework_id}/skills'
@@ -94,8 +103,8 @@ def test_load_descriptor(self, frameworks_skills_client):
94103
skill_1 = ApiHelper.post_a_skill(c, "skill1")
95104
skill_2 = ApiHelper.post_a_skill(c, "skill2")
96105
skills_list = [skill_1, skill_2]
97-
98106
framework_id = ApiHelper.post_a_framework(c, skills_list)
107+
99108
ApiHelper.check_for_skills_on_framework(c, framework_id, skills_list)
100109

101110
def test_relationships(self, frameworks_skills_client):
@@ -132,8 +141,8 @@ def test_mn_put(self, frameworks_skills_client):
132141
skill_2 = ApiHelper.post_a_skill(c, "skill2")
133142
skills_list = [skill_1, skill_2]
134143
skill_3 = ApiHelper.post_a_skill(c, "skill3")
135-
136144
framework_id = ApiHelper.post_a_framework(c, skills_list)
145+
137146
ApiHelper.put_a_framework_skill(c, framework_id, [skill_3])
138147
ApiHelper.check_for_skills_on_framework(c, framework_id, [skill_3])
139148

@@ -168,8 +177,8 @@ def test_mn_delete_many(self, frameworks_skills_client):
168177
skill_2 = ApiHelper.post_a_skill(c, "skill2")
169178
skill_3 = ApiHelper.post_a_skill(c, "skill3")
170179
skills_list = [skill_1, skill_2, skill_3]
171-
172180
framework_id = ApiHelper.post_a_framework(c, skills_list)
181+
173182
resp = ApiHelper.delete_a_framework_skill(c, framework_id, [skill_1, skill_3])
174183

175184
expect(resp['skills']).to(equal([skill_2]))
@@ -181,8 +190,18 @@ def test_mn_delete_relationship_that_does_not_exist(self, frameworks_skills_clie
181190
skill_2 = ApiHelper.post_a_skill(c, "skill2")
182191
skill_3 = ApiHelper.post_a_skill(c, "skill3")
183192
skills_list = [skill_1, skill_2]
184-
185193
framework_id = ApiHelper.post_a_framework(c, skills_list)
194+
186195
resp = ApiHelper.delete_a_framework_skill(c, framework_id, [skill_3])
187196

188197
expect(resp['skills']).to(equal([skill_1, skill_2]))
198+
199+
def test_mn_reverse_relationship(self, frameworks_skills_client):
200+
c = frameworks_skills_client
201+
202+
skill_1 = ApiHelper.post_a_skill(c, "skill1")
203+
framework_id = ApiHelper.post_a_framework(c, [skill_1])
204+
205+
resp = ApiHelper.get_frameworks_on_skill(c, skill_1)
206+
207+
expect(resp['frameworks']).to(equal([framework_id]))

0 commit comments

Comments
 (0)