test: fix NegativeStockError (#24936)

This commit is contained in:
Sagar Vora 2021-03-18 16:36:40 +05:30 committed by GitHub
parent 78e576ee6a
commit b134b89fb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 52 deletions

View File

@ -6,10 +6,12 @@ from __future__ import unicode_literals
import frappe
import unittest
test_dependencies = ["Customer", "Supplier"]
from frappe.cache_manager import clear_doctype_cache
from frappe.custom.doctype.property_setter.property_setter import make_property_setter
from erpnext.accounts.doctype.opening_invoice_creation_tool.opening_invoice_creation_tool import get_temporary_opening_account
test_dependencies = ["Customer", "Supplier"]
class TestOpeningInvoiceCreationTool(unittest.TestCase):
def setUp(self):
if not frappe.db.exists("Company", "_Test Opening Invoice Company"):
@ -24,6 +26,7 @@ class TestOpeningInvoiceCreationTool(unittest.TestCase):
def test_opening_sales_invoice_creation(self):
property_setter = make_property_setter("Sales Invoice", "update_stock", "default", 1, "Check")
try:
invoices = self.make_invoices(company="_Test Opening Invoice Company")
self.assertEqual(len(invoices), 2)
@ -39,7 +42,9 @@ class TestOpeningInvoiceCreationTool(unittest.TestCase):
# Check if update stock is not enabled
self.assertEqual(si.update_stock, 0)
finally:
property_setter.delete()
clear_doctype_cache("Sales Invoice")
def check_expected_values(self, invoices, expected_value, invoice_type="Sales"):
doctype = "Sales Invoice" if invoice_type == "Sales" else "Purchase Invoice"

View File

@ -5,7 +5,7 @@ from __future__ import unicode_literals
import frappe
import unittest, os, json
from frappe.utils import cstr
from frappe.utils import cstr, cint
from erpnext.erpnext_integrations.connectors.shopify_connection import create_order
from erpnext.erpnext_integrations.doctype.shopify_settings.sync_product import make_item
from erpnext.erpnext_integrations.doctype.shopify_settings.sync_customer import create_customer
@ -13,9 +13,14 @@ from frappe.core.doctype.data_import.data_import import import_doc
class ShopifySettings(unittest.TestCase):
def setUp(self):
@classmethod
def setUpClass(cls):
frappe.set_user("Administrator")
cls.allow_negative_stock = cint(frappe.db.get_value('Stock Settings', None, 'allow_negative_stock'))
if not cls.allow_negative_stock:
frappe.db.set_value('Stock Settings', None, 'allow_negative_stock', 1)
# use the fixture data
import_doc(frappe.get_app_path("erpnext", "erpnext_integrations/doctype/shopify_settings/test_data/custom_field.json"))
@ -24,9 +29,15 @@ class ShopifySettings(unittest.TestCase):
frappe.reload_doctype("Delivery Note")
frappe.reload_doctype("Sales Invoice")
self.setup_shopify()
cls.setup_shopify()
def setup_shopify(self):
@classmethod
def tearDownClass(cls):
if not cls.allow_negative_stock:
frappe.db.set_value('Stock Settings', None, 'allow_negative_stock', 0)
@classmethod
def setup_shopify(cls):
shopify_settings = frappe.get_doc("Shopify Settings")
shopify_settings.taxes = []
@ -56,21 +67,20 @@ class ShopifySettings(unittest.TestCase):
"delivery_note_series": "DN-"
}).save(ignore_permissions=True)
self.shopify_settings = shopify_settings
cls.shopify_settings = shopify_settings
def test_order(self):
### Create Customer ###
# Create Customer
with open (os.path.join(os.path.dirname(__file__), "test_data", "shopify_customer.json")) as shopify_customer:
shopify_customer = json.load(shopify_customer)
create_customer(shopify_customer.get("customer"), self.shopify_settings)
### Create Item ###
# Create Item
with open (os.path.join(os.path.dirname(__file__), "test_data", "shopify_item.json")) as shopify_item:
shopify_item = json.load(shopify_item)
make_item("_Test Warehouse - _TC", shopify_item.get("product"))
### Create Order ###
# Create Order
with open (os.path.join(os.path.dirname(__file__), "test_data", "shopify_order.json")) as shopify_order:
shopify_order = json.load(shopify_order)
@ -80,17 +90,17 @@ class ShopifySettings(unittest.TestCase):
self.assertEqual(cstr(shopify_order.get("order").get("id")), sales_order.shopify_order_id)
#check for customer
# Check for customer
shopify_order_customer_id = cstr(shopify_order.get("order").get("customer").get("id"))
sales_order_customer_id = frappe.get_value("Customer", sales_order.customer, "shopify_customer_id")
self.assertEqual(shopify_order_customer_id, sales_order_customer_id)
#check sales invoice
# Check sales invoice
sales_invoice = frappe.get_doc("Sales Invoice", {"shopify_order_id": sales_order.shopify_order_id})
self.assertEqual(sales_invoice.rounded_total, sales_order.rounded_total)
#check delivery note
# Check delivery note
delivery_note_count = frappe.db.sql("""select count(*) from `tabDelivery Note`
where shopify_order_id = %s""", sales_order.shopify_order_id)[0][0]