-
-
Notifications
You must be signed in to change notification settings - Fork 620
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
Conversation
@kupsum thanks for this, can you please add a test to validate your change. |
@jamaalscarlett Thanks! I've added unit tests now! |
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. |
sorry @kupsum can you fix the merge conflicts, and I will merge this ASAP. |
@jamaalscarlett I have resolved the merge conflicts |
@kupsum When I use your branch a new migration is generated. Please include it in your PR.
|
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. |
d046596
to
9156078
Compare
@jamaalscarlett I've added the missing migrations! @jkoestinger Thank you for your effort! I've added the migration here. |
Good catch @jkoestinger |
("GCM", "Google Cloud Message"), | ||
], | ||
default="FCM", | ||
help_text="You should choose FCM or GCM", |
There was a problem hiding this comment.
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(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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.
@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") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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") |
I'm closing this PR. Now that |
Allow default value of
GCMDevice.cloud_message_type
to be overridden/specified inSETTINGS
.