|
| 1 | +"""Offline tests for the uri property of the file controllers. |
| 2 | +
|
| 3 | +Covers #68: every controller reports where its file lives, in the URI scheme of |
| 4 | +its own storage backend. |
| 5 | +""" |
| 6 | + |
| 7 | +from io import StringIO |
| 8 | +from pathlib import Path |
| 9 | +from types import SimpleNamespace |
| 10 | +from typing import IO, Any, Dict |
| 11 | + |
| 12 | +from osw.controller.file.local import LocalFileController |
| 13 | +from osw.controller.file.memory import InMemoryController |
| 14 | +from osw.controller.file.remote import RemoteFileController |
| 15 | +from osw.controller.file.wiki import WikiFileController |
| 16 | +from osw.core import model |
| 17 | + |
| 18 | + |
| 19 | +class OfflineRemoteFileController(RemoteFileController): |
| 20 | + """A remote controller carrying nothing but a url, as model.S3File does""" |
| 21 | + |
| 22 | + url: str |
| 23 | + |
| 24 | + def get(self) -> IO: |
| 25 | + pass |
| 26 | + |
| 27 | + def put(self, file: IO, **kwargs: Dict[str, Any]): |
| 28 | + pass |
| 29 | + |
| 30 | + |
| 31 | +def test_local_uri_is_a_file_url(tmp_path): |
| 32 | + path = tmp_path / "test.txt" |
| 33 | + path.write_text("content", encoding="utf-8") |
| 34 | + |
| 35 | + uri = LocalFileController(path=path).uri |
| 36 | + |
| 37 | + assert uri.startswith("file:///") |
| 38 | + assert uri.endswith("/test.txt") |
| 39 | + |
| 40 | + |
| 41 | +def test_local_uri_is_absolute_for_a_relative_path(tmp_path, monkeypatch): |
| 42 | + """as_uri() rejects a relative path, so the path has to be resolved first.""" |
| 43 | + monkeypatch.chdir(tmp_path) |
| 44 | + |
| 45 | + uri = LocalFileController(path=Path("test.txt")).uri |
| 46 | + |
| 47 | + assert uri == (Path.cwd() / "test.txt").as_uri() |
| 48 | + |
| 49 | + |
| 50 | +def test_local_uri_escapes_reserved_characters(tmp_path): |
| 51 | + uri = LocalFileController(path=tmp_path / "a file.txt").uri |
| 52 | + |
| 53 | + assert uri.endswith("/a%20file.txt") |
| 54 | + |
| 55 | + |
| 56 | +def test_in_memory_uri_is_none(): |
| 57 | + """A stream has no location, so there is nothing to point at.""" |
| 58 | + assert InMemoryController().uri is None |
| 59 | + |
| 60 | + |
| 61 | +def test_in_memory_keeps_the_stream_it_is_given(): |
| 62 | + stream = StringIO("content") |
| 63 | + |
| 64 | + assert InMemoryController(stream=stream).get() is stream |
| 65 | + |
| 66 | + |
| 67 | +def test_remote_uri_is_the_url(): |
| 68 | + url = "s3://s3.example.org/bucket/OSW0000.txt" |
| 69 | + controller = OfflineRemoteFileController( |
| 70 | + url=url, label=[model.Label(text="OSW0000.txt")] |
| 71 | + ) |
| 72 | + |
| 73 | + assert controller.uri == url |
| 74 | + |
| 75 | + |
| 76 | +def test_wiki_uri_is_the_file_page_url(): |
| 77 | + """WikiFileController builds url itself, so uri needs no override there.""" |
| 78 | + page = SimpleNamespace( |
| 79 | + osw=SimpleNamespace( |
| 80 | + mw_site=SimpleNamespace(scheme="https", host="wiki.example.org") |
| 81 | + ), |
| 82 | + title="OSW0000.txt", |
| 83 | + ) |
| 84 | + |
| 85 | + url = WikiFileController.url.fget(page) |
| 86 | + |
| 87 | + assert url == "https://wiki.example.org/wiki/File:OSW0000.txt" |
| 88 | + assert RemoteFileController.uri.fget(SimpleNamespace(url=url)) == url |
0 commit comments