-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Add couple of tests for compat module #4845
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from django.test import TestCase | ||
|
||
from rest_framework import compat | ||
|
||
|
||
class CompatTests(TestCase): | ||
|
||
def test_total_seconds(self): | ||
class MockTimedelta(object): | ||
days = 1 | ||
seconds = 1 | ||
microseconds = 100 | ||
timedelta = MockTimedelta() | ||
expected = (timedelta.days * 86400.0) + float(timedelta.seconds) + (timedelta.microseconds / 1000000.0) | ||
assert compat.total_seconds(timedelta) == expected | ||
|
||
def test_get_remote_field_with_old_django_version(self): | ||
class MockField(object): | ||
rel = 'example_rel' | ||
original_django_version = compat.django.VERSION | ||
compat.django.VERSION = (1, 8) | ||
assert compat.get_remote_field(MockField(), default='default_value') == 'example_rel' | ||
assert compat.get_remote_field(object(), default='default_value') == 'default_value' | ||
compat.django.VERSION = original_django_version | ||
|
||
def test_get_remote_field_with_new_django_version(self): | ||
class MockField(object): | ||
remote_field = 'example_remote_field' | ||
original_django_version = compat.django.VERSION | ||
compat.django.VERSION = (1, 10) | ||
assert compat.get_remote_field(MockField(), default='default_value') == 'example_remote_field' | ||
assert compat.get_remote_field(object(), default='default_value') == 'default_value' | ||
compat.django.VERSION = original_django_version | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't reset the Django version if this test fails, because an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point - lazy review on my part. |
||
|
||
def test_patch_in_http_method_names(self): | ||
assert 'patch' in compat.View.http_method_names | ||
|
||
def test_set_rollback_for_transaction_in_managed_mode(self): | ||
class MockTransaction(object): | ||
called_rollback = False | ||
called_leave_transaction_management = False | ||
|
||
def is_managed(self): | ||
return True | ||
|
||
def is_dirty(self): | ||
return True | ||
|
||
def rollback(self): | ||
self.called_rollback = True | ||
|
||
def leave_transaction_management(self): | ||
self.called_leave_transaction_management = True | ||
|
||
original_transaction = compat.transaction | ||
dirty_mock_transaction = MockTransaction() | ||
compat.transaction = dirty_mock_transaction | ||
compat.set_rollback() | ||
assert dirty_mock_transaction.called_rollback is True | ||
assert dirty_mock_transaction.called_leave_transaction_management is True | ||
|
||
clean_mock_transaction = MockTransaction() | ||
clean_mock_transaction.is_dirty = lambda: False | ||
compat.transaction = clean_mock_transaction | ||
compat.set_rollback() | ||
assert clean_mock_transaction.called_rollback is False | ||
assert clean_mock_transaction.called_leave_transaction_management is True | ||
|
||
compat.transaction = original_transaction |
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.
Is there any reason why we chose to calculate the expected value in the test instead of working from a known-good value for how many seconds there are?