Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions packages/django-google-spanner/django_spanner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,15 @@ def autofield_init(self, *args, **kwargs):
== "true"
):
self.default = gen_rand_int64
self.db_returning = False
self.validators = []
break


AutoField.__init__ = autofield_init
AutoField.db_returning = False
AutoField.validators = []

SmallAutoField.__init__ = autofield_init
BigAutoField.__init__ = autofield_init
SmallAutoField.db_returning = False
BigAutoField.db_returning = False
SmallAutoField.validators = []
BigAutoField.validators = []


def get_prep_value(self, value):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,19 +408,27 @@ def test_autofield_no_default(self):
"""Spanner, default is not provided."""
field = AutoField(name="field_name")
assert gen_rand_int64 == field.default
# db_returning must be explicitly False because Spanner is handling ID generation client-side
assert getattr(field, "db_returning", True) is False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency and clarity, it's better to access the db_returning attribute directly. The AutoField class defines this attribute, so it will always be present on the field instance. Using getattr with a default is unnecessary and makes the assertion slightly harder to read compared to a direct access.

Suggested change
assert getattr(field, "db_returning", True) is False
assert field.db_returning is False


def test_autofield_default(self):
"""Spanner, default provided."""
mock_func = mock.Mock()
field = AutoField(name="field_name", default=mock_func)
assert gen_rand_int64 != field.default
assert mock_func == field.default
# A default was already provided, so Spanner does not generate random IDs client-side.
# Therefore, db_returning does not need to be overridden to False.
assert field.db_returning is True

def test_autofield_not_spanner(self):
"""Not Spanner, default not provided."""
connection.settings_dict["ENGINE"] = "another_db"
field = AutoField(name="field_name")
assert gen_rand_int64 != field.default
# db_returning should remain untouched (implicitly True) for non-Spanner databases
# so that Django retrieves the auto-increment ID correctly.
assert field.db_returning is True
connection.settings_dict["ENGINE"] = "django_spanner"

def test_autofield_not_spanner_w_default(self):
Expand All @@ -430,6 +438,8 @@ def test_autofield_not_spanner_w_default(self):
field = AutoField(name="field_name", default=mock_func)
assert gen_rand_int64 != field.default
assert mock_func == field.default
# Because it's not a Spanner database, the behavior shouldn't be altered in any way.
assert field.db_returning is True
connection.settings_dict["ENGINE"] = "django_spanner"

def test_autofield_spanner_as_non_default_db_random_generation_enabled(
Expand All @@ -441,6 +451,9 @@ def test_autofield_spanner_as_non_default_db_random_generation_enabled(
connections.settings["secondary"]["RANDOM_ID_GENERATION_ENABLED"] = "true"
field = AutoField(name="field_name")
assert gen_rand_int64 == field.default
# Since this specific connection explicitly enables client-side random generation,
# we must tell Django not to attempt retrieving the DB's returned ID.
assert getattr(field, "db_returning", True) is False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to my other comment, direct attribute access is preferred here for better readability and consistency. Since db_returning is a standard attribute of AutoField, getattr is not needed.

Suggested change
assert getattr(field, "db_returning", True) is False
assert field.db_returning is False

connections.settings["default"]["ENGINE"] = "django_spanner"
connections.settings["secondary"]["ENGINE"] = "django_spanner"
del connections.settings["secondary"]["RANDOM_ID_GENERATION_ENABLED"]
Expand All @@ -450,4 +463,7 @@ def test_autofield_random_generation_disabled(self):
connections.settings["default"]["RANDOM_ID_GENERATION_ENABLED"] = "false"
field = AutoField(name="field_name")
assert gen_rand_int64 != field.default
# Because we're delegating ID generation back to the database backend,
# Django needs to be able to retrieve the assigned ID.
assert field.db_returning is True
del connections.settings["default"]["RANDOM_ID_GENERATION_ENABLED"]
Loading