test_bulk_load.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Copyright © 2023 Ingram Micro Inc. All rights reserved.
  2. import pytest
  3. from django.core.management import CommandError, call_command
  4. from django.utils.timezone import now
  5. from tests.dj_replica.models import AuthorRef
  6. from tests.utils import db_error
  7. COMMAND_NAME = 'cqrs_bulk_load'
  8. DUMPS_PATH = 'tests/test_commands/dumps/'
  9. def test_no_input():
  10. with pytest.raises(CommandError):
  11. call_command(COMMAND_NAME)
  12. def test_no_file():
  13. with pytest.raises(CommandError) as e:
  14. call_command(COMMAND_NAME, '-i=__init__1.py')
  15. assert "File __init__1.py doesn't exist!" in str(e)
  16. def test_empty_file():
  17. with pytest.raises(CommandError) as e:
  18. call_command(COMMAND_NAME, '-i={0}empty_file.dump'.format(DUMPS_PATH))
  19. assert 'empty_file.dump is empty!' in str(e)
  20. def test_no_cqrs_id():
  21. with pytest.raises(CommandError) as e:
  22. call_command(COMMAND_NAME, '-i={0}bad_cqrs_id.dump'.format(DUMPS_PATH))
  23. assert 'Wrong CQRS ID: publisher!' in str(e)
  24. @pytest.mark.django_db
  25. def test_unparseable_line(capsys):
  26. call_command(COMMAND_NAME, '-i={0}unparseable.dump'.format(DUMPS_PATH))
  27. assert AuthorRef.objects.count() == 0
  28. captured = capsys.readouterr()
  29. assert "Dump file can't be parsed: line 2!" in captured.err
  30. assert '0 instance(s) loaded.' in captured.err
  31. @pytest.mark.django_db
  32. def test_bad_master_data(capsys):
  33. call_command(COMMAND_NAME, '-i={0}bad_master_data.dump'.format(DUMPS_PATH))
  34. assert AuthorRef.objects.count() == 1
  35. captured = capsys.readouterr()
  36. assert "Instance can't be saved: line 3!" in captured.err
  37. assert '1 instance(s) loaded.' in captured.err
  38. @pytest.mark.django_db
  39. def test_no_rows(capsys):
  40. AuthorRef.objects.create(id=1, name='1', cqrs_revision=0, cqrs_updated=now())
  41. call_command(COMMAND_NAME, '--input={0}no_rows.dump'.format(DUMPS_PATH))
  42. assert AuthorRef.objects.count() == 1
  43. captured = capsys.readouterr()
  44. assert '0 instance(s) loaded.' in captured.err
  45. @pytest.mark.django_db(transaction=True)
  46. def test_loaded_correctly(capsys):
  47. AuthorRef.objects.create(id=1, name='1', cqrs_revision=0, cqrs_updated=now())
  48. call_command(COMMAND_NAME, '--input={0}author.dump'.format(DUMPS_PATH))
  49. assert AuthorRef.objects.count() == 2
  50. captured = capsys.readouterr()
  51. assert '2 instance(s) loaded.' in captured.err
  52. @pytest.mark.django_db
  53. def test_delete_before_upload_ok(capsys):
  54. AuthorRef.objects.create(id=1, name='1', cqrs_revision=0, cqrs_updated=now())
  55. call_command(COMMAND_NAME, '--input={0}no_rows.dump'.format(DUMPS_PATH), '--clear=true')
  56. assert AuthorRef.objects.count() == 0
  57. captured = capsys.readouterr()
  58. assert '0 instance(s) loaded.' in captured.err
  59. @pytest.mark.django_db
  60. def test_delete_operation_fails(mocker):
  61. mocker.patch('django.db.models.manager.BaseManager.all', side_effect=db_error)
  62. with pytest.raises(CommandError) as e:
  63. call_command(COMMAND_NAME, '--input={0}no_rows.dump'.format(DUMPS_PATH), '--clear=true')
  64. assert 'Delete operation fails!' in str(e)
  65. @pytest.mark.django_db
  66. def test_unexpected_error(mocker, capsys):
  67. mocker.patch('tests.dj_replica.models.AuthorRef.cqrs_save', side_effect=db_error)
  68. call_command(COMMAND_NAME, '--input={0}author.dump'.format(DUMPS_PATH))
  69. captured = capsys.readouterr()
  70. assert 'Unexpected error: line 2!' in captured.err
  71. assert 'Unexpected error: line 3!' in captured.err
  72. assert '0 instance(s) loaded.' in captured.err
  73. @pytest.mark.django_db(transaction=True)
  74. def test_loaded_correctly_batch(capsys):
  75. AuthorRef.objects.create(id=1, name='1', cqrs_revision=0, cqrs_updated=now())
  76. call_command(COMMAND_NAME, '--input={0}author.dump'.format(DUMPS_PATH), '--batch=1')
  77. assert AuthorRef.objects.count() == 2
  78. captured = capsys.readouterr()
  79. assert '2 instance(s) loaded.' in captured.err