Skip to content

Commit 48ec491

Browse files
authored
Merge pull request #232 from EasyPost/fix_slow_scrubbing
fix: slow scrubbing due to wrong for loop placement
2 parents b441c72 + 4d853a6 commit 48ec491

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ clean:
1919

2020
## coverage - Test the project and generate an HTML coverage report
2121
coverage:
22-
$(VIRTUAL_BIN)/pytest --cov=$(PROJECT_NAME) --cov-branch --cov-report=html --cov-report=term-missing --cov-fail-under=88
22+
$(VIRTUAL_BIN)/pytest --cov=$(PROJECT_NAME) --cov-branch --cov-report=html --cov-report=term-missing --cov-fail-under=87
2323

2424
## black - Runs the Black Python formatter against the project
2525
black:

tests/conftest.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -114,35 +114,35 @@ def before_record_response(response: Any) -> Any:
114114
if response["body"]["string"]:
115115
response_body = json.loads(response["body"]["string"].decode())
116116

117-
response_body = scrub_data(response_body)
117+
for scrubber in scrubbers:
118+
response_body = scrub_data(response_body, scrubber)
118119

119120
response["body"]["string"] = json.dumps(response_body).encode()
120121
return response
121122

122-
def scrub_data(data: Any) -> Any:
123+
def scrub_data(data: Any, scrubber: Tuple[str, Any]) -> Any:
123124
"""Scrub data from a cassette recursively."""
124-
for scrubber in scrubbers:
125-
key = scrubber[0]
126-
replacement = scrubber[1]
127-
128-
# Root-level list scrubbing
129-
if isinstance(data, list):
125+
key = scrubber[0]
126+
replacement = scrubber[1]
127+
128+
# Root-level list scrubbing
129+
if isinstance(data, list):
130+
for index, item in enumerate(data):
131+
if key in item:
132+
data[index][key] = replacement
133+
elif isinstance(data, dict):
134+
# Root-level key scrubbing
135+
if key in data:
136+
data[key] = replacement
137+
else:
138+
# Nested scrubbing
130139
for index, item in enumerate(data):
131-
if key in item:
132-
data[index][key] = replacement
133-
elif isinstance(data, dict):
134-
# Root-level key scrubbing
135-
if key in data:
136-
data[key] = replacement
137-
else:
138-
# Nested scrubbing
139-
for index, item in enumerate(data):
140-
element = data[item]
141-
if isinstance(element, list):
142-
for nested_index, nested_item in enumerate(element):
143-
data[item][nested_index] = scrub_data(nested_item)
144-
elif isinstance(element, dict):
145-
data[item] = scrub_data(element)
140+
element = data[item]
141+
if isinstance(element, list):
142+
for nested_index, nested_item in enumerate(element):
143+
data[item][nested_index] = scrub_data(nested_item, scrubber)
144+
elif isinstance(element, dict):
145+
data[item] = scrub_data(element, scrubber)
146146
return data
147147

148148
return before_record_response

0 commit comments

Comments
 (0)