test_admin.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright © 2023 Ingram Micro Inc. All rights reserved.
  2. import pytest
  3. from django.contrib import admin
  4. from dj_cqrs.admin import CQRSAdminMasterSyncMixin
  5. class MockAdminModel(CQRSAdminMasterSyncMixin, admin.ModelAdmin):
  6. pass
  7. @pytest.mark.parametrize(
  8. ('total_sync', 'total_failed'),
  9. (
  10. (2, 1),
  11. (3, 0),
  12. (0, 3),
  13. (0, 0),
  14. ),
  15. )
  16. def test_sync_items_function(total_sync, total_failed, mocker):
  17. model_path = 'dj_cqrs.mixins.MasterMixin'
  18. mock_model = mocker.patch(model_path)
  19. request = None
  20. qs = []
  21. for _ in range(total_sync):
  22. m = mocker.patch(model_path)
  23. m.cqrs_sync.return_value = True
  24. qs.append(m)
  25. failed_items = []
  26. for _ in range(total_failed):
  27. m = mocker.patch(model_path)
  28. m.cqrs_sync.return_value = False
  29. qs.append(m)
  30. failed_items.append(m)
  31. mixin = MockAdminModel(model=mock_model, admin_site=admin.sites.AdminSite())
  32. mixin.message_user = mocker.Mock()
  33. mixin.sync_items(request, qs)
  34. mixin.message_user.assert_called_once_with(
  35. request,
  36. f'{total_sync} successfully synced. {total_failed} failed: {failed_items}',
  37. )
  38. def test_admin_actions_enabled_with_sync_items_action(mocker):
  39. mock_model = mocker.Mock()
  40. request = mocker.patch('django.http.HttpRequest')
  41. mixin = MockAdminModel(model=mock_model, admin_site=admin.sites.AdminSite())
  42. actions = mixin.get_actions(request)
  43. assert 'sync_items' in actions
  44. assert 'delete_selected' in actions
  45. def test_actions_not_enabled(mocker):
  46. mock_model = mocker.Mock()
  47. request = mocker.patch('django.http.HttpRequest')
  48. mixin = MockAdminModel(model=mock_model, admin_site=admin.sites.AdminSite())
  49. mixin.actions = None
  50. actions = mixin.get_actions(request)
  51. assert actions == {}