Skip to content

Commit 683fd43

Browse files
Resolve linting errors from ruff
1 parent 6a080a2 commit 683fd43

File tree

9 files changed

+139
-148
lines changed

9 files changed

+139
-148
lines changed

tests/conftest.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
import os
2-
31
import pytest
42
import vcr
53
from click.testing import CliRunner
64

75
from tim.opensearch import configure_opensearch_client
86

7+
EXIT_CODES = {
8+
"success": 0,
9+
"error": 1,
10+
"invalid_command": 2,
11+
}
912
my_vcr = vcr.VCR(
1013
cassette_library_dir="tests/fixtures/cassettes",
1114
filter_headers=["authorization"],
1215
)
1316

1417

1518
@pytest.fixture(autouse=True)
16-
def test_env():
17-
os.environ = {
18-
"AWS_ACCESS_KEY_ID": "test",
19-
"AWS_SECRET_ACCESS_KEY": "test",
20-
"AWS_SESSION_TOKEN": "test",
21-
"TIMDEX_OPENSEARCH_ENDPOINT": "localhost",
22-
"SENTRY_DSN": None,
23-
"WORKSPACE": "test",
24-
}
25-
yield
26-
27-
28-
@pytest.fixture()
19+
def _test_env(monkeypatch):
20+
monkeypatch.setenv("SENTRY_DSN", "None")
21+
monkeypatch.setenv("WORKSPACE", "test")
22+
monkeypatch.setenv("AWS_ACCESS_KEY_ID", "test")
23+
monkeypatch.setenv("AWS_SECRET_ACCESS_KEY", "test")
24+
monkeypatch.setenv("AWS_SESSION_TOKEN", "test")
25+
monkeypatch.setenv("TIMDEX_OPENSEARCH_ENDPOINT", "localhost")
26+
27+
28+
@pytest.fixture
2929
def test_opensearch_client():
3030
return configure_opensearch_client("localhost")
3131

3232

33-
@pytest.fixture()
33+
@pytest.fixture
3434
def runner():
3535
return CliRunner()

tests/test_cli.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from tim.cli import main
66

7-
from .conftest import my_vcr
7+
from .conftest import EXIT_CODES, my_vcr
88

99

1010
def escape_ansi(line):
@@ -19,7 +19,7 @@ def test_main_group_no_options_configures_correctly_and_invokes_result_callback(
1919
):
2020
monkeypatch.delenv("TIMDEX_OPENSEARCH_ENDPOINT", raising=False)
2121
result = runner.invoke(main, ["ping"])
22-
assert result.exit_code == 0
22+
assert result.exit_code == EXIT_CODES["success"]
2323
assert "Logger 'root' configured with level=INFO" in caplog.text
2424
assert "OpenSearch client configured for endpoint 'localhost'" in caplog.text
2525
assert "Total time to complete process" in caplog.text
@@ -31,7 +31,7 @@ def test_main_group_all_options_configures_correctly_and_invokes_result_callback
3131
):
3232
monkeypatch.delenv("TIMDEX_OPENSEARCH_ENDPOINT", raising=False)
3333
result = runner.invoke(main, ["--verbose", "--url", "localhost", "ping"])
34-
assert result.exit_code == 0
34+
assert result.exit_code == EXIT_CODES["success"]
3535
assert "Logger 'root' configured with level=DEBUG" in caplog.text
3636
assert "OpenSearch client configured for endpoint 'localhost'" in caplog.text
3737
assert "Total time to complete process" in caplog.text
@@ -42,7 +42,7 @@ def test_main_group_options_from_env_configures_correctly_and_invokes_result_cal
4242
caplog, runner
4343
):
4444
result = runner.invoke(main, ["ping"])
45-
assert result.exit_code == 0
45+
assert result.exit_code == EXIT_CODES["success"]
4646
assert "Logger 'root' configured with level=INFO" in caplog.text
4747
assert "OpenSearch client configured for endpoint 'localhost'" in caplog.text
4848
assert "Total time to complete process" in caplog.text
@@ -51,27 +51,27 @@ def test_main_group_options_from_env_configures_correctly_and_invokes_result_cal
5151
@my_vcr.use_cassette("get_aliases.yaml")
5252
def test_aliases(runner):
5353
result = runner.invoke(main, ["aliases"])
54-
assert result.exit_code == 0
54+
assert result.exit_code == EXIT_CODES["success"]
5555
assert "Alias: alias-with-multiple-indexes" in result.stdout
5656

5757

