Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Loembe committed Apr 4, 2024
1 parent 27a07ce commit eb7e990
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
17 changes: 14 additions & 3 deletions python/nistoar/midas/dbio/wsgi/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,21 @@ def do_POST(self, path):
try:
input = self.get_json_body()
except self.FatalError as ex:
print("fatal Error")
return self.send_fatal_error(ex)

path = path.rstrip('/')
if not path:
return self.create_record(input)
elif path == ':selected':
return self.adv_select_records(input)
try:
return self.adv_select_records(input)
except SyntaxError as syntax:
print("400 Bad Request from filter")
return send_error_resp(400, "Wrong query structure for filter")
except ValueError as value:
print("204 Empty Set")
return send_error_resp(204, "No Content")
else:
return send_error_resp(400, "Selection resource not found")

Expand All @@ -495,7 +503,10 @@ def adv_select_records(self, input: Mapping):
sortd.add_record(rec)

out = [rec.to_dict() for rec in sortd.sorted()]
return self.send_json(out)
if not out:
raise ValueError("Empty Set")
else:
return self.send_json(out)

def _adv_select_records(self, filter, perms) -> Iterator[ProjectRecord]:
"""
Expand Down
49 changes: 43 additions & 6 deletions python/tests/nistoar/midas/dbio/wsgi/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_adv_select(self):
}))
hdlr = self.app.create_handler(req, self.start, path, nistr)
body = hdlr.handle()
self.assertIn("201 ", self.resp[0])
self.assertIn("201 ", self.resp[4])
resp=self.body2dict(body)
self.assertEqual(resp['data']['title'],'Standard Reference Materials')

Expand All @@ -159,7 +159,7 @@ def test_adv_select(self):
}))
hdlr = self.app.create_handler(req, self.start, path, nistr)
body = hdlr.handle()
self.assertIn("201 ", self.resp[0])
self.assertIn("201 ", self.resp[8])
resp=self.body2dict(body)
self.assertEqual(resp['data']['title'],'Supplementary material for: The detection of carbon dioxide leaks using quasi-tomographic laser absorption spectroscopy')

Expand All @@ -173,7 +173,7 @@ def test_adv_select(self):
}))
hdlr = self.app.create_handler(req, self.start, path, nistr)
body = hdlr.handle()
self.assertIn("201 ", self.resp[0])
self.assertIn("201 ", self.resp[12])
resp=self.body2dict(body)
self.assertEqual(resp['data']['title'],'Supplementary material for: The detection of carbon dioxide leaks using quasi-tomographic laser absorption spectroscopy')

Expand All @@ -185,6 +185,7 @@ def test_adv_select(self):
}
hdlr = self.app.create_handler(req, self.start, path, nistr)
body = hdlr.handle()
self.assertIn("200 ", self.resp[16])
resp = self.body2dict(body)
self.assertEqual(resp['data']['title'],'Supplementary material for: The detection of carbon dioxide leaks using quasi-tomographic laser absorption spectroscopy')

Expand All @@ -193,26 +194,62 @@ def test_adv_select(self):
'REQUEST_METHOD': 'POST',
'PATH_INFO': self.rootpath + path
}

req['wsgi.input'] = StringIO(json.dumps( {"filter": {"$and": [ {"data.title": "Supplementary material for: The detection of carbon dioxide leaks using quasi-tomographic laser absorption spectroscopy"} ]},
"permissions": ["read", "write"]} ))
hdlr = self.app.create_handler(req, self.start, path, nistr)
body = hdlr.handle()
self.assertIn("201 ", self.resp[0])
self.assertIn("200 ", self.resp[20])
resp=self.body2dict(body)
self.assertEqual(len(resp),2)
self.assertEqual(resp[0]['data']['title'],"Supplementary material for: The detection of carbon dioxide leaks using quasi-tomographic laser absorption spectroscopy")
self.assertEqual(resp[1]['data']['title'],"Supplementary material for: The detection of carbon dioxide leaks using quasi-tomographic laser absorption spectroscopy")


req['wsgi.input'] = StringIO(json.dumps(request_body))
hdlr = self.app.create_handler(req, self.start, path, nistr)
body = hdlr.handle()
self.assertIn("201 ", self.resp[0])
self.assertIn("200 ", self.resp[24])
resp=self.body2dict(body)
self.assertEqual(len(resp),1)
self.assertEqual(resp[0]['data']['title'],"Standard Reference Materials")


req['wsgi.input'] = StringIO(json.dumps( {"filter": {"$and": [ {"data.name": "Supplementary material for: The detection of carbon dioxide leaks using quasi-tomographic laser absorption spectroscopy"} ]},
"permissions": ["read", "write"]} ))
hdlr = self.app.create_handler(req, self.start, path, nistr)
body = hdlr.handle()
self.assertIn("500 ", self.resp[28])



req['wsgi.input'] = StringIO(json.dumps( {"filter": {"$a-nd": [ {"dat-a.name": "test"} ]},
"permissions": ["read", "write"]} ))
hdlr = self.app.create_handler(req, self.start, path, nistr)
body = hdlr.handle()
self.assertIn("500 ", self.resp[30])



req['wsgi.input'] = StringIO(json.dumps( "YEsssay" ))
hdlr = self.app.create_handler(req, self.start, path, nistr)
body = hdlr.handle()
self.assertIn("500 ", self.resp[32])


req['wsgi.input'] = StringIO(json.dumps({
"name": "John Doe",
"age": 30,
"email": "[email protected]",
"address": {
"street": "123 Main Street",
"city": "Anytown",
"zipcode": "12345"
},
}
))
hdlr = self.app.create_handler(req, self.start, path, nistr)
body = hdlr.handle()
self.assertIn("500 ", self.resp[34])

def test_get_name(self):
path = "mdm1:0003/name"
Expand Down

0 comments on commit eb7e990

Please sign in to comment.