from django.contrib.auth.models import User
from ummeli.vlive.tests.utils import VLiveClient, VLiveTestCase
from ummeli.opportunities.models import *
from django.core.urlresolvers import reverse
from datetime import datetime, timedelta
from django.contrib.sites.models import Site


class OpportunitiesTest(VLiveTestCase):
    fixtures = [
        'vlive/tests/auth/fixtures/sample.json',
        'fixtures/opportunities.provinces.json',
        'opportunities/fixtures/test.opportunities.json',
    ]

    def setUp(self):
        self.msisdn = '27123456789'
        self.pin = '1234'
        self.client = VLiveClient(HTTP_X_UP_CALLING_LINE_ID=self.msisdn)

    def test_province_from_str(self):
        p = Province.from_str('Gauteng')
        self.assertEqual(p.get_province_display(), 'Gauteng')

        p = Province.from_str('KwaZulu Natal')
        self.assertEqual(p.get_province_display(), 'KwaZulu Natal')
        p = Province.from_str('Kwa-Zulu Natal')
        self.assertEqual(p.get_province_display(), 'KwaZulu Natal')

        p = Province.from_str('Western Cape')
        self.assertEqual(p.get_province_display(), 'Western Cape')

    def test_internship(self):
        self.login()
        self.fill_in_basic_info()

        user = User.objects.get(username=self.msisdn)

        salary = Salary(amount=50, frequency=1)
        salary.save()

        i = Internship.objects.create(title='Test op',
                                    description='This is a test',
                                    owner=user,
                                    salary=salary,
                                    state='published')
        i.province.add(2)
        i.province.add(3)
        i.save()

        self.assertEqual(user.modelbase_set.filter(slug=i.slug).count(), 1)
        self.assertEqual(user.modelbase_set.all()[0].ummeliopportunity.internship.salary.amount, 50)