Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow default value for cloud message type to be overridden with SETTINGS #699

Closed
wants to merge 9 commits into from

Conversation

kupsum
Copy link

@kupsum kupsum commented Jan 8, 2024

Allow default value of GCMDevice.cloud_message_type to be overridden/specified in SETTINGS.

@jamaalscarlett
Copy link
Member

@kupsum thanks for this, can you please add a test to validate your change.

@kupsum
Copy link
Author

kupsum commented Jan 10, 2024

@jamaalscarlett Thanks! I've added unit tests now!

Copy link

codecov bot commented Jan 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 70.33%. Comparing base (9bf5fda) to head (9558516).
Report is 1 commits behind head on master.

❗ Current head 9558516 differs from pull request most recent head 53759b1. Consider uploading reports for the commit 53759b1 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #699      +/-   ##
==========================================
+ Coverage   70.28%   70.33%   +0.05%     
==========================================
  Files          26       26              
  Lines        1097     1099       +2     
  Branches      249      249              
==========================================
+ Hits          771      773       +2     
  Misses        288      288              
  Partials       38       38              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

jamaalscarlett
jamaalscarlett previously approved these changes Mar 10, 2024
@jamaalscarlett
Copy link
Member

sorry @kupsum can you fix the merge conflicts, and I will merge this ASAP.

@kupsum
Copy link
Author

kupsum commented Mar 16, 2024

@jamaalscarlett I have resolved the merge conflicts

@jamaalscarlett
Copy link
Member

jamaalscarlett commented Mar 31, 2024

@kupsum When I use your branch a new migration is generated. Please include it in your PR.

Migrations for 'push_notifications':
  /Users/jamaalscarlett/git/personal/old-mac/git/django-push-notifications-demos/django-push-notifications-server/venv/lib/python3.8/site-packages/push_notifications/migrations/0010_alter_gcmdevice_options_and_more.py
    - Change Meta options on gcmdevice
    - Alter field cloud_message_type on gcmdevice

@jkoestinger
Copy link
Contributor

jkoestinger commented Apr 10, 2024

Hi there, I'm no reviewer so I'm not sure if I should leave a comment directly on the codebase, but I gave it a look since I opened #714 , and I think this line should be changed as follow (not calling the default):

choices=CLOUD_MESSAGE_TYPES, default=get_default_cloud_message_type

Otherwise it's effectively not doing anything unless we change the settings, and then it would complain that changes are not reflected in migrations again.

If it's done and has its migration for it, then it would superseed #714 and I can close it.

@kupsum kupsum force-pushed the master branch 2 times, most recently from d046596 to 9156078 Compare April 12, 2024 16:32
@kupsum
Copy link
Author

kupsum commented Apr 13, 2024

@jamaalscarlett I've added the missing migrations!

@jkoestinger Thank you for your effort! I've added the migration here.

@jamaalscarlett
Copy link
Member

Good catch @jkoestinger

("GCM", "Google Cloud Message"),
],
default="FCM",
help_text="You should choose FCM or GCM",
Copy link

Choose a reason for hiding this comment

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

Could you please rebase and recreate this migration? The help_text changed in #702

@@ -98,7 +102,7 @@ class GCMDevice(Device):
registration_id = models.TextField(verbose_name=_("Registration ID"), unique=SETTINGS["UNIQUE_REG_ID"])
cloud_message_type = models.CharField(
verbose_name=_("Cloud Message Type"), max_length=3,
choices=CLOUD_MESSAGE_TYPES, default="FCM",
choices=CLOUD_MESSAGE_TYPES, default=get_default_cloud_message_type(),
Copy link

Choose a reason for hiding this comment

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

Suggested change
choices=CLOUD_MESSAGE_TYPES, default=get_default_cloud_message_type(),
choices=CLOUD_MESSAGE_TYPES, default=get_default_cloud_message_type,

having the callable method as default makes it easier to override the setting in tests. also requires the regeneration of the migration

Comment on lines +492 to +499
def _validate_device_cloud_message_type(self, expected_cloud_message_type: str) -> None:
# Reload the models so that cached model is evicted and
# field default value is re-read from settings
import push_notifications.models
importlib.reload(push_notifications.models)
from push_notifications.models import GCMDevice
field_object = GCMDevice._meta.get_field("cloud_message_type")
self.assertEqual(field_object.default, expected_cloud_message_type)
Copy link

@tuky tuky Apr 17, 2024

Choose a reason for hiding this comment

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

all of this should become obsolete and can be removed after switching to the callable default.

Copy link

@tuky tuky Apr 17, 2024

Choose a reason for hiding this comment

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

instead I would recommend to create model instances in each of the tests and assert they have been created with the correct cloud_message_type (see #699 (comment))

("FCM", "Firebase Cloud Message"),
("GCM", "Google Cloud Message"),
],
default="FCM",
Copy link

Choose a reason for hiding this comment

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

actually, this line is the main reason for a callable default. otherwise the migration would be hard coded to "FCM" and projects using a different default in their settings would complain about a missing migration.

Comment on lines +478 to +490
@override_settings()
def test_cloud_message_type_is_set_to_gcm(self) -> None:
settings.PUSH_NOTIFICATIONS_SETTINGS.update({
"DEFAULT_CLOUD_MESSAGE_TYPE": "GCM",
})
self._validate_device_cloud_message_type(expected_cloud_message_type="GCM")

@override_settings()
def test_cloud_message_type_is_set_to_fcm(self) -> None:
settings.PUSH_NOTIFICATIONS_SETTINGS.update({
"DEFAULT_CLOUD_MESSAGE_TYPE": "FCM",
})
self._validate_device_cloud_message_type(expected_cloud_message_type="FCM")
Copy link

Choose a reason for hiding this comment

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

Suggested change
@override_settings()
def test_cloud_message_type_is_set_to_gcm(self) -> None:
settings.PUSH_NOTIFICATIONS_SETTINGS.update({
"DEFAULT_CLOUD_MESSAGE_TYPE": "GCM",
})
self._validate_device_cloud_message_type(expected_cloud_message_type="GCM")
@override_settings()
def test_cloud_message_type_is_set_to_fcm(self) -> None:
settings.PUSH_NOTIFICATIONS_SETTINGS.update({
"DEFAULT_CLOUD_MESSAGE_TYPE": "FCM",
})
self._validate_device_cloud_message_type(expected_cloud_message_type="FCM")
@override_settings()
def test_cloud_message_type_is_set_to_gcm(self) -> None:
settings.PUSH_NOTIFICATIONS_SETTINGS.update({
"DEFAULT_CLOUD_MESSAGE_TYPE": "GCM",
})
device = GCMDevice.objects.create(registration_id="a valid registration id")
self.assertEqual(device.cloud_message_type, "GCM")
@override_settings()
def test_cloud_message_type_is_set_to_fcm(self) -> None:
settings.PUSH_NOTIFICATIONS_SETTINGS.update({
"DEFAULT_CLOUD_MESSAGE_TYPE": "FCM",
})
device = GCMDevice.objects.create(registration_id="a valid registration id")
self.assertEqual(device.cloud_message_type, "FCM")

@kupsum
Copy link
Author

kupsum commented Apr 20, 2024

I'm closing this PR. Now that FCM is default, my goal is done. I was trying to it with a backwards compatible way but I'm fine the current approach. Thank you everyone who spent time on this PR!

@kupsum kupsum closed this Apr 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants