def test_filtered(self): """Make sure watches cull properly by additional filters.""" # A watch with just the filter we're searching for: registered_user = user(email='*****@*****.**', save=True) exact_watch = watch(event_type=TYPE, user=registered_user, save=True) watch_filter(watch=exact_watch, name='color', value=1).save() # A watch with extra filters: extra_watch = watch(event_type=TYPE, email='*****@*****.**', save=True) watch_filter(watch=extra_watch, name='color', value=1).save() watch_filter(watch=extra_watch, name='flavor', value=2).save() # A watch with no row for the filter we're searching on: watch(event_type=TYPE, email='*****@*****.**').save() # A watch with a mismatching filter--shouldn't be found mismatch_watch = watch(event_type=TYPE, email='*****@*****.**', save=True) watch_filter(watch=mismatch_watch, name='color', value=3).save() self._emails_eq(['*****@*****.**', '*****@*****.**', '*****@*****.**'], FilteredEvent(), color=1) # Search on multiple filters to test joining the filters table twice. # We provide values that match for both filters, as [email protected] # suffices to test exclusion. self._emails_eq(['*****@*****.**', '*****@*****.**', '*****@*****.**'], FilteredEvent(), color=1, flavor=2)
def test_duplicate_tolerance(self): """Assure notify() returns an existing watch if there is a matching one. Also make sure it returns only 1 watch even if there are duplicate matches. """ w1 = watch(event_type=TYPE, email='*****@*****.**', save=True) w2 = watch(event_type=TYPE, email='*****@*****.**', save=True) assert SimpleEvent.notify('*****@*****.**').pk in [w1.pk, w2.pk]
def test_mock_model(self): """Deleting an instance of MockModel should delete watches. Create instance of MockModel from notifications.tests.models, then delete it and watch the cascade go. """ mock_m = MockModel.objects.create() watch(event_type=TYPE, email='*****@*****.**', content_object=mock_m, save=True) MockModel.objects.all().delete() assert not Watch.objects.count(), 'Cascade delete failed.'
def test_new_user_claim_watches(self, get_current): """Claim user watches upon activation.""" watch(email='*****@*****.**', save=True) get_current.return_value.domain = 'su.mo.com' user = RegistrationProfile.objects.create_inactive_user( 'sumouser1234', 'testpass', '*****@*****.**') key = RegistrationProfile.objects.all()[0].activation_key self.client.get(reverse('users.activate', args=[key]), follow=True) # Watches are claimed. assert user.watch_set.exists()
def test_new_user_claim_watches(self, get_current): """Claim user watches upon activation.""" old, settings.CELERY_ALWAYS_EAGER = settings.CELERY_ALWAYS_EAGER, True get_current.return_value.domain = 'su.mo.com' watch(email='*****@*****.**', save=True) now = time() username = '******' % now user = RegistrationProfile.objects.create_inactive_user( username, 'testpass', '*****@*****.**') key = RegistrationProfile.objects.all()[0].activation_key self.client.get(reverse('users.activate', args=[key]), follow=True) # Watches are claimed. assert user.watch_set.exists() settings.CELERY_ALWAYS_EAGER = old
def test_simple(self): """Test whether a watch scoped only by event type fires for both anonymous and registered users.""" registered_user = user(email='*****@*****.**', save=True) watch(event_type=TYPE, user=registered_user).save() watch(event_type=TYPE, email='*****@*****.**').save() watch(event_type='something else', email='*****@*****.**').save() self._emails_eq(['*****@*****.**', '*****@*****.**'], SimpleEvent())
def test_duplicates(self): """Don't return duplicate email addresses.""" watch(event_type=TYPE, user=user(email='*****@*****.**', save=True), save=True) watch(event_type=TYPE, email='*****@*****.**').save() watch(event_type=TYPE, email='*****@*****.**').save() eq_(3, Watch.objects.all().count()) # We created what we meant to. self._emails_eq(['*****@*****.**'], SimpleEvent())
def test_registered_users_favored(self): """When removing duplicates, make sure registered users are kept in favor of anonymous ones having the same email address.""" def make_anonymous_watches(): for x in xrange(3): watch(event_type=TYPE, email='*****@*****.**').save() # Throw some anonymous watches in there in the hope that they would # come out on top if we didn't purposely favor registered users. # Suggestions on how to make this test more reliable are welcome. make_anonymous_watches() # File the registered watch: watch(event_type=TYPE, user=user(first_name='Jed', email='*****@*****.**', save=True)).save() # A few more anonymous watches in case the BTrees flop in the other # direction: make_anonymous_watches() users_and_watches = list(SimpleEvent()._users_watching_by_filter()) u, w = users_and_watches[0] eq_('Jed', u.first_name)
def test_merging(self): """Test that duplicate emails across multiple events get merged.""" # Remember to keep the emails in order when writing these test cases. # [Ed: But doesn't the SQL query have an ORDER BY?] watch(event_type=TYPE, email='*****@*****.**').save() watch(event_type=TYPE, email='*****@*****.**').save() registered_user = user(email='*****@*****.**', save=True) watch(event_type=ANOTHER_TYPE, user=registered_user).save() self._emails_eq(['*****@*****.**', '*****@*****.**'], EventUnion(SimpleEvent(), AnotherEvent()))
def test_duplicates_case_insensitive(self): """De-duping should compare case-insensitively.""" watch(event_type=TYPE, user=user(email='*****@*****.**', save=True), save=True) watch(event_type=TYPE, email='*****@*****.**').save() watch(event_type=TYPE, email='*****@*****.**').save() eq_(3, Watch.objects.all().count()) # We created what we meant to. addresses = [u.email for u, w in SimpleEvent()._users_watching_by_filter()] eq_(1, len(addresses)) eq_('*****@*****.**', addresses[0].lower())
def test_content_type(self): """Make sure watches filter properly by content type.""" watch_type = ContentType.objects.get_for_model(Watch) content_type_type = ContentType.objects.get_for_model(ContentType) registered_user = user(email='*****@*****.**', save=True) watch(event_type=TYPE, content_type=content_type_type, user=registered_user).save() watch(event_type=TYPE, content_type=content_type_type, email='*****@*****.**').save() watch(event_type=TYPE, content_type=watch_type, email='*****@*****.**').save() self._emails_eq(['*****@*****.**', '*****@*****.**'], ContentTypeEvent())
def test_duplicates_case_insensitive(self): """De-duping should compare case-insensitively.""" watch(event_type=TYPE, user=user(email='*****@*****.**', save=True), save=True) watch(event_type=TYPE, email='*****@*****.**').save() watch(event_type=TYPE, email='*****@*****.**').save() eq_(3, Watch.objects.all().count()) # We created what we meant to. addresses = [ u.email for u, w in SimpleEvent()._users_watching_by_filter() ] eq_(1, len(addresses)) eq_('*****@*****.**', addresses[0].lower())
def test_anonymous_only(self): """Make sure having mixed watches claims right ones.""" # Watch some before registering. watch(email='*****@*****.**', save=True) watch(email='*****@*****.**', save=True) watch(email='*****@*****.**', save=True) # Register. u = user(email='*****@*****.**', save=True) claim_watches(u) # Original anonymous watch is claimed. assert not Watch.objects.filter(email='*****@*****.**').exists() eq_(2, Watch.objects.filter(email=None).count()) eq_(2, Watch.objects.filter(user=u).count()) # No other watches are affected. assert Watch.objects.filter(email='*****@*****.**').exists()
def test_inactive(self): """Make sure inactive watches don't fire.""" watch(event_type=TYPE, email='*****@*****.**', is_active=False).save() watch(event_type=TYPE, email='*****@*****.**').save() self._emails_eq(['*****@*****.**'], SimpleEvent())
def test_fire(self, _mails): """Assert firing the union gets the mails from the first event.""" watch(event_type=TYPE, email='*****@*****.**').save() EventUnion(SimpleEvent(), AnotherEvent()).fire() assert _mails.called
def test_unique_by_email(self): """Test the routine that sorts through users and watches having the same email addresses.""" # Test the best in a cluster coming first, in the middle, and last. # We mark the correct choices with secret='a'. users_and_watches = [ (user(email='hi'), watch(secret='a')), (user(email='hi'), watch()), (user(), watch(email='hi')), (user(), watch(email='mid')), (user(email='mid'), watch(secret='a')), (user(), watch(email='mid')), (user(), watch(email='lo')), (user(), watch(email='lo')), (user(email='lo'), watch(secret='a')), (user(), watch(email='none', secret='a')), (user(), watch(email='none')), (user(), watch(email='hi', secret='a')), (user(), watch(email='hi')), (user(), watch(email='hi'))] favorites = list(_unique_by_email(users_and_watches)) num_clusters = 5 eq_(num_clusters, len(favorites)) eq_(['a'] * num_clusters, [w.secret for u, w in favorites])
def _users_watching(self): return [(user(email='*****@*****.**'), watch()), (user(email='*****@*****.**'), watch())]
def _users_watching(self): return [(user(email='*****@*****.**'), watch())]
def make_anonymous_watches(): for x in xrange(3): watch(event_type=TYPE, email='*****@*****.**').save()
def test_unique_by_email(self): """Test the routine that sorts through users and watches having the same email addresses.""" # Test the best in a cluster coming first, in the middle, and last. # We mark the correct choices with secret='a'. users_and_watches = [(user(email='hi'), watch(secret='a')), (user(email='hi'), watch()), (user(), watch(email='hi')), (user(), watch(email='mid')), (user(email='mid'), watch(secret='a')), (user(), watch(email='mid')), (user(), watch(email='lo')), (user(), watch(email='lo')), (user(email='lo'), watch(secret='a')), (user(), watch(email='none', secret='a')), (user(), watch(email='none')), (user(), watch(email='hi', secret='a')), (user(), watch(email='hi')), (user(), watch(email='hi'))] favorites = list(_unique_by_email(users_and_watches)) num_clusters = 5 eq_(num_clusters, len(favorites)) eq_(['a'] * num_clusters, [w.secret for u, w in favorites])