utils.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright © 2023 Ingram Micro Inc. All rights reserved.
  2. from django.db import DatabaseError
  3. def assert_is_sub_dict(dict1, dict2):
  4. assert dict1.items() <= dict2.items()
  5. def assert_publisher_once_called_with_args(publisher_mock, *args):
  6. assert publisher_mock.call_count == 1
  7. call_t_payload = publisher_mock.call_args[0][0]
  8. assert call_t_payload.signal_type == args[0]
  9. assert call_t_payload.cqrs_id == args[1]
  10. assert call_t_payload.pk == args[3]
  11. assert_is_sub_dict(args[2], call_t_payload.instance_data)
  12. required_fields = {'cqrs_revision', 'cqrs_updated'}
  13. assert not (required_fields - set(call_t_payload.instance_data.keys()))
  14. def db_error(*args, **kwargs):
  15. raise DatabaseError()
  16. def assert_tracked_fields(model_cls, fields):
  17. if model_cls.CQRS_TRACKED_FIELDS == '__all__':
  18. fields_to_track = {
  19. f.attname if f.is_relation else f.name for f in model_cls._meta.concrete_fields
  20. }
  21. else:
  22. fields_to_track = set()
  23. for fname in model_cls.CQRS_TRACKED_FIELDS:
  24. field = model_cls._meta.get_field(fname)
  25. if field.is_relation:
  26. fields_to_track.add(field.attname)
  27. else:
  28. fields_to_track.add(field.name)
  29. assert fields_to_track == fields