test_telemetry.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import pytest
  2. from packaging.version import parse as parse_python_version
  3. from reflex.utils import telemetry
  4. def test_telemetry():
  5. """Test that telemetry is sent correctly."""
  6. # Check that the user OS is one of the supported operating systems.
  7. user_os = telemetry.get_os()
  8. assert user_os is not None
  9. assert user_os in ["Linux", "Darwin", "Java", "Windows"]
  10. # Check that the CPU count and memory are greater than 0.
  11. assert telemetry.get_cpu_count() > 0
  12. # Check that the available memory is greater than 0
  13. assert telemetry.get_memory() > 0
  14. # Check that the Reflex version is not None.
  15. assert telemetry.get_reflex_version() is not None
  16. # Check that the Python version is greater than 3.7.
  17. python_version = telemetry.get_python_version()
  18. assert python_version is not None
  19. assert parse_python_version(python_version) >= parse_python_version("3.7")
  20. def test_disable():
  21. """Test that disabling telemetry works."""
  22. assert not telemetry._send("test", telemetry_enabled=False)
  23. @pytest.mark.parametrize("event", ["init", "reinit", "run-dev", "run-prod", "export"])
  24. def test_send(mocker, event):
  25. httpx_post_mock = mocker.patch("httpx.post")
  26. # Mock the read_text method of Path
  27. pathlib_path_read_text_mock = mocker.patch(
  28. "pathlib.Path.read_text",
  29. return_value='{"project_hash": "78285505863498957834586115958872998605"}',
  30. )
  31. mocker.patch("platform.platform", return_value="Mocked Platform")
  32. telemetry._send(event, telemetry_enabled=True)
  33. httpx_post_mock.assert_called_once()
  34. assert pathlib_path_read_text_mock.call_count == 2