tests.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import os
  2. import asyncio
  3. import time
  4. import uuid
  5. import bcrypt
  6. from pydantic import ValidationError
  7. if __name__ == "__main__":
  8. os.chdir('..')
  9. from src.main import db, logger
  10. from src.models import *
  11. from src.exceptions import *
  12. import src.config as config
  13. async def get_all_users_test() -> str:
  14. res = await db.get_users()
  15. if isinstance(res, list):
  16. return "PASS"
  17. else:
  18. return f"FAIL. all users return must be list but {type(res)} was returned"
  19. async def get_user_test(user_id: uuid.UUID) -> str:
  20. res = await db.get_user(user_id)
  21. if isinstance(res, User):
  22. return "PASS"
  23. else:
  24. return f"FAIL. user return must be model.User but {type(res)} was returned"
  25. async def del_user_test(user_id: uuid.UUID) -> str:
  26. await db.del_user(user_id)
  27. try:
  28. user = await db.get_user(user_id)
  29. return "FAIL. User found in database."
  30. except UserNotFound:
  31. return "PASS"
  32. async def create_user_test() -> (User, str):
  33. new_user = User(**{"user_id": uuid.uuid4(),
  34. "username": "TEST",
  35. "password": bcrypt.hashpw("test123".encode(), bcrypt.gensalt()),
  36. "created_at": time.time(),
  37. "updated_at": None})
  38. if not bcrypt.checkpw("test123".encode(), new_user.password):
  39. raise InvalidPassword("error while checking passwords hash")
  40. res = await db.add_user(new_user)
  41. if res is None:
  42. return new_user, "PASS"
  43. else:
  44. return new_user, f"FAIL. res is not None."
  45. async def update_user_test(user: User) -> str:
  46. try:
  47. user.id = uuid.uuid4()
  48. return 'FAIL. uuid must be frozen'
  49. except ValidationError:
  50. pass
  51. try:
  52. user.created_at = time.time()
  53. return 'FAIL. created_at must be frozen'
  54. except ValidationError:
  55. pass
  56. user.username = "NEW TEST"
  57. user.password = bcrypt.hashpw("new_test123".encode(), bcrypt.gensalt())
  58. if not bcrypt.checkpw("new_test123".encode(), user.password):
  59. raise InvalidPassword("error while checking passwords hash")
  60. await db.update_user(user)
  61. updated_user = await db.get_user(user.id)
  62. logger.debug(updated_user)
  63. if updated_user != user:
  64. return 'FAIL. user not changed'
  65. return 'PASS'
  66. async def tests():
  67. logger.debug("Test 1. Getting all users")
  68. logger.debug(await get_all_users_test())
  69. logger.debug("Test 2. Creating user")
  70. created_user, msg = await create_user_test()
  71. logger.debug(created_user)
  72. logger.debug(msg)
  73. logger.debug("Test 3. Selecting created user")
  74. logger.debug(await get_user_test(created_user.id))
  75. logger.debug("Test 4. Updating created user")
  76. logger.debug(await update_user_test(created_user))
  77. logger.debug("Test 5. Deleting created user")
  78. logger.debug(await del_user_test(created_user.id))
  79. asyncio.run(tests())