Advertisement
bozhilov

Ivan explanation controllers and routes in python

Aug 2nd, 2023
1,546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. # controller
  2. ## users_controllers.py
  3.  
  4. def query_all_users():
  5.     return db.get.users
  6.  
  7.  
  8. def format_users():
  9.     users = query_all_users()
  10.     if users >= 0:
  11.         return json.dumps({"users": users}), 200
  12.  
  13.     return json.dumps({"message": "failed fetching users"}), 500
  14.  
  15. # routes
  16. ## user_routes
  17. @endpoint("/something", "GET")
  18. def get_users_route(req):
  19.     # pass req if you need to work with any info provided from the request
  20.     return format_users(req) # you can call this one get_users_controller if you wish
  21.  
  22.  
  23. # how to test
  24. def test_format_users__users_are_present():
  25.     users = [{}, {}, {}]
  26.     mock("query_all_users", users)
  27.     _, status_code = format_users()
  28.  
  29.     assert status_code == 200
  30.  
  31. def test_format_users__users_do_not_exist():
  32.     users = None
  33.     mock("query_all_users", users)
  34.     _, status_code = format_users()
  35.  
  36.     assert status_code == 500
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement