Skip to content

Commit fe3363c

Browse files
committed
refactor: make service batching and tests more pythonic
1 parent 0ffc3ba commit fe3363c

File tree

3 files changed

+19
-40
lines changed

3 files changed

+19
-40
lines changed

src/lazy_ecs/features/service/service.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ def get_service_info(self, cluster_name: str) -> list[ServiceInfo]:
3131
if not service_names:
3232
return []
3333

34-
all_services = []
35-
for batch in batch_items(service_names, 10):
36-
response = self.ecs_client.describe_services(cluster=cluster_name, services=batch)
37-
all_services.extend(response.get("services", []))
34+
all_services = [
35+
service
36+
for batch in batch_items(service_names, 10)
37+
for service in self.ecs_client.describe_services(cluster=cluster_name, services=batch).get("services", [])
38+
]
3839

3940
return [_create_service_info(service) for service in all_services]
4041

tests/test_aws_service.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,8 @@ def test_get_service_info_with_more_than_10_services():
290290
service_info = service.get_service_info("production")
291291

292292
assert len(service_info) == 15
293-
service_names = [info["name"] for info in service_info]
294-
for i in range(15):
295-
assert any(f"service-{i:02d}" in name for name in service_names)
293+
service_names = {info["name"] for info in service_info}
294+
assert all(any(f"service-{i:02d}" in name for name in service_names) for i in range(15))
296295

297296

298297
def test_get_tasks(ecs_client_with_tasks) -> None:

tests/test_utils.py

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,36 @@
44

55

66
def test_batch_items_basic():
7-
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
8-
batches = list(batch_items(items, 3))
7+
batches = list(batch_items([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3))
98

10-
assert len(batches) == 4
11-
assert batches[0] == [1, 2, 3]
12-
assert batches[1] == [4, 5, 6]
13-
assert batches[2] == [7, 8, 9]
14-
assert batches[3] == [10]
9+
assert batches == [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
1510

1611

1712
def test_batch_items_exact_fit():
18-
items = [1, 2, 3, 4, 5, 6]
19-
batches = list(batch_items(items, 3))
13+
batches = list(batch_items([1, 2, 3, 4, 5, 6], 3))
2014

21-
assert len(batches) == 2
22-
assert batches[0] == [1, 2, 3]
23-
assert batches[1] == [4, 5, 6]
15+
assert batches == [[1, 2, 3], [4, 5, 6]]
2416

2517

2618
def test_batch_items_single_batch():
27-
items = [1, 2, 3]
28-
batches = list(batch_items(items, 10))
19+
batches = list(batch_items([1, 2, 3], 10))
2920

30-
assert len(batches) == 1
31-
assert batches[0] == [1, 2, 3]
21+
assert batches == [[1, 2, 3]]
3222

3323

3424
def test_batch_items_empty_list():
35-
items = []
36-
batches = list(batch_items(items, 5))
25+
batches = list(batch_items([], 5))
3726

38-
assert len(batches) == 0
27+
assert batches == []
3928

4029

4130
def test_batch_items_size_one():
42-
items = [1, 2, 3, 4, 5]
43-
batches = list(batch_items(items, 1))
31+
batches = list(batch_items([1, 2, 3, 4, 5], 1))
4432

45-
assert len(batches) == 5
46-
assert batches[0] == [1]
47-
assert batches[1] == [2]
48-
assert batches[2] == [3]
49-
assert batches[3] == [4]
50-
assert batches[4] == [5]
33+
assert batches == [[1], [2], [3], [4], [5]]
5134

5235

5336
def test_batch_items_strings():
54-
items = ["a", "b", "c", "d", "e", "f", "g"]
55-
batches = list(batch_items(items, 3))
37+
batches = list(batch_items(["a", "b", "c", "d", "e", "f", "g"], 3))
5638

57-
assert len(batches) == 3
58-
assert batches[0] == ["a", "b", "c"]
59-
assert batches[1] == ["d", "e", "f"]
60-
assert batches[2] == ["g"]
39+
assert batches == [["a", "b", "c"], ["d", "e", "f"], ["g"]]

0 commit comments

Comments
 (0)