<commit_before>
from __future__ import absolute_import

from datetime import timedelta, time, date

from django.conf import settings
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.utils.timezone import now

from base.models import User, UserProfile, ProfileRequest
from farnsworth import pre_fill
from managers.models import Manager
from utils.variables import MESSAGES
from workshift.fill import REGULAR_WORKSHIFTS, WEEK_LONG, HUMOR_WORKSHIFTS, \
    BATHROOM_WORKSHIFTS
from workshift.models import *
from workshift.forms import *
from workshift.fields import DAY_CHOICES
from workshift.cron import CollectBlownCronJob, UpdateWeeklyStandings
from workshift import utils, signals

class TestStart(TestCase):
    """
    Tests the behavior of the workshift website before any semester has been
    initialized. Also tests that initializing a semester works correctly.
    """
    def setUp(self):
        self.u = User.objects.create_user(username="u", password="<PASSWORD>")
        self.wu = User.objects.create_user(username="wu", password="<PASSWORD>")

        self.wu.first_name, self.wu.last_name = "Cooperative", "User"
        self.wu.save()

        self.wm = Manager.objects.create(
            title="Workshift Manager",
            incumbent=UserProfile.objects.get(user=self.wu),
            workshift_manager=True,
            )

        self.assertTrue(self.client.login(username="wu", password="<PASSWORD>"))

    def test_unauthenticated(self):
        self.client.logout()
        url = reverse("workshift:view_semester")
        response = self.client.get(url, follow=True)
        self.assertRedirects(response, reverse("login") + "?next=" + url)

    def test_before(self):
        url = reverse("workshift:view_semester")
        response = self.client.get(url, follow=True)
        self.assertRedirects(response, reverse("workshift:start_semester"))

        self.client.logout()
        self.assertTrue(self.client.login(username="u", password="<PASSWORD>"))

        response = self.client.get(url, follow=True)
        self.assertRedirects(response, reverse("homepage"))

    def test_start(self):
        url = reverse("workshift:start_semester")
        response = self.client.post(url, {
            "semester-season": Semester.SUMMER,
            "semester-year": 2014,
            "semester-rate": 13.30,
            "semester-policy": "http://bsc.coop",
            "semester-start_date": date(2014, 5, 22),
            "semester-end_date": date(2014, 8, 15),
        }, follow=True)

        self.assertRedirects(response, reverse("workshift:manage"))

        self.assertEqual(
            1,
            Semester.objects.filter(year=2014).filter(season=Semester.SUMMER).count(),
            )

        semester = Semester.objects.get(year=2014, season=Semester.SUMMER)

        self.assertEqual(
            2,
            WorkshiftProfile.objects.filter(semester=semester).count(),
            )
        self.assertEqual(
            1,
            WorkshiftPool.objects.filter(semester=semester).count(),
            )

        pool = WorkshiftPool.objects.get(semester=semester)

        self.assertEqual(PoolHours.objects.filter(pool=pool).count(), 2)

        pool_hours = PoolHours.objects.filter(pool=pool)

        for profile in WorkshiftProfile.objects.filter(semester=semester):
            self.assertEqual(1, profile.pool_hours.count())
            self.assertIn(profile.pool_hours.all()[0], pool_hours)
            self.assertEqual(1, profile.pool_hours.filter(pool=pool).count())

