Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DI & Upgrade Pytest #4

Merged
merged 1 commit into from
Aug 27, 2022
Merged

Use DI & Upgrade Pytest #4

merged 1 commit into from
Aug 27, 2022

Conversation

gsweene2
Copy link
Owner

Copy link
Owner Author

@gsweene2 gsweene2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context

@@ -11,7 +11,7 @@ mock = "^4.0.3"
pre-commit = "^2.17.0"

[tool.poetry.dev-dependencies]
pytest = "^5.2"
pytest = "^6.2.5"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class S3Util:
def __init__(self):
self.s3_client = boto3.client("s3")
def __init__(self, s3_client):
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DI: Inject dependencies in constructor for testability



@patch("python_poetry_template.s3_util_class.boto3")
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer have to patch. Can create the mock directly and pass it in when instantiating the class.

mock_boto3.client.return_value = client_mock
# Mock the s3_client with a return value on chained method calls
mock_s3_client = MagicMock()
mock_s3_client.get_paginator().paginate().build_full_result.return_value = {"Contents": []}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplifying the chaining call instead of creating 3 different mocks & their individual responses.

@gsweene2 gsweene2 merged commit 7844050 into master Aug 27, 2022
@gsweene2 gsweene2 deleted the use-di branch August 27, 2022 02:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant