generated from softwareone-platform/swo-extension-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
143 lines (95 loc) · 5.14 KB
/
test_cli.py
File metadata and controls
143 lines (95 loc) · 5.14 KB
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
import pytest
from freezegun import freeze_time
from mpt_tool.cli import app
def test_help(runner):
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0, result.output
assert "MPT CLI - Migration tool for extensions." in result.output
assert "migrate" in result.output
def test_migrate_command(runner):
result = runner.invoke(app, ["migrate"])
assert result.exit_code == 2, result.output
assert "Invalid value for migrate:" in result.output
assert "At least one param must be used." in result.output
def test_migrate_command_multiple_params_error(runner):
result = runner.invoke(app, ["migrate", "--new-data", "bla", "--new-schema", "foo"])
assert result.exit_code == 2, result.output
assert "Invalid value for migrate:" in result.output
assert "Only one param can be used." in result.output
def test_migrate_migration_id_without_type_error(runner):
result = runner.invoke(app, ["migrate", "fake_data_file_name"])
assert result.exit_code == 2, result.output
assert "Invalid value for migrate:" in result.output
assert "MIGRATION_ID can only be used with --data or" in result.output
def test_migrate_data_duplicate_migration(runner, migration_folder):
(migration_folder / "20250406020202_fake_file_name.py").touch()
(migration_folder / "20260107010101_fake_file_name.py").touch()
result = runner.invoke(app, ["migrate", "--data"])
assert result.exit_code == 1, result.output
assert "Duplicate migration filename found: fake_file_name" in result.output
def test_migrate_data_migration_folder_not_found(runner):
result = runner.invoke(app, ["migrate", "--data"])
assert result.exit_code == 1, result.output
assert "Migration folder not found:" in result.output
def test_migrate_manual_folder_not_found(runner):
result = runner.invoke(app, ["migrate", "--manual", "not_existing_migration"])
assert result.exit_code == 1, result.output
assert "Error running manual command: Migration folder not found: migrations" in result.output
@pytest.mark.usefixtures("data_migration_file")
def test_migrate_manual_migration_not_found(runner):
result = runner.invoke(app, ["migrate", "--manual", "not_existing_migration"])
assert result.exit_code == 1, result.output
assert (
"Error running manual command: Migration not_existing_migration not found" in result.output
)
@freeze_time("2025-04-06 12:21:34")
@pytest.mark.parametrize(
("migration_type", "expected_command"),
[
("--new-data", "DataBaseMigration"),
("--new-schema", "SchemaBaseMigration"),
],
)
def test_migrate_command_new(migration_type, expected_command, migration_folder, runner):
migration_filename = "fake_file_name"
result = runner.invoke(app, ["migrate", migration_type, migration_filename])
assert result.exit_code == 0, result.output
assert f"Scaffolding migration: {migration_filename}." in result.output
new_migration_filename = f"20250406122134_{migration_filename}.py"
assert f"Migration file: {new_migration_filename} has been created." in result.output
new_migration_file = migration_folder / new_migration_filename
assert new_migration_file.exists()
assert f"class Migration({expected_command})" in new_migration_file.read_text()
@freeze_time("2025-04-06 12:21:34")
def test_migrate_command_file_already_exists(migration_folder, runner):
migration_filename = "fake_file_name"
(migration_folder / f"20250406122134_{migration_filename}.py").touch()
result = runner.invoke(app, ["migrate", "--new-data", migration_filename])
assert result.exit_code == 1, result.output
assert f"File already exists: 20250406122134_{migration_filename}" in result.output
assert result.stderr == "Aborted.\n"
def test_migrate_list_no_migrations(runner):
result = runner.invoke(app, ["migrate", "--list"])
assert result.exit_code == 0, result.output
assert "No migrations found." in result.output
def test_migrate_check_no_duplicates(runner, migration_folder):
(migration_folder / "20260101010101_first.py").touch()
(migration_folder / "20260102020202_second.py").touch()
result = runner.invoke(app, ["migrate", "--check"])
assert result.exit_code == 0, result.output
assert "Checking migrations..." in result.output
assert "Migrations check passed successfully." in result.output
def test_migrate_check_with_duplicate_id(runner, migration_folder):
(migration_folder / "20260101010101_duplicate_name.py").touch()
(migration_folder / "20260102020202_duplicate_name.py").touch()
result = runner.invoke(app, ["migrate", "--check"])
assert result.exit_code == 1, result.output
assert "Duplicate migration_id found in migrations" in result.output
assert "20260101010101_duplicate_name.py" in result.output
assert "20260102020202_duplicate_name.py" in result.output
@pytest.mark.usefixtures("migration_folder")
def test_migrate_check_empty_folder(runner):
result = runner.invoke(app, ["migrate", "--check"])
assert result.exit_code == 0, result.output
assert "Checking migrations..." in result.output
assert "Migrations check passed successfully." in result.output