Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronsteers committed Mar 12, 2024
1 parent 7c70f14 commit 42a8f42
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions tests/unit_tests/test_anonymous_usage_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import itertools
from contextlib import nullcontext as does_not_raise
import json
import os
from pathlib import Path
import re
from unittest.mock import Mock, call, patch
from unittest.mock import MagicMock, call, patch
from freezegun import freeze_time

import responses
Expand All @@ -16,8 +18,6 @@
from airbyte.version import get_version
import airbyte as ab
from airbyte._util import telemetry
import requests
import datetime


@responses.activate
Expand Down Expand Up @@ -174,3 +174,45 @@ def test_tracking(
}
)
])

def test_setup_analytics_existing_file(monkeypatch):
# Mock the environment variable and the analytics file
monkeypatch.setattr(Path, 'exists', lambda x: True)
monkeypatch.setattr(Path, 'read_text', lambda x: "anonymous_user_id: test_id\n")
assert telemetry._setup_analytics() == 'test_id'


def test_setup_analytics_missing_file(monkeypatch):
"""Mock the environment variable and the missing analytics file."""
monkeypatch.setenv(telemetry._ENV_ANALYTICS_ID, 'test_id')
monkeypatch.setattr(Path, 'exists', lambda x: False)

mock_path = MagicMock()
monkeypatch.setattr(Path, 'write_text', mock_path)

assert telemetry._setup_analytics() == 'test_id'

assert mock_path.call_count == 1


def test_setup_analytics_corrupt_file(monkeypatch):
"""Mock the environment variable and the missing analytics file."""
monkeypatch.setattr(Path, 'exists', lambda x: True)
monkeypatch.setattr(Path, 'read_text', lambda x: "not-a-valid ::: yaml file\n")

mock = MagicMock()
monkeypatch.setattr(Path, 'write_text', mock)

assert telemetry._setup_analytics()

assert mock.call_count == 1


def test_get_analytics_id(monkeypatch):
# Mock the _ANALYTICS_ID variable
monkeypatch.setattr(telemetry, '_ANALYTICS_ID', 'test_id')

mock = MagicMock()
monkeypatch.setattr(Path, 'write_text', mock)

assert telemetry._get_analytics_id() == 'test_id'

0 comments on commit 42a8f42

Please sign in to comment.