Skip to content

Commit

Permalink
handle other responses than 500
Browse files Browse the repository at this point in the history
  • Loading branch information
jchate6 committed Oct 23, 2023
1 parent 864a114 commit 2cdfde0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
1 change: 1 addition & 0 deletions tom_base/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
'tom_observations.facilities.lco.LCOFacility',
'tom_observations.facilities.gemini.GEMFacility',
'tom_observations.facilities.soar.SOARFacility',
'tom_swift.swift.SwiftFacility',
]

# Define MATCH_MANAGERS here.
Expand Down
3 changes: 2 additions & 1 deletion tom_dataproducts/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib import admin

from tom_dataproducts.models import DataProduct, DataProductGroup
from tom_dataproducts.models import DataProduct, DataProductGroup, ReducedDatum

admin.site.register(DataProduct)
admin.site.register(DataProductGroup)
admin.site.register(ReducedDatum)
5 changes: 3 additions & 2 deletions tom_dataproducts/sharing.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,11 @@ def share_data_with_tom(share_destination, form_data, product_id=None, target_id
serialized_data['data_product'] = ''
if not serialized_data['source_name']:
serialized_data['source_name'] = settings.TOM_NAME
serialized_data['source_location'] = "TOM-TOM Direct Sharing"
serialized_data['source_location'] = f"ReducedDatum shared from " \
f"<{settings.TOM_NAME}.url>/api/reduceddatums/{datum.id}/"
response = requests.post(reduced_datums_url, json=serialized_data, headers=headers, auth=auth)
response_codes.append(response.status_code)
failed_data_count = response_codes.count(500)
failed_data_count = len([rc for rc in response_codes if rc >= 300])
if failed_data_count < len(response_codes):
return {'message': f'{len(response_codes)-failed_data_count} of {len(response_codes)} '
'datums successfully saved.'}
Expand Down
70 changes: 70 additions & 0 deletions tom_dataproducts/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,3 +748,73 @@ def test_share_reduced_datums_valid_responses(self):
follow=True
)
self.assertContains(response, '2 of 2 datums successfully saved.')

@responses.activate
def test_share_reduced_datums_invalid_responses(self):
share_destination = 'local_host'
destination_tom_base_url = settings.DATA_SHARING[share_destination]['BASE_URL']

rsp1 = responses.Response(
method="GET",
url=destination_tom_base_url + 'api/targets/',
json={"results": [{'id': 1}]},
status=200
)
responses.add(rsp1)
responses.add(
responses.GET,
"http://hermes-dev.lco.global/api/v0/profile/",
json={"error": "not found"},
status=404,
)

sharing_dict = {
'share_authors': ['test_author'],
'target': self.target.id,
'submitter': ['test_submitter'],
'share_destination': [share_destination],
'share_title': ['Updated data for thingy.'],
'share_message': ['test_message'],
'share-box': [1, 2]
}
# Check 500 error
responses.add(
responses.POST,
destination_tom_base_url + 'api/reduceddatums/',
json={},
status=500,
)
response = self.client.post(
reverse('dataproducts:share_all', kwargs={'tg_pk': self.target.id}),
sharing_dict,
follow=True
)
self.assertContains(response, 'No valid data shared. These data may already exist in target TOM.')

# Check 400 error
responses.add(
responses.POST,
destination_tom_base_url + 'api/reduceddatums/',
json={},
status=400,
)
response = self.client.post(
reverse('dataproducts:share_all', kwargs={'tg_pk': self.target.id}),
sharing_dict,
follow=True
)
self.assertContains(response, 'No valid data shared. These data may already exist in target TOM.')

# Check 300 error
responses.add(
responses.POST,
destination_tom_base_url + 'api/reduceddatums/',
json={},
status=300,
)
response = self.client.post(
reverse('dataproducts:share_all', kwargs={'tg_pk': self.target.id}),
sharing_dict,
follow=True
)
self.assertContains(response, 'No valid data shared. These data may already exist in target TOM.')

0 comments on commit 2cdfde0

Please sign in to comment.