47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
import json
|
|
import os
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
|
|
from garmin.sync import GarminSync
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_client():
|
|
return MagicMock()
|
|
|
|
@pytest.fixture
|
|
def temp_storage(tmp_path):
|
|
return str(tmp_path / "data")
|
|
|
|
def test_sync_activities(mock_client, temp_storage):
|
|
mock_client.get_activities.return_value = [
|
|
{"activityId": 1, "name": "Running"},
|
|
{"activityId": 2, "name": "Cycling"}
|
|
]
|
|
|
|
sync = GarminSync(mock_client, storage_dir=temp_storage)
|
|
count = sync.sync_activities(days=1)
|
|
|
|
assert count == 2
|
|
assert os.path.exists(os.path.join(temp_storage, "activity_1.json"))
|
|
assert os.path.exists(os.path.join(temp_storage, "activity_2.json"))
|
|
|
|
def test_load_local_activities(mock_client, temp_storage):
|
|
os.makedirs(temp_storage, exist_ok=True)
|
|
with open(os.path.join(temp_storage, "activity_1.json"), "w") as f:
|
|
json.dump({"activityId": 1}, f)
|
|
|
|
sync = GarminSync(mock_client, storage_dir=temp_storage)
|
|
activities = sync.load_local_activities()
|
|
|
|
assert len(activities) == 1
|
|
assert activities[0]["activityId"] == 1
|
|
|
|
def test_save_activity_no_id(mock_client, temp_storage):
|
|
sync = GarminSync(mock_client, storage_dir=temp_storage)
|
|
sync._save_activity({"name": "No ID"})
|
|
|
|
assert len(os.listdir(temp_storage)) == 0 if os.path.exists(temp_storage) else True
|