| """Unit tests for the HF Dataset sync — fully mocked, no network / huggingface_hub. |
| |
| Verifies: the local-dev guard (no token => no-op), pull copying the remote file |
| in, debounced vs forced push, and flush of pending writes. |
| """ |
|
|
| from __future__ import annotations |
|
|
| import pytest |
|
|
| pytestmark = pytest.mark.unit |
|
|
|
|
| class _FakeApi: |
| def __init__(self): |
| self.uploads = 0 |
| self.repos = [] |
|
|
| def create_repo(self, **kw): |
| self.repos.append(kw.get("repo_id")) |
|
|
| def upload_file(self, **kw): |
| self.uploads += 1 |
|
|
|
|
| def test_disabled_without_token(tmp_path): |
| from src.accounts.hf_sync import HFSync |
|
|
| s = HFSync(repo_id="user/data", local_db_path=tmp_path / "x.db", token=None) |
| assert s.enabled is False |
| |
| assert s.pull() is False |
| assert s.push(force=True) is False |
| assert s.flush() is False |
|
|
|
|
| def test_disabled_without_repo(tmp_path): |
| from src.accounts.hf_sync import HFSync |
|
|
| s = HFSync(repo_id=None, local_db_path=tmp_path / "x.db", token="tok") |
| assert s.enabled is False |
|
|
|
|
| def test_pull_copies_remote_file(tmp_path): |
| from src.accounts.hf_sync import HFSync |
|
|
| |
| remote = tmp_path / "remote.db" |
| remote.write_bytes(b"SQLITEDATA") |
| local = tmp_path / "local" / "accounts.db" |
|
|
| def fake_downloader(**kw): |
| return str(remote) |
|
|
| s = HFSync( |
| repo_id="user/data", |
| local_db_path=local, |
| token="tok", |
| downloader=fake_downloader, |
| ) |
| assert s.enabled is True |
| assert s.pull() is True |
| assert local.read_bytes() == b"SQLITEDATA" |
|
|
|
|
| def test_pull_missing_remote_is_not_error(tmp_path): |
| from src.accounts.hf_sync import HFSync |
|
|
| def boom(**kw): |
| raise FileNotFoundError("not there yet") |
|
|
| s = HFSync( |
| repo_id="user/data", |
| local_db_path=tmp_path / "a.db", |
| token="tok", |
| downloader=boom, |
| ) |
| assert s.pull() is False |
|
|
|
|
| def test_push_debounce_and_force(tmp_path): |
| from src.accounts.hf_sync import HFSync |
|
|
| db = tmp_path / "a.db" |
| db.write_bytes(b"data") |
| api = _FakeApi() |
| s = HFSync( |
| repo_id="user/data", |
| local_db_path=db, |
| token="tok", |
| min_push_interval=1000, |
| api=api, |
| ) |
| assert s.push(force=True) is True |
| assert api.uploads == 1 |
| assert s.maybe_push() is False |
| assert api.uploads == 1 |
| assert s.flush() is True |
| assert api.uploads == 2 |
|
|
|
|
| def test_ensure_repo(tmp_path): |
| from src.accounts.hf_sync import HFSync |
|
|
| api = _FakeApi() |
| s = HFSync(repo_id="user/data", local_db_path=tmp_path / "a.db", token="tok", api=api) |
| s.ensure_repo() |
| assert "user/data" in api.repos |
|
|