from freezegun import freeze_time
from rest_framework import test

from waldur_mastermind.billing.tests.utils import get_financial_report_url
from waldur_mastermind.invoices import models as invoice_models
from waldur_mastermind.invoices.tests import factories as invoice_factories
from waldur_mastermind.invoices.tests import fixtures as invoice_fixtures


@freeze_time('2017-01-10')
class PriceCurrentTest(test.APITransactionTestCase):
    def setUp(self):
        self.fixture = invoice_fixtures.InvoiceFixture()

        invoice_factories.InvoiceItemFactory(
            invoice=self.fixture.invoice,
            project=self.fixture.project,
            unit=invoice_models.InvoiceItem.Units.PER_MONTH,
            unit_price=100,
            quantity=1,
        )
        invoice_factories.InvoiceItemFactory(
            invoice=self.fixture.invoice,
            project=self.fixture.project,
            unit=invoice_models.InvoiceItem.Units.PER_DAY,
            unit_price=3,
            quantity=31,
        )

    def test_current_price(self):
        self.client.force_authenticate(self.fixture.staff)
        url = get_financial_report_url(self.fixture.project.customer)
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = response.json()
        self.assertEqual(data['billing_price_estimate']['current'], 100 + 9 * 3)
        diff = (
            data['billing_price_estimate']['total']
            - data['billing_price_estimate']['current']
        )
        self.assertEqual(diff, 22 * 3)

[((10, 1, 10, 26), 'freezegun.freeze_time', 'freeze_time', ({(10, 13, 10, 25): '"""2017-01-10"""'}, {}), "('2017-01-10')", False, 'from freezegun import freeze_time\n'), ((13, 23, 13, 56), 'waldur_mastermind.invoices.tests.fixtures.InvoiceFixture', 'invoice_fixtures.InvoiceFixture', ({}, {}), '()', True, 'from waldur_mastermind.invoices.tests import fixtures as invoice_fixtures\n'), ((15, 8, 21, 9), 'waldur_mastermind.invoices.tests.factories.InvoiceItemFactory', 'invoice_factories.InvoiceItemFactory', (), '', True, 'from waldur_mastermind.invoices.tests import factories as invoice_factories\n'), ((22, 8, 28, 9), 'waldur_mastermind.invoices.tests.factories.InvoiceItemFactory', 'invoice_factories.InvoiceItemFactory', (), '', True, 'from waldur_mastermind.invoices.tests import factories as invoice_factories\n'), ((32, 14, 32, 69), 'waldur_mastermind.billing.tests.utils.get_financial_report_url', 'get_financial_report_url', ({(32, 39, 32, 68): 'self.fixture.project.customer'}, {}), '(self.fixture.project.customer)', False, 'from waldur_mastermind.billing.tests.utils import get_financial_report_url\n')]