File: test_cli.py

package info (click to toggle)
python-advanced-alchemy 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,848 kB
  • sloc: python: 35,975; makefile: 153; sh: 4
file content (461 lines) | stat: -rw-r--r-- 17,681 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
from __future__ import annotations

import os
import tempfile
from collections.abc import Generator
from pathlib import Path
from typing import TYPE_CHECKING
from unittest.mock import MagicMock, patch

import pytest
from click.testing import CliRunner
from sqlalchemy.ext.asyncio import AsyncEngine

from advanced_alchemy.cli import add_migration_commands, get_alchemy_group

if TYPE_CHECKING:
    from click import Group


@pytest.fixture
def cli_runner() -> Generator[CliRunner, None, None]:
    """Create a Click CLI test runner."""
    yield CliRunner()


@pytest.fixture
def mock_config() -> Generator[MagicMock, None, None]:
    """Create a mock SQLAlchemy config."""
    config = MagicMock()
    config.bind_key = "default"
    config.alembic_config.script_location = "migrations"
    config.get_engine.return_value = MagicMock(spec=AsyncEngine)
    yield config


@pytest.fixture
def mock_context(mock_config: MagicMock) -> Generator[MagicMock, None, None]:
    """Create a mock Click context."""
    ctx = MagicMock()
    ctx.obj = {"configs": [mock_config]}
    yield ctx


@pytest.fixture
def database_cli(mock_context: MagicMock) -> Generator[Group, None, None]:
    """Create the database CLI group."""
    cli_group = get_alchemy_group()
    cli_group = add_migration_commands()
    cli_group.ctx = mock_context  # pyright: ignore[reportAttributeAccessIssue]
    yield cli_group


def test_show_current_revision(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock) -> None:
    """Test the show-current-revision command."""
    with patch("advanced_alchemy.alembic.commands.AlembicCommands") as mock_alembic:
        result = cli_runner.invoke(
            database_cli,
            ["--config", "tests.unit.fixtures.configs", "show-current-revision"],
        )
        assert result.exit_code == 0
        mock_alembic.assert_called_once()
        mock_alembic.return_value.current.assert_called_once_with(verbose=False)


@pytest.mark.parametrize("no_prompt", [True, False])
def test_downgrade_database(
    cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock, no_prompt: bool
) -> None:
    """Test the downgrade command."""
    with patch("advanced_alchemy.alembic.commands.AlembicCommands") as mock_alembic:
        args = ["--config", "tests.unit.fixtures.configs", "downgrade"]
        if no_prompt:
            args.append("--no-prompt")

        result = cli_runner.invoke(database_cli, args)

        if no_prompt:
            assert result.exit_code == 0
            mock_alembic.assert_called_once()
            mock_alembic.return_value.downgrade.assert_called_once_with(revision="-1", sql=False, tag=None)
        else:
            # it's going to be -1 because we abort the task since we don't fill in the prompt
            assert result.exit_code == 1
            # When prompting is enabled, we need to check if the confirmation was shown
            assert "Are you sure you want to downgrade" in result.output


@pytest.mark.parametrize("no_prompt", [True, False])
def test_upgrade_database(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock, no_prompt: bool) -> None:
    """Test the upgrade command."""
    with patch("advanced_alchemy.alembic.commands.AlembicCommands") as mock_alembic:
        args = ["--config", "tests.unit.fixtures.configs", "upgrade", "head"]
        if no_prompt:
            args.append("--no-prompt")

        result = cli_runner.invoke(database_cli, args)

        if no_prompt:
            assert result.exit_code == 0
            mock_alembic.assert_called_once()
            mock_alembic.return_value.upgrade.assert_called_once_with(revision="head", sql=False, tag=None)
        else:
            # it's going to be -1 because we abort the task since we don't fill in the prompt
            assert result.exit_code == 1
            assert "Are you sure you want migrate the database" in result.output


