test_diff_sync.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Copyright © 2023 Ingram Micro Inc. All rights reserved.
  2. import sys
  3. from io import StringIO
  4. import pytest
  5. from django.core.management import CommandError, call_command
  6. from django.utils.timezone import now
  7. from dj_cqrs.constants import NO_QUEUE
  8. from dj_cqrs.management.commands import cqrs_sync
  9. from tests.dj_master.models import Author
  10. from tests.dj_replica.models import AuthorRef
  11. COMMAND_NAME = 'cqrs_diff_sync'
  12. def test_bad_cqrs_id(mocker):
  13. mocker.patch.object(sys, 'stdin', StringIO('invalid,datetime,replica\n'))
  14. with pytest.raises(CommandError) as e:
  15. call_command(COMMAND_NAME)
  16. assert 'Wrong CQRS ID: invalid!' in str(e)
  17. def diff_pipe(capsys, mocker, *args, progress=False):
  18. call_command('cqrs_diff_master', '--cqrs-id=author', *args)
  19. captured = capsys.readouterr()
  20. mocker.patch.object(sys, 'stdin', StringIO(captured.out))
  21. call_command('cqrs_diff_replica')
  22. captured = capsys.readouterr()
  23. mocker.stopall()
  24. sync_mock = mocker.patch.object(cqrs_sync.Command, 'handle')
  25. mocker.patch.object(sys, 'stdin', StringIO(captured.out))
  26. sync_args = args
  27. if progress:
  28. sync_args += ('--progress',)
  29. call_command(COMMAND_NAME, *sync_args)
  30. return sync_mock
  31. @pytest.mark.django_db
  32. def test_all_synced(capsys, mocker):
  33. mocker.patch('dj_cqrs.controller.producer.produce')
  34. Author.objects.create(id=1, name='name')
  35. AuthorRef.objects.create(id=1, name='name', cqrs_revision=0, cqrs_updated=now())
  36. sync_mock = diff_pipe(capsys, mocker)
  37. sync_mock.assert_not_called()
  38. @pytest.mark.django_db
  39. def test_sync_for_all(mocker, capsys):
  40. mocker.patch('dj_cqrs.controller.producer.produce')
  41. Author.objects.create(name='a', id=1)
  42. sync_mock = diff_pipe(capsys, mocker, '--batch=5000', progress=True)
  43. sync_mock.assert_called_once_with(
  44. **{
  45. 'cqrs_id': 'author',
  46. 'filter': '{"id__in": [1]}',
  47. 'queue': 'replica',
  48. 'progress': True,
  49. 'batch': 5000,
  50. },
  51. )
  52. @pytest.mark.django_db
  53. def test_partial_sync(mocker, capsys):
  54. mocker.patch('dj_cqrs.controller.producer.produce')
  55. Author.objects.create(id=1, name='name')
  56. AuthorRef.objects.create(id=1, name='name', cqrs_revision=0, cqrs_updated=now())
  57. Author.objects.create(id=2, name='2')
  58. sync_mock = diff_pipe(capsys, mocker)
  59. sync_mock.assert_called_once_with(
  60. **{
  61. 'cqrs_id': 'author',
  62. 'filter': '{"id__in": [2]}',
  63. 'queue': 'replica',
  64. 'batch': 10000,
  65. 'progress': False,
  66. },
  67. )
  68. @pytest.mark.django_db
  69. def test_sync_batch(mocker, capsys):
  70. mocker.patch('dj_cqrs.controller.producer.produce')
  71. for i in range(3):
  72. Author.objects.create(id=i, name=str(i))
  73. AuthorRef.objects.create(id=1, name='name', cqrs_revision=0, cqrs_updated=now())
  74. sync_mock = diff_pipe(capsys, mocker, '--batch=1')
  75. assert sync_mock.call_count == 2
  76. @pytest.mark.django_db
  77. def test_sync_no_queue(mocker):
  78. sync_mock = mocker.patch.object(cqrs_sync.Command, 'handle')
  79. mocker.patch.object(sys, 'stdin', StringIO('author,dt,{0}\n[1]\n'.format(NO_QUEUE)))
  80. call_command(COMMAND_NAME, '--progress')
  81. sync_mock.assert_called_once_with(
  82. **{
  83. 'cqrs_id': 'author',
  84. 'filter': '{"id__in": [1]}',
  85. 'batch': 10000,
  86. 'progress': True,
  87. }
  88. )