def test_unique_by_email_user_selection(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 first_name='a'.
        users_and_watches = [
            (user(first_name='a', email='hi'), [watch()]),
            (user(email='hi'), [watch()]),
            (user(), [watch(email='hi')]),

            (user(), [watch(email='mid')]),
            (user(first_name='a', email='mid'), [watch()]),
            (user(), [watch(email='mid')]),

            (user(), [watch(email='lo')]),
            (user(), [watch(email='lo')]),
            (user(first_name='a', email='lo'), [watch()]),

            (user(), [watch(email='none', secret='a')]),
            (user(), [watch(email='none')])]

        favorites = list(_unique_by_email(users_and_watches))
        eq_(4, len(favorites))

        # Test that we chose the correct users, where there are distinguishable
        # (registered) users to choose from:
        eq_(['a'] * 3, [u.first_name for u, w in favorites[:3]])
    def test_unique_by_email_watch_collection(self):
        """Ensure _unique_by_email() collects all watches in each cluster."""
        w1, w2, w3 = watch(), watch(), watch(email='hi')
        w4, w5, w6 = watch(), watch(), watch(email='lo')
        users_and_watches = [(user(email='hi'), [w1]),
                             (user(email='hi'), [w2]), (user(), [w3]),
                             (user(email='lo'), [w4]),
                             (user(email='lo'), [w5]), (user(), [w6])]
        result = list(_unique_by_email(users_and_watches))

        _, watches = result[0]
        self.assertEqual(set([w1, w2, w3]), set(watches))

        # Make sure the watches accumulator gets cleared between clusters:
        _, watches = result[1]
        self.assertEqual(set([w4, w5, w6]), set(watches))
    def test_unique_by_email_watch_collection(self):
        """Make sure _unique_by_email() collects all watches in each cluster."""
        w1, w2, w3 = watch(), watch(), watch(email='hi')
        w4, w5, w6 = watch(), watch(), watch(email='lo')
        users_and_watches = [
            (user(email='hi'), [w1]),
            (user(email='hi'), [w2]),
            (user(), [w3]),

            (user(email='lo'), [w4]),
            (user(email='lo'), [w5]),
            (user(), [w6])]
        result = list(_unique_by_email(users_and_watches))

        _, watches = result[0]
        eq_(set([w1, w2, w3]), set(watches))

        # Make sure the watches accumulator gets cleared between clusters:
        _, watches = result[1]
        eq_(set([w4, w5, w6]), set(watches))
    def test_unique_by_email_user_selection(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 first_name='a'.
        users_and_watches = [(user(first_name='a', email='hi'), [watch()]),
                             (user(email='hi'), [watch()]),
                             (user(), [watch(email='hi')]),
                             (user(), [watch(email='mid')]),
                             (user(first_name='a', email='mid'), [watch()]),
                             (user(), [watch(email='mid')]),
                             (user(), [watch(email='lo')]),
                             (user(), [watch(email='lo')]),
                             (user(first_name='a', email='lo'), [watch()]),
                             (user(), [watch(email='none', secret='a')]),
                             (user(), [watch(email='none')])]

        favorites = list(_unique_by_email(users_and_watches))
        self.assertEqual(4, len(favorites))

        # Test that we chose the correct users, where there are distinguishable
        # (registered) users to choose from:
        self.assertEqual(['a'] * 3, [u.first_name for u, w in favorites[:3]])