prole/tests/test_database_options.py
chrisfu 4ee2b259c9 Checkpoint: rename installer to knoe + harden db build context
- Add build-context helper to copy Docker context safely (ignore runtime data, keep symlinks)

- Update UI and core actions to use ~/.prole/build and shared copy helper

- Add/adjust tests and scripts; introduce knoe ops helpers and update manifests

Co-authored-by: Junie <junie@jetbrains.com>
2026-03-22 01:45:21 -07:00

192 lines
7.9 KiB
Python

import sys
from unittest.mock import patch, MagicMock
from contextlib import ExitStack
import pytest
from pathlib import Path
import tkinter as tk
class MockVar:
def __init__(self, value=None):
self.value = value
def get(self):
return self.value
def set(self, value):
self.value = value
def trace_add(self, mode, callback):
pass
# Mock tkinter and other GUI/macOS specific imports
# We use patch.dict to avoid polluting other tests if possible,
# but sys.modules is global.
# Better: just use patch in the fixture.
sys.modules["Foundation"] = MagicMock()
sys.modules["AppKit"] = MagicMock()
sys.modules["PIL"] = MagicMock()
sys.modules["PIL.Image"] = MagicMock()
sys.modules["PIL.ImageTk"] = MagicMock()
import install
from install import ProleInstaller
@pytest.fixture
def mock_installer(tmp_path):
root = MagicMock()
# We need to patch the tk references in the modules that use them,
# because they might have already been imported with a different mock.
patches = [
patch("knoe.ui.screens.PROJECT_ROOT", tmp_path),
patch("knoe.ui.screens.database_options.PROJECT_ROOT", tmp_path),
patch("knoe.core.controller.ProleController.run_script"),
patch.object(ProleInstaller, "_load_database_versions"),
patch("knoe.ui.screens.database_options.tk.BooleanVar", side_effect=lambda value=None: MockVar(value)),
patch("knoe.ui.screens.database_options.tk.StringVar", side_effect=lambda value=None: MockVar(value)),
patch("knoe.ui.screens.database_options.tk.ttk.Treeview"),
patch("knoe.ui.screens.database_options.tk.ttk.Scrollbar"),
patch("knoe.ui.screens.database_options.tk.ttk.Combobox"),
patch("knoe.ui.screens.database_options.tk.Radiobutton"),
patch("knoe.ui.screens.database_options.tk.Checkbutton"),
patch("knoe.ui.screens.database_options.tk.Label"),
# Also for the main class if it uses them
patch("knoe.ui.screens.tk.BooleanVar", side_effect=lambda value=None: MockVar(value)),
patch("knoe.ui.screens.tk.StringVar", side_effect=lambda value=None: MockVar(value)),
]
with ExitStack() as stack:
for p in patches:
stack.enter_context(p)
# Create necessary directories
(tmp_path / "prole-db").mkdir()
(tmp_path / "conf" / "postgresql").mkdir(parents=True)
(tmp_path / "etc").mkdir()
app = ProleInstaller(root)
app.bg_canvas = MagicMock()
app.root = root
yield app
def test_database_options_state_init(mock_installer):
assert hasattr(mock_installer, "db_at_rest_encryption")
assert mock_installer.db_at_rest_encryption.get() is True
assert mock_installer.db_distribution.get() == "percona"
# Default is Percona 18
assert mock_installer.db_version_type.get() == "v18"
def test_encryption_toggle(mock_installer):
# Initial state: Encryption ON -> Percona
assert mock_installer.db_at_rest_encryption.get() is True
assert mock_installer.db_distribution.get() == "percona"
# Toggle OFF -> should switch to postgresql
mock_installer.db_at_rest_encryption.set(False)
# We need to manually call the command since we're setting the variable directly in test
# In real UI, the command=on_encryption_toggle would handle it.
# Find the encryption toggle callback (it's local to _render_database_options_page but let's see)
# Actually it might be easier to just test the logic if I can access it.
# Let's mock the render to get the callback
with patch("knoe.ui.screens.database_options.tk.Checkbutton") as mock_cb:
mock_installer._render_database_options_page()
args, kwargs = mock_cb.call_args
on_encryption_toggle = kwargs["command"]
mock_installer.db_at_rest_encryption.set(False)
on_encryption_toggle()
assert mock_installer.db_distribution.get() == "postgresql"
mock_installer.db_at_rest_encryption.set(True)
on_encryption_toggle()
assert mock_installer.db_distribution.get() == "percona"
def test_version_selection(mock_installer):
mock_installer.db_versions_data = {
"postgresql": {"stable": "14", "current": "15", "latest": "16"},
"percona": {"stable": "15", "current": "16", "latest": "17", "v18": "18"}
}
mock_installer.db_distribution.set("percona")
mock_installer._render_database_options_page()
# Initially it should be Latest
mock_installer._refresh_database_options_ui()
assert mock_installer.db_selected_version.get() == "18 (Percona 18)"
# Test switching to stable
mock_installer.db_selected_version.set("15") # simulate partial string match or manual set
mock_installer._refresh_database_options_ui()
assert mock_installer.db_selected_version.get() == "15 (Stable)"
def test_dockerfile_generation(mock_installer, tmp_path):
mock_installer.db_distribution.set("percona")
mock_installer.db_selected_version.set("17 (Latest)")
template_path = tmp_path / "prole-db" / "Dockerfile.percona.template"
template_path.write_text("FROM percona:{{MAJOR_VERSION}}\n{{EXTENSION_INSTALL_STEPS}}\n{{EXTENSION_CREATE_STEPS}}")
mock_installer.db_extensions["postgis"].set(True)
mock_installer.db_extensions["pgvector"].set(False)
success = mock_installer._generate_prole_db_dockerfile()
assert success is True
dockerfile = tmp_path / "prole-db" / "Dockerfile"
assert dockerfile.exists()
content = dockerfile.read_text()
assert "FROM percona:17" in content
assert "percona-postgresql-17-postgis-3" in content
assert "CREATE EXTENSION IF NOT EXISTS postgis;" in content
assert "vector" not in content
def test_dockerfile_generation_percona_18(mock_installer, tmp_path):
mock_installer.db_distribution.set("percona")
mock_installer.db_selected_version.set("18 (Latest)")
template_path = tmp_path / "prole-db" / "Dockerfile.percona.template"
template_path.write_text("FROM percona:{{MAJOR_VERSION}}\n{{EXTENSION_INSTALL_STEPS}}\n{{EXTENSION_CREATE_STEPS}}")
# Enable a contrib extension
mock_installer.db_extensions["pgcrypto"].set(True)
# Enable a separate package extension
mock_installer.db_extensions["pg_repack"].set(True)
# Enable pgvector (verify name 'vector')
mock_installer.db_extensions["pgvector"].set(True)
success = mock_installer._generate_prole_db_dockerfile()
assert success is True
dockerfile = tmp_path / "prole-db" / "Dockerfile"
content = dockerfile.read_text()
assert "FROM percona:18" in content
# pgcrypto should NOT have an apt-get install line because it's in contrib
assert "percona-postgresql-18-pgcrypto" not in content
# pg_repack should use 'repack' instead of 'pg_repack' for Percona
assert "percona-postgresql-18-repack" in content
# pgvector should use 'pgvector' for package
assert "percona-postgresql-18-pgvector" in content
# SQL creation steps
assert "CREATE EXTENSION IF NOT EXISTS pgcrypto;" in content
assert "CREATE EXTENSION IF NOT EXISTS pg_repack;" in content
assert "CREATE EXTENSION IF NOT EXISTS vector;" in content
def test_pgbadger_handling(mock_installer, tmp_path):
mock_installer.db_distribution.set("percona")
mock_installer.db_selected_version.set("18")
template_path = tmp_path / "prole-db" / "Dockerfile.percona.template"
template_path.write_text("{{EXTENSION_INSTALL_STEPS}}\n{{EXTENSION_CREATE_STEPS}}")
mock_installer.db_extensions["pgbadger"].set(True)
success = mock_installer._generate_prole_db_dockerfile()
assert success is True
dockerfile = tmp_path / "prole-db" / "Dockerfile"
content = dockerfile.read_text()
assert "apt-get install -y --no-install-recommends percona-pgbadger" in content
assert "CREATE EXTENSION IF NOT EXISTS pgbadger" not in content