test_db.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # test_db.py/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro).
  2. # This copyright was auto-generated on Mon May 12 23:03:50 UTC 2025
  3. from re import L
  4. import pytest
  5. from tinydb import TinyDB
  6. from tinydb.storages import MemoryStorage
  7. from open_gopro.database.cohn_db import CohnDb
  8. from open_gopro.models.general import CohnInfo
  9. @pytest.fixture(scope="module")
  10. def cohn_db():
  11. tiny_db = TinyDB(storage=MemoryStorage)
  12. yield CohnDb(tiny_db)
  13. def test_search_fails(cohn_db: CohnDb):
  14. # GIVEN
  15. camera = "1234"
  16. # WHEN
  17. credentials = cohn_db.search_credentials(camera)
  18. # THEN
  19. assert credentials is None
  20. def test_add_camera(cohn_db: CohnDb):
  21. # GIVEN
  22. camera = "1234"
  23. credentials = CohnInfo(ip_address="ip_address", password="password", username="user", certificate="cert")
  24. # WHEN
  25. cohn_db.insert_or_update_credentials(camera, credentials)
  26. retrieved_credentials = cohn_db.search_credentials(camera)
  27. # THEN
  28. assert credentials == retrieved_credentials
  29. def test_update_camera_doesnt_take_empty_fields(cohn_db: CohnDb):
  30. # GIVEN
  31. camera = "1234"
  32. credentials = CohnInfo(ip_address="", password="new password", username="", certificate="new cert")
  33. # WHEN
  34. cohn_db.insert_or_update_credentials(camera, credentials)
  35. retrieved_credentials = cohn_db.search_credentials(camera)
  36. # THEN
  37. assert retrieved_credentials == CohnInfo(
  38. ip_address="ip_address", password="new password", username="user", certificate="new cert"
  39. )
  40. def test_delete_camera(cohn_db: CohnDb):
  41. # GIVEN
  42. camera = "1234"
  43. # WHEN
  44. cohn_db.delete_credentials(camera)
  45. retrieved_credentials = cohn_db.search_credentials(camera)
  46. # THEN
  47. assert retrieved_credentials is None
  48. def main():
  49. from pathlib import Path
  50. partial1 = CohnInfo(ip_address="", username="user1", password="password", certificate="cert")
  51. complete1 = CohnInfo(ip_address="ip_address", username="user1", password="password", certificate="cert")
  52. partial2 = CohnInfo(ip_address="", username="", password="password", certificate="")
  53. complete2 = CohnInfo(ip_address="ip_address", username="user2", password="password", certificate="cert")
  54. db = CohnDb(TinyDB(Path("temp_db.json"), indent=4))
  55. db.insert_or_update_credentials("one", partial1)
  56. db.insert_or_update_credentials("one", complete1)
  57. db.insert_or_update_credentials("two", partial2)
  58. db.insert_or_update_credentials("two", complete2)
  59. if __name__ == "__main__":
  60. main()