Skip to content

Commit

Permalink
Update parsers.py, views.py, and 2 more files...
Browse files Browse the repository at this point in the history
  • Loading branch information
jokiefer committed Jul 6, 2023
1 parent 61e2a0c commit 3552be6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
5 changes: 4 additions & 1 deletion atomic_operations/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,19 @@ def parse_data(self, result, parser_context):
result=result
)

operation_code = f'{operation["op"]}-relationship'

else:
_parsed_data = self.parse_operation(
resource_identifier_object=operation.get(
"data", operation.get("ref")
),
result=result
)
operation_code = operation["op"]

parsed_data.append({
operation["op"]: _parsed_data
operation_code: _parsed_data
})

return parsed_data
8 changes: 4 additions & 4 deletions atomic_operations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ def perform_operations(self, parsed_operations: List[Dict]):
serializer = self.get_serializer(
idx=idx,
data=obj,
operation_code=op_code,
operation_code="update" if op_code == "update-relationship" else op_code,
resource_type=obj["type"],
partial=True if op_code == "update" else False
partial=True if "update" in op_code else False
)
if op_code in ["add", "update"]:
if op_code in ["add", "update", "update-relationship"]:
serializer.is_valid(raise_exception=True)
serializer.save()
# FIXME: check if it is just a relationship update
if op_code == "update" and "ref" in obj:
if op_code == "update-relationship":
# relation update. No response data
continue
response_data.append(serializer.data)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ def test_parse(self):
"type": "articles",
}
}, {
"update": {
"update-relationship": {
"id": "13",
"type": "articles",
"author": {"type": "people", "id": "9"}
}
}, {
"update": {
"update-relationship": {
"id": "13",
"type": "articles",
"tags": [{'type': 'tags', 'id': '2'}, {'type': 'tags', 'id': '3'}]
Expand Down
18 changes: 16 additions & 2 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,14 @@ def test_view_processing_with_valid_request(self):
{
"data": {
"id": "1",

"type": "BasicModel",
"attributes": {
"text": "JSON API paints my bikeshed!"
},
"relationships": {
"to_many": {'data': [], 'meta': {'count': 0}},
"to_one": {'data': None},
}
}
},
Expand All @@ -129,6 +134,10 @@ def test_view_processing_with_valid_request(self):
"type": "BasicModel",
"attributes": {
"text": "JSON API paints my bikeshed!"
},
"relationships": {
"to_many": {'data': [], 'meta': {'count': 0}},
"to_one": {'data': None},
}
}
},
Expand Down Expand Up @@ -165,7 +174,12 @@ def test_view_processing_with_valid_request(self):
"type": "BasicModel",
"attributes": {
"text": "JSON API paints my bikeshed2!"
},
"relationships": {
"to_many": {'data': [], 'meta': {'count': 0}},
"to_one": {'data': None},
}

}
},
]
Expand All @@ -181,8 +195,8 @@ def test_view_processing_with_valid_request(self):

self.assertEqual(RelatedModel.objects.get(pk=1),
BasicModel.objects.get(pk=2).to_one)
self.assertEqual(RelatedModelTwo.objects.filter(pk__in=[1, 2]),
BasicModel.objects.get(pk=2).to_many)
self.assertQuerysetEqual(RelatedModelTwo.objects.filter(pk__in=[1, 2]),
BasicModel.objects.get(pk=2).to_many.all())

def test_parser_exception_with_pointer(self):
operations = [
Expand Down

0 comments on commit 3552be6

Please sign in to comment.