test(audiobook_library): add test functions
This commit is contained in:
1 parent
8f027e2226
commit
0ab31d16c2
6 files changed
+486
No files matched your search
@@ -0,0 +1,62 @@
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from typing import ClassVar
|
||||
from unittest.mock import patch
|
||||
|
||||
from audiobook_library import AudiobookshelfApi
|
||||
|
||||
|
||||
class AudiobookshelfApiTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tempdir = tempfile.TemporaryDirectory()
|
||||
self.root = Path(self.tempdir.name)
|
||||
|
||||
def tearDown(self):
|
||||
self.tempdir.cleanup()
|
||||
|
||||
def test_api_methods(self):
|
||||
api = AudiobookshelfApi("https://example.test/", "key")
|
||||
|
||||
class Response:
|
||||
status_code = 200
|
||||
content = b"{}"
|
||||
headers: ClassVar[dict[str, str]] = {"Content-Type": "application/json"}
|
||||
text = ""
|
||||
|
||||
def raise_for_status(self):
|
||||
return None
|
||||
|
||||
def json(self):
|
||||
return {"ok": True}
|
||||
|
||||
with patch.object(api.session, "request", return_value=Response()):
|
||||
self.assertEqual(api.get("/x"), {"ok": True})
|
||||
self.assertEqual(api.post("/x", {}), {"ok": True})
|
||||
self.assertEqual(api.put("/x", {}), {"ok": True})
|
||||
self.assertEqual(api.delete("/x"), {"ok": True})
|
||||
|
||||
responses = iter(
|
||||
[
|
||||
{"libraries": [{"id": "l1"}]},
|
||||
{"results": [{"id": "b1", "media": {"metadata": {"title": "Book"}}}]},
|
||||
]
|
||||
)
|
||||
with patch.object(api, "get", side_effect=lambda *args, **kwargs: next(responses)):
|
||||
self.assertEqual(
|
||||
api.load_books(),
|
||||
[{"id": "b1", "media": {"metadata": {"title": "Book"}}}],
|
||||
)
|
||||
api.post = lambda *args, **kwargs: {"ok": True}
|
||||
api.embed_metadata_for_book("b1")
|
||||
api.embed_metadata_for_book("missing")
|
||||
api.embed_metadata_for_all_books(SimpleNamespace(dry_run=True))
|
||||
|
||||
ids = self.root / "ids.txt"
|
||||
ids.write_text("# comment\nb1\n")
|
||||
api.embed_metadata_from_file(str(ids))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user