Skip to content
This repository has been archived by the owner on May 12, 2022. It is now read-only.

Commit

Permalink
renamed the user preference name and added the assertNumQueries to ac…
Browse files Browse the repository at this point in the history
…cessor methods
  • Loading branch information
Muhammad Shoaib committed Apr 1, 2015
1 parent 10e58b1 commit dbdce9f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 37 deletions.
2 changes: 1 addition & 1 deletion edx_notifications/stores/sql/store_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def get_user_preference(self, user_id, name):

return obj.to_data_object()

def set_user_reference(self, user_preference):
def set_user_preference(self, user_preference):
"""
Will save (create or update) a UserNotificationPreference in the
StorageProvider
Expand Down
77 changes: 45 additions & 32 deletions edx_notifications/stores/sql/tests/test_store_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ def _save_notification_preference(self, name, display_name, display_description=
display_description=display_description
)

test_notification_preference_saved = self.provider.save_notification_preference(test_notification_preference)
with self.assertNumQueries(3):
test_notification_preference_saved = self.provider.save_notification_preference(test_notification_preference)

self.assertIsNotNone(test_notification_preference_saved)
self.assertIsNotNone(test_notification_preference_saved.name)
Expand All @@ -124,7 +125,7 @@ def _save_user_notification_preference(self, preference_name, user_id, value):
value=value
)

user_notification_preference = self.provider.set_user_reference(test_user_notification_preference)
user_notification_preference = self.provider.set_user_preference(test_user_notification_preference)

self.assertIsNotNone(user_notification_preference)
self.assertIsNotNone(user_notification_preference.user_id)
Expand Down Expand Up @@ -913,27 +914,31 @@ def test_save_notification_preference(self):
display_description="This is the test preference"
)

notification_preference_read = self.provider.get_notification_preference(
notification_preference.name
)
with self.assertNumQueries(1):
notification_preference_read = self.provider.get_notification_preference(
notification_preference.name
)
self.assertEqual(notification_preference, notification_preference_read)

def test_save_update_notification_preference(self):
"""
test update the saved notification preference
"""
notification_preference = self._save_notification_preference(
name='test_notification_preference',
display_name="Test Preference",
display_description="This is the test preference"
)
with self.assertNumQueries(3):
notification_preference = self._save_notification_preference(
name='test_notification_preference',
display_name="Test Preference",
display_description="This is the test preference"
)

notification_preference.display_name = 'Updated Test Preference'
notification_preference_saved_twice = self.provider.save_notification_preference(notification_preference)
with self.assertNumQueries(3):
notification_preference_saved_twice = self.provider.save_notification_preference(notification_preference)

notification_preference_read = self.provider.get_notification_preference(
notification_preference_saved_twice.name
)
with self.assertNumQueries(1):
notification_preference_read = self.provider.get_notification_preference(
notification_preference_saved_twice.name
)
self.assertEqual(notification_preference_saved_twice, notification_preference_read)

def test_get_all_notification_preferences(self):
Expand All @@ -952,7 +957,9 @@ def test_get_all_notification_preferences(self):
display_description="This is the second test preference"
)

all_notification_preferences = self.provider.get_all_notification_preferences()
with self.assertNumQueries(1):
all_notification_preferences = self.provider.get_all_notification_preferences()

self.assertEqual(len(all_notification_preferences), 2)
self.assertEqual(all_notification_preferences[0], test_notification_preference)
self.assertEqual(all_notification_preferences[1], test2_notification_preference)
Expand All @@ -975,10 +982,11 @@ def test_get_saved_user_preference(self):
user_id=1,
value='User Preference 1'
)
read_user_notification_preference = self.provider.get_user_preference(
user_notification_preference.user_id,
user_notification_preference.preference.name
)
with self.assertNumQueries(2):
read_user_notification_preference = self.provider.get_user_preference(
user_notification_preference.user_id,
user_notification_preference.preference.name
)
self.assertEqual(user_notification_preference, read_user_notification_preference)

def test_update_saved_user_preference(self):
Expand All @@ -991,11 +999,15 @@ def test_update_saved_user_preference(self):
value='User Preference 1')
user_notification_preferences.value = 'Updated user Preference'
user_notification_preferences.user_id = 2
updated_user_preferences = self.provider.set_user_reference(user_notification_preferences)
read_updated_user_notification_preferences = self.provider.get_user_preference(
updated_user_preferences.user_id,
updated_user_preferences.preference.name
)

with self.assertNumQueries(2):
updated_user_preferences = self.provider.set_user_preference(user_notification_preferences)

with self.assertNumQueries(2):
read_updated_user_notification_preferences = self.provider.get_user_preference(
updated_user_preferences.user_id,
updated_user_preferences.preference.name
)
self.assertEqual(user_notification_preferences, read_updated_user_notification_preferences)

def test_get_non_existing_user_notification_preferences(self):
Expand All @@ -1018,8 +1030,8 @@ def test_get_all_user_preferences(self):
user_id=user_id,
value='User Preferences'
)

result = self.provider.get_all_user_preferences_for_user(user_id)
with self.assertNumQueries(6):
result = self.provider.get_all_user_preferences_for_user(user_id)
self.assertEqual(len(result), 5)

def test_get_all_user_preferences_with_name(self):
Expand All @@ -1039,12 +1051,13 @@ def test_get_all_user_preferences_with_name(self):
)

# make sure we can't pass along a huge limit size
with self.assertRaises(ValueError):
self.provider.get_all_user_preferences_with_name(
name='test_preference',
value='User-Preferences',
size=const.USER_PREFERENCE_MAX_LIST_SIZE + 1
)
with self.assertNumQueries(0):
with self.assertRaises(ValueError):
self.provider.get_all_user_preferences_with_name(
name='test_preference',
value='User-Preferences',
size=const.USER_PREFERENCE_MAX_LIST_SIZE + 1
)

# test limit, we should only get the first one
with self.assertNumQueries(2):
Expand Down
2 changes: 1 addition & 1 deletion edx_notifications/stores/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def get_user_preference(self, user_id, name):
raise NotImplementedError()

@abc.abstractmethod
def set_user_reference(self, user_preference):
def set_user_preference(self, user_preference):
"""
Will save (create or update) a UserNotificationPreference in the
StorageProvider
Expand Down
6 changes: 3 additions & 3 deletions edx_notifications/stores/tests/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ def get_all_notification_preferences(self):
"""
super(BadImplementationStoreProvider, self).get_all_notification_preferences()

def set_user_reference(self, user_preference):
def set_user_preference(self, user_preference):
"""
Fake implementation of method which calls base class, which should throw NotImplementedError
"""
super(BadImplementationStoreProvider, self).set_user_reference(user_preference)
super(BadImplementationStoreProvider, self).set_user_preference(user_preference)

def get_notification_message_by_id(self, msg_id, options=None):
"""
Expand Down Expand Up @@ -274,7 +274,7 @@ def test_base_methods_exceptions(self):
bad_provider.get_user_preference(None, None)

with self.assertRaises(NotImplementedError):
bad_provider.set_user_reference(None)
bad_provider.set_user_preference(None)

with self.assertRaises(NotImplementedError):
bad_provider.get_all_user_preferences_for_user(None)
Expand Down

0 comments on commit dbdce9f

Please sign in to comment.