feat: add atomic state management (TripState, Photo, Group)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import json
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from app.state import TripState, Photo, Group, load_state, save_state
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app_ctx(flask_app):
|
||||
with flask_app.app_context():
|
||||
yield flask_app
|
||||
|
||||
|
||||
def test_save_and_load_roundtrip(app_ctx, state_dir):
|
||||
state = TripState(
|
||||
album_id="test-album",
|
||||
album_name="Test",
|
||||
grav_trip_slug="test-trip",
|
||||
photos=[Photo(id="p1", original_filename="a.jpg",
|
||||
local_datetime="2023-01-01T10:00:00")],
|
||||
groups=[],
|
||||
)
|
||||
save_state(state, app_ctx)
|
||||
loaded = load_state("test-album", app_ctx)
|
||||
assert loaded.album_id == "test-album"
|
||||
assert loaded.photos[0].id == "p1"
|
||||
|
||||
|
||||
def test_atomic_write_uses_tmp(app_ctx, state_dir, monkeypatch):
|
||||
written_paths = []
|
||||
real_rename = __import__("os").rename
|
||||
def fake_rename(src, dst):
|
||||
written_paths.append(src)
|
||||
real_rename(src, dst)
|
||||
monkeypatch.setattr("app.state.os.rename", fake_rename)
|
||||
state = TripState(album_id="atomic-test", album_name="X", grav_trip_slug="x")
|
||||
save_state(state, app_ctx)
|
||||
assert any(str(p).endswith(".tmp") for p in written_paths)
|
||||
|
||||
|
||||
def test_load_nonexistent_returns_none(app_ctx):
|
||||
assert load_state("no-such-album", app_ctx) is None
|
||||
|
||||
|
||||
def test_exported_status_field_preserved(app_ctx):
|
||||
state = TripState(
|
||||
album_id="export-test", album_name="E", grav_trip_slug="e",
|
||||
groups=[Group(id="g1", photo_ids=[], entry_type="journal",
|
||||
status="exported")]
|
||||
)
|
||||
save_state(state, app_ctx)
|
||||
loaded = load_state("export-test", app_ctx)
|
||||
assert loaded.groups[0].status == "exported"
|
||||
Reference in New Issue
Block a user