__author__ = 'traviswarren'

import datetime

from django.test import TestCase

from train_track.tests.model_factory import UserProfileFactory, UserProfileEventFactory
from train_track.apps.profile.models import UserProfile


class ProfileManagerTestCases(TestCase):
    def test_query_set(self):
        female = UserProfileFactory(sex=UserProfile.FEMALE)
        male = UserProfileFactory(sex=UserProfile.MALE)

        self.assertQuerysetEqual(UserProfile.objects.all(), [repr(male), repr(female)], ordered=False)
        self.assertQuerysetEqual(UserProfile.objects.women(), [repr(female)])
        self.assertQuerysetEqual(UserProfile.objects.men(), [repr(male)])

    def test_attendee_counts(self):
        UserProfileFactory()
        training_attendee = UserProfileFactory()

        first_event_date = datetime.datetime.now() - datetime.timedelta(weeks=10)
        second_event_date = datetime.datetime.now() - datetime.timedelta(weeks=5)
        last_event_date = datetime.datetime.now() - datetime.timedelta(weeks=1)

        UserProfileEventFactory(user_profile=training_attendee, event__event_date=first_event_date)
        UserProfileEventFactory(user_profile=training_attendee, event__event_date=second_event_date)
        UserProfileEventFactory(user_profile=training_attendee, event__event_date=last_event_date)

        result_set = UserProfile.objects.with_attendee_counts()

        self.assertEqual(result_set[0].attendance_count, 3)
        self.assertEqual(result_set[0].first_event, first_event_date.strftime("%Y-%m-%d"))
        self.assertEqual(result_set[0].last_event, last_event_date.strftime("%Y-%m-%d"))
        self.assertEqual(result_set[1].attendance_count, 0)