def test_init_alembic(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock) -> None:
    """Test the init command."""
    with patch("advanced_alchemy.alembic.commands.AlembicCommands") as mock_alembic:
        result = cli_runner.invoke(
            database_cli,
            ["--config", "tests.unit.fixtures.configs", "init", "--no-prompt", "migrations"],
        )
        assert result.exit_code == 0
        mock_alembic.assert_called_once()
        mock_alembic.return_value.init.assert_called_once_with(directory="migrations", multidb=False, package=True)


def test_make_migrations(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock) -> None:
    """Test the make-migrations command."""
    with patch("advanced_alchemy.alembic.commands.AlembicCommands") as mock_alembic:
        result = cli_runner.invoke(
            database_cli,
            ["--config", "tests.unit.fixtures.configs", "make-migrations", "--no-prompt", "-m", "test migration"],
        )
        assert result.exit_code == 0
        mock_alembic.assert_called_once()
        mock_alembic.return_value.revision.assert_called_once()


def test_drop_all(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock) -> None:
    """Test the drop-all command."""

    result = cli_runner.invoke(database_cli, ["--config", "tests.unit.fixtures.configs", "drop-all", "--no-prompt"])
    assert result.exit_code == 0


def test_dump_data(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock, tmp_path: Path) -> None:
    """Test the dump-data command."""

    result = cli_runner.invoke(
        database_cli,
        ["--config", "tests.unit.fixtures.configs", "dump-data", "--table", "test_table", "--dir", str(tmp_path)],
    )

    assert result.exit_code == 0


def test_stamp(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock) -> None:
    """Test the stamp command."""
    with patch("advanced_alchemy.alembic.commands.AlembicCommands") as mock_alembic:
        result = cli_runner.invoke(
            database_cli,
            ["--config", "tests.unit.fixtures.configs", "stamp", "head"],
        )

        assert result.exit_code == 0
        mock_alembic.assert_called_once()
        mock_alembic.return_value.stamp.assert_called_once_with(revision="head", sql=False, tag=None, purge=False)


def test_stamp_with_options(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock) -> None:
    """Test the stamp command with all options."""
    with patch("advanced_alchemy.alembic.commands.AlembicCommands") as mock_alembic:
        result = cli_runner.invoke(
            database_cli,
            ["--config", "tests.unit.fixtures.configs", "stamp", "--sql", "--tag", "v1.0", "--purge", "head"],
        )

        assert result.exit_code == 0
        mock_alembic.assert_called_once()
        mock_alembic.return_value.stamp.assert_called_once_with(revision="head", sql=True, tag="v1.0", purge=True)


def test_check(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock) -> None:
    """Test the check command."""
    with patch("advanced_alchemy.alembic.commands.AlembicCommands") as mock_alembic:
        result = cli_runner.invoke(
            database_cli,
            ["--config", "tests.unit.fixtures.configs", "check"],
        )

        assert result.exit_code == 0
        mock_alembic.assert_called_once()
        mock_alembic.return_value.check.assert_called_once()


def test_edit(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock) -> None:
    """Test the edit command."""
    with patch("advanced_alchemy.alembic.commands.AlembicCommands") as mock_alembic:
        result = cli_runner.invoke(
            database_cli,
            ["--config", "tests.unit.fixtures.configs", "edit", "abc123"],
        )

        assert result.exit_code == 0
        mock_alembic.assert_called_once()
        mock_alembic.return_value.edit.assert_called_once_with(revision="abc123")


def test_ensure_version(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock) -> None:
    """Test the ensure-version command."""
    with patch("advanced_alchemy.alembic.commands.AlembicCommands") as mock_alembic:
        result = cli_runner.invoke(
            database_cli,
            ["--config", "tests.unit.fixtures.configs", "ensure-version"],
        )

        assert result.exit_code == 0
        mock_alembic.assert_called_once()
        mock_alembic.return_value.ensure_version.assert_called_once_with(sql=False)


