test_client.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import pytest
  2. from embedchain import Client
  3. class TestClient:
  4. @pytest.fixture
  5. def mock_requests_post(self, mocker):
  6. return mocker.patch("embedchain.client.requests.post")
  7. def test_valid_api_key(self, mock_requests_post):
  8. mock_requests_post.return_value.status_code = 200
  9. client = Client(api_key="valid_api_key")
  10. assert client.check("valid_api_key") is True
  11. def test_invalid_api_key(self, mock_requests_post):
  12. mock_requests_post.return_value.status_code = 401
  13. with pytest.raises(ValueError):
  14. Client(api_key="invalid_api_key")
  15. def test_update_valid_api_key(self, mock_requests_post):
  16. mock_requests_post.return_value.status_code = 200
  17. client = Client(api_key="valid_api_key")
  18. client.update("new_valid_api_key")
  19. assert client.get() == "new_valid_api_key"
  20. def test_clear_api_key(self, mock_requests_post):
  21. mock_requests_post.return_value.status_code = 200
  22. client = Client(api_key="valid_api_key")
  23. client.clear()
  24. assert client.get() is None
  25. def test_save_api_key(self, mock_requests_post):
  26. mock_requests_post.return_value.status_code = 200
  27. api_key_to_save = "valid_api_key"
  28. client = Client(api_key=api_key_to_save)
  29. client.save()
  30. assert client.get() == api_key_to_save
  31. def test_load_api_key_from_config(self, mocker):
  32. mocker.patch("embedchain.Client.load_config", return_value={"api_key": "test_api_key"})
  33. client = Client()
  34. assert client.get() == "test_api_key"
  35. def test_load_invalid_api_key_from_config(self, mocker):
  36. mocker.patch("embedchain.Client.load_config", return_value={})
  37. with pytest.raises(ValueError):
  38. Client()
  39. def test_load_missing_api_key_from_config(self, mocker):
  40. mocker.patch("embedchain.Client.load_config", return_value={})
  41. with pytest.raises(ValueError):
  42. Client()