class TestAssignment(TestCase):
    """
    Test the functionality of workshift.utils.auto_assign_shifts. This should
    include respecting member's shift preferences and schedules.
    """
    def setUp(self):
        self.u = User.objects.create_user(username="u0")
        self.semester = Semester.objects.create(
            year=2014,
            season=Semester.SUMMER,
            start_date=date(2014, 5, 25),
            end_date=date(2014, 8, 16),
        )
        self.profile = WorkshiftProfile.objects.get(
            user=self.u,
            semester=self.semester,
        )
        self.p1 = WorkshiftPool.objects.get(
            is_primary=True,
            semester=self.semester,
        )
        self.p2 = WorkshiftPool.objects.create(
            title="Alternate Workshift",
            semester=self.semester,
        )
        self.wtype1 = WorkshiftType.objects.create(
            title="Like Type",
        )
        self.wtype2 = WorkshiftType.objects.create(
            title="Indifferent Type",
        )
        self.wtype3 = WorkshiftType.objects.create(
            title="Dislike Type",
        )
        preference1 = WorkshiftRating.objects.create(
            rating=WorkshiftRating.LIKE,
            workshift_type=self.wtype1,
        )
        preference2 = WorkshiftRating.objects.create(
            rating=WorkshiftRating.INDIFFERENT,
            workshift_type=self.wtype2,
        )
        preference3 = WorkshiftRating.objects.create(
            rating=WorkshiftRating.DISLIKE,
            workshift_type=self.wtype3,
        )
        self.profile.ratings = [preference1, preference2, preference3]
        self.profile.save()
        utils.make_workshift_pool_hours(semester=self.semester)

    def test_auto_assign_one(self):
        """
        Assign one shift to a member.
        """
        shift1 = RegularWorkshift.objects.create(
            workshift_type=self.wtype1,
            pool=self.p1,
            hours=5,
            )
        unfinished = utils.auto_assign_shifts(self.semester)
        self.assertEqual([], unfinished)
        self.assertIn(self.profile, shift1.current_assignees.all())

    def test_pre_assigned(self):
        """
        Test that assignment behaves correctly when members are already assigned
        to other workshifts.
        """
        print "test_pre_assigned"
        shift1 = RegularWorkshift.objects.create(
            workshift_type=self.wtype1,
            pool=self.p1,
            hours=5,
        )
        shift2 = RegularWorkshift.objects.create(
            workshift_type=self.wtype3,
            pool=self.p1,
            hours=1,
        )
        shift2.current_assignees = [self.profile]
        shift2.save()
        unfinished = utils.auto_assign_shifts(self.semester)
        self.assertEqual([self.profile], unfinished)
        self.assertNotIn(self.profile, shift1.current_assignees.all())

    def test_auto_assign_one_overflow(self):<commit_msg>Removed debugging statement<commit_after>
from __future__ import absolute_import

from datetime import timedelta, time, date

from django.conf import settings
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.utils.timezone import now

from base.models import User, UserProfile, ProfileRequest
from farnsworth import pre_fill
from managers.models import Manager
from utils.variables import MESSAGES
from workshift.fill import REGULAR_WORKSHIFTS, WEEK_LONG, HUMOR_WORKSHIFTS, \
    BATHROOM_WORKSHIFTS
from workshift.models import *
from workshift.forms import *
from workshift.fields import DAY_CHOICES
from workshift.cron import CollectBlownCronJob, UpdateWeeklyStandings
from workshift import utils, signals

class TestStart(TestCase):
    """
    Tests the behavior of the workshift website before any semester has been
    initialized. Also tests that initializing a semester works correctly.
    """
    def setUp(self):
        self.u = User.objects.create_user(username="u", password="<PASSWORD>")
        self.wu = User.objects.create_user(username="wu", password="<PASSWORD>")

        self.wu.first_name, self.wu.last_name = "Cooperative", "User"
        self.wu.save()

        self.wm = Manager.objects.create(
            title="Workshift Manager",
            incumbent=UserProfile.objects.get(user=self.wu),
            workshift_manager=True,
            )

        self.assertTrue(self.client.login(username="wu", password="<PASSWORD>"))

    def test_unauthenticated(self):
        self.client.logout()
        url = reverse("workshift:view_semester")
        response = self.client.get(url, follow=True)
        self.assertRedirects(response, reverse("login") + "?next=" + url)

    def test_before(self):
        url = reverse("workshift:view_semester")
        response = self.client.get(url, follow=True)
        self.assertRedirects(response, reverse("workshift:start_semester"))

        self.client.logout()
        self.assertTrue(self.client.login(username="u", password="<PASSWORD>"))

        response = self.client.get(url, follow=True)
        self.assertRedirects(response, reverse("homepage"))

    def test_start(self):
        url = reverse("workshift:start_semester")
        response = self.client.post(url, {
            "semester-season": Semester.SUMMER,
            "semester-year": 2014,
            "semester-rate": 13.30,
            "semester-policy": "http://bsc.coop",
            "semester-start_date": date(2014, 5, 22),
            "semester-end_date": date(2014, 8, 15),
        }, follow=True)

        self.assertRedirects(response, reverse("workshift:manage"))

        self.assertEqual(
            1,
            Semester.objects.filter(year=2014).filter(season=Semester.SUMMER).count(),
            )

        semester = Semester.objects.get(year=2014, season=Semester.SUMMER)

        self.assertEqual(
            2,
            WorkshiftProfile.objects.filter(semester=semester).count(),
            )
        self.assertEqual(
            1,
            WorkshiftPool.objects.filter(semester=semester).count(),
            )

        pool = WorkshiftPool.objects.get(semester=semester)

        self.assertEqual(PoolHours.objects.filter(pool=pool).count(), 2)

        pool_hours = PoolHours.objects.filter(pool=pool)

        for profile in WorkshiftProfile.objects.filter(semester=semester):
            self.assertEqual(1, profile.pool_hours.count())
            self.assertIn(profile.pool_hours.all()[0], pool_hours)
            self.assertEqual(1, profile.pool_hours.filter(pool=pool).count())

class TestAssignment(TestCase):
    """
    Test the functionality of workshift.utils.auto_assign_shifts. This should
    include respecting member's shift preferences and schedules.
    """
    def setUp(self):
        self.u = User.objects.create_user(username="u0")
        self.semester = Semester.objects.create(
            year=2014,
            season=Semester.SUMMER,
            start_date=date(2014, 5, 25),
            end_date=date(2014, 8, 16),
        )
        self.profile = WorkshiftProfile.objects.get(
            user=self.u,
            semester=self.semester,
        )
        self.p1 = WorkshiftPool.objects.get(
            is_primary=True,
            semester=self.semester,
        )
        self.p2 = WorkshiftPool.objects.create(
            title="Alternate Workshift",
            semester=self.semester,
        )
        self.wtype1 = WorkshiftType.objects.create(
            title="Like Type",
        )
        self.wtype2 = WorkshiftType.objects.create(
            title="Indifferent Type",
        )
        self.wtype3 = WorkshiftType.objects.create(
            title="Dislike Type",
        )
        preference1 = WorkshiftRating.objects.create(
            rating=WorkshiftRating.LIKE,
            workshift_type=self.wtype1,
        )
        preference2 = WorkshiftRating.objects.create(
            rating=WorkshiftRating.INDIFFERENT,
            workshift_type=self.wtype2,
        )
        preference3 = WorkshiftRating.objects.create(
            rating=WorkshiftRating.DISLIKE,
            workshift_type=self.wtype3,
        )
        self.profile.ratings = [preference1, preference2, preference3]
        self.profile.save()
        utils.make_workshift_pool_hours(semester=self.semester)

    def test_auto_assign_one(self):
        """
        Assign one shift to a member.
        """
        shift1 = RegularWorkshift.objects.create(
            workshift_type=self.wtype1,
            pool=self.p1,
            hours=5,
            )
        unfinished = utils.auto_assign_shifts(self.semester)
        self.assertEqual([], unfinished)
        self.assertIn(self.profile, shift1.current_assignees.all())

    def test_pre_assigned(self):
        """
        Test that assignment behaves correctly when members are already assigned
        to other workshifts.
        """
        shift1 = RegularWorkshift.objects.create(
            workshift_type=self.wtype1,
            pool=self.p1,
            hours=5,
        )
        shift2 = RegularWorkshift.objects.create(
            workshift_type=self.wtype3,
            pool=self.p1,
            hours=1,
        )
        shift2.current_assignees = [self.profile]
        shift2.save()
        unfinished = utils.auto_assign_shifts(self.semester)
        self.assertEqual([self.profile], unfinished)
        self.assertNotIn(self.profile, shift1.current_assignees.all())

    def test_auto_assign_one_overflow(self):
{'fraction_non_alphanumeric': 0.10412844036697248, 'fraction_numerical': 0.013149847094801223, 'mean_word_length': 2.494790275180337, 'pattern_counts': {'":': 12, '<': 11, '<?xml version=': 0, '>': 11, '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': '1831348', 'n_tokens_mistral': 3966, 'n_tokens_neox': 3714, 'n_words': 655}