From 90c66b19986091db954eaa5a0f7b0baa18bb1161 Mon Sep 17 00:00:00 2001 From: Neil Trini Lasrado Date: Thu, 4 Jun 2015 15:42:38 +0530 Subject: [PATCH] test cases added --- .../projects/doctype/project/test_project.py | 1 + erpnext/stock/doctype/item/item.py | 10 ++++ erpnext/stock/doctype/item/test_item.py | 60 +++---------------- erpnext/stock/doctype/item/test_records.json | 5 -- .../manage_variants/manage_variants.py | 15 +---- .../manage_variants/test_manage_variants.py | 48 +++++++++++++++ .../stock_ledger_entry/stock_ledger_entry.py | 2 +- 7 files changed, 70 insertions(+), 71 deletions(-) create mode 100644 erpnext/stock/doctype/manage_variants/test_manage_variants.py diff --git a/erpnext/projects/doctype/project/test_project.py b/erpnext/projects/doctype/project/test_project.py index f69ce80824..f1be54492f 100644 --- a/erpnext/projects/doctype/project/test_project.py +++ b/erpnext/projects/doctype/project/test_project.py @@ -5,3 +5,4 @@ from __future__ import unicode_literals import frappe test_records = frappe.get_test_records('Project') +test_ignore = ["Sales Order"] diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py index c907993d65..22e6b212f5 100644 --- a/erpnext/stock/doctype/item/item.py +++ b/erpnext/stock/doctype/item/item.py @@ -12,6 +12,7 @@ from frappe.website.doctype.website_slideshow.website_slideshow import get_slide from erpnext.stock.doctype.manage_variants.manage_variants import update_variant class WarehouseNotSet(frappe.ValidationError): pass +class ItemTemplateCannotHaveStock(frappe.ValidationError): pass class Item(WebsiteGenerator): website = frappe._dict( @@ -61,6 +62,7 @@ class Item(WebsiteGenerator): self.update_item_desc() self.synced_with_hub = 0 self.validate_has_variants() + self.validate_stock_for_template_must_be_zero() if not self.get("__islocal"): self.old_item_group = frappe.db.get_value(self.doctype, self.name, "item_group") @@ -338,6 +340,14 @@ class Item(WebsiteGenerator): if not self.has_variants and frappe.db.get_value("Item", self.name, "has_variants"): if frappe.db.exists("Item", {"variant_of": self.name}): frappe.throw("Item has variants.") + + def validate_stock_for_template_must_be_zero(self): + if self.has_variants: + stock_in = frappe.db.sql_list("""select warehouse from tabBin + where item_code=%s and ifnull(actual_qty, 0) > 0""", self.name) + if stock_in: + frappe.throw(_("Item Template cannot have stock and varaiants. Please remove \ + stock from warehouses {0}").format(", ".join(stock_in)), ItemTemplateCannotHaveStock) def validate_end_of_life(item_code, end_of_life=None, verbose=1): if not end_of_life: diff --git a/erpnext/stock/doctype/item/test_item.py b/erpnext/stock/doctype/item/test_item.py index 02ff714bda..9cf3c077b4 100644 --- a/erpnext/stock/doctype/item/test_item.py +++ b/erpnext/stock/doctype/item/test_item.py @@ -6,7 +6,7 @@ import unittest import frappe from frappe.test_runner import make_test_records -from erpnext.stock.doctype.item.item import WarehouseNotSet, DuplicateVariant, ItemTemplateCannotHaveStock +from erpnext.stock.doctype.item.item import WarehouseNotSet, ItemTemplateCannotHaveStock from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry test_ignore = ["BOM"] @@ -20,60 +20,14 @@ class TestItem(unittest.TestCase): item.insert() else: item = frappe.get_doc("Item", item_code) - return item - - def test_duplicate_variant(self): - item = frappe.copy_doc(test_records[11]) - item.append("variants", {"item_attribute": "Test Size", "item_attribute_value": "Small"}) - self.assertRaises(DuplicateVariant, item.insert) - + def test_template_cannot_have_stock(self): - item = self.get_item(10) - - se = make_stock_entry(item_code=item.name, target="Stores - _TC", qty=1, incoming_rate=1) - - item.has_variants = 1 - item.append("variants", {"item_attribute": "Test Size", "item_attribute_value": "Small"}) - - self.assertRaises(ItemTemplateCannotHaveStock, item.save) - - def test_variant_item_codes(self): - item = self.get_item(11) - - variants = ['_Test Variant Item-S', '_Test Variant Item-M', '_Test Variant Item-L'] - self.assertEqual(item.get_variant_item_codes(), variants) - for v in variants: - self.assertTrue(frappe.db.get_value("Item", {"variant_of": item.name, "name": v})) - - item.append("variants", {"item_attribute": "Test Colour", "item_attribute_value": "Red"}) - item.append("variants", {"item_attribute": "Test Colour", "item_attribute_value": "Blue"}) - item.append("variants", {"item_attribute": "Test Colour", "item_attribute_value": "Green"}) - - self.assertEqual(item.get_variant_item_codes(), ['_Test Variant Item-S-R', - '_Test Variant Item-S-G', '_Test Variant Item-S-B', - '_Test Variant Item-M-R', '_Test Variant Item-M-G', - '_Test Variant Item-M-B', '_Test Variant Item-L-R', - '_Test Variant Item-L-G', '_Test Variant Item-L-B']) - - self.assertEqual(item.variant_attributes['_Test Variant Item-L-R'], [['Test Size', 'Large'], ['Test Colour', 'Red']]) - self.assertEqual(item.variant_attributes['_Test Variant Item-S-G'], [['Test Size', 'Small'], ['Test Colour', 'Green']]) - - # check stock entry cannot be made - def test_stock_entry_cannot_be_made_for_template(self): - item = self.get_item(11) - - se = frappe.new_doc("Stock Entry") - se.purpose = "Material Receipt" - se.append("items", { - "item_code": item.name, - "t_warehouse": "Stores - _TC", - "qty": 1, - "incoming_rate": 1 - }) - se.insert() - self.assertRaises(ItemTemplateCannotHaveStock, se.submit) - + item = self.get_item(10) + se = make_stock_entry(item_code=item.name, target="Stores - _TC", qty=1, incoming_rate=1) + item.has_variants = 1 + self.assertRaises(ItemTemplateCannotHaveStock, item.save) + def test_default_warehouse(self): item = frappe.copy_doc(test_records[0]) item.is_stock_item = "Yes" diff --git a/erpnext/stock/doctype/item/test_records.json b/erpnext/stock/doctype/item/test_records.json index dc095c6b14..5a02c6bde5 100644 --- a/erpnext/stock/doctype/item/test_records.json +++ b/erpnext/stock/doctype/item/test_records.json @@ -273,11 +273,6 @@ "item_name": "_Test Variant Item", "stock_uom": "_Test UOM", "has_variants": 1, - "variants": [ - {"item_attribute": "Test Size", "item_attribute_value": "Small"}, - {"item_attribute": "Test Size", "item_attribute_value": "Medium"}, - {"item_attribute": "Test Size", "item_attribute_value": "Large"} - ], "apply_warehouse_wise_reorder_level": 1, "reorder_levels": [ { diff --git a/erpnext/stock/doctype/manage_variants/manage_variants.py b/erpnext/stock/doctype/manage_variants/manage_variants.py index 913c2d8136..8b1a13b0a1 100644 --- a/erpnext/stock/doctype/manage_variants/manage_variants.py +++ b/erpnext/stock/doctype/manage_variants/manage_variants.py @@ -10,7 +10,6 @@ import copy import json class DuplicateAttribute(frappe.ValidationError): pass -class ItemTemplateCannotHaveStock(frappe.ValidationError): pass class ManageVariants(Document): @@ -23,7 +22,6 @@ class ManageVariants(Document): def generate_combinations(self): self.validate_attributes() self.validate_template_item() - self.validate_stock_for_template_must_be_zero() self.validate_attribute_values() self.validate_attributes_are_unique() self.get_variant_item_codes() @@ -54,7 +52,7 @@ class ManageVariants(Document): for attribute in frappe.db.sql("""select attribute, attribute_value from `tabVariant Attribute` where parent = %s""", d): variant_attributes += attribute[1] + " | " attributes.append([attribute[0], attribute[1]]) - self.append('variants',{"variant": d, "variant_attributes": variant_attributes[: -2], "attributes": json.dumps(attributes)}) + self.append('variants',{"variant": d, "variant_attributes": variant_attributes[: -3], "attributes": json.dumps(attributes)}) def validate_attributes(self): if not self.attributes: @@ -64,17 +62,10 @@ class ManageVariants(Document): template_item = frappe.get_doc("Item", self.item) if not template_item.has_variants: frappe.throw(_("Selected Item cannot have Variants.")) - + if template_item.variant_of: frappe.throw(_("Item cannot be a variant of a variant")) - def validate_stock_for_template_must_be_zero(self): - stock_in = frappe.db.sql_list("""select warehouse from tabBin - where item_code=%s and ifnull(actual_qty, 0) > 0""", self.item) - if stock_in: - frappe.throw(_("Item Template cannot have stock and varaiants. Please remove \ - stock from warehouses {0}").format(", ".join(stock_in)), ItemTemplateCannotHaveStock) - def validate_attribute_values(self): attributes = {} for d in self.attributes: @@ -119,7 +110,7 @@ class ManageVariants(Document): for d in _my_attributes: variant_attributes += d[1] + " | " self.append('variants', {"variant": item_code + "-" + value.abbr, - "attributes": json.dumps(_my_attributes), "variant_attributes": variant_attributes[: -2]}) + "attributes": json.dumps(_my_attributes), "variant_attributes": variant_attributes[: -3]}) add_attribute_suffixes(self.item, [], attributes) def sync_variants(self): diff --git a/erpnext/stock/doctype/manage_variants/test_manage_variants.py b/erpnext/stock/doctype/manage_variants/test_manage_variants.py new file mode 100644 index 0000000000..e3624a29ad --- /dev/null +++ b/erpnext/stock/doctype/manage_variants/test_manage_variants.py @@ -0,0 +1,48 @@ +# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors +# License: GNU General Public License v3. See license.txt + +from __future__ import unicode_literals +import unittest +import frappe + +from erpnext.stock.doctype.manage_variants.manage_variants import DuplicateAttribute + +class TestManageVariants(unittest.TestCase): + def test_variant_item_codes(self): + manage_variant = frappe.new_doc("Manage Variants") + manage_variant.update({ + "item": "_Test Variant Item", + "attributes": [ + { + "attribute": "Test Size", + "attribute_value": "Small" + }, + { + "attribute": "Test Size", + "attribute_value": "Large" + } + ] + }) + manage_variant.generate_combinations() + self.assertEqual(manage_variant.variants[0].variant, "_Test Variant Item-S") + self.assertEqual(manage_variant.variants[1].variant, "_Test Variant Item-L") + + self.assertEqual(manage_variant.variants[0].variant_attributes, "Small") + self.assertEqual(manage_variant.variants[1].variant_attributes, "Large") + + def test_attributes_are_unique(self): + manage_variant = frappe.new_doc("Manage Variants") + manage_variant.update({ + "item": "_Test Variant Item", + "attributes": [ + { + "attribute": "Test Size", + "attribute_value": "Small" + }, + { + "attribute": "Test Size", + "attribute_value": "Small" + } + ] + }) + self.assertRaises(DuplicateAttribute, manage_variant.generate_combinations) diff --git a/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.py b/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.py index 74fd4d1140..de6b3a6950 100644 --- a/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.py +++ b/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.py @@ -8,7 +8,7 @@ from frappe import _ from frappe.utils import flt, getdate, add_days, formatdate from frappe.model.document import Document from datetime import date -from erpnext.stock.doctype.manage_variants.manage_variants import ItemTemplateCannotHaveStock +from erpnext.stock.doctype.item.item import ItemTemplateCannotHaveStock class StockFreezeError(frappe.ValidationError): pass