"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""

from django.test import TestCase

import models

class MailingListTreeTestCase(TestCase):
    '''Test mailing lists member resolution in a classical tree setup.'''
    def setUp(self):
        self.users = []
        self.mls = []
        for i in range(20):
            self.users.append(models.User.objects.create(username='%s' % i))
        sublist = []
        for i in range(19, -1, -1):
            self.mls.insert(0, models.MailingList.objects.create(
                    name='%s' % i))
            self.mls[0].members = [self.users[i]]
            self.mls[0].mailing_list_members = sublist
            sublist = [ self.mls[0] ]

    def test_mailing_list_recursive(self):
        """
        Tests that 1 + 1 always equals 2.
        """
        for i in range(19, -1, -1):
            members = self.mls[i].recursive_members()
            self.assertEqual(len(members), 20-i)

    def test_mailing_list_recursive_with_origin(self):
        """
        Tests that 1 + 1 always equals 2.
        """
        for i in range(19, -1, -1):
            mwo = self.mls[i].recursive_members_with_origin()
            self.assertEqual(len(mwo), 20-i)
            for j in range(i, 20):
                self.assertEqual(mwo[self.users[j]], set(self.mls[i:j+1]))


class MailingListCycle(TestCase):
    '''Test mailing lists member resolution in a cyclic setup.'''
    def setUp(self):
        self.users = []
        self.mls = []
        for i in range(20):
            self.users.append(models.User.objects.create(username='%s' % i))
        sublist = []
        for i in range(19, -1, -1):
            self.mls.insert(0, models.MailingList.objects.create(
                    name='%s' % i))
            self.mls[0].members = [self.users[i]]
            self.mls[0].mailing_list_members = sublist
            sublist = [ self.mls[0] ]
        self.mls[19].mailing_list_members = [ self.mls[0] ]

    def test_mailing_list_recursive(self):
        """
        Tests that 1 + 1 always equals 2.
        """
        for i in range(19, -1, -1):
            members = self.mls[i].recursive_members()
            self.assertEqual(len(members), 20)

    def test_mailing_list_recursive_with_origin(self):
        """
        Tests that 1 + 1 always equals 2.
        """
        for i in range(19, -1, -1):
            mwo = self.mls[i].recursive_members_with_origin()
            self.assertEqual(len(mwo), 20)
            for j in range(i, 20):
                self.assertEqual(mwo[self.users[j]], set(self.mls))
