from __future__ import annotations import json import tempfile from pathlib import Path from unittest import TestCase from pixiv_app.core.library import PixivLibrary class PixivLibraryArtistRepairTest(TestCase): def test_setup_repairs_missing_artwork_artist_from_metadata(self) -> None: with tempfile.TemporaryDirectory() as tmp: db_path = Path(tmp) / "library.db" library = PixivLibrary(db_path) try: with library.connection: library.connection.execute( """ INSERT INTO artworks (artwork_id, artist_id, type, title, metadata_json, updated_at) VALUES (?, NULL, 'illust', 'work', ?, CURRENT_TIMESTAMP) """, (123, json.dumps({"userId": 456, "userName": "Alice"})), ) finally: library.close() repaired = PixivLibrary(db_path) try: row = repaired.connection.execute( "SELECT artist_id FROM artworks WHERE artwork_id = 123" ).fetchone() artist = repaired.connection.execute( "SELECT name FROM artists WHERE artist_id = 456" ).fetchone() finally: repaired.close() self.assertEqual(row["artist_id"], 456) self.assertEqual(artist["name"], "Alice")