-
Notifications
You must be signed in to change notification settings - Fork 7.1k
add: constructor injection #186
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
882e663
add: quick and dirty implementation
04190cb
change: improve comments / remove: obsolete single file test invocation
18ea228
add: .pyc ignore for local build / remove: obsolete .pyc file
27e4539
remove: obsolete local test execution file
4cca032
change: string concatenations
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| __pycache__ | ||
| *.pyc |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #!/usr/bin/python | ||
| # -*- coding : utf-8 -*- | ||
| import datetime | ||
|
|
||
| """ | ||
| Port of the Java example of "Constructor Injection" in | ||
| "xUnit Test Patterns - Refactoring Test Code" by Gerard Meszaros | ||
| (ISBN-10: 0131495054, ISBN-13: 978-0131495050) accessible in outdated version on | ||
| http://xunitpatterns.com/Dependency%20Injection.html. | ||
|
|
||
| production code which is untestable: | ||
|
|
||
| class TimeDisplay(object): | ||
|
|
||
| def __init__(self): | ||
| self.time_provider = datetime.datetime | ||
|
|
||
| def get_current_time_as_html_fragment(self): | ||
| current_time = self.time_provider.now() | ||
| current_time_as_html_fragment = "<span class=\"tinyBoldText\">" + current_time.hour + ":" + current_time.minute + "</span>" | ||
| return current_time_as_html_fragment | ||
| """ | ||
|
|
||
| class TimeDisplay(object): | ||
|
|
||
| def __init__(self, time_provider): | ||
| self.time_provider = time_provider | ||
|
|
||
| def get_current_time_as_html_fragment(self): | ||
| current_time = self.time_provider.now() | ||
| current_time_as_html_fragment = "<span class=\"tinyBoldText\">" + current_time + "</span>" | ||
| return current_time_as_html_fragment | ||
|
|
||
| class ProductionCodeTimeProvider(object): | ||
| """ | ||
| Production code version of the time provider (just a wrapper for formatting | ||
| datetime for this example). | ||
| """ | ||
|
|
||
| def now(self): | ||
| current_time = datetime.datetime.now() | ||
| current_time_formatted = str(current_time.hour) + ":" + str(current_time.minute) | ||
| return current_time_formatted | ||
|
|
||
| class MidnightTimeProvider(object): | ||
| """ | ||
| Class implemented as hard-coded stub (in contrast to configurable stub). | ||
| """ | ||
|
|
||
| def now(self): | ||
| current_time_is_always_midnight = "24:01" | ||
| return current_time_is_always_midnight | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| #!/usr/bin/env python | ||
| # -*- coding: utf-8 -*- | ||
| import unittest | ||
|
|
||
| from dft.constructor_injection import TimeDisplay, MidnightTimeProvider, ProductionCodeTimeProvider, datetime | ||
|
|
||
| """ | ||
| Port of the Java example of "Constructor Injection" in | ||
| "xUnit Test Patterns - Refactoring Test Code" by Gerard Meszaros | ||
| (ISBN-10: 0131495054, ISBN-13: 978-0131495050) accessible in outdated version on | ||
| http://xunitpatterns.com/Dependency%20Injection.html. | ||
|
|
||
| Test code which will almost always fail (if not exactly 12:01) when untestable | ||
| production code (have a look into constructor_injection.py) is used: | ||
|
|
||
| def test_display_current_time_at_midnight(self): | ||
| class_under_test = TimeDisplay() | ||
| expected_time = "24:01" | ||
| result = class_under_test.get_current_time_as_as_html_fragment() | ||
| self.assertEqual(result, expected_time) | ||
| """ | ||
|
|
||
| class ConstructorInjectionTest(unittest.TestCase): | ||
|
|
||
| def test_display_current_time_at_midnight(self): | ||
| """ | ||
| Would almost always fail (despite of right at/after midnight) if | ||
| untestable production code would have been used. | ||
| """ | ||
| time_provider_stub = MidnightTimeProvider() | ||
| class_under_test = TimeDisplay(time_provider_stub) | ||
| expected_time = "<span class=\"tinyBoldText\">24:01</span>" | ||
| self.assertEqual(class_under_test.get_current_time_as_html_fragment(), expected_time) | ||
|
|
||
| def test_display_current_time_at_current_time(self): | ||
| """ | ||
| Just as justification for working example with the time provider used in | ||
| production. (Will always pass.) | ||
| """ | ||
| production_code_time_provider = ProductionCodeTimeProvider() | ||
| class_under_test = TimeDisplay(production_code_time_provider) | ||
| current_time = datetime.datetime.now() | ||
| expected_time = "<span class=\"tinyBoldText\">{}:{}</span>".format(current_time.hour, current_time.minute) | ||
| self.assertEqual(class_under_test.get_current_time_as_html_fragment(), expected_time) | ||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to use
.formathere instead of+(true for all string concatenations that you do)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DONE
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks but that's not what I meant. The idea is to avoid string concatenations completely since a) strings are immutable and there is some overhead included, b) it is more idiomatic in Python to use
.format. The idea looks like that:"<span class=\"tinyBoldText\">{}{}</span>".format(current_time.hour, current_time.minute)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't that exactly what I changed in commit 4cca032?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, sorry, I missed that :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is already late 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for reviewing