test_bulk_dump.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.db import transaction
  6. from tests.dj_master.models import Author, Publisher
  7. from tests.test_commands.utils import remove_file
  8. from tests.utils import db_error
  9. COMMAND_NAME = 'cqrs_bulk_dump'
  10. def test_no_cqrs_id():
  11. with pytest.raises(CommandError):
  12. call_command(COMMAND_NAME)
  13. def test_bad_cqrs_id():
  14. with pytest.raises(CommandError) as e:
  15. call_command(COMMAND_NAME, '--cqrs-id=invalid')
  16. assert 'Wrong CQRS ID: invalid!' in str(e)
  17. def test_output_file_exists():
  18. with pytest.raises(CommandError) as e:
  19. call_command(COMMAND_NAME, '--cqrs-id=author', '-o=pyproject.toml')
  20. assert 'File pyproject.toml exists!' in str(e)
  21. @pytest.mark.django_db
  22. def test_dumps_no_rows(capsys):
  23. remove_file('author.dump')
  24. call_command(COMMAND_NAME, '--cqrs-id=author')
  25. with open('author.dump', 'r') as f:
  26. lines = f.readlines()
  27. assert len(lines) == 1
  28. assert lines[0] == 'author'
  29. captured = capsys.readouterr()
  30. assert 'Done!\n0 instance(s) saved.\n0 instance(s) processed.' in captured.err
  31. @pytest.mark.django_db
  32. def tests_dumps_several_rows(capsys):
  33. remove_file('author.dump')
  34. Author.objects.create(id=2, name='2')
  35. with transaction.atomic():
  36. publisher = Publisher.objects.create(id=1, name='publisher')
  37. author = Author.objects.create(id=1, name='1', publisher=publisher)
  38. call_command(COMMAND_NAME, '--cqrs-id=author')
  39. with open('author.dump', 'r') as f:
  40. lines = f.readlines()
  41. assert len(lines) == 3
  42. assert lines[0].strip() == 'author'
  43. line_with_publisher = next(ln for ln in lines[1:] if '"name":"publisher"' in ln)
  44. assert author.to_cqrs_dict() == ujson.loads(line_with_publisher)
  45. captured = capsys.readouterr()
  46. assert 'Done!\n2 instance(s) saved.\n2 instance(s) processed.' in captured.err
  47. @pytest.mark.django_db
  48. def tests_dumps_more_than_batch(capsys):
  49. remove_file('author.dump')
  50. Author.objects.bulk_create(
  51. (Author(id=index, name='n') for index in range(1, 150)),
  52. )
  53. call_command(COMMAND_NAME, '--cqrs-id=author')
  54. with open('author.dump', 'r') as f:
  55. lines = f.readlines()
  56. assert len(lines) == 150
  57. captured = capsys.readouterr()
  58. assert 'Done!\n149 instance(s) saved.\n149 instance(s) processed.' in captured.err
  59. @pytest.mark.django_db
  60. def test_error(capsys, mocker):
  61. remove_file('author.dump')
  62. Author.objects.create(id=2, name='2')
  63. mocker.patch('tests.dj_master.models.Author.to_cqrs_dict', side_effect=db_error)
  64. call_command(COMMAND_NAME, '--cqrs-id=author')
  65. captured = capsys.readouterr()
  66. assert 'Dump record failed for pk=2' in captured.err
  67. assert '1 instance(s) processed.' in captured.err
  68. assert '0 instance(s) saved.' in captured.err
  69. @pytest.mark.django_db
  70. def test_progress(capsys):
  71. remove_file('author.dump')
  72. Author.objects.create(id=2, name='2')
  73. call_command(COMMAND_NAME, '--cqrs-id=author', '--progress')
  74. captured = capsys.readouterr()
  75. assert 'Processing 1 records with batch size 10000' in captured.err
  76. assert '1 of 1 processed - 100% with rate' in captured.err