<commit_before># coding=utf-8

from __future__ import unicode_literals

from datetime import date
from decimal import Decimal

from django.test import TestCase

from zivinetz.models import Assignment, ExpenseReport

from testapp.tests import factories
from testapp.tests.utils import admin_login


class ExpenseReportsAdminViewsTestCase(TestCase):
    def test_expensereport_list(self):
        admin_login(self)

        factories.CompensationSetFactory.create()
        for i in range(10):
            factories.AssignmentFactory.create(
                status=Assignment.MOBILIZED,
                arranged_on=date.today(),
                mobilized_on=date.today(),
            ).generate_expensereports()

        self.assertContains(
            self.client.get(ExpenseReport().urls.url('list')),
            'class="batch"',
            50)

        response = self.client.get(ExpenseReport().urls.url('pdf'))
        self.assertEqual(response['content-type'], 'application/pdf')
        self.assertEqual(
            response['content-disposition'],
            'attachment; filename="expense-statistics.pdf"')

    def test_expensereport_editing(self):
        admin_login(self)

        factories.CompensationSetFactory.create()
        factories.AssignmentFactory.create(
            status=Assignment.MOBILIZED,
            date_from=date(2014, 9, 1),
            date_until=date(2014, 9, 26),
            arranged_on=date(2014, 9, 1),
            mobilized_on=date(2014, 9, 1),
        ).generate_expensereports()

        report = ExpenseReport.objects.get()

        self.assertAlmostEqual(report.total, Decimal('130.00'))

        state = {
            'working_days': 20,
            'free_days': 6,
            'sick_days': 0,
            'holi_days': 0,
            'forced_leave_days': 0,
        }
        for type, days in state.items():
            self.assertEqual(getattr(report, type), days)
        self.assertEqual(report.calculated_total_days, 26)

        base_data = {
            'date_from': report.date_from,
            'date_until': report.date_until,
            'report_no': report.report_no,
            'status': report.status,
            'specification': report.specification.id,

            'clothing_expenses': report.clothing_expenses,
            'transport_expenses': report.transport_expenses,
            'miscellaneous': report.miscellaneous,
        }

        data = {}
        data.update(base_data)
        data.update(state)

        response = self.client.post(report.urls.url('edit'), data)
        self.assertRedirects(response, report.urls.url('detail'))

        del data['holi_days']
        response = self.client.post(report.urls.url('edit'), data)
        self.assertEqual(response.status_code, 200)

        state['holi_days'] = 2
        data.update(state)
        response = self.client.post(report.urls.url('edit'), data)
        self.assertContains(response, 'id="id_ignore_warnings"', 1)
<commit_msg>Test that it it not allowed to edit paid expense reports<commit_after># coding=utf-8

from __future__ import unicode_literals

from datetime import date
from decimal import Decimal

from django.test import TestCase

from zivinetz.models import Assignment, ExpenseReport

from testapp.tests import factories
from testapp.tests.utils import (
    admin_login, get_messages, model_to_postable_dict)


class ExpenseReportsAdminViewsTestCase(TestCase):
    def test_expensereport_list(self):
        admin_login(self)

        factories.CompensationSetFactory.create()
        for i in range(10):
            factories.AssignmentFactory.create(
                status=Assignment.MOBILIZED,
                arranged_on=date.today(),
                mobilized_on=date.today(),
            ).generate_expensereports()

        self.assertContains(
            self.client.get(ExpenseReport().urls.url('list')),
            'class="batch"',
            50)

        response = self.client.get(ExpenseReport().urls.url('pdf'))
        self.assertEqual(response['content-type'], 'application/pdf')
        self.assertEqual(
            response['content-disposition'],
            'attachment; filename="expense-statistics.pdf"')

    def _create_report(self):
        factories.CompensationSetFactory.create()
        factories.AssignmentFactory.create(
            status=Assignment.MOBILIZED,
            date_from=date(2014, 9, 1),
            date_until=date(2014, 9, 26),
            arranged_on=date(2014, 9, 1),
            mobilized_on=date(2014, 9, 1),
        ).generate_expensereports()
        return ExpenseReport.objects.get()

    def test_expensereport_editing_allowed(self):
        report = self._create_report()
        admin_login(self)
        self.assertEqual(
            self.client.get(report.urls.url('detail')).status_code,
            200)
        self.assertEqual(
            self.client.get(report.urls.url('edit')).status_code,
            200)

        report.status = report.PAID
        report.save()

        response = self.client.get(
            report.urls.url('edit'),
            follow=True)

        self.assertRedirects(
            response,
            'http://testserver%s' % report.urls.url('detail'))

        self.assertEqual(
            get_messages(response),
            [])

    def test_expensereport_editing(self):
        report = self._create_report()
        self.assertAlmostEqual(report.total, Decimal('130.00'))

        state = {
            'working_days': 20,
            'free_days': 6,
            'sick_days': 0,
            'holi_days': 0,
            'forced_leave_days': 0,
        }
        for type, days in state.items():
            self.assertEqual(getattr(report, type), days)
        self.assertEqual(report.calculated_total_days, 26)

        base_data = {
            'date_from': report.date_from,
            'date_until': report.date_until,
            'report_no': report.report_no,
            'status': report.status,
            'specification': report.specification.id,

            'clothing_expenses': report.clothing_expenses,
            'transport_expenses': report.transport_expenses,
            'miscellaneous': report.miscellaneous,
        }

        data = {}
        data.update(base_data)
        data.update(state)

        admin_login(self)
        response = self.client.post(report.urls.url('edit'), data)
        self.assertRedirects(response, report.urls.url('detail'))

        del data['holi_days']
        response = self.client.post(report.urls.url('edit'), data)
        self.assertEqual(response.status_code, 200)

        state['holi_days'] = 2
        data.update(state)
        response = self.client.post(report.urls.url('edit'), data)
        self.assertContains(response, 'id="id_ignore_warnings"', 1)

{'fraction_non_alphanumeric': 0.11247051886792453, 'fraction_numerical': 0.015035377358490566, 'mean_word_length': 2.3473112974839663, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2487973', 'n_tokens_mistral': 2046, 'n_tokens_neox': 1922, 'n_words': 326}