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_airtable_storage.py
More file actions
199 lines (163 loc) · 7.23 KB
/
test_cli_airtable_storage.py
File metadata and controls
199 lines (163 loc) · 7.23 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
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
import pytest
from freezegun import freeze_time
from pyairtable import Api
from pyairtable.testing import MockAirtable
from requests import HTTPError
from mpt_tool.cli import app
@pytest.fixture(autouse=True)
def set_airtable_env_vars(monkeypatch):
monkeypatch.setenv("MPT_TOOL_STORAGE_TYPE", "airtable")
monkeypatch.setenv("MPT_TOOL_STORAGE_AIRTABLE_API_KEY", "fake_api_key")
monkeypatch.setenv("MPT_TOOL_STORAGE_AIRTABLE_BASE_ID", "fake_base_id")
monkeypatch.setenv("MPT_TOOL_STORAGE_AIRTABLE_TABLE_NAME", "fake_table_name")
@pytest.fixture
def mock_airtable():
with MockAirtable() as mock:
yield mock
@pytest.fixture
def applied_migration(data_migration_file, mock_airtable):
mock_airtable.add_records(
"fake_base_id",
"fake_table_name",
[
{
"migration_id": "fake_data_file_name",
"order_id": 20250406020202,
"type": "data",
"started_at": "2025-04-06T13:00:00+00:00",
"applied_at": "2025-04-06T13:00:00+00:00",
"version": None,
}
],
)
return data_migration_file
@freeze_time("2025-04-06 13:00:10")
@pytest.mark.usefixtures("data_migration_file", "schema_migration_file")
def test_migrate_data_migration(mock_airtable, runner, log):
result = runner.invoke(app, ["migrate", "--data"])
assert result.exit_code == 0, result.output
records = mock_airtable.records.get(("fake_base_id", "fake_table_name"))
record_key = next(iter(records.keys()))
assert records.get(record_key)["fields"] == {
"migration_id": "fake_data_file_name",
"order_id": 20250406020202,
"type": "data",
"started_at": "2025-04-06T13:00:10.000Z",
"applied_at": "2025-04-06T13:00:10.000Z",
"version": "",
}
assert "Running data migrations..." in result.output
assert "Running migration: fake_data_file_name" in log.text
assert "Migrations completed successfully." in result.output
@freeze_time("2025-04-06 13:00:00")
@pytest.mark.usefixtures("applied_migration", "mock_airtable")
def test_migrate_skip_migration_already_applied(runner, log):
result = runner.invoke(app, ["migrate", "--data"])
assert result.exit_code == 0, result.output
assert "Running data migrations..." in result.output
assert "Skipping applied migration: fake_data_file_name" in log.text
assert "Migrations completed successfully." in result.output
@freeze_time("2025-04-06 13:00:00")
@pytest.mark.usefixtures("data_migration_file_error")
def test_migrate_data_run_script_fail(monkeypatch, mock_airtable, runner, log):
monkeypatch.setenv("SERVICE_VERSION", "5.1.2")
result = runner.invoke(app, ["migrate", "--data"])
assert result.exit_code == 1, result.output
records = mock_airtable.records.get(("fake_base_id", "fake_table_name"))
record_key = next(iter(records.keys()))
assert records.get(record_key)["fields"] == {
"migration_id": "fake_error_file_name",
"order_id": 20250406020202,
"type": "data",
"started_at": None,
"applied_at": None,
"version": None,
}
assert "Running data migrations..." in result.output
assert "Running migration: fake_error_file_name" in log.text
assert "Migration fake_error_file_name failed: Fake Error" in result.output
@freeze_time("2025-04-06 10:11:24")
@pytest.mark.usefixtures("data_migration_file")
def test_migrate_manual(monkeypatch, mock_airtable, runner):
monkeypatch.setenv("SERVICE_VERSION", "5.2.3")
result = runner.invoke(app, ["migrate", "--manual", "fake_data_file_name"])
assert result.exit_code == 0, result.output
assert "Running migration fake_data_file_name in manual mode." in result.output
assert "Migration fake_data_file_name applied successfully." in result.output
records = mock_airtable.records.get(("fake_base_id", "fake_table_name"))
record_key = next(iter(records.keys()))
assert records.get(record_key)["fields"] == {
"migration_id": "fake_data_file_name",
"order_id": 20250406020202,
"type": "data",
"started_at": None,
"applied_at": "2025-04-06T10:11:24.000Z",
"version": "5.2.3",
}
@pytest.mark.usefixtures("applied_migration", "mock_airtable")
def test_migrate_manual_migration_already_applied(runner):
result = runner.invoke(app, ["migrate", "--manual", "fake_data_file_name"])
assert result.exit_code == 1, result.output
assert (
"Error running manual command: Migration fake_data_file_name already applied"
in result.output
)
def test_migrate_init(mocker, runner):
mock_base = mocker.Mock(create_table=mocker.Mock(), spec=["create_table"])
mocker.patch.object(Api, "base", return_value=mock_base, autospec=True)
result = runner.invoke(app, ["migrate", "--init"])
assert result.exit_code == 0, result.output
assert "Initializing migration tool..." in result.output
assert "Migration tool initialized successfully." in result.output
mock_base.create_table.assert_called_once_with(
"fake_table_name",
fields=[
{"name": "migration_id", "type": "singleLineText"},
{"name": "order_id", "type": "number", "options": {"precision": 0}},
{
"name": "type",
"type": "singleSelect",
"options": {"choices": [{"name": "data"}, {"name": "schema"}]},
},
{
"name": "started_at",
"type": "dateTime",
"options": {
"dateFormat": {"name": "iso"},
"timeFormat": {"name": "24hour"},
"timeZone": "utc",
},
},
{
"name": "applied_at",
"type": "dateTime",
"options": {
"dateFormat": {"name": "iso"},
"timeFormat": {"name": "24hour"},
"timeZone": "utc",
},
},
{"name": "version", "type": "singleLineText"},
],
)
def test_migrate_init_table_already_exists(mocker, runner):
mock_base = mocker.Mock(
create_table=mocker.Mock(side_effect=HTTPError("Error")), spec=["create_table"]
)
mocker.patch.object(Api, "base", return_value=mock_base, autospec=True)
result = runner.invoke(app, ["migrate", "--init"])
assert result.exit_code == 1, result.output
assert "Initializing migration tool..." in result.output
assert "Error running init command:" in result.output
@pytest.mark.usefixtures("applied_migration", "schema_migration_file")
def test_migrate_list(runner, log):
result = runner.invoke(app, ["migrate", "--list"])
assert result.exit_code == 0, result.output
assert "No state found for migration: fake_schema_file_name" in log.text
formatted_output = "".join(result.output.split())
assert "┃order_id┃migration_id┃started_at┃applied_at┃type┃status┃version┃" in formatted_output
assert (
"│20250406020202│fake_data_file_name│2025-04-06T13:00:00+00:00│2025-04-06T13:00:00+00:00│data│Applied│-│"
in formatted_output
)
assert "│20260101010101│fake_schema_file_name│-│-│-│NotApplied│-│" in formatted_output