rewrite Supplier test cases
diff --git a/erpnext/accounts/doctype/payment_term/test_records.json b/erpnext/accounts/doctype/payment_term/test_records.json
new file mode 100644
index 0000000..ef6e069
--- /dev/null
+++ b/erpnext/accounts/doctype/payment_term/test_records.json
@@ -0,0 +1,34 @@
+[
+  {
+    "doctype":"Payment Term",
+    "due_date_based_on":"Day(s) after invoice date",
+    "payment_term_name":"_Test N30",
+    "description":"_Test Net 30 Days",
+    "invoice_portion":50,
+    "credit_days":30
+  },
+  {
+    "doctype":"Payment Term",
+    "due_date_based_on":"Day(s) after invoice date",
+    "payment_term_name":"_Test COD",
+    "description":"_Test Cash on Delivery",
+    "invoice_portion":50,
+    "credit_days":0
+  },
+  {
+    "doctype":"Payment Term",
+    "due_date_based_on":"Month(s) after the end of the invoice month",
+    "payment_term_name":"_Test EONM",
+    "description":"_Test End of Next Month",
+    "invoice_portion":100,
+    "credit_months":1
+  },
+  {
+    "doctype":"Payment Term",
+    "due_date_based_on":"Day(s) after invoice date",
+    "payment_term_name":"_Test N30 1",
+    "description":"_Test Net 30 Days",
+    "invoice_portion":100,
+    "credit_days":30
+  }
+]
\ No newline at end of file
diff --git a/erpnext/accounts/doctype/payment_terms_template/test_records.json b/erpnext/accounts/doctype/payment_terms_template/test_records.json
new file mode 100644
index 0000000..fea0b35
--- /dev/null
+++ b/erpnext/accounts/doctype/payment_terms_template/test_records.json
@@ -0,0 +1,60 @@
+[
+  {
+    "doctype":"Payment Terms Template",
+    "terms":[
+          {
+            "doctype":"Payment Terms Template Detail",
+            "due_date_based_on":"Day(s) after invoice date",
+            "idx":1,
+            "description":"Cash on Delivery",
+            "invoice_portion":50,
+            "credit_days":0,
+            "credit_months":0,
+            "payment_term":"_Test COD"
+        },
+        {
+            "doctype":"Payment Terms Template Detail",
+            "due_date_based_on":"Day(s) after invoice date",
+            "idx":2,
+            "description":"Net 30 Days ",
+            "invoice_portion":50,
+            "credit_days":30,
+            "credit_months":0,
+            "payment_term":"_Test N30"
+        }
+    ],
+    "template_name":"_Test Payment Term Template"
+  },
+  {
+    "doctype":"Payment Terms Template",
+    "terms":[
+          {
+            "doctype":"Payment Terms Template Detail",
+            "due_date_based_on":"Month(s) after the end of the invoice month",
+            "idx":1,
+            "description":"_Test End of Next Months",
+            "invoice_portion":100,
+            "credit_days":0,
+            "credit_months":1,
+            "payment_term":"_Test EONM"
+        }
+    ],
+    "template_name":"_Test Payment Term Template 1"
+  },
+  {
+    "doctype":"Payment Terms Template",
+    "terms":[
+          {
+            "doctype":"Payment Terms Template Detail",
+            "due_date_based_on":"Day(s) after invoice date",
+            "idx":1,
+            "description":"_Test Net Within 30 days",
+            "invoice_portion":100,
+            "credit_days":30,
+            "credit_months":0,
+            "payment_term":"_Test N30 1"
+        }
+    ],
+    "template_name":"_Test Payment Term Template 3"
+  }
+]
\ No newline at end of file
diff --git a/erpnext/buying/doctype/supplier/test_records.json b/erpnext/buying/doctype/supplier/test_records.json
index fa728be..1c78807 100644
--- a/erpnext/buying/doctype/supplier/test_records.json
+++ b/erpnext/buying/doctype/supplier/test_records.json
@@ -1,6 +1,12 @@
 [
  {
   "doctype": "Supplier",
+  "supplier_name": "_Test Supplier With Template 1",
+  "supplier_type": "_Test Supplier Type",
+  "payment_terms": "_Test Payment Term Template 3"
+ },
+ {
+  "doctype": "Supplier",
   "supplier_name": "_Test Supplier P",
   "supplier_type": "_Test Supplier Type",
   "credit_days_based_on": "Fixed Days"
diff --git a/erpnext/buying/doctype/supplier/test_supplier.py b/erpnext/buying/doctype/supplier/test_supplier.py
index 89e0862..2af6759 100644
--- a/erpnext/buying/doctype/supplier/test_supplier.py
+++ b/erpnext/buying/doctype/supplier/test_supplier.py
@@ -5,13 +5,59 @@
 
 import frappe, unittest
 from erpnext.accounts.party import get_due_date
-from erpnext.exceptions import PartyFrozen, PartyDisabled
+from erpnext.exceptions import PartyDisabled
 from frappe.test_runner import make_test_records
+from frappe.utils import add_days, add_months, get_last_day
 
+test_dependencies = ['Payment Term', 'Payment Terms Template']
 test_records = frappe.get_test_records('Supplier')
 
 
 class TestSupplier(unittest.TestCase):
+    def test_supplier_default_payment_terms(self):
+        # Payment Term based on Days after invoice date
+        frappe.db.set_value(
+            "Supplier", "_Test Supplier With Template 1", "payment_terms", "_Test Payment Term Template 3")
+
+        due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier With Template 1")
+        self.assertEqual(due_date, "2016-02-21")
+
+        due_date = get_due_date("2017-01-22", "Supplier", "_Test Supplier With Template 1")
+        self.assertEqual(due_date, "2017-02-21")
+
+        # Payment Term based on last day of month
+        frappe.db.set_value(
+            "Supplier", "_Test Supplier With Template 1", "payment_terms", "_Test Payment Term Template 1")
+
+        due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier With Template 1")
+        self.assertEqual(due_date, "2016-02-29")
+
+        due_date = get_due_date("2017-01-22", "Supplier", "_Test Supplier With Template 1")
+        self.assertEqual(due_date, "2017-02-28")
+
+        frappe.db.set_value("Supplier", "_Test Supplier With Template 1", "payment_terms", "")
+
+        # Set credit limit for the supplier type instead of supplier and evaluate the due date
+        # frappe.db.set_value("Supplier Type", "_Test Supplier Type", "credit_days_based_on",
+        #                     "Fixed Days")
+        # frappe.db.set_value("Supplier Type", "_Test Supplier Type", "credit_days", 10)
+        #
+        # due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier", "_Test Company")
+        # self.assertEqual(due_date, "2016-02-01")
+        #
+        # # Set credit limit for the supplier type instead of supplier and evaluate the due date
+        # # based on Last day of next month
+        # frappe.db.set_value("Supplier", "_Test Supplier Type", "credit_days", 0)
+        # frappe.db.set_value("Supplier Type", "_Test Supplier Type", "credit_days_based_on",
+        #                     "Last Day of the Next Month")
+        #
+        # # Leap year
+        # due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier", "_Test Company")
+        # self.assertEqual(due_date, "2016-02-29")
+        # # Non Leap year
+        # due_date = get_due_date("2017-01-22", "Supplier", "_Test Supplier", "_Test Company")
+        # self.assertEqual(due_date, "2017-02-28")
+
     def test_supplier_disabled(self):
         make_test_records("Item")
 
diff --git a/erpnext/setup/doctype/supplier_type/test_supplier_type.js b/erpnext/setup/doctype/supplier_type/test_supplier_type.js
new file mode 100644
index 0000000..085dddd
--- /dev/null
+++ b/erpnext/setup/doctype/supplier_type/test_supplier_type.js
@@ -0,0 +1,23 @@
+/* eslint-disable */
+// rename this file from _test_[name] to test_[name] to activate
+// and remove above this line
+
+QUnit.test("test: Supplier Type", function (assert) {
+	let done = assert.async();
+
+	// number of asserts
+	assert.expect(1);
+
+	frappe.run_serially([
+		// insert a new Supplier Type
+		() => frappe.tests.make('Supplier Type', [
+			// values to be set
+			{key: 'value'}
+		]),
+		() => {
+			assert.equal(cur_frm.doc.key, 'value');
+		},
+		() => done()
+	]);
+
+});