Skip to content

Commit

Permalink
Refactor unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nateprewitt committed Jul 27, 2023
1 parent bd6249a commit 9f6a7bc
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 204 deletions.
9 changes: 5 additions & 4 deletions botocore/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
"""

import gzip
import io
import logging
from gzip import GzipFile
from gzip import compress as gzip_compress

from botocore.compat import urlencode
from botocore.utils import determine_content_length
Expand Down Expand Up @@ -88,9 +89,9 @@ def _get_body_size(body):

def _gzip_compress_body(body):
if isinstance(body, str):
return gzip.compress(body.encode('utf-8'))
return gzip_compress(body.encode('utf-8'))
elif isinstance(body, (bytes, bytearray)):
return gzip.compress(body)
return gzip_compress(body)
elif hasattr(body, 'read'):
if hasattr(body, 'seek') and hasattr(body, 'tell'):
current_position = body.tell()
Expand All @@ -102,7 +103,7 @@ def _gzip_compress_body(body):

def _gzip_compress_fileobj(body):
compressed_obj = io.BytesIO()
with gzip.GzipFile(fileobj=compressed_obj, mode='wb') as gz:
with GzipFile(fileobj=compressed_obj, mode='wb') as gz:
while True:
chunk = body.read(8192)
if not chunk:
Expand Down
24 changes: 24 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,30 @@ def __exit__(self, *args, **kwargs):
self.datetime_patcher.stop()


class PatchObject(ContextDecorator):
"""
Context manager for patching out objects in specific classes for tests.
:type module: module
:param module: reference to imported module to patch
:type obj: str
:param obj: object within the module to replace.
:type new: Any
:param new: Any functional replacement for object to be patched.
"""

def __init__(self, module, obj, new):
self.obj_patcher = mock.patch.object(module, obj, new)

def __enter__(self, *args, **kwargs):
self.obj_patcher.start()

def __exit__(self, *args, **kwargs):
self.obj_patcher.stop()


def patch_load_service_model(
session, monkeypatch, service_model_json, ruleset_json
):
Expand Down
Loading

0 comments on commit 9f6a7bc

Please sign in to comment.