5858
@my_vcr.use_cassette("get_indexes.yaml")
5959
def test_indexes(runner):
6060
result = runner.invoke(main, ["indexes"])
61-
assert result.exit_code == 0
61+
assert result.exit_code == EXIT_CODES["success"]
6262
assert "Name: index-with-multiple-aliases" in result.stdout
6363

6464

6565
@my_vcr.use_cassette("ping_localhost.yaml")
6666
def test_ping(runner):
6767
result = runner.invoke(main, ["ping"])
68-
assert result.exit_code == 0
68+
assert result.exit_code == EXIT_CODES["success"]
6969
assert "Name: docker-cluster" in result.stdout
7070

7171

7272
def test_create_index_neither_name_nor_source_passed(runner):
7373
result = runner.invoke(main, ["create"])
74-
assert result.exit_code == 2
74+
assert result.exit_code == EXIT_CODES["invalid_command"]
7575
assert "Must provide either a name or source for the new index." in result.stdout
7676

7777

@@ -80,7 +80,7 @@ def test_create_index_name_and_source_passed(runner):
8080
main,
8181
["create", "--index", "aspace-2022-09-01t12-34-56", "--source", "aspace"],
8282
)
83-
assert result.exit_code == 2
83+
assert result.exit_code == EXIT_CODES["invalid_command"]
8484
assert (
8585
"Only one of --index and --source options is allowed, not both."
8686
in escape_ansi(result.stdout)
@@ -89,18 +89,18 @@ def test_create_index_name_and_source_passed(runner):
8989

9090
def test_create_index_invalid_name_passed(runner):
9191
result = runner.invoke(main, ["create", "--index", "wrong"])
92-
assert result.exit_code == 2
92+
assert result.exit_code == EXIT_CODES["invalid_command"]
9393

9494

9595
def test_create_index_invalid_source_passed(runner):
9696
result = runner.invoke(main, ["create", "--source", "wrong"])
97-
assert result.exit_code == 2
97+
assert result.exit_code == EXIT_CODES["invalid_command"]
9898

9999

100100
@my_vcr.use_cassette("cli/create_index_exists.yaml")
101101
def test_create_index_exists(caplog, runner):
102102
result = runner.invoke(main, ["create", "--index", "aspace-2022-09-20t15-59-38"])
103-
assert result.exit_code == 1
103+
assert result.exit_code == EXIT_CODES["error"]
104104
assert (
105105
"tim.cli",
106106
40,
@@ -113,37 +113,37 @@ def test_create_index_exists(caplog, runner):
113113
@my_vcr.use_cassette("cli/create_index_success.yaml")
114114
def test_create_index_success(caplog, runner):
115115
result = runner.invoke(main, ["create", "--source", "aspace"])
116-
assert result.exit_code == 0
116+
assert result.exit_code == EXIT_CODES["success"]
117117
assert "Index 'aspace-2022-09-01t00-00-00' created." in caplog.text
118118

119119

120120
@my_vcr.use_cassette("delete_success.yaml")
121121
def test_delete_index_with_force(runner):
122122
result = runner.invoke(main, ["delete", "-i", "test-index", "-f"])
123-
assert result.exit_code == 0
123+
assert result.exit_code == EXIT_CODES["success"]
124124
assert "Index 'test-index' deleted." in result.stdout
125125

126126

127127
@my_vcr.use_cassette("delete_success.yaml")
128128
def test_delete_index_with_confirmation(monkeypatch, runner):
129129
monkeypatch.setattr("builtins.input", lambda _: "y")
130130
result = runner.invoke(main, ["delete", "-i", "test-index"])
131-
assert result.exit_code == 0
131+
assert result.exit_code == EXIT_CODES["success"]
132132
assert "Index 'test-index' deleted." in result.stdout
133133

134134

135135
@my_vcr.use_cassette("delete_without_confirmation.yaml")
136136
def test_delete_index_without_confirmation(monkeypatch, runner):
137137
monkeypatch.setattr("builtins.input", lambda _: "n")
138138
result = runner.invoke(main, ["delete", "-i", "test-index"])
139-
assert result.exit_code == 1
139+
assert result.exit_code == EXIT_CODES["error"]
140140
assert "Ok, index will not be deleted." in result.stdout
141141

142142

143143
@my_vcr.use_cassette("demote_no_aliases_for_index.yaml")
144144
def test_demote_index_no_aliases_for_index(runner):
145145
result = runner.invoke(main, ["demote", "-i", "test-index"])
146-
assert result.exit_code == 1
146+
assert result.exit_code == EXIT_CODES["error"]
147147
assert (
148148
"Index 'test-index' has no aliases, please check aliases and try again."
149149
in result.stdout
@@ -154,29 +154,29 @@ def test_demote_index_no_aliases_for_index(runner):
154154
def test_demote_index_from_primary_alias_with_confirmation(monkeypatch, runner):
155155
monkeypatch.setattr("builtins.input", lambda _: "y")
156156
result = runner.invoke(main, ["demote", "-i", "test-index"])
157-
assert result.exit_code == 0
157+
assert result.exit_code == EXIT_CODES["success"]
158158
assert "Index 'test-index' demoted from aliases: ['all-current']" in result.stdout
159159

160160

161161
@my_vcr.use_cassette("demote_from_primary_alias_without_confirmation.yaml")
162162
def test_demote_index_from_primary_alias_without_confirmation(monkeypatch, runner):
163163
monkeypatch.setattr("builtins.input", lambda _: "n")
164164
result = runner.invoke(main, ["demote", "-i", "test-index"])
165-
assert result.exit_code == 1
165+
assert result.exit_code == EXIT_CODES["error"]
166166
assert "Ok, index will not be demoted." in result.stdout
167167

168168

169169
@my_vcr.use_cassette("demote_no_primary_alias.yaml")
170170
def test_demote_index_no_primary_alias(runner):
171171
result = runner.invoke(main, ["demote", "-i", "test-index"])
172-
assert result.exit_code == 0
172+
assert result.exit_code == EXIT_CODES["success"]
173173
assert "Index 'test-index' demoted from aliases: ['not-primary']" in result.stdout
174174

175175

176176
@my_vcr.use_cassette("promote_index.yaml")
177177
def test_promote_index(caplog, runner):
178178
result = runner.invoke(main, ["promote", "-i", "testsource-index"])
179-
assert result.exit_code == 0
179+
assert result.exit_code == EXIT_CODES["success"]
180180
assert "Index promoted" in caplog.text
181181

182182

@@ -195,7 +195,7 @@ def test_bulk_index_with_index_name_success(caplog, runner):
195195
"tests/fixtures/sample_records.json",
196196
],
197197
)
198-
assert result.exit_code == 0
198+
assert result.exit_code == EXIT_CODES["success"]
199199
assert (
200200
"Bulk indexing records from file 'tests/fixtures/sample_records.json' into "
201201
"index 'dspace-2022-09-01t00-00-00'" in caplog.text
@@ -210,7 +210,7 @@ def test_bulk_index_with_source_success(caplog, runner):
210210
main,
211211
["bulk-index", "--source", "dspace", "tests/fixtures/sample_records.json"],
212212
)
213-
assert result.exit_code == 0
213+
assert result.exit_code == EXIT_CODES["success"]
214214
assert (
215215
"Bulk indexing records from file 'tests/fixtures/sample_records.json' into "
216216
"index 'dspace-2022-09-01t00-00-00'" in caplog.text
@@ -230,7 +230,7 @@ def test_bulk_delete_with_index_name_success(caplog, runner):
230230
"tests/fixtures/sample_deleted_records.txt",
231231
],
232232
)
233-
assert result.exit_code == 0
233+
assert result.exit_code == EXIT_CODES["success"]
234234
assert (
235235
"Bulk deleting records in file 'tests/fixtures/sample_deleted_records.txt' "
236236
"from index 'alma-2022-09-01t00-00-00'" in caplog.text
@@ -250,7 +250,7 @@ def test_bulk_delete_with_source_success(caplog, runner):
250250
"tests/fixtures/sample_deleted_records.txt",
251251
],
252252
)
253-
assert result.exit_code == 0
253+
assert result.exit_code == EXIT_CODES["success"]
254254
assert (
255255
"Bulk deleting records in file 'tests/fixtures/sample_deleted_records.txt' "
256256
"from index 'alma-2022-09-01t00-00-00'" in caplog.text

tests/test_config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ def test_configure_index_settings():
1818
def test_configure_logger_not_verbose():
1919
logger = logging.getLogger(__name__)
2020
result = configure_logger(logger, verbose=False)
21-
assert logger.getEffectiveLevel() == 20
21+
22+
assert logger.getEffectiveLevel() == logging.INFO
2223
assert result == "Logger 'tests.test_config' configured with level=INFO"
2324

2425

2526
def test_configure_logger_verbose():
2627
logger = logging.getLogger(__name__)
2728
result = configure_logger(logger, verbose=True)
28-
assert logger.getEffectiveLevel() == 10
29+
assert logger.getEffectiveLevel() == logging.DEBUG
2930
assert result == "Logger 'tests.test_config' configured with level=DEBUG"
3031

3132

tests/test_helpers.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99

1010
def test_confirm_action_yes(monkeypatch):
1111
monkeypatch.setattr("builtins.input", lambda _: "Y")
12-
assert helpers.confirm_action("test-index", "delete") is True
12+
assert helpers.confirm_action("delete test-index") is True
1313

1414

1515
def test_confirm_action_no(monkeypatch):
1616
monkeypatch.setattr("builtins.input", lambda _: "n")
17-
assert helpers.confirm_action("test-index", "delete") is False
17+
assert helpers.confirm_action("delete test-index") is False
1818

1919

2020
def test_confirm_action_invalid(capsys, monkeypatch):
2121
inputs = iter(["wrong", "y"])
2222
monkeypatch.setattr("builtins.input", lambda _: next(inputs))
23-
assert helpers.confirm_action("test-index", "delete") is True
23+
assert helpers.confirm_action("delete test-index") is True
2424
out, _ = capsys.readouterr()
2525
assert out == "Invalid input: 'wrong', must be one of 'y' or 'n'.\n"
2626

@@ -54,9 +54,8 @@ def test_generate_bulk_actions_delete():
5454
def test_generate_bulk_actions_invalid_action_raises_error():
5555
records = [{"timdex_record_id": "12345", "other_fields": "some_data"}]
5656
actions = helpers.generate_bulk_actions("test-index", records, "wrong")
57-
with pytest.raises(ValueError) as error:
57+
with pytest.raises(ValueError, match="Invalid action parameter"):
5858
next(actions)
59-
assert "Invalid action parameter" in str(error.value)
6059

6160

6261
def test_get_source_from_index():
@@ -69,53 +68,56 @@ def test_get_source_from_index_without_dash():
6968

7069
def test_parse_records():
7170
records = list(helpers.parse_records("tests/fixtures/sample_records.json"))
72-
assert len(records) == 6
71+
n_sample_records = 6
72+
assert len(records) == n_sample_records
7373
assert isinstance(records[0], dict)
7474

7575

7676
def test_parse_deleted_records():
7777
records = list(
7878
helpers.parse_deleted_records("tests/fixtures/sample_deleted_records.txt")
7979
)
80-
assert len(records) == 3
80+
n_sample_deleted_records = 3
81+
assert len(records) == n_sample_deleted_records
8182
assert isinstance(records[0], dict)
8283

8384

8485
def test_validate_bulk_cli_options_neither_index_nor_source_passed(
8586
test_opensearch_client,
8687
):
87-
with pytest.raises(UsageError) as error:
88+
with pytest.raises(
89+
UsageError, match="Must provide either an existing index name or a valid source."
90+
):
8891
helpers.validate_bulk_cli_options(None, None, test_opensearch_client)
89-
assert "Must provide either an existing index name or a valid source." == str(
90-
error.value
91-
)
9292

9393

9494
def test_validate_bulk_cli_options_index_and_source_passed(test_opensearch_client):
95-
with pytest.raises(UsageError) as error:
95+
with pytest.raises(
96+
UsageError, match="Only one of --index and --source options is allowed, not both."
97+
):
9698
helpers.validate_bulk_cli_options(
9799
"index-name", "source-name", test_opensearch_client
98100
)
99-
assert "Only one of --index and --source options is allowed, not both." == str(
100-
error.value
101-
)
102101

103102

104103
@my_vcr.use_cassette("helpers/bulk_cli_nonexistent_index.yaml")
105104
def test_validate_bulk_cli_options_nonexistent_index_passed(test_opensearch_client):
106-
with pytest.raises(BadParameter) as error:
105+
with pytest.raises(
106+
BadParameter, match="Index 'wrong' does not exist in the cluster."
107+
):
107108
helpers.validate_bulk_cli_options("wrong", None, test_opensearch_client)
108-
assert "Index 'wrong' does not exist in the cluster." == str(error.value)
109109

110110

111111
@my_vcr.use_cassette("helpers/bulk_cli_no_primary_index_for_source.yaml")
112112
def test_validate_bulk_cli_options_no_primary_index_for_source(test_opensearch_client):
113-
with pytest.raises(BadParameter) as error:
113+
with pytest.raises(
114+
BadParameter,
115+
match=(
116+
"No index name was passed and there is no "
117+
"primary-aliased index for source 'dspace'."
118+
),
119+
):
114120
helpers.validate_bulk_cli_options(None, "dspace", test_opensearch_client)
115-
assert (
116-
"No index name was passed and there is no primary-aliased index for source "
117-
"'dspace'." == str(error.value)
118-
)
119121

120122

121123
def test_validate_index_name_no_value():

0 commit comments

Comments
 (0)