|
| 1 | +from mock import ( |
| 2 | + ANY, |
| 3 | + call, |
| 4 | + patch |
| 5 | +) |
| 6 | + |
| 7 | +from django.test import TestCase |
| 8 | + |
| 9 | +from algoliasearch_django import algolia_engine |
| 10 | +from algoliasearch_django.decorators import disable_auto_indexing |
| 11 | + |
| 12 | +from .factories import UserFactory, WebsiteFactory |
| 13 | +from .models import User |
| 14 | + |
| 15 | + |
| 16 | +class DecoratorsTestCase(TestCase): |
| 17 | + def test_disable_auto_indexing_as_decorator_for_all(self): |
| 18 | + """Test that the `disable_auto_indexing` should work as a decorator for all the model""" |
| 19 | + |
| 20 | + @disable_auto_indexing() |
| 21 | + def decorated_operation(): |
| 22 | + WebsiteFactory() |
| 23 | + UserFactory() |
| 24 | + |
| 25 | + def non_decorated_operation(): |
| 26 | + WebsiteFactory() |
| 27 | + UserFactory() |
| 28 | + |
| 29 | + with patch.object(algolia_engine, 'save_record') as mocked_save_record: |
| 30 | + decorated_operation() |
| 31 | + |
| 32 | + # The decorated method should have prevented the indexing operations |
| 33 | + mocked_save_record.assert_not_called() |
| 34 | + |
| 35 | + with patch.object(algolia_engine, 'save_record') as mocked_save_record: |
| 36 | + non_decorated_operation() |
| 37 | + |
| 38 | + # The non-decorated method is not preventing the indexing operations |
| 39 | + # (the signal was correctly re-connected for both of the models) |
| 40 | + mocked_save_record.assert_has_calls([ |
| 41 | + call( |
| 42 | + ANY, |
| 43 | + created=True, |
| 44 | + raw=False, |
| 45 | + sender=ANY, |
| 46 | + signal=ANY, |
| 47 | + update_fields=None, |
| 48 | + using=ANY |
| 49 | + ) |
| 50 | + ] * 2) |
| 51 | + |
| 52 | + def test_disable_auto_indexing_as_decorator_for_model(self): |
| 53 | + """Test that the `disable_auto_indexing` should work as a decorator for a specific model""" |
| 54 | + |
| 55 | + @disable_auto_indexing(model=User) |
| 56 | + def decorated_operation(): |
| 57 | + WebsiteFactory() |
| 58 | + UserFactory() |
| 59 | + |
| 60 | + def non_decorated_operation(): |
| 61 | + WebsiteFactory() |
| 62 | + UserFactory() |
| 63 | + |
| 64 | + with patch.object(algolia_engine, 'save_record') as mocked_save_record: |
| 65 | + decorated_operation() |
| 66 | + |
| 67 | + # The decorated method should have prevented the indexing operation for the `User` model |
| 68 | + # but not for the `Website` model (we get only one call) |
| 69 | + mocked_save_record.assert_called_once_with( |
| 70 | + ANY, |
| 71 | + created=True, |
| 72 | + raw=False, |
| 73 | + sender=ANY, |
| 74 | + signal=ANY, |
| 75 | + update_fields=None, |
| 76 | + using=ANY |
| 77 | + ) |
| 78 | + |
| 79 | + with patch.object(algolia_engine, 'save_record') as mocked_save_record: |
| 80 | + non_decorated_operation() |
| 81 | + |
| 82 | + # The non-decorated method is not preventing the indexing operations |
| 83 | + # (the signal was correctly re-connected for both of the models) |
| 84 | + mocked_save_record.assert_has_calls([ |
| 85 | + call( |
| 86 | + ANY, |
| 87 | + created=True, |
| 88 | + raw=False, |
| 89 | + sender=ANY, |
| 90 | + signal=ANY, |
| 91 | + update_fields=None, |
| 92 | + using=ANY |
| 93 | + ) |
| 94 | + ] * 2) |
| 95 | + |
| 96 | + def test_disable_auto_indexing_as_context_manager(self): |
| 97 | + """Test that the `disable_auto_indexing` should work as a context manager""" |
| 98 | + |
| 99 | + with patch.object(algolia_engine, 'save_record') as mocked_save_record: |
| 100 | + with disable_auto_indexing(): |
| 101 | + WebsiteFactory() |
| 102 | + |
| 103 | + mocked_save_record.assert_not_called() |
| 104 | + |
| 105 | + with patch.object(algolia_engine, 'save_record') as mocked_save_record: |
| 106 | + WebsiteFactory() |
| 107 | + |
| 108 | + mocked_save_record.assert_called_once() |
0 commit comments