test_deleted_sync_replica.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 tests.dj_master.models import Author
  8. from tests.dj_replica.models import AuthorRef
  9. COMMAND_NAME = 'cqrs_deleted_sync_replica'
  10. def test_bad_cqrs_id(mocker):
  11. mocker.patch.object(sys, 'stdin', StringIO('invalid,datetime\n'))
  12. with pytest.raises(CommandError) as e:
  13. call_command(COMMAND_NAME)
  14. assert 'Wrong CQRS ID: invalid!' in str(e)
  15. def diff_pipe(capsys, mocker, *args):
  16. call_command('cqrs_deleted_diff_replica', '--cqrs-id=author', *args)
  17. captured = capsys.readouterr()
  18. mocker.patch.object(sys, 'stdin', StringIO(captured.out))
  19. call_command('cqrs_deleted_diff_master')
  20. captured = capsys.readouterr()
  21. mocker.stopall()
  22. mocker.patch.object(sys, 'stdin', StringIO(captured.out))
  23. call_command(COMMAND_NAME)
  24. @pytest.mark.django_db
  25. def test_all_synced(capsys, mocker):
  26. mocker.patch('dj_cqrs.controller.producer.produce')
  27. Author.objects.create(id=1, name='name')
  28. AuthorRef.objects.create(id=1, name='name', cqrs_revision=0, cqrs_updated=now())
  29. diff_pipe(capsys, mocker)
  30. assert AuthorRef.objects.count() == 1
  31. @pytest.mark.django_db
  32. def test_sync_for_all(mocker, capsys):
  33. AuthorRef.objects.create(id=1, name='name', cqrs_revision=0, cqrs_updated=now())
  34. diff_pipe(capsys, mocker)
  35. assert not AuthorRef.objects.exists()
  36. @pytest.mark.django_db
  37. def test_partial_sync(mocker, capsys):
  38. mocker.patch('dj_cqrs.controller.producer.produce')
  39. Author.objects.create(id=1, name='name')
  40. AuthorRef.objects.create(id=1, name='name', cqrs_revision=0, cqrs_updated=now())
  41. AuthorRef.objects.create(id=2, name='name', cqrs_revision=0, cqrs_updated=now())
  42. diff_pipe(capsys, mocker)
  43. assert list(AuthorRef.objects.all()) == [AuthorRef(id=1)]
  44. @pytest.mark.django_db
  45. def test_sync_batch(mocker, capsys):
  46. mocker.patch('dj_cqrs.controller.producer.produce')
  47. for i in range(3):
  48. AuthorRef.objects.create(id=i, name='name', cqrs_revision=0, cqrs_updated=now())
  49. Author.objects.create(id=2, name='name')
  50. diff_pipe(capsys, mocker, '--batch=1')
  51. assert list(AuthorRef.objects.all()) == [AuthorRef(id=2)]