|
| 1 | +from django.test import TestCase |
| 2 | + |
| 3 | +from rest_framework import compat |
| 4 | + |
| 5 | + |
| 6 | +class CompatTests(TestCase): |
| 7 | + |
| 8 | + def test_total_seconds(self): |
| 9 | + class MockTimedelta(object): |
| 10 | + days = 1 |
| 11 | + seconds = 1 |
| 12 | + microseconds = 100 |
| 13 | + timedelta = MockTimedelta() |
| 14 | + expected = (timedelta.days * 86400.0) + float(timedelta.seconds) + (timedelta.microseconds / 1000000.0) |
| 15 | + assert compat.total_seconds(timedelta) == expected |
| 16 | + |
| 17 | + def test_get_remote_field_with_old_django_version(self): |
| 18 | + class MockField(object): |
| 19 | + rel = 'example_rel' |
| 20 | + original_django_version = compat.django.VERSION |
| 21 | + compat.django.VERSION = (1, 8) |
| 22 | + assert compat.get_remote_field(MockField(), default='default_value') == 'example_rel' |
| 23 | + assert compat.get_remote_field(object(), default='default_value') == 'default_value' |
| 24 | + compat.django.VERSION = original_django_version |
| 25 | + |
| 26 | + def test_get_remote_field_with_new_django_version(self): |
| 27 | + class MockField(object): |
| 28 | + remote_field = 'example_remote_field' |
| 29 | + original_django_version = compat.django.VERSION |
| 30 | + compat.django.VERSION = (1, 10) |
| 31 | + assert compat.get_remote_field(MockField(), default='default_value') == 'example_remote_field' |
| 32 | + assert compat.get_remote_field(object(), default='default_value') == 'default_value' |
| 33 | + compat.django.VERSION = original_django_version |
| 34 | + |
| 35 | + def test_patch_in_http_method_names(self): |
| 36 | + assert 'patch' in compat.View.http_method_names |
| 37 | + |
| 38 | + def test_set_rollback_for_transaction_in_managed_mode(self): |
| 39 | + class MockTransaction(object): |
| 40 | + called_rollback = False |
| 41 | + called_leave_transaction_management = False |
| 42 | + |
| 43 | + def is_managed(self): |
| 44 | + return True |
| 45 | + |
| 46 | + def is_dirty(self): |
| 47 | + return True |
| 48 | + |
| 49 | + def rollback(self): |
| 50 | + self.called_rollback = True |
| 51 | + |
| 52 | + def leave_transaction_management(self): |
| 53 | + self.called_leave_transaction_management = True |
| 54 | + |
| 55 | + original_transaction = compat.transaction |
| 56 | + dirty_mock_transaction = MockTransaction() |
| 57 | + compat.transaction = dirty_mock_transaction |
| 58 | + compat.set_rollback() |
| 59 | + assert dirty_mock_transaction.called_rollback is True |
| 60 | + assert dirty_mock_transaction.called_leave_transaction_management is True |
| 61 | + |
| 62 | + clean_mock_transaction = MockTransaction() |
| 63 | + clean_mock_transaction.is_dirty = lambda: False |
| 64 | + compat.transaction = clean_mock_transaction |
| 65 | + compat.set_rollback() |
| 66 | + assert clean_mock_transaction.called_rollback is False |
| 67 | + assert clean_mock_transaction.called_leave_transaction_management is True |
| 68 | + |
| 69 | + compat.transaction = original_transaction |
0 commit comments