test_deleted_diff_master.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_diff_master'
  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. @pytest.mark.django_db
  16. def test_no_rows(mocker, capsys):
  17. mocker.patch.object(sys, 'stdin', StringIO('author,datetime\n'))
  18. call_command(COMMAND_NAME)
  19. captured = capsys.readouterr()
  20. assert not captured.err
  21. assert 'author,datetime\n' == captured.out
  22. def replica_master_pipe(capsys, mocker, *args):
  23. call_command('cqrs_deleted_diff_replica', '--cqrs-id=author', *args)
  24. captured = capsys.readouterr()
  25. mocker.patch.object(sys, 'stdin', StringIO(captured.out))
  26. call_command(COMMAND_NAME)
  27. return capsys.readouterr()
  28. @pytest.mark.django_db
  29. def test_first_row(capsys, mocker):
  30. AuthorRef.objects.create(name='author', id=1, cqrs_revision=0, cqrs_updated=now())
  31. captured = replica_master_pipe(capsys, mocker)
  32. first_row = captured.out.split('\n')[0]
  33. assert '{0},'.format(Author.CQRS_ID) in first_row
  34. @pytest.mark.django_db
  35. def test_all_synced(capsys, mocker):
  36. mocker.patch('dj_cqrs.controller.producer.produce')
  37. Author.objects.create(id=1, name='name')
  38. AuthorRef.objects.create(id=1, name='name', cqrs_revision=0, cqrs_updated=now())
  39. captured = replica_master_pipe(capsys, mocker)
  40. assert not captured.err
  41. @pytest.mark.django_db
  42. def test_sync_for_all(mocker, capsys):
  43. for i in range(3):
  44. AuthorRef.objects.create(name=str(i), id=i, cqrs_revision=i, cqrs_updated=now())
  45. captured = replica_master_pipe(capsys, mocker)
  46. for err_text in ('PK to delete', '0', '1', '2'):
  47. assert err_text in captured.err
  48. second_row = captured.out.split('\n')[1]
  49. assert '[0,1,2]' in second_row
  50. @pytest.mark.django_db
  51. def test_partial_sync(mocker, capsys):
  52. mocker.patch('dj_cqrs.controller.producer.produce')
  53. Author.objects.create(id=1, name='name')
  54. AuthorRef.objects.create(id=1, name='name', cqrs_revision=0, cqrs_updated=now())
  55. AuthorRef.objects.create(id=2, name='2', cqrs_revision=0, cqrs_updated=now())
  56. captured = replica_master_pipe(capsys, mocker)
  57. assert 'PK to delete' in captured.err
  58. assert '2' in captured.err
  59. second_row = captured.out.split('\n')[1]
  60. assert '[2]' in second_row
  61. @pytest.mark.django_db
  62. def test_sync_batch(mocker, capsys):
  63. mocker.patch('dj_cqrs.controller.producer.produce')
  64. for i in range(3):
  65. AuthorRef.objects.create(id=i, name=str(i), cqrs_revision=0, cqrs_updated=now())
  66. Author.objects.create(id=1, name='name')
  67. captured = replica_master_pipe(capsys, mocker, '--batch=1')
  68. for err_text in ('PK to delete', '0', '2'):
  69. assert err_text in captured.err
  70. second_row = captured.out.split('\n')[1]
  71. assert '[0]' in second_row
  72. third_row = captured.out.split('\n')[2]
  73. assert '[2]' in third_row