test_deleted_diff_replica.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright © 2023 Ingram Micro Inc. All rights reserved.
  2. import pytest
  3. import ujson
  4. from django.core.management import CommandError, call_command
  5. from django.utils.timezone import now
  6. from tests.dj_replica.models import AuthorRef
  7. COMMAND_NAME = 'cqrs_deleted_diff_replica'
  8. def test_no_cqrs_id():
  9. with pytest.raises(CommandError):
  10. call_command(COMMAND_NAME)
  11. def test_bad_cqrs_id():
  12. with pytest.raises(CommandError) as e:
  13. call_command(COMMAND_NAME, '--cqrs-id=invalid')
  14. assert 'Wrong CQRS ID: invalid!' in str(e)
  15. @pytest.mark.django_db
  16. def test_first_row(capsys):
  17. AuthorRef.objects.create(name='author', id=1, cqrs_revision=0, cqrs_updated=now())
  18. call_command(COMMAND_NAME, '--cqrs-id=author')
  19. captured = capsys.readouterr()
  20. assert '{0},'.format(AuthorRef.CQRS_ID) in captured.out
  21. @pytest.mark.django_db
  22. def test_objects_less_than_batch(capsys):
  23. AuthorRef.objects.create(name='author', id=1, cqrs_revision=0, cqrs_updated=now())
  24. call_command(COMMAND_NAME, '--cqrs-id=author')
  25. captured = capsys.readouterr()
  26. out_lines = captured.out.split('\n')
  27. assert ujson.loads(out_lines[1]) == [1]
  28. @pytest.mark.django_db
  29. def test_objects_more_than_batch(capsys):
  30. for i in range(3):
  31. AuthorRef.objects.create(name=str(i), id=i, cqrs_revision=i, cqrs_updated=now())
  32. call_command(COMMAND_NAME, '--cqrs-id=author', '--batch=2')
  33. captured = capsys.readouterr()
  34. out_lines = captured.out.split('\n')
  35. assert ujson.loads(out_lines[1]) == [0, 1]
  36. assert ujson.loads(out_lines[2]) == [2]
  37. @pytest.mark.django_db
  38. def test_filter_no_objects(capsys):
  39. call_command(COMMAND_NAME, '--cqrs-id=author', '-f={"id__in": [1, 2]}')
  40. captured = capsys.readouterr()
  41. assert 'No objects found for filter!' in captured.err
  42. @pytest.mark.django_db
  43. def test_objects_are_filtered(capsys):
  44. for i in range(2):
  45. AuthorRef.objects.create(name=str(i), id=i, cqrs_revision=i, cqrs_updated=now())
  46. call_command(COMMAND_NAME, '--cqrs-id=author', '-f={"id__in": [1, 3]}')
  47. captured = capsys.readouterr()
  48. out_lines = captured.out.split('\n')
  49. assert ujson.loads(out_lines[1]) == [1]