def test_heads(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock) -> None:
    """Test the heads command."""
    with patch("advanced_alchemy.alembic.commands.AlembicCommands") as mock_alembic:
        result = cli_runner.invoke(
            database_cli,
            ["--config", "tests.unit.fixtures.configs", "heads", "--verbose", "--resolve-dependencies"],
        )

        assert result.exit_code == 0
        mock_alembic.assert_called_once()
        mock_alembic.return_value.heads.assert_called_once_with(verbose=True, resolve_dependencies=True)


def test_history(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock) -> None:
    """Test the history command."""
    with patch("advanced_alchemy.alembic.commands.AlembicCommands") as mock_alembic:
        result = cli_runner.invoke(
            database_cli,
            [
                "--config",
                "tests.unit.fixtures.configs",
                "history",
                "--verbose",
                "--rev-range",
                "base:head",
                "--indicate-current",
            ],
        )

        assert result.exit_code == 0
        mock_alembic.assert_called_once()
        mock_alembic.return_value.history.assert_called_once_with(
            rev_range="base:head",
            verbose=True,
            indicate_current=True,
        )


def test_merge(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock) -> None:
    """Test the merge command."""
    with patch("advanced_alchemy.alembic.commands.AlembicCommands") as mock_alembic:
        result = cli_runner.invoke(
            database_cli,
            ["--config", "tests.unit.fixtures.configs", "merge", "--no-prompt", "-m", "test merge", "heads"],
        )

        assert result.exit_code == 0
        mock_alembic.assert_called_once()
        mock_alembic.return_value.merge.assert_called_once_with(
            revisions="heads",
            message="test merge",
            branch_label=None,
            rev_id=None,
        )


def test_show(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock) -> None:
    """Test the show command."""
    with patch("advanced_alchemy.alembic.commands.AlembicCommands") as mock_alembic:
        result = cli_runner.invoke(
            database_cli,
            ["--config", "tests.unit.fixtures.configs", "show", "head"],
        )

        assert result.exit_code == 0
        mock_alembic.assert_called_once()
        mock_alembic.return_value.show.assert_called_once_with(rev="head")


def test_branches(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock) -> None:
    """Test the branches command."""
    with patch("advanced_alchemy.alembic.commands.AlembicCommands") as mock_alembic:
        result = cli_runner.invoke(
            database_cli,
            ["--config", "tests.unit.fixtures.configs", "branches", "--verbose"],
        )

        assert result.exit_code == 0
        mock_alembic.assert_called_once()
        mock_alembic.return_value.branches.assert_called_once_with(verbose=True)


def test_list_templates(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock) -> None:
    """Test the list-templates command."""
    with patch("advanced_alchemy.alembic.commands.AlembicCommands") as mock_alembic:
        result = cli_runner.invoke(
            database_cli,
            ["--config", "tests.unit.fixtures.configs", "list-templates"],
        )

        assert result.exit_code == 0
        mock_alembic.assert_called_once()
        mock_alembic.return_value.list_templates.assert_called_once()


def test_cli_group_creation() -> None:
    """Test that the CLI group is created correctly."""
    cli_group = add_migration_commands()
    assert cli_group.name == "alchemy"
    # Original commands
    assert "show-current-revision" in cli_group.commands
    assert "upgrade" in cli_group.commands
    assert "downgrade" in cli_group.commands
    assert "init" in cli_group.commands
    assert "make-migrations" in cli_group.commands
    assert "drop-all" in cli_group.commands
    assert "dump-data" in cli_group.commands
    assert "stamp" in cli_group.commands
    # New commands added for Alembic CLI alignment
    assert "check" in cli_group.commands
    assert "edit" in cli_group.commands
    assert "ensure-version" in cli_group.commands
    assert "heads" in cli_group.commands
    assert "history" in cli_group.commands
    assert "merge" in cli_group.commands
    assert "show" in cli_group.commands
    assert "branches" in cli_group.commands
    assert "list-templates" in cli_group.commands


