Skip to content

Commit

Permalink
Merge pull request #100 from cloudblue/fix-2/LITE-23778
Browse files Browse the repository at this point in the history
LITE-23778 Fixed serialization of DT / UUID instance_pk
  • Loading branch information
maxipavlovic authored Dec 5, 2022
2 parents e55c8e1 + 9682756 commit 5a1aad0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
6 changes: 3 additions & 3 deletions dj_cqrs/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from dateutil.parser import parse as dateutil_parse

from dj_cqrs.correlation import get_correlation_id
from dj_cqrs.utils import get_message_expiration_dt
from dj_cqrs.utils import get_json_valid_value, get_message_expiration_dt

from django.utils import timezone

Expand Down Expand Up @@ -152,8 +152,8 @@ def to_dict(self):
'cqrs_id': self.__cqrs_id,
'instance_data': self.__instance_data,
'previous_data': self.__previous_data,
'instance_pk': self.__instance_pk,
'correlation_id': self.__correlation_id,
'instance_pk': get_json_valid_value(self.__instance_pk),
'correlation_id': get_json_valid_value(self.__correlation_id),
'retries': self.__retries,
'expires': expires,
'meta': self.__meta,
Expand Down
8 changes: 2 additions & 6 deletions dj_cqrs/tracker.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Copyright © 2022 Ingram Micro Inc. All rights reserved.

from datetime import date, datetime

from dj_cqrs.constants import ALL_BASIC_FIELDS, FIELDS_TRACKER_FIELD_NAME
from dj_cqrs.utils import get_json_valid_value

from model_utils import FieldTracker
from model_utils.tracker import FieldInstanceTracker
Expand Down Expand Up @@ -30,10 +29,7 @@ def changed_initial(self):
def get_field_value(self, field):
value = super().get_field_value(field)

if isinstance(value, (date, datetime)):
value = str(value)

return value
return get_json_valid_value(value)


class CQRSTracker(FieldTracker):
Expand Down
7 changes: 6 additions & 1 deletion dj_cqrs/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Copyright © 2022 Ingram Micro Inc. All rights reserved.

import logging
from datetime import timedelta
from datetime import date, datetime, timedelta
from uuid import UUID

from django.conf import settings
from django.utils import timezone
Expand Down Expand Up @@ -49,3 +50,7 @@ def get_messages_prefetch_count_per_worker():
return 0

return delay_queue_max_size + 1


def get_json_valid_value(value):
return str(value) if isinstance(value, (date, datetime, UUID)) else value
22 changes: 20 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Copyright © 2022 Ingram Micro Inc. All rights reserved.

from datetime import datetime, timedelta, timezone
from datetime import date, datetime, timedelta, timezone
from uuid import UUID

from dj_cqrs.utils import (
get_delay_queue_max_size, get_message_expiration_dt, get_messages_prefetch_count_per_worker,
get_delay_queue_max_size,
get_json_valid_value,
get_message_expiration_dt,
get_messages_prefetch_count_per_worker,
)

import pytest


def test_get_message_expiration_dt_fixed(mocker, settings):
settings.CQRS['master']['CQRS_MESSAGE_TTL'] = 3600
Expand Down Expand Up @@ -60,3 +66,15 @@ def test_get_messaged_prefetch_count_per_worker_with_delay_queue(settings):
settings.CQRS['replica']['delay_queue_max_size'] = 4

assert get_messages_prefetch_count_per_worker() == 5


@pytest.mark.parametrize('value,result', (
(None, None),
(1, 1),
(datetime(2022, 1, 1, second=0, tzinfo=timezone.utc), '2022-01-01 00:00:00+00:00'),
(date(2022, 2, 1), '2022-02-01'),
(UUID('0419d87b-d477-44e4-82c4-310f56faa3c7'), '0419d87b-d477-44e4-82c4-310f56faa3c7'),
('abc', 'abc'),
))
def test_get_json_valid_value(value, result):
assert get_json_valid_value(value) == result

0 comments on commit 5a1aad0

Please sign in to comment.