<commit_before>from newsletter.models import *
from newsletter.forms import *

from utils import *

class MailingTestCase(MailTestCase):
    def setUp(self):
        self.n = Newsletter(title='Test newsletter',
                            slug='test-newsletter',
                            sender='<NAME>',
                            email='<EMAIL>')
        self.n.save()
        self.n.site = get_default_sites()
        
        self.m = Message(title='Test message',
                         newsletter=self.n)
        self.m.save()
        
        self.s = Subscription(name='<NAME>', email='<EMAIL>', newsletter=self.n, subscribed=True)
        self.s.save()
        

class ArticleTestCase(MailingTestCase):
    def make_article(self):
        a = Article()
        a.title = 'Test title'
        a.text = 'This should be a very long text with <html> in it as well.'
        a.post = self.m
        a.save()
        
        return a

    def update(self, article):
        return Article.objects.get(pk=article.pk)
        
    def test_article(self):
        self.make_article()        
    
    def test_sortorder_defaults(self):
        total = 3
        
        last = 0
        for current in xrange(total):
            a = self.make_article()
            if last:
                self.assert_(a.sortorder > last)
            last = a.sortorder
        
    
    def test_moving(self):
        a1 = self.make_article()
        a1o = a1.sortorder
        
        a2 = self.make_article()
        a2o = a2.sortorder

        a3 = self.make_article()
        a3o = a3.sortorder
        
        a1.move_down()
        self.assertEqual(self.update(a1).sortorder, a2o)
        self.assertEqual(self.update(a2).sortorder, a1o)
        self.assertEqual(self.update(a3).sortorder, a3o)
        
        a1.move_up()
        self.assertEqual(self.update(a1).sortorder, a1o)
        self.assertEqual(self.update(a2).sortorder, a2o)
        self.assertEqual(self.update(a3).sortorder, a3o)
        
        a1.move_up()
        self.assertEqual(self.update(a1).sortorder, a1o)
        self.assertEqual(self.update(a2).sortorder, a2o)
        self.assertEqual(self.update(a3).sortorder, a3o)
        
        a3.move_down()
        self.assertEqual(self.update(a1).sortorder, a1o)
        self.assertEqual(self.update(a2).sortorder, a2o)
        self.assertEqual(self.update(a3).sortorder, a3o)


class CreateSubmissionTestCase(MailingTestCase):
    def test_subscription(self):
        self.assertEqual(self.s.get_recipient(), '<NAME> <<EMAIL>>')
        
    def test_submission_from_message(self):        
        sub = Submission.from_message(self.m)
        
        subscriptions = sub.subscriptions.all()
        self.assertEqual(list(subscriptions), [self.s,])
        
        self.assertFalse(sub.prepared)
        self.assertFalse(sub.sent)
        self.assertFalse(sub.sending)
    
    def test_submission_unsubscribed(self):
        self.s.subscribed=False
        self.s.save()
        
        sub = Submission.from_message(self.m)
        
        subscriptions = sub.subscriptions.all()
        self.assertEqual(list(subscriptions), [])
    
    def test_submission_unsubscribed(self):
        self.s.unsubscribed=True
        self.s.save()
        
        sub = Submission.from_message(self.m)
        
        subscriptions = sub.subscriptions.all()
        self.assertEqual(list(subscriptions), [])
    
    def test_submission_unsubscribed_unsubscribed(self):
        self.s.subscribed=False
        self.s.unsubscribed=True
        self.s.save()
        
        sub = Submission.from_message(self.m)
        
        subscriptions = sub.subscriptions.all()
        self.assertEqual(list(subscriptions), [])
    
    def test_twosubmissions(self):
        s2 = Subscription(name='<NAME>', email='<EMAIL>', newsletter=self.n, subscribed=True)
        s2.save()
        
        sub = Submission.from_message(self.m)
        
        subscriptions = sub.subscriptions.all()
        self.assert_(self.s in list(subscriptions))
        self.assert_(s2 in list(subscriptions))
    
    def test_twosubmissions(self):
        s2 = Subscription(name='<NAME>', email='<EMAIL>', newsletter=self.n, subscribed=False)
        s2.save()
        
        sub = Submission.from_message(self.m)
        
        subscriptions = sub.subscriptions.all()
        self.assertEqual(list(subscriptions), [self.s] )

class SubmitSubmissionTestCase(MailingTestCase):
    def setUp(self):
        super(SubmitSubmissionTestCase, self).setUp()
        
        self.sub = Submission.from_message(self.m)
        self.sub.save()
    
    def test_submission(self):
        self.assertFalse(self.sub.prepared)
        self.assertFalse(self.sub.sent)
        self.assertFalse(self.sub.sending)
    
    def test_submitsubmission(self):
        self.sub.prepared=True
        self.sub.save()
        
        self.sub.submit()
        
        self.assert_(self.sub.sent)
        self.assertFalse(self.sub.sending)
<commit_msg>First test for UserSubscription.<commit_after>from newsletter.models import *
from newsletter.forms import *

from utils import *

class MailingTestCase(MailTestCase):
    def setUp(self):
        self.n = Newsletter(title='Test newsletter',
                            slug='test-newsletter',
                            sender='<NAME>',
                            email='<EMAIL>')
        self.n.save()
        self.n.site = get_default_sites()
        
        self.m = Message(title='Test message',
                         newsletter=self.n)
        self.m.save()
        
        self.s = Subscription(name='<NAME>', email='<EMAIL>', newsletter=self.n, subscribed=True)
        self.s.save()
        

class ArticleTestCase(MailingTestCase):
    def make_article(self):
        a = Article()
        a.title = 'Test title'
        a.text = 'This should be a very long text with <html> in it as well.'
        a.post = self.m
        a.save()
        
        return a

    def update(self, article):
        return Article.objects.get(pk=article.pk)
        
    def test_article(self):
        self.make_article()        
    
    def test_sortorder_defaults(self):
        total = 3
        
        last = 0
        for current in xrange(total):
            a = self.make_article()
            if last:
                self.assert_(a.sortorder > last)
            last = a.sortorder
        
    
    def test_moving(self):
        a1 = self.make_article()
        a1o = a1.sortorder
        
        a2 = self.make_article()
        a2o = a2.sortorder

        a3 = self.make_article()
        a3o = a3.sortorder
        
        a1.move_down()
        self.assertEqual(self.update(a1).sortorder, a2o)
        self.assertEqual(self.update(a2).sortorder, a1o)
        self.assertEqual(self.update(a3).sortorder, a3o)
        
        a1.move_up()
        self.assertEqual(self.update(a1).sortorder, a1o)
        self.assertEqual(self.update(a2).sortorder, a2o)
        self.assertEqual(self.update(a3).sortorder, a3o)
        
        a1.move_up()
        self.assertEqual(self.update(a1).sortorder, a1o)
        self.assertEqual(self.update(a2).sortorder, a2o)
        self.assertEqual(self.update(a3).sortorder, a3o)
        
        a3.move_down()
        self.assertEqual(self.update(a1).sortorder, a1o)
        self.assertEqual(self.update(a2).sortorder, a2o)
        self.assertEqual(self.update(a3).sortorder, a3o)


class CreateSubmissionTestCase(MailingTestCase):
    def test_subscription(self):
        self.assertEqual(self.s.get_recipient(), '<NAME> <<EMAIL>>')
        
    def test_submission_from_message(self):        
        sub = Submission.from_message(self.m)
        
        subscriptions = sub.subscriptions.all()
        self.assertEqual(list(subscriptions), [self.s,])
        
        self.assertFalse(sub.prepared)
        self.assertFalse(sub.sent)
        self.assertFalse(sub.sending)
    
    def test_submission_unsubscribed(self):
        self.s.subscribed=False
        self.s.save()
        
        sub = Submission.from_message(self.m)
        
        subscriptions = sub.subscriptions.all()
        self.assertEqual(list(subscriptions), [])
    
    def test_submission_unsubscribed(self):
        self.s.unsubscribed=True
        self.s.save()
        
        sub = Submission.from_message(self.m)
        
        subscriptions = sub.subscriptions.all()
        self.assertEqual(list(subscriptions), [])
    
    def test_submission_unsubscribed_unsubscribed(self):
        self.s.subscribed=False
        self.s.unsubscribed=True
        self.s.save()
        
        sub = Submission.from_message(self.m)
        
        subscriptions = sub.subscriptions.all()
        self.assertEqual(list(subscriptions), [])
    
    def test_twosubmissions(self):
        s2 = Subscription(name='<NAME>', email='<EMAIL>', newsletter=self.n, subscribed=True)
        s2.save()
        
        sub = Submission.from_message(self.m)
        
        subscriptions = sub.subscriptions.all()
        self.assert_(self.s in list(subscriptions))
        self.assert_(s2 in list(subscriptions))
    
    def test_twosubmissions(self):
        s2 = Subscription(name='<NAME>', email='<EMAIL>', newsletter=self.n, subscribed=False)
        s2.save()
        
        sub = Submission.from_message(self.m)
        
        subscriptions = sub.subscriptions.all()
        self.assertEqual(list(subscriptions), [self.s] )

class SubmitSubmissionTestCase(MailingTestCase):
    def setUp(self):
        super(SubmitSubmissionTestCase, self).setUp()
        
        self.sub = Submission.from_message(self.m)
        self.sub.save()
    
    def test_submission(self):
        self.assertFalse(self.sub.prepared)
        self.assertFalse(self.sub.sent)
        self.assertFalse(self.sub.sending)
    
    def test_submitsubmission(self):
        self.sub.prepared=True
        self.sub.save()
        
        self.sub.submit()
        
        self.assert_(self.sub.sent)
        self.assertFalse(self.sub.sending)

class UserSubscriptionTestCase(MailingTestCase):
    def setUp(self):
       super(UserSubscriptionTestCase, self).setUp()
       
       self.u = User.objects.all()[0]
       
    def test_subscription(self):
        s = Subscription(user=self.u, newsletter=self.n)
        s.save()
        
        self.assertEqual(s.name, self.u.get_full_name())
        self.assertEqual(s.email, self.u.email)

    

{'fraction_non_alphanumeric': 0.11440839694656489, 'fraction_numerical': 0.00849236641221374, 'mean_word_length': 2.0432636469221834, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '500236', 'n_tokens_mistral': 3239, 'n_tokens_neox': 3212, 'n_words': 477}