def test_invoice_can_be_created_with_legal_entity_and_its_address(self):
     assert not Invoice.objects.exists()
     InvoiceCreator().create_invoice(self.order)
     assert Invoice.objects.exists()
     invoice = Invoice.objects.first()
     # Document created and saved
     assert invoice.document is not None
 def test_str_method_of_invoice_model_instance(self):
     """
     Checks correct representation of `Invoice` instance
     (e.g. in invoices list in the admin site).
     """
     order_number = '0000042'
     order = create_order(number=order_number, user=self.user)
     invoice = InvoiceCreator().create_invoice(order)
     assert str(invoice) == 'Invoice #{} for order #{}'.format(invoice.number, order_number)
 def test_str_method_of_invoice_model_instance_when_order_is_deleted(self):
     """
     Checks correct representation of `Invoice` instance
     (E.g. in invoices list in the admin site) when related order
     is deleted.
     """
     order_number = '0000043'
     order = create_order(number=order_number, user=self.user)
     invoice = InvoiceCreator().create_invoice(order)
     order.delete()
     invoice.refresh_from_db()
     assert str(invoice) == 'Invoice #{}'.format(invoice.number)
    def test_concurrent_invoice_creation(self):
        num_threads = 5
        creator = InvoiceCreator()

        org_create_invoice = InvoiceCreator.create_invoice

        def new_create_invoice(order, **kwargs):
            time.sleep(0.5)
            return org_create_invoice(creator, order, **kwargs)

        def worker():
            order_number = threading.current_thread().name
            order = create_order(number=order_number, user=self.user)
            new_create_invoice(order)

        exceptions = run_concurrently(worker, num_threads=num_threads)

        assert len(exceptions) == 0
        assert Invoice.objects.count() == 5
    def test_invoice_cannot_be_created_without_legal_entity_address(self):
        LegalEntityAddress.objects.all().delete()

        assert not Invoice.objects.exists()
        InvoiceCreator().create_invoice(self.order)
        self.assertFalse(Invoice.objects.exists())
    def test_invoice_cannot_be_created_without_legal_entity(self):
        LegalEntity.objects.all().delete()

        assert not Invoice.objects.exists()
        InvoiceCreator().create_invoice(self.order)
        assert not Invoice.objects.exists()
 def _test_invoice_is_created(self, order_number='000043'):
     order = create_order(number=order_number, user=self.user)
     InvoiceCreator().create_invoice(order)
     assert Invoice.objects.exists()
     invoice = Invoice.objects.first()
     return invoice
 def test_custom_invoice_model_used(self, *args, **kwargs):
     order_number = 'TEST_number_000d6'
     order = create_order(number=order_number, user=self.user)
     InvoiceCreator().create_invoice(order)
     assert CustomInvoice.objects.exists()