test_registries.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright © 2023 Ingram Micro Inc. All rights reserved.
  2. import pytest
  3. from dj_cqrs.registries import MasterRegistry, ReplicaRegistry
  4. from tests.dj_master import models as master_models
  5. from tests.dj_replica import models as replica_models
  6. @pytest.mark.parametrize('registry', [MasterRegistry, ReplicaRegistry])
  7. def test_duplicate_cqrs_id(registry):
  8. class Cls(object):
  9. CQRS_ID = 'basic'
  10. with pytest.raises(AssertionError) as e:
  11. registry.register_model(Cls)
  12. assert str(e.value) == "Two models can't have the same CQRS_ID: basic."
  13. @pytest.mark.parametrize(
  14. 'model,registry',
  15. (
  16. (master_models.SimplestModel, MasterRegistry),
  17. (master_models.AutoFieldsModel, MasterRegistry),
  18. (replica_models.BasicFieldsModelRef, ReplicaRegistry),
  19. (replica_models.BadTypeModelRef, ReplicaRegistry),
  20. ),
  21. )
  22. def test_models_are_registered(model, registry):
  23. assert registry.models[model.CQRS_ID] == model
  24. assert registry.get_model_by_cqrs_id(model.CQRS_ID) == model
  25. def test_get_model_by_cqrs_id_no_id(caplog):
  26. assert ReplicaRegistry.get_model_by_cqrs_id('invalid') is None
  27. assert 'No model with such CQRS_ID: invalid.' in caplog.text
  28. def test_no_cqrs_queue(settings):
  29. settings.CQRS.update({'queue': None})
  30. with pytest.raises(AssertionError) as e:
  31. ReplicaRegistry.register_model(replica_models.MappedFieldsModelRef)
  32. assert str(e.value) == 'CQRS queue must be set for the service, that has replica models.'