# -*- coding: utf-8 -*-
"""Test of usecase CreateJob"""
from datetime import timedelta

import factory
from django.test import TestCase
from django.utils import timezone

# To create fake data
from faker import Factory

from posts.models import Job
from posts.internal_services.services import CreateJobService

faker = Factory.create()


# TODO: Creation of all fields, not only required ones.

# ------------------------------------------------------------------------------
# |            Test Creation, actual creation through services                 |
# ------------------------------------------------------------------------------


class JobFactory(factory.DjangoModelFactory):
    class Meta:
        model = Job

    title = faker.word()
    email = faker.email()
    date_start = timezone.now()
    date_end = timezone.now() + timedelta(hours=3)
    amount_to_pay = faker.random_number(1, 100)

    @classmethod
    def _create(cls, model_class, *args, **kwargs):
        """Override the default ``_create`` with our custom call."""
        if 'id' in kwargs.keys():
            kwargs.pop('id')
        service = CreateJobService()
        return service.create_job(**kwargs)


class JobCreationServiceTest(TestCase):
    """
        Test creation of Job model, but using directly the models method. So only
        validations declared in the definition of the model are taken into account.
        The service module is the one that we are going to use to create Jobs, so
        that is the one that should be tested completely.
    """

    def setUp(self, **kwargs):
        self.start = timezone.now()
        self.end = timezone.now() + timedelta(hours=3)

    def test_simple_creation(self):
        errors, _ = JobFactory()
        assert errors is ''

    def test_job_field_not_modified_on_creation(self):
        import datetime
        import pytz

        aware_start = datetime.datetime(2011, 8, 15, 8, 15, 12, 0, pytz.UTC)
        aware_end = datetime.datetime(2012, 8, 15, 8, 15, 12, 0, pytz.UTC)
        errors, data = JobFactory(
            title="my faked title",
            date_start=aware_start,
            date_end=aware_end,
            amount_to_pay=10,
            email='fake@gmail.com',
            slug='asd',
        )

        assert errors is ''
        j = data.get('data')

        self.assertTrue(isinstance(j, Job))
        self.assertEqual(j.title, "my faked title")
        self.assertEqual(j.date_start, aware_start)
        self.assertEqual(j.date_end, aware_end)
        self.assertEqual(j.amount_to_pay, 10)
        self.assertEqual(j.email, 'fake@gmail.com')

    def test_creation(self):
        long_str = "this is ignored and this is one veeeeeeeeeeeeeeeeeeee \
            eeeeeeeeeeeeeery long slug, it should be ignored, i guess this \
            has more than 157 chars 123456789123456789123456789123456789"
        # Invalid slugs, long slugs, no slugs. Slug is autogenerated, so no
        # matter what we pass there, it should be ok
        self.assertTrue(JobFactory(slug="a/s")[1])
        self.assertTrue(JobFactory(slug="this is ignored")[1])
        self.assertTrue(JobFactory(slug=long_str)[1])

        # ## All failing cases ###

        # Title very long
        e, j = JobFactory(title=long_str)
        assert e is not ''
        # No email
        e, j = JobFactory(email=None)
        assert e is not ''
        # No date_start
        e, j = JobFactory(date_start=None)
        assert e is not ''
        # No date_end
        e, j = JobFactory(date_end=None)
        assert e is not ''
        # No amount_to_pay
        e, j = JobFactory(amount_to_pay=None)
        assert e is not ''
