import runpy import sqlite3 import tempfile import unittest from pathlib import Path CLI = runpy.run_path(str(Path(__file__).with_name("pageswiki-cli")))["PagesWikiCLI"] class AssetMetadataTest(unittest.TestCase): def test_head_metadata_and_history_follow_cli_uploads(self): with tempfile.TemporaryDirectory() as directory: cli = CLI.__new__(CLI) cli.documents = Path(directory) / "documents" cli.domain_uuid = "test-domain" cli.db = sqlite3.connect(":memory:") self.addCleanup(cli.db.close) cli.db.row_factory = sqlite3.Row cli.db.executescript(""" CREATE TABLE objects(uuid TEXT PRIMARY KEY,kind TEXT,created_at INTEGER); CREATE TABLE assets(domain_uuid TEXT,address TEXT,object_uuid TEXT, head_document_hash TEXT,content_type TEXT,filename TEXT,size_bytes INTEGER, created_at_ms INTEGER,updated_at_ms INTEGER,PRIMARY KEY(domain_uuid,address)); """) address = "asset:image:logo" payload = b"\x89PNG\r\n\x1a\nfixture" self.assertEqual(cli.put_asset(address, payload, "logo.png"), "updated") self.assertEqual(cli.put_asset(address, payload, "logo.png"), "skipped") self.assertEqual(cli.put_asset(address, payload, "renamed.png"), "updated") row = cli.db.execute("SELECT * FROM assets").fetchone() self.assertEqual(row["content_type"], "image/png") self.assertEqual(row["filename"], "renamed.png") self.assertEqual(row["size_bytes"], len(payload)) self.assertEqual(cli.document_path(row["head_document_hash"]).read_bytes(), payload) history = cli.db.execute("SELECT * FROM asset_head_history ORDER BY id").fetchall() self.assertEqual(len(history), 2) self.assertEqual(history[-1]["document_hash"], row["head_document_hash"]) self.assertEqual(history[-1]["filename"], "renamed.png") if __name__ == "__main__": unittest.main()