test_diff_master.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 tests.dj_master.models import Author
  6. COMMAND_NAME = 'cqrs_diff_master'
  7. def test_no_cqrs_id():
  8. with pytest.raises(CommandError):
  9. call_command(COMMAND_NAME)
  10. def test_bad_cqrs_id():
  11. with pytest.raises(CommandError) as e:
  12. call_command(COMMAND_NAME, '--cqrs-id=invalid')
  13. assert 'Wrong CQRS ID: invalid!' in str(e)
  14. @pytest.mark.django_db
  15. def test_first_row(capsys):
  16. Author.objects.create(name='author', id=1)
  17. call_command(COMMAND_NAME, '--cqrs-id=author')
  18. captured = capsys.readouterr()
  19. assert '{0},'.format(Author.CQRS_ID) in captured.out
  20. @pytest.mark.django_db
  21. def test_objects_less_than_batch(capsys):
  22. author = Author.objects.create(name='author', id=1)
  23. call_command(COMMAND_NAME, '--cqrs-id=author')
  24. captured = capsys.readouterr()
  25. out_lines = captured.out.split('\n')
  26. assert ujson.loads(out_lines[1]) == [[author.pk, author.cqrs_revision]]
  27. @pytest.mark.django_db
  28. def test_objects_more_than_batch(capsys):
  29. for i in range(3):
  30. Author.objects.create(name=str(i), id=i)
  31. call_command(COMMAND_NAME, '--cqrs-id=author', '--batch=2')
  32. captured = capsys.readouterr()
  33. out_lines = captured.out.split('\n')
  34. assert ujson.loads(out_lines[1]) == [
  35. [author.pk, author.cqrs_revision] for author in Author.objects.all()[:2]
  36. ]
  37. assert ujson.loads(out_lines[2]) == [
  38. [author.pk, author.cqrs_revision] for author in Author.objects.all()[2:]
  39. ]
  40. @pytest.mark.django_db
  41. def test_filter_no_objects(capsys):
  42. call_command(COMMAND_NAME, '--cqrs-id=author', '-f={"id__in": [1, 2]}')
  43. captured = capsys.readouterr()
  44. assert 'No objects found for filter!' in captured.err
  45. @pytest.mark.django_db
  46. def test_objects_are_filtered(capsys):
  47. for i in range(2):
  48. Author.objects.create(name=str(i), id=i)
  49. call_command(COMMAND_NAME, '--cqrs-id=author', '-f={"id__in": [1, 3]}')
  50. captured = capsys.readouterr()
  51. out_lines = captured.out.split('\n')
  52. assert ujson.loads(out_lines[1]) == [[1, 0]]