def test_external_config_loading(cli_runner: CliRunner) -> None:
    """Test loading config from external module in current working directory."""
    with tempfile.TemporaryDirectory() as temp_dir:
        temp_path = Path(temp_dir)

        # Create an external config file in the temp directory
        config_file = temp_path / "external_config.py"
        config_file.write_text("""
from advanced_alchemy.config import SQLAlchemyAsyncConfig

config = SQLAlchemyAsyncConfig(
    connection_string="sqlite+aiosqlite:///:memory:",
    bind_key="external",
)
""")

        # Change to the temp directory
        original_cwd = os.getcwd()
        try:
            os.chdir(temp_dir)

            # Test that the external config can be loaded
            cli_group = add_migration_commands()

            # Use a minimal command that doesn't require database setup
            # but still needs the config to be loaded successfully
            result = cli_runner.invoke(cli_group, ["--config", "external_config.config", "--help"])

            # Should succeed without import errors
            assert result.exit_code == 0
            assert "Error loading config" not in result.output
            assert "No module named" not in result.output

        finally:
            os.chdir(original_cwd)


def test_external_config_loading_multiple_configs(cli_runner: CliRunner) -> None:
    """Test loading multiple configs from external module."""
    with tempfile.TemporaryDirectory() as temp_dir:
        temp_path = Path(temp_dir)

        # Create an external config file with multiple configs
        config_file = temp_path / "multi_config.py"
        config_file.write_text("""
from advanced_alchemy.config import SQLAlchemyAsyncConfig

configs = [
    SQLAlchemyAsyncConfig(
        connection_string="sqlite+aiosqlite:///:memory:",
        bind_key="primary",
    ),
    SQLAlchemyAsyncConfig(
        connection_string="sqlite+aiosqlite:///:memory:",
        bind_key="secondary",
    ),
]
""")

        # Change to the temp directory
        original_cwd = os.getcwd()
        try:
            os.chdir(temp_dir)

            cli_group = add_migration_commands()
            result = cli_runner.invoke(cli_group, ["--config", "multi_config.configs", "--help"])

            # Should succeed without import errors
            assert result.exit_code == 0
            assert "Error loading config" not in result.output
            assert "No module named" not in result.output

        finally:
            os.chdir(original_cwd)


def test_external_config_loading_nonexistent_module(cli_runner: CliRunner) -> None:
    """Test appropriate error when external module doesn't exist."""
    with tempfile.TemporaryDirectory() as temp_dir:
        # Change to empty temp directory
        original_cwd = os.getcwd()
        try:
            os.chdir(temp_dir)

            cli_group = add_migration_commands()
            # Use actual command to trigger config loading, not --help
            result = cli_runner.invoke(cli_group, ["--config", "nonexistent_module.config", "show-current-revision"])

            # Should fail with appropriate error
            assert result.exit_code == 1
            assert "Error loading config" in result.output
            assert "No module named 'nonexistent_module'" in result.output

        finally:
            os.chdir(original_cwd)


def test_external_config_loading_nonexistent_attribute(cli_runner: CliRunner) -> None:
    """Test appropriate error when module exists but attribute doesn't."""
    with tempfile.TemporaryDirectory() as temp_dir:
        temp_path = Path(temp_dir)

        # Create an external config file without the expected attribute
        config_file = temp_path / "bad_config.py"
        config_file.write_text("""
# This module exists but doesn't have a 'missing_attr' attribute
from advanced_alchemy.config import SQLAlchemyAsyncConfig

some_other_var = "not a config"
""")

        # Change to the temp directory
        original_cwd = os.getcwd()
        try:
            os.chdir(temp_dir)

            cli_group = add_migration_commands()
            # Use actual command to trigger config loading, not --help
            result = cli_runner.invoke(cli_group, ["--config", "bad_config.missing_attr", "show-current-revision"])

            # Should fail with appropriate error
            assert result.exit_code == 1
            assert "Error loading config" in result.output
            # The actual error message may vary, but it should indicate the attribute issue

        finally:
            os.chdir(original_cwd)