test_single_basic_instance.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright © 2023 Ingram Micro Inc. All rights reserved.
  2. import pytest
  3. from django.utils.timezone import now
  4. from integration_tests.tests.utils import (
  5. REPLICA_BASIC_TABLE,
  6. count_replica_rows,
  7. get_replica_first,
  8. transport_delay,
  9. )
  10. from tests.dj_master.models import BasicFieldsModel
  11. @pytest.mark.django_db(transaction=True)
  12. def test_flow(replica_cursor, clean_rabbit_transport_connection):
  13. assert count_replica_rows(replica_cursor, REPLICA_BASIC_TABLE) == 0
  14. # Create
  15. master_instance = BasicFieldsModel.objects.create(
  16. int_field=1,
  17. char_field='text',
  18. date_field=now().date(),
  19. bool_field=False,
  20. )
  21. assert master_instance.cqrs_revision == 0
  22. transport_delay()
  23. assert count_replica_rows(replica_cursor, REPLICA_BASIC_TABLE) == 1
  24. replica_tuple = get_replica_first(
  25. replica_cursor,
  26. REPLICA_BASIC_TABLE,
  27. ('int_field', 'char_field', 'date_field', 'cqrs_revision', 'cqrs_updated', 'bool_field'),
  28. )
  29. assert (
  30. master_instance.int_field,
  31. master_instance.char_field,
  32. master_instance.date_field,
  33. master_instance.cqrs_revision,
  34. master_instance.cqrs_updated,
  35. master_instance.bool_field,
  36. ) == replica_tuple
  37. # Update
  38. master_instance.bool_field = True
  39. master_instance.save()
  40. if hasattr(master_instance, 'get_tracked_fields_data'):
  41. previous_values = master_instance.get_tracked_fields_data()
  42. assert 'bool_field' in previous_values
  43. assert previous_values['bool_field'] is False
  44. master_instance.refresh_from_db()
  45. assert master_instance.cqrs_revision == 1
  46. transport_delay()
  47. assert count_replica_rows(replica_cursor, REPLICA_BASIC_TABLE) == 1
  48. updated_replica_tuple = get_replica_first(
  49. replica_cursor,
  50. REPLICA_BASIC_TABLE,
  51. ('int_field', 'cqrs_revision', 'cqrs_updated', 'bool_field'),
  52. )
  53. assert (
  54. master_instance.int_field,
  55. master_instance.cqrs_revision,
  56. master_instance.cqrs_updated,
  57. master_instance.bool_field,
  58. ) == updated_replica_tuple
  59. # Delete
  60. master_instance.delete()
  61. transport_delay()
  62. assert count_replica_rows(replica_cursor, REPLICA_BASIC_TABLE) == 0