-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: extend integration tests (#133)
- Loading branch information
Showing
9 changed files
with
320 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,77 @@ | ||
"""Petstore controllers.""" | ||
|
||
import logging | ||
|
||
from flask import (current_app, make_response) | ||
from werkzeug.exceptions import NotFound | ||
from pymongo.collection import Collection | ||
|
||
logger = logging.getLogger(__name__) | ||
from exceptions import NotFound | ||
|
||
error_response = { | ||
'code': 500, | ||
'message': 'Something went wrong.', | ||
} | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def findPets(limit=None, tags=None): | ||
try: | ||
db_collection = ( | ||
current_app.config.foca.db.dbs['petstore'] | ||
.collections['pets'].client | ||
) | ||
filter_dict = {} if tags is None else {'tag': {'$in': tags}} | ||
if not limit: | ||
limit = 0 | ||
records = db_collection.find( | ||
filter_dict, | ||
{'_id': False} | ||
).sort([('$natural', -1)]).limit(limit) | ||
return list(records) | ||
except Exception as e: | ||
logger.error(f"{type(e).__name__}: {e}") | ||
return error_response | ||
db_collection: Collection = ( | ||
current_app.config.foca.db.dbs['petstore'] | ||
.collections['pets'].client | ||
) | ||
filter_dict = {} if tags is None else {'tag': {'$in': tags}} | ||
if not limit: | ||
limit = 0 | ||
records = db_collection.find( | ||
filter_dict, | ||
{'_id': False} | ||
).sort([('$natural', -1)]).limit(limit) | ||
return list(records) | ||
|
||
|
||
def addPet(pet): | ||
try: | ||
db_collection = ( | ||
current_app.config.foca.db.dbs['petstore'] | ||
.collections['pets'].client | ||
) | ||
counter = 0 | ||
ctr = db_collection.find({}).sort([('$natural', -1)]) | ||
if not ctr.count() == 0: | ||
counter = ctr[0].get('id') + 1 | ||
record = { | ||
"id": counter, | ||
"name": pet['name'], | ||
"tag": pet['tag'] | ||
} | ||
db_collection.insert_one(record) | ||
del record['_id'] | ||
return record | ||
except Exception as e: | ||
logger.error(f"{type(e).__name__}: {e}") | ||
return error_response | ||
db_collection: Collection = ( | ||
current_app.config.foca.db.dbs['petstore'] | ||
.collections['pets'].client | ||
) | ||
counter = 0 | ||
ctr = db_collection.find({}).sort([('$natural', -1)]) | ||
if not ctr.count() == 0: | ||
counter = ctr[0].get('id') + 1 | ||
record = { | ||
"id": counter, | ||
"name": pet['name'], | ||
"tag": pet['tag'] | ||
} | ||
db_collection.insert_one(record) | ||
del record['_id'] | ||
return record | ||
|
||
|
||
def findPetById(id): | ||
try: | ||
db_collection = ( | ||
current_app.config.foca.db.dbs['petstore'] | ||
.collections['pets'].client | ||
) | ||
record = db_collection.find_one( | ||
{"id": id}, | ||
{'_id': False}, | ||
) | ||
if not record: | ||
raise NotFound | ||
return record | ||
except Exception as e: | ||
logger.error(f"{type(e).__name__}: {e}") | ||
return error_response | ||
db_collection: Collection = ( | ||
current_app.config.foca.db.dbs['petstore'] | ||
.collections['pets'].client | ||
) | ||
record = db_collection.find_one( | ||
{"id": id}, | ||
{'_id': False}, | ||
) | ||
if record is None: | ||
raise NotFound | ||
return record | ||
|
||
|
||
def deletePet(id): | ||
try: | ||
db_collection = ( | ||
current_app.config.foca.db.dbs['petstore'] | ||
.collections['pets'].client | ||
) | ||
record = db_collection.find_one( | ||
{"id": id}, | ||
{'_id': False}, | ||
) | ||
if not record: | ||
raise NotFound | ||
db_collection.delete_one({"id": id}) | ||
response = make_response('', 204) | ||
response.mimetype = current_app.config['JSONIFY_MIMETYPE'] | ||
return response | ||
except Exception as e: | ||
logger.error(f"{type(e).__name__}: {e}") | ||
return error_response | ||
db_collection: Collection = ( | ||
current_app.config.foca.db.dbs['petstore'] | ||
.collections['pets'].client | ||
) | ||
record = db_collection.find_one( | ||
{"id": id}, | ||
{'_id': False}, | ||
) | ||
if record is None: | ||
raise NotFound | ||
db_collection.delete_one( | ||
{"id": id}, | ||
) | ||
response = make_response('', 204) | ||
response.mimetype = current_app.config['JSONIFY_MIMETYPE'] | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""Petstore exceptions.""" | ||
|
||
from connexion.exceptions import BadRequestProblem | ||
from werkzeug.exceptions import ( | ||
InternalServerError, | ||
NotFound, | ||
) | ||
|
||
exceptions = { | ||
Exception: { | ||
"message": "An unexpected error occurred.", | ||
"code": 500, | ||
}, | ||
BadRequestProblem: { | ||
"message": "The request is malformed.", | ||
"code": 400, | ||
}, | ||
NotFound: { | ||
"message": "The requested resource wasn't found.", | ||
"code": 404, | ||
}, | ||
InternalServerError: { | ||
"message": "An unexpected error occurred.", | ||
"code": 500, | ||
}, | ||
} |
Oops, something went wrong.