mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-02 07:18:25 +01:00
65 lines
1.6 KiB
Python
65 lines
1.6 KiB
Python
import json
|
|
import pytest
|
|
import mock
|
|
|
|
from dvc import analytics
|
|
|
|
|
|
@pytest.fixture
|
|
def tmp_global_config(tmp_path):
|
|
with mock.patch(
|
|
"dvc.config.Config.get_global_config_dir", return_value=tmp_path
|
|
):
|
|
yield
|
|
|
|
|
|
def test_collect(tmp_global_config):
|
|
report = analytics.collect(return_code=0)
|
|
|
|
assert report["cmd_return_code"] == 0
|
|
assert report["scm_class"] in ["Git", None]
|
|
assert not report["cmd_class"]
|
|
assert type(report["is_binary"]) is bool
|
|
assert type(report["system_info"]) is dict
|
|
assert type(report["dvc_version"]) is str
|
|
assert type(report["user_id"]) is str
|
|
|
|
|
|
@mock.patch("requests.post")
|
|
def test_send(mock_post, tmp_path):
|
|
url = "https://analytics.dvc.org"
|
|
report = {"name": "dummy report"}
|
|
fname = tmp_path / "report"
|
|
|
|
fname.write_text(json.dumps(report))
|
|
analytics.send(fname)
|
|
mock_post.assert_called_with(url, json=report, timeout=5)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"config, result",
|
|
[
|
|
({}, True),
|
|
({"analytics": "false"}, False),
|
|
({"analytics": "true"}, True),
|
|
({"unknown": "broken"}, True),
|
|
({"analytics": "false", "unknown": "broken"}, False),
|
|
],
|
|
)
|
|
def test_is_enabled(dvc_repo, config, result, monkeypatch):
|
|
configobj = dvc_repo.config._repo_config
|
|
configobj["core"] = config
|
|
configobj.write()
|
|
|
|
# reset DVC_TEST env var, which affects `is_enabled()`
|
|
monkeypatch.delenv("DVC_TEST")
|
|
|
|
assert result == analytics.is_enabled()
|
|
|
|
|
|
def test_find_or_create_user_id(tmp_global_config):
|
|
created = analytics.find_or_create_user_id()
|
|
found = analytics.find_or_create_user_id()
|
|
|
|
assert created == found
|