askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
test_dron_version.py · 4.8 KB · 105 lines
| 1 | """A package says which DRON it was written for, and a server refuses one it cannot read. |
| 2 | |
| 3 | The manifest's optional `dron` key is "MAJOR.MINOR" (docs/dron.md, |
| 4 | "Versioning"). A server reads its own version and every earlier minor of the |
| 5 | same major; a later minor or another major may say something this server |
| 6 | would misread, so the install is refused before anything is written, with a |
| 7 | message naming both versions. A manifest without the key was written before |
| 8 | versions were declared and installs as it always did. Only a package |
| 9 | declaration is held to this: nothing stored on a box is refused for the |
| 10 | version it names. |
| 11 | """ |
| 12 | |
| 13 | from __future__ import annotations |
| 14 | |
| 15 | import shutil |
| 16 | from pathlib import Path |
| 17 | |
| 18 | import pytest |
| 19 | |
| 20 | import object_packages |
| 21 | from test_dron_loader_refusals import _refused_install, _snapshot, _write_package |
| 22 | |
| 23 | |
| 24 | def _install(tmp_path: Path, **manifest_extra): |
| 25 | packages, data, objects = tmp_path / "packages", tmp_path / "data", tmp_path / "objects" |
| 26 | objects.mkdir(exist_ok=True) |
| 27 | _write_package(packages, manifest_extra=manifest_extra) |
| 28 | object_packages.install_package("widgets", root=packages, base_dir=data, object_roots=[objects]) |
| 29 | return data |
| 30 | |
| 31 | |
| 32 | def test_the_server_reads_one_dron_version_and_says_which(): |
| 33 | major, minor = object_packages.DRON_VERSION.split(".") |
| 34 | assert (major, minor) == ("0", "2") |
| 35 | |
| 36 | |
| 37 | def test_a_manifest_without_the_key_installs_as_before(tmp_path): |
| 38 | data = _install(tmp_path) |
| 39 | assert (data / "schemas" / "widgets.json").is_file() |
| 40 | |
| 41 | |
| 42 | @pytest.mark.parametrize("version", ["0.2", "0.1", "0.0"]) |
| 43 | def test_the_servers_version_or_an_earlier_minor_installs(tmp_path, version): |
| 44 | data = _install(tmp_path, dron=version) |
| 45 | assert (data / "schemas" / "widgets.json").is_file() |
| 46 | |
| 47 | |
| 48 | @pytest.mark.parametrize("value", [0.2, 2, True, None, "", "0", "0.2.0", "v0.2", " 0.2", "0.2 ", "0.02", "00.2", |
| 49 | "0.x", "zero.two", ["0", "2"], {"major": 0}]) |
| 50 | def test_a_value_that_is_not_major_dot_minor_text_is_refused_and_names_the_key(tmp_path, value): |
| 51 | message = _refused_install(tmp_path, manifest_extra={"dron": value}) |
| 52 | assert "dbbasic-package.json" in message |
| 53 | assert f"dron {value!r} is not a DRON version" in message |
| 54 | assert '"MAJOR.MINOR"' in message and "this server reads DRON 0.2" in message |
| 55 | |
| 56 | |
| 57 | def test_a_later_minor_is_refused_naming_both_versions_and_the_upgrade(tmp_path): |
| 58 | message = _refused_install(tmp_path, manifest_extra={"dron": "0.3"}) |
| 59 | assert "dbbasic-package.json" in message |
| 60 | assert "written for DRON 0.3" in message and "this server reads DRON 0.2" in message |
| 61 | assert "upgrade the server" in message |
| 62 | |
| 63 | |
| 64 | @pytest.mark.parametrize("version", ["1.0", "1.2", "7.0"]) |
| 65 | def test_a_later_major_is_refused_naming_both_versions_and_the_upgrade(tmp_path, version): |
| 66 | message = _refused_install(tmp_path, manifest_extra={"dron": version}) |
| 67 | assert f"written for DRON {version}" in message and "this server reads DRON 0.2" in message |
| 68 | assert "upgrade the server" in message |
| 69 | |
| 70 | |
| 71 | def test_an_earlier_major_is_refused_and_says_the_package_needs_updating(tmp_path, monkeypatch): |
| 72 | monkeypatch.setattr(object_packages, "DRON_VERSION", "1.0") |
| 73 | message = _refused_install(tmp_path, manifest_extra={"dron": "0.2"}) |
| 74 | assert "written for DRON 0.2" in message and "this server reads DRON 1.0" in message |
| 75 | assert "update the package" in message |
| 76 | assert "upgrade the server" not in message |
| 77 | |
| 78 | |
| 79 | def test_the_version_is_judged_before_the_sections_a_later_dron_may_add(tmp_path): |
| 80 | """A later DRON may add a section. The author is told about the version, |
| 81 | which is the cause, not about the section.""" |
| 82 | message = _refused_install(tmp_path, manifest_extra={"dron": "0.3", "workflows": []}) |
| 83 | assert "written for DRON 0.3" in message |
| 84 | assert "unknown section" not in message |
| 85 | |
| 86 | |
| 87 | def test_a_refused_upgrade_leaves_the_installed_version_exactly_as_it_was(tmp_path): |
| 88 | packages, data, objects = tmp_path / "packages", tmp_path / "data", tmp_path / "objects" |
| 89 | _install(tmp_path) |
| 90 | before = _snapshot(data, objects) |
| 91 | shutil.rmtree(packages / "widgets") |
| 92 | _write_package(packages, manifest_extra={"dron": "0.3"}) |
| 93 | with pytest.raises(object_packages.InvalidPackageManifestError, match="written for DRON 0.3"): |
| 94 | object_packages.install_package("widgets", root=packages, base_dir=data, |
| 95 | object_roots=[objects], allow_replace=True) |
| 96 | assert _snapshot(data, objects) == before |
| 97 | |
| 98 | |
| 99 | def test_a_dry_run_reports_the_refusal_and_its_reason(tmp_path): |
| 100 | packages, data = tmp_path / "packages", tmp_path / "data" |
| 101 | _write_package(packages, manifest_extra={"dron": "0.3"}) |
| 102 | with pytest.raises(object_packages.InvalidPackageManifestError, |
| 103 | match=r"written for DRON 0\.3.*this server reads DRON 0\.2"): |
| 104 | object_packages.dry_run_package("widgets", root=packages, base_dir=data, object_roots=[tmp_path]) |
| 105 | assert not data.exists() |