import json

from django.utils import timezone
from rest_framework import status
from rest_framework.reverse import reverse
from rest_framework.test import APITestCase

from silver.tests.factories import (AdminUserFactory, CustomerFactory,
                                    ProviderFactory)


class TestProformaEndpoints(APITestCase):
    def setUp(self):
        admin_user = AdminUserFactory.create()
        self.client.force_authenticate(user=admin_user)

    def test_post_invoice_without_invoice_entries(self):
        CustomerFactory.create()
        ProviderFactory.create()
        url = reverse('proforma-list')

        data = {
            'provider': 'http://testserver/providers/1/',
            'customer': 'http://testserver/customers/1/',
            'number': "",
            'currency': 'RON',
            'proforma_entries': []
        }

        response = self.client.post(url, data=data)
        assert response.status_code == status.HTTP_201_CREATED
        assert response.data == {
            "id": 1,
            "series": "ProformaSeries",
            "number": 1,
            "provider": "http://testserver/providers/1/",
            "customer": "http://testserver/customers/1/",
            "archived_provider": {},
            "archived_customer": {},
            "due_date": None,
            "issue_date": None,
            "paid_date": None,
            "cancel_date": None,
            "sales_tax_name": "VAT",
            "sales_tax_percent": '1.00',
            "currency": "RON",
            "state": "draft",
            "invoice": None,
            "proforma_entries": []
        }


@@ -473,23 +473,24 @@
 st_post_
-invoice
+proforma
 _without
@@ -490,23 +490,24 @@
 without_
-invoice
+proforma
 _entries
@@ -1605,24 +1605,867 @@
 entries%22: %5B%5D%0A        %7D%0A%0A
+    def test_post_proforma_with_proforma_entries(self):%0A        CustomerFactory.create()%0A        ProviderFactory.create()%0A        url = reverse('proforma-list')%0A        data = %7B%0A            'provider': 'http://testserver/providers/1/',%0A            'customer': 'http://testserver/customers/1/',%0A            'number': None,%0A            'currency': 'RON',%0A            'proforma_entries': %5B%7B%0A                %22description%22: %22Page views%22,%0A                %22unit_price%22: 10.0,%0A                %22quantity%22: 20%0A            %7D%5D%0A        %7D%0A%0A        response = self.client.post(url, data=json.dumps(data),%0A                                    content_type='application/json')%0A%0A        assert response.status_code == status.HTTP_201_CREATED%0A        # TODO: Check the body of the response. There were some problems%0A        # related to the invoice_entries list.%0A

Add test_post_proforma_with_proforma_entries