Skip to content

Commit

Permalink
Merge pull request #3 from RegioHelden/2-save-method-signature
Browse files Browse the repository at this point in the history
Update save method signature to specify arguments individually
  • Loading branch information
stefan-cardnell-rh authored Aug 1, 2024
2 parents 5dc1f06 + a6033a0 commit d97053f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.bumpversion]
current_version = "0.0.2"
current_version = "0.0.3"
parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)"
serialize = ["{major}.{minor}.{patch}"]
search = "{current_version}"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ Configuration for [confluent_kafka.schema_registry.SchemaRegistryClient](https:/
Default: `django_kafka.error_handlers.ClientErrorHandler`

This is an `error_cb` hook (see [Kafka Client Configuration](https://docs.confluent.io/platform/current/clients/confluent-kafka-python/html/index.html#kafka-client-configuration) for reference).
It is triggered for client global errors and in case of fatal error it raises `DjangoKafkaException`.
It is triggered for client global errors and in case of fatal error it raises `DjangoKafkaError`.


## Bidirectional data sync with no infinite event loop.
Expand Down
2 changes: 1 addition & 1 deletion django_kafka/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

logger = logging.getLogger(__name__)

__version__ = "0.0.2"
__version__ = "0.0.3"

__all__ = [
"autodiscover",
Expand Down
2 changes: 1 addition & 1 deletion django_kafka/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ def commit_offset(self, msg: cimpl.Message):
self.store_offsets(msg)

def handle_error(self, error):
logger.error(error)
logger.error(error, exc_info=True)
23 changes: 17 additions & 6 deletions django_kafka/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,23 @@ class Meta:
abstract = True
base_manager_name = "objects"

def save(self, **kwargs):
if "update_fields" not in kwargs:
def save(
self,
force_insert=False,
force_update=False,
using=None,
update_fields=None,
):
if update_fields is None:
self.kafka_skip = False

elif "kafka_skip" not in kwargs["update_fields"]:
elif "kafka_skip" not in update_fields:
self.kafka_skip = False
kwargs["update_fields"] = ["kafka_skip", *kwargs["update_fields"]]

super().save(**kwargs)
update_fields = ["kafka_skip", *update_fields]

super().save(
force_insert=force_insert,
force_update=force_update,
using=using,
update_fields=update_fields,
)
45 changes: 26 additions & 19 deletions django_kafka/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,68 @@ def test_save_update_fields_not_in_kwargs(self, super_mock):
"""
kafka_skip should be set to False when update_fields is not provided
"""
save_kwargs = {}

instance = self.model(pk=1, kafka_skip=True)

instance.save(**save_kwargs)
instance.save()

# save sets kafka_skip value to False
self.assertFalse(instance.kafka_skip)
# didn't forget to call super
super_mock.assert_called_once_with()
# save kwargs are not changed
super_mock.return_value.save.assert_called_once_with(**save_kwargs)
# no update_fields set
super_mock.return_value.save.assert_called_once_with(
force_insert=False,
force_update=False,
using=None,
update_fields=None,
)

@patch("django_kafka.models.super")
def test_save_update_fields_in_kwargs_without_kafka_skip(self, super_mock):
"""
kafka_skip should be set to False when update_fields does not contain kafka_skip
"""
save_kwargs = {
"update_fields": ["some_field"],
}
instance = self.model(pk=1, kafka_skip=True)

instance.save(**save_kwargs)
update_fields = ["some_fields"]

instance.save(update_fields=update_fields)

# save sets kafka_skip value to False
self.assertFalse(instance.kafka_skip)
# didn't forget to call super
super_mock.assert_called_once_with()
# kafka_skip added to the update fields
# kafka_skip added to update_fields
super_mock.return_value.save.assert_called_once_with(
**{
**save_kwargs,
"update_fields": ["kafka_skip", *save_kwargs["update_fields"]],
},
force_insert=False,
force_update=False,
using=None,
update_fields=["kafka_skip", *update_fields],
)

@patch("django_kafka.models.super")
def test_save_update_fields_in_kwargs_with_kafka_skip(self, super_mock):
"""
kafka_skip should not be changed if provided in update_fields
"""
save_kwargs = {
"update_fields": ["some_field", "kafka_skip"],
}
update_fields = ["some_field", "kafka_skip"]

instance = self.model(pk=1, kafka_skip=True)

instance.save(**save_kwargs)
instance.save(update_fields=update_fields)

# save does not change kafka_skip value
self.assertTrue(instance.kafka_skip)
# didn't forget to call super
super_mock.assert_called_once_with()
# save kwargs are not changed
super_mock.return_value.save.assert_called_once_with(**save_kwargs)
# update_fields are not changed
super_mock.return_value.save.assert_called_once_with(
force_insert=False,
force_update=False,
using=None,
update_fields=update_fields,
)

@patch("django_kafka.models.super")
def test_queryset_update_sets_kafka_skip(self, super_mock):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "django-kafka"
version = "0.0.2"
version = "0.0.3"
dependencies = [
"django>=4.0,<6.0",
"confluent-kafka[avro, schema-registry]==2.4.0"
Expand Down

0 comments on commit d97053f

Please sign in to comment.