Skip to content

Commit 14a5003

Browse files
authored
fix: Resolve ruff B904 errors + cleanup unused imports (#35)
fix: Resolve ruff B904 errors + cleanup unused imports
2 parents 441e651 + 65244e8 commit 14a5003

File tree

11 files changed

+148
-108
lines changed

11 files changed

+148
-108
lines changed

tests/test_backend_dynamo.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,25 @@
55
from __future__ import annotations
66

77
import json
8-
import os
98
import time
109
from unittest import mock
1110

1211
import boto3
1312
import pytest
1413
from moto import mock_aws
1514

16-
from zebra_day.backends.dynamo import DynamoBackend, _BACKUP_DEBOUNCE_SECONDS
15+
from zebra_day.backends.dynamo import DynamoBackend
1716
from zebra_day.exceptions import (
1817
ConfigError,
1918
LabelTemplateNotFoundError,
2019
VersionConflictError,
2120
)
2221

23-
2422
# ---------------------------------------------------------------------------
2523
# Fixtures
2624
# ---------------------------------------------------------------------------
2725

26+
2827
@pytest.fixture
2928
def aws_env(monkeypatch):
3029
"""Set fake AWS credentials for moto."""
@@ -60,6 +59,7 @@ def dynamo_backend(aws_env):
6059
# Config CRUD
6160
# ---------------------------------------------------------------------------
6261

62+
6363
class TestLoadConfig:
6464
def test_empty_table_returns_default(self, dynamo_backend):
6565
config = dynamo_backend.load_config()
@@ -119,6 +119,7 @@ def test_config_exists(self, dynamo_backend):
119119
# Template CRUD
120120
# ---------------------------------------------------------------------------
121121

122+
122123
class TestTemplateCRUD:
123124
def test_save_and_get(self, dynamo_backend):
124125
dynamo_backend.save_template("my_label", "^XA^FO10,10^A0N,30,30^FDHello^FS^XZ")
@@ -163,11 +164,11 @@ def test_template_version_conflict(self, dynamo_backend):
163164
dynamo_backend.save_template("tpl", "v2")
164165

165166

166-
167167
# ---------------------------------------------------------------------------
168168
# S3 Backup & Restore
169169
# ---------------------------------------------------------------------------
170170

171+
171172
class TestS3Backup:
172173
def test_backup_creates_manifest(self, dynamo_backend):
173174
dynamo_backend.save_config({"schema_version": "2.1.0", "labs": {"a": {}}})
@@ -230,6 +231,7 @@ def test_restore_round_trip(self, dynamo_backend):
230231
# Resource Tagging
231232
# ---------------------------------------------------------------------------
232233

234+
233235
class TestResourceTagging:
234236
def test_dynamodb_table_tags(self, aws_env):
235237
with mock_aws():
@@ -243,7 +245,9 @@ def test_dynamodb_table_tags(self, aws_env):
243245
backend.create_table()
244246

245247
# Check tags via describe
246-
arn = backend._ddb_client.describe_table(TableName="tag-test-table")["Table"]["TableArn"]
248+
arn = backend._ddb_client.describe_table(TableName="tag-test-table")["Table"][
249+
"TableArn"
250+
]
247251
tags_resp = backend._ddb_client.list_tags_of_resource(ResourceArn=arn)
248252
tags = {t["Key"]: t["Value"] for t in tags_resp.get("Tags", [])}
249253
assert tags["lsmc-cost-center"] == "my-cc"
@@ -293,6 +297,7 @@ def test_tag_from_env(self, aws_env, monkeypatch):
293297
# Factory & Profile Rules
294298
# ---------------------------------------------------------------------------
295299

300+
296301
class TestFromEnv:
297302
def test_from_env_basic(self, aws_env, monkeypatch):
298303
monkeypatch.setenv("ZEBRA_DAY_DYNAMO_TABLE", "my-table")
@@ -341,6 +346,7 @@ def test_no_explicit_default_profile(self, aws_env):
341346
# Status
342347
# ---------------------------------------------------------------------------
343348

349+
344350
class TestStatus:
345351
def test_get_status(self, dynamo_backend):
346352
dynamo_backend.save_config({"schema_version": "2.1.0", "labs": {}})
@@ -355,6 +361,7 @@ def test_get_status(self, dynamo_backend):
355361
# Integration: zpl() with DynamoBackend
356362
# ---------------------------------------------------------------------------
357363

364+
358365
class TestZplIntegration:
359366
def test_zpl_init_with_backend(self, dynamo_backend):
360367
from zebra_day.print_mgr import zpl
@@ -419,6 +426,7 @@ def test_env_var_selection(self, aws_env, monkeypatch):
419426
# AWS Permission Checks
420427
# ---------------------------------------------------------------------------
421428

429+
422430
class TestCheckAWSPermissions:
423431
def test_all_checks_pass_with_resources(self, dynamo_backend):
424432
result = dynamo_backend.check_aws_permissions()

tests/test_cli_dynamo.py

Lines changed: 84 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
from __future__ import annotations
66

77
import json
8-
import os
9-
from pathlib import Path
108

11-
import boto3
129
import pytest
1310
from moto import mock_aws
1411
from typer.testing import CliRunner
@@ -67,10 +64,14 @@ def test_init_success(self, aws_env):
6764
result = runner.invoke(
6865
app,
6966
[
70-
"dynamo", "init",
71-
"--table-name", "test-zebra-config",
72-
"--s3-bucket", "test-backup-bucket",
73-
"--region", "us-east-1",
67+
"dynamo",
68+
"init",
69+
"--table-name",
70+
"test-zebra-config",
71+
"--s3-bucket",
72+
"test-backup-bucket",
73+
"--region",
74+
"us-east-1",
7475
],
7576
)
7677
assert result.exit_code == 0, result.output
@@ -94,10 +95,14 @@ def test_init_with_s3_config_file(self, aws_env, tmp_path):
9495
result = runner.invoke(
9596
app,
9697
[
97-
"dynamo", "init",
98-
"--table-name", "test-zebra-config",
99-
"--region", "us-east-1",
100-
"--s3-config-file", str(cfg),
98+
"dynamo",
99+
"init",
100+
"--table-name",
101+
"test-zebra-config",
102+
"--region",
103+
"us-east-1",
104+
"--s3-config-file",
105+
str(cfg),
101106
],
102107
)
103108
assert result.exit_code == 0, result.output
@@ -110,11 +115,16 @@ def test_init_s3_bucket_flag_overrides_config_file(self, aws_env, tmp_path):
110115
result = runner.invoke(
111116
app,
112117
[
113-
"dynamo", "init",
114-
"--table-name", "test-zebra-config",
115-
"--region", "us-east-1",
116-
"--s3-bucket", "flag-bucket",
117-
"--s3-config-file", str(cfg),
118+
"dynamo",
119+
"init",
120+
"--table-name",
121+
"test-zebra-config",
122+
"--region",
123+
"us-east-1",
124+
"--s3-bucket",
125+
"flag-bucket",
126+
"--s3-config-file",
127+
str(cfg),
118128
],
119129
)
120130
assert result.exit_code == 0, result.output
@@ -127,10 +137,14 @@ def test_init_bad_s3_config_file(self, aws_env, tmp_path):
127137
result = runner.invoke(
128138
app,
129139
[
130-
"dynamo", "init",
131-
"--table-name", "test-zebra-config",
132-
"--region", "us-east-1",
133-
"--s3-config-file", str(cfg),
140+
"dynamo",
141+
"init",
142+
"--table-name",
143+
"test-zebra-config",
144+
"--region",
145+
"us-east-1",
146+
"--s3-config-file",
147+
str(cfg),
134148
],
135149
)
136150
assert result.exit_code == 1
@@ -140,10 +154,14 @@ def test_init_nonexistent_s3_config_file(self, aws_env):
140154
result = runner.invoke(
141155
app,
142156
[
143-
"dynamo", "init",
144-
"--table-name", "test-zebra-config",
145-
"--region", "us-east-1",
146-
"--s3-config-file", "/tmp/no-such-file-12345.json",
157+
"dynamo",
158+
"init",
159+
"--table-name",
160+
"test-zebra-config",
161+
"--region",
162+
"us-east-1",
163+
"--s3-config-file",
164+
"/tmp/no-such-file-12345.json",
147165
],
148166
)
149167
assert result.exit_code == 1
@@ -154,24 +172,35 @@ def test_init_shows_permission_checks(self, aws_env):
154172
result = runner.invoke(
155173
app,
156174
[
157-
"dynamo", "init",
158-
"--table-name", "test-zebra-config",
159-
"--s3-bucket", "test-backup-bucket",
160-
"--region", "us-east-1",
175+
"dynamo",
176+
"init",
177+
"--table-name",
178+
"test-zebra-config",
179+
"--s3-bucket",
180+
"test-backup-bucket",
181+
"--region",
182+
"us-east-1",
161183
],
162184
)
163185
assert result.exit_code == 0, result.output
164-
assert "permission checks passed" in result.output.lower() or "Checking AWS" in result.output
186+
assert (
187+
"permission checks passed" in result.output.lower()
188+
or "Checking AWS" in result.output
189+
)
165190

166191
def test_init_skip_checks(self, aws_env):
167192
with mock_aws():
168193
result = runner.invoke(
169194
app,
170195
[
171-
"dynamo", "init",
172-
"--table-name", "test-zebra-config",
173-
"--s3-bucket", "test-backup-bucket",
174-
"--region", "us-east-1",
196+
"dynamo",
197+
"init",
198+
"--table-name",
199+
"test-zebra-config",
200+
"--s3-bucket",
201+
"test-backup-bucket",
202+
"--region",
203+
"us-east-1",
175204
"--skip-checks",
176205
],
177206
)
@@ -225,9 +254,12 @@ def test_bootstrap_with_config_and_templates(self, aws_env, tmp_path):
225254
result = runner.invoke(
226255
app,
227256
[
228-
"dynamo", "bootstrap",
229-
"--config-file", str(cfg),
230-
"--templates-dir", str(tpl_dir),
257+
"dynamo",
258+
"bootstrap",
259+
"--config-file",
260+
str(cfg),
261+
"--templates-dir",
262+
str(tpl_dir),
231263
"--no-include-package",
232264
],
233265
)
@@ -237,8 +269,6 @@ def test_bootstrap_with_config_and_templates(self, aws_env, tmp_path):
237269
assert "Backup written" in result.output
238270

239271

240-
241-
242272
# ---------------------------------------------------------------------------
243273
# export
244274
# ---------------------------------------------------------------------------
@@ -381,7 +411,6 @@ def test_destroy_success(self, aws_env):
381411
assert "Backups preserved" in result.output
382412

383413

384-
385414
# ---------------------------------------------------------------------------
386415
# Interactive S3 bucket prompt
387416
# ---------------------------------------------------------------------------
@@ -398,9 +427,7 @@ def test_status_prompts_for_bucket_interactively(self, aws_env, monkeypatch):
398427
with mock_aws():
399428
_provision_resources()
400429
# Provide bucket name via CliRunner input
401-
result = runner.invoke(
402-
app, ["dynamo", "status"], input="test-backup-bucket\n"
403-
)
430+
result = runner.invoke(app, ["dynamo", "status"], input="test-backup-bucket\n")
404431
assert result.exit_code == 0, result.output
405432
assert "test-zebra-config" in result.output
406433

@@ -429,9 +456,7 @@ def test_backup_prompts_for_bucket(self, aws_env, monkeypatch):
429456
with mock_aws():
430457
backend = _provision_resources()
431458
backend.save_config({"labs": {}})
432-
result = runner.invoke(
433-
app, ["dynamo", "backup"], input="test-backup-bucket\n"
434-
)
459+
result = runner.invoke(app, ["dynamo", "backup"], input="test-backup-bucket\n")
435460
assert result.exit_code == 0, result.output
436461
assert "Backup written" in result.output
437462

@@ -443,9 +468,12 @@ def test_init_prompts_for_bucket(self, aws_env, monkeypatch):
443468
result = runner.invoke(
444469
app,
445470
[
446-
"dynamo", "init",
447-
"--table-name", "test-zebra-config",
448-
"--region", "us-east-1",
471+
"dynamo",
472+
"init",
473+
"--table-name",
474+
"test-zebra-config",
475+
"--region",
476+
"us-east-1",
449477
],
450478
input="test-backup-bucket\n",
451479
)
@@ -467,6 +495,7 @@ def test_status_create_s3_if_missing(self, aws_env, monkeypatch):
467495
_provision_resources()
468496
# Delete the bucket so it doesn't exist
469497
import boto3
498+
470499
s3 = boto3.client("s3", region_name="us-east-1")
471500
# Empty and delete the bucket
472501
try:
@@ -479,9 +508,7 @@ def test_status_create_s3_if_missing(self, aws_env, monkeypatch):
479508

480509
# Use a new bucket name via env
481510
monkeypatch.setenv("ZEBRA_DAY_S3_BACKUP_BUCKET", "new-auto-bucket")
482-
result = runner.invoke(
483-
app, ["dynamo", "status", "--create-s3-if-missing"]
484-
)
511+
result = runner.invoke(app, ["dynamo", "status", "--create-s3-if-missing"])
485512
assert result.exit_code == 0, result.output
486513
# Bucket should have been created
487514
assert "created" in result.output.lower() or "test-zebra-config" in result.output
@@ -494,9 +521,7 @@ def test_backup_create_s3_if_missing(self, aws_env, monkeypatch):
494521

495522
# Point to a new bucket that doesn't exist
496523
monkeypatch.setenv("ZEBRA_DAY_S3_BACKUP_BUCKET", "auto-backup-bucket")
497-
result = runner.invoke(
498-
app, ["dynamo", "backup", "--create-s3-if-missing"]
499-
)
524+
result = runner.invoke(app, ["dynamo", "backup", "--create-s3-if-missing"])
500525
assert result.exit_code == 0, result.output
501526
assert "Backup written" in result.output
502527

@@ -510,11 +535,13 @@ def test_bootstrap_create_s3_if_missing(self, aws_env, monkeypatch, tmp_path):
510535
result = runner.invoke(
511536
app,
512537
[
513-
"dynamo", "bootstrap",
514-
"--config-file", str(cfg),
538+
"dynamo",
539+
"bootstrap",
540+
"--config-file",
541+
str(cfg),
515542
"--no-include-package",
516543
"--create-s3-if-missing",
517544
],
518545
)
519546
assert result.exit_code == 0, result.output
520-
assert "created" in result.output.lower() or "Config uploaded" in result.output
547+
assert "created" in result.output.lower() or "Config uploaded" in result.output

tests/test_core_functions.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,10 @@ def test_probe_accepts_valid_stub(self):
354354
zp = zdpm.zpl()
355355
# Mock HTTPConnection and HTTPSConnection so the 255-IP loop
356356
# completes instantly without real network calls.
357-
with mock.patch("http.client.HTTPConnection") as mock_http, \
358-
mock.patch("http.client.HTTPSConnection") as mock_https:
357+
with (
358+
mock.patch("http.client.HTTPConnection") as mock_http,
359+
mock.patch("http.client.HTTPSConnection") as mock_https,
360+
):
359361
# Make every connection attempt raise immediately (no printer)
360362
mock_http.return_value.request.side_effect = OSError("mocked")
361363
mock_https.return_value.request.side_effect = OSError("mocked")

0 commit comments

Comments
 (0)