Skip to content

Commit

Permalink
adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edublancas committed Dec 6, 2023
1 parent 77832a2 commit 0e10255
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion tests/telemetry/test_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def test_get_version_timeout():
assert total_runtime < datetime.timedelta(milliseconds=1500)


def write_to_conf_file(tmp_directory, monkeypatch, last_check):
def write_to_conf_file(tmp_directory, monkeypatch, last_check, last_cloud_check=None):
stats = Path("stats")
stats.mkdir()
conf_path = stats / "config.yaml"
Expand All @@ -392,6 +392,9 @@ def write_to_conf_file(tmp_directory, monkeypatch, last_check):
conf_path.write_text("version_check_enabled: True\n")
version_path.write_text(f"last_version_check: {last_check}\n")

if last_cloud_check:
version_path.write_text(f"last_cloud_check: {last_cloud_check}\n")

# force to reset data so we load from the data we just wrote
monkeypatch.setattr(telemetry, "internal", telemetry.Internal())

Expand Down Expand Up @@ -1031,3 +1034,43 @@ def my_function(x, y, z):
}

assert my_function.__wrapped__._telemetry_error is None


@pytest.mark.parametrize(
"last_cloud_check",
[
None,
"2022-01-20 10:51:41.082376",
],
ids=[
"first-time",
"not-first-time",
],
)
def test_check_cloud(tmp_directory, monkeypatch, capsys, last_cloud_check):
write_to_conf_file(
tmp_directory=tmp_directory,
monkeypatch=monkeypatch,
last_check="2022-01-20 10:51:41.082376",
last_cloud_check=last_cloud_check,
)

now = datetime.datetime.now()
fake_datetime = Mock()
fake_datetime.now.return_value = now

with monkeypatch.context() as m:
m.setattr(telemetry.datetime, "datetime", fake_datetime)
telemetry.check_cloud()

config = yaml.safe_load(Path("stats", "uid.yaml").read_text())

captured = capsys.readouterr()

expected = (
"Deploy AI and data apps for free on Ploomber Cloud!"
" Sign up here: https://www.platform.ploomber.io/register"
)

assert expected in captured.out
assert config["last_cloud_check"] == now

0 comments on commit 0e10255

Please sign in to comment.