-
Notifications
You must be signed in to change notification settings - Fork 6
Improve test coverage #102
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
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
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
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,39 @@ | ||
| import unittest | ||
| import sys | ||
|
|
||
|
|
||
| class TestMD5(unittest.TestCase): | ||
| def setUp(self): | ||
| # Store original implementation name | ||
| self.original_implementation = sys.implementation.name | ||
| # Clear the module from sys.modules to force reload | ||
| if 'notecard.md5' in sys.modules: | ||
| del sys.modules['notecard.md5'] | ||
|
|
||
| def tearDown(self): | ||
| # Restore original implementation | ||
| sys.implementation.name = self.original_implementation | ||
| # Clear the module again | ||
| if 'notecard.md5' in sys.modules: | ||
| del sys.modules['notecard.md5'] | ||
|
|
||
| def test_non_cpython_implementation(self): | ||
| """Test our custom MD5 implementation used in non-CPython environments""" | ||
| # Set implementation to non-cpython before importing | ||
| sys.implementation.name = 'non-cpython' | ||
| import notecard.md5 | ||
|
|
||
| test_cases = [ | ||
| (b'', 'd41d8cd98f00b204e9800998ecf8427e'), | ||
| (b'hello', '5d41402abc4b2a76b9719d911017c592'), | ||
| (b'hello world', '5eb63bbbe01eeed093cb22bb8f5acdc3'), | ||
| (b'The quick brown fox jumps over the lazy dog', | ||
| '9e107d9d372bb6826bd81d3542a419d6'), | ||
| (b'123456789', '25f9e794323b453885f5181f1b624d0b'), | ||
| (b'!@#$%^&*()', '05b28d17a7b6e7024b6e5d8cc43a8bf7') | ||
| ] | ||
|
|
||
| for input_bytes, expected in test_cases: | ||
| with self.subTest(input_bytes=input_bytes): | ||
| result = notecard.md5.digest(input_bytes) | ||
| self.assertEqual(result, expected) |
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,73 @@ | ||
| import unittest | ||
| import sys | ||
| from unittest.mock import MagicMock, patch | ||
| from notecard import Notecard | ||
| from notecard.validators import validate_card_object | ||
|
|
||
|
|
||
| class TestValidators(unittest.TestCase): | ||
|
|
||
| def setUp(self): | ||
| self.mock_notecard = MagicMock(spec=Notecard) | ||
| # Store original implementation name | ||
| self.original_implementation = sys.implementation.name | ||
| # Clear the module from sys.modules to force reload | ||
| if 'notecard.validators' in sys.modules: | ||
| del sys.modules['notecard.validators'] | ||
|
|
||
| def tearDown(self): | ||
| # Restore original implementation | ||
| sys.implementation.name = self.original_implementation | ||
| # Clear the module again | ||
| if 'notecard.validators' in sys.modules: | ||
| del sys.modules['notecard.validators'] | ||
|
|
||
| def test_validate_card_object_with_valid_notecard(self): | ||
| @validate_card_object | ||
| def test_func(card): | ||
| return True | ||
|
|
||
| result = test_func(self.mock_notecard) | ||
| self.assertTrue(result) | ||
|
|
||
| def test_validate_card_object_with_invalid_notecard(self): | ||
| @validate_card_object | ||
| def test_func(card): | ||
| return True | ||
|
|
||
| with self.assertRaises(Exception) as context: | ||
| test_func("not a notecard") | ||
| self.assertEqual(str(context.exception), "Notecard object required") | ||
|
|
||
| @unittest.skipIf(sys.implementation.name != "cpython", "Function metadata only preserved in CPython") | ||
| def test_validate_card_object_preserves_metadata(self): | ||
| @validate_card_object | ||
| def test_func(card): | ||
| """Test function docstring.""" | ||
| return True | ||
|
|
||
| self.assertEqual(test_func.__name__, "test_func") | ||
| self.assertEqual(test_func.__doc__, "Test function docstring.") | ||
|
|
||
| def test_validate_card_object_non_cpython(self): | ||
| sys.implementation.name = 'non-cpython' | ||
| from notecard.validators import validate_card_object | ||
|
|
||
| @validate_card_object | ||
| def test_func(card): | ||
| return True | ||
|
|
||
| result = test_func(self.mock_notecard) | ||
| self.assertTrue(result) | ||
|
|
||
| def test_validate_card_object_non_cpython_with_invalid_notecard(self): | ||
| sys.implementation.name = 'non-cpython' | ||
| from notecard.validators import validate_card_object | ||
|
|
||
| @validate_card_object | ||
| def test_func(card): | ||
| return True | ||
|
|
||
| with self.assertRaises(Exception) as context: | ||
| test_func("not a notecard") | ||
| self.assertEqual(str(context.exception), "Notecard object required") |
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.
Uh oh!
There was an error while loading. Please reload this page.