From 4e95bb59a05929980ccb62bb2eae16fac614af04 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Tue, 5 Feb 2013 19:20:51 +0530 Subject: [PATCH 1/3] added test_runner --- hr/doctype/leave_application/leave_application.py | 2 +- hr/doctype/leave_type/leave_type.py | 9 ++++++++- setup/doctype/company/company.py | 11 +++++++++-- setup/doctype/currency/currency.py | 5 ++++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/hr/doctype/leave_application/leave_application.py b/hr/doctype/leave_application/leave_application.py index cfd6f701c5..33bb51655f 100755 --- a/hr/doctype/leave_application/leave_application.py +++ b/hr/doctype/leave_application/leave_application.py @@ -39,7 +39,7 @@ class DocType: self.validate_balance_leaves() self.validate_leave_overlap() self.validate_max_days() - #self.validate_block_days() + self.validate_block_days() def on_submit(self): if self.doc.status != "Approved": diff --git a/hr/doctype/leave_type/leave_type.py b/hr/doctype/leave_type/leave_type.py index 7f48feb2eb..5b2e5e8827 100644 --- a/hr/doctype/leave_type/leave_type.py +++ b/hr/doctype/leave_type/leave_type.py @@ -19,4 +19,11 @@ import webnotes class DocType: def __init__(self, d, dl): - self.doc, self.doclist = d, dl \ No newline at end of file + self.doc, self.doclist = d, dl + +test_records = [ + [{ + "leave_type_name": "_Test Leave Type", + "doctype": "Leave Type" + }] +] \ No newline at end of file diff --git a/setup/doctype/company/company.py b/setup/doctype/company/company.py index e1919a5fe2..b36253ed39 100644 --- a/setup/doctype/company/company.py +++ b/setup/doctype/company/company.py @@ -165,8 +165,6 @@ class DocType: ac_obj.validate() ac_obj.doc.save(1) ac_obj.on_update() - sql("commit") - sql("start transaction") # Set letter head @@ -257,3 +255,12 @@ class DocType: sql("update `tabSingles` set value = %s where doctype='Global Defaults' and field = 'default_company' and value = %s", (newdn, olddn)) if get_defaults('company') == olddn: set_default('company', newdn) + +test_records = [ + [{ + "company_name": "_Test Company", + "abbr": "TC", + "default_currency": "INR", + "doctype": "Company" + }] +] \ No newline at end of file diff --git a/setup/doctype/currency/currency.py b/setup/doctype/currency/currency.py index 7f48feb2eb..008175eea7 100644 --- a/setup/doctype/currency/currency.py +++ b/setup/doctype/currency/currency.py @@ -19,4 +19,7 @@ import webnotes class DocType: def __init__(self, d, dl): - self.doc, self.doclist = d, dl \ No newline at end of file + self.doc, self.doclist = d, dl + +# already set +test_records = [] \ No newline at end of file From c2563effbf58e8069ef90c74370dfd9c725aa737 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Tue, 5 Feb 2013 23:25:37 +0530 Subject: [PATCH 2/3] added test case for leave_application --- accounts/doctype/account/account.py | 2 + accounts/doctype/fiscal_year/fiscal_year.py | 5 ++- accounts/utils.py | 8 +++- hr/doctype/branch/branch.py | 4 +- hr/doctype/department/department.py | 8 +++- hr/doctype/designation/designation.py | 4 +- hr/doctype/employee/employee.py | 12 ++++++ hr/doctype/employment_type/employment_type.py | 5 ++- hr/doctype/grade/grade.py | 4 +- .../holiday_block_list/holiday_block_list.py | 13 ++++++ hr/doctype/holiday_list/holiday_list.py | 12 ++++++ .../leave_application/leave_application.py | 40 ++++++++++++++----- .../test_leave_application.py | 26 ++++++++++++ setup/doctype/currency/currency.py | 1 - 14 files changed, 124 insertions(+), 20 deletions(-) create mode 100644 hr/doctype/leave_application/test_leave_application.py diff --git a/accounts/doctype/account/account.py b/accounts/doctype/account/account.py index 042cd5a259..04e13d8efe 100644 --- a/accounts/doctype/account/account.py +++ b/accounts/doctype/account/account.py @@ -25,6 +25,8 @@ from webnotes import msgprint sql = webnotes.conn.sql get_value = webnotes.conn.get_value +test_records = [] + class DocType: def __init__(self,d,dl): self.doc, self.doclist = d,dl diff --git a/accounts/doctype/fiscal_year/fiscal_year.py b/accounts/doctype/fiscal_year/fiscal_year.py index e23351c3ac..d5dff6ee4b 100644 --- a/accounts/doctype/fiscal_year/fiscal_year.py +++ b/accounts/doctype/fiscal_year/fiscal_year.py @@ -30,4 +30,7 @@ class DocType: webnotes.clear_cache() msgprint(self.doc.name + _(""" is now the default Fiscal Year. \ - Please refresh your browser for the change to take effect.""")) \ No newline at end of file + Please refresh your browser for the change to take effect.""")) + +test_records = [[{"doctype":"Fiscal Year", "year":"_Test Fiscal Year", + "year_start_date":"2013-01-01"}]] \ No newline at end of file diff --git a/accounts/utils.py b/accounts/utils.py index e4d967cf50..9596cd4ca9 100644 --- a/accounts/utils.py +++ b/accounts/utils.py @@ -25,6 +25,9 @@ from webnotes.utils import formatdate class FiscalYearError(webnotes.ValidationError): pass def get_fiscal_year(date, verbose=1): + return get_fiscal_years(date, verbose=1)[0] + +def get_fiscal_years(date, verbose=1): # if year start date is 2012-04-01, year end date should be 2013-03-31 (hence subdate) fy = webnotes.conn.sql("""select name, year_start_date, subdate(adddate(year_start_date, interval 1 year), interval 1 day) @@ -38,10 +41,11 @@ def get_fiscal_year(date, verbose=1): if verbose: webnotes.msgprint(error_msg) raise FiscalYearError, error_msg - return fy[0] + return fy def validate_fiscal_year(date, fiscal_year, label="Date"): - if get_fiscal_year(date)[0] != fiscal_year: + years = [f[0] for f in get_fiscal_years(date)] + if fiscal_year not in years: webnotes.msgprint(("%(label)s '%(posting_date)s': " + _("not within Fiscal Year") + \ ": '%(fiscal_year)s'") % { "label": label, diff --git a/hr/doctype/branch/branch.py b/hr/doctype/branch/branch.py index 7f48feb2eb..8b4a24df55 100644 --- a/hr/doctype/branch/branch.py +++ b/hr/doctype/branch/branch.py @@ -19,4 +19,6 @@ import webnotes class DocType: def __init__(self, d, dl): - self.doc, self.doclist = d, dl \ No newline at end of file + self.doc, self.doclist = d, dl + +test_records = [[{"doctype":"Branch", "branch":"_Test Branch"}]] \ No newline at end of file diff --git a/hr/doctype/department/department.py b/hr/doctype/department/department.py index 7f48feb2eb..c38ee93cf5 100644 --- a/hr/doctype/department/department.py +++ b/hr/doctype/department/department.py @@ -19,4 +19,10 @@ import webnotes class DocType: def __init__(self, d, dl): - self.doc, self.doclist = d, dl \ No newline at end of file + self.doc, self.doclist = d, dl + +test_records = [ + [{"doctype":"Department", "department_name":"_Test Department"}], + [{"doctype":"Department", "department_name":"_Test Department with Block List", + "holiday_block_list": "_Test Holiday Block List"}], +] diff --git a/hr/doctype/designation/designation.py b/hr/doctype/designation/designation.py index 7f48feb2eb..d101ee317e 100644 --- a/hr/doctype/designation/designation.py +++ b/hr/doctype/designation/designation.py @@ -19,4 +19,6 @@ import webnotes class DocType: def __init__(self, d, dl): - self.doc, self.doclist = d, dl \ No newline at end of file + self.doc, self.doclist = d, dl + +test_records = [[{"doctype":"Designation", "designation_name":"_Test Designation"}]] \ No newline at end of file diff --git a/hr/doctype/employee/employee.py b/hr/doctype/employee/employee.py index ab3f842d22..4360c7e7ac 100644 --- a/hr/doctype/employee/employee.py +++ b/hr/doctype/employee/employee.py @@ -123,3 +123,15 @@ class DocType: if self.doc.status == 'Left' and not self.doc.relieving_date: msgprint("Please enter relieving date.") raise Exception + +test_records = [[{ + "doctype":"Employee", + "employee_name": "_Test Employee", + "naming_series": "_T-Employee-", + "date_of_joining": "2010-01-01", + "date_of_birth": "1980-01-01", + "gender": "Female", + "status": "Active", + "company": "_Test Company", + "user_id": "test@erpnext.com" +}]] \ No newline at end of file diff --git a/hr/doctype/employment_type/employment_type.py b/hr/doctype/employment_type/employment_type.py index 7f48feb2eb..ac34d45686 100644 --- a/hr/doctype/employment_type/employment_type.py +++ b/hr/doctype/employment_type/employment_type.py @@ -19,4 +19,7 @@ import webnotes class DocType: def __init__(self, d, dl): - self.doc, self.doclist = d, dl \ No newline at end of file + self.doc, self.doclist = d, dl + +test_records = [[{"doctype":"Employment Type", + "employee_type_name": "_Test Employment Type"}]] \ No newline at end of file diff --git a/hr/doctype/grade/grade.py b/hr/doctype/grade/grade.py index 7f48feb2eb..b1eaefa4d4 100644 --- a/hr/doctype/grade/grade.py +++ b/hr/doctype/grade/grade.py @@ -19,4 +19,6 @@ import webnotes class DocType: def __init__(self, d, dl): - self.doc, self.doclist = d, dl \ No newline at end of file + self.doc, self.doclist = d, dl + +test_records = [[{"doctype":"Grade", "grade_name":"Test Grade"}]] \ No newline at end of file diff --git a/hr/doctype/holiday_block_list/holiday_block_list.py b/hr/doctype/holiday_block_list/holiday_block_list.py index 5dcf88ecde..4ebe49e188 100644 --- a/hr/doctype/holiday_block_list/holiday_block_list.py +++ b/hr/doctype/holiday_block_list/holiday_block_list.py @@ -19,3 +19,16 @@ class DocType: if d.block_date in dates: webnotes.msgprint(_("Date is repeated") + ":" + d.block_date, raise_exception=1) dates.append(d.block_date) + +test_records = [[{ + "doctype":"Holiday Block List", + "holiday_block_list_name": "_Test Holiday Block List", + "year": "_Test Fiscal Year" + }, { + "doctype": "Holiday Block List Date", + "parent": "_Test Holiday Block List", + "parenttype": "Holiday Block List", + "parentfield": "holiday_block_list_dates", + "block_date": "2013-01-02", + "reason": "First work day" + }]] \ No newline at end of file diff --git a/hr/doctype/holiday_list/holiday_list.py b/hr/doctype/holiday_list/holiday_list.py index 7ca519fc00..00ea0bad89 100644 --- a/hr/doctype/holiday_list/holiday_list.py +++ b/hr/doctype/holiday_list/holiday_list.py @@ -90,3 +90,15 @@ class DocType: webnotes.conn.sql("""update `tabHoliday List` set is_default = 0 where ifnull(is_default, 0) = 1 and fiscal_year = %s""", (self.doc.fiscal_year,)) +test_records = [[{ + "doctype": "Holiday List", + "holiday_list_name": "_Test Holiday List", + "fiscal_year": "_Test Fiscal Year" + }, { + "doctype": "Holiday", + "parent": "_Test Holiday List", + "parenttype": "Holiday List", + "parentfield": "holiday_list_details", + "holiday_date": "2013-01-01", + "description": "New Year" + }]] diff --git a/hr/doctype/leave_application/leave_application.py b/hr/doctype/leave_application/leave_application.py index 33bb51655f..8c7124e92d 100755 --- a/hr/doctype/leave_application/leave_application.py +++ b/hr/doctype/leave_application/leave_application.py @@ -23,7 +23,6 @@ from webnotes.model import db_exists from webnotes.model.wrapper import copy_doclist from webnotes import form, msgprint -sql = webnotes.conn.sql import datetime class LeaveDayBlockedError(Exception): pass @@ -55,7 +54,7 @@ class DocType: block_list = webnotes.conn.get_value("Department", department, "holiday_block_list") if block_list: for d in webnotes.conn.sql("""select block_date, reason from - `tabHoliday Block List Date` where parent=%s""", block_list): + `tabHoliday Block List Date` where parent=%s""", block_list, as_dict=1): block_date = getdate(d.block_date) if block_date > from_date and block_date < to_date: webnotes.msgprint(_("You cannot apply for a leave on the following date because it is blocked") @@ -63,11 +62,11 @@ class DocType: raise LeaveDayBlockedError def get_holidays(self): - tot_hol = sql("""select count(*) from `tabHoliday` h1, `tabHoliday List` h2, `tabEmployee` e1 + tot_hol = webnotes.conn.sql("""select count(*) from `tabHoliday` h1, `tabHoliday List` h2, `tabEmployee` e1 where e1.name = %s and h1.parent = h2.name and e1.holiday_list = h2.name and h1.holiday_date between %s and %s""", (self.doc.employee, self.doc.from_date, self.doc.to_date)) if not tot_hol: - tot_hol = sql("""select count(*) from `tabHoliday` h1, `tabHoliday List` h2 + tot_hol = webnotes.conn.sql("""select count(*) from `tabHoliday` h1, `tabHoliday List` h2 where h1.parent = h2.name and h1.holiday_date between %s and %s and ifnull(h2.is_default,0) = 1 and h2.fiscal_year = %s""", (self.doc.from_date, self.doc.to_date, self.doc.fiscal_year)) @@ -95,13 +94,13 @@ class DocType: self.doc.leave_balance = get_leave_balance(self.doc.employee, self.doc.leave_type, self.doc.fiscal_year)["leave_balance"] self.doc.total_leave_days = self.get_total_leave_days()["total_leave_days"] - + if self.doc.leave_balance - self.doc.total_leave_days < 0: msgprint("There is not enough leave balance for Leave Type: %s" % \ (self.doc.leave_type,), raise_exception=1) def validate_leave_overlap(self): - for d in sql("""select name, leave_type, posting_date, from_date, to_date + for d in webnotes.conn.sql("""select name, leave_type, posting_date, from_date, to_date from `tabLeave Application` where (from_date <= %(to_date)s and to_date >= %(from_date)s) @@ -112,7 +111,7 @@ class DocType: msgprint("Employee : %s has already applied for %s between %s and %s on %s. Please refer Leave Application : %s" % (self.doc.employee, cstr(d['leave_type']), formatdate(d['from_date']), formatdate(d['to_date']), formatdate(d['posting_date']), d['name']), raise_exception = 1) def validate_max_days(self): - max_days = sql("select max_days_allowed from `tabLeave Type` where name = '%s'" %(self.doc.leave_type)) + max_days = webnotes.conn.sql("select max_days_allowed from `tabLeave Type` where name = '%s'" %(self.doc.leave_type)) max_days = max_days and flt(max_days[0][0]) or 0 if max_days and self.doc.total_leave_days > max_days: msgprint("Sorry ! You cannot apply for %s for more than %s days" % (self.doc.leave_type, max_days)) @@ -120,12 +119,12 @@ class DocType: @webnotes.whitelist() -def get_leave_balance(employee, leave_type, fiscal_year): +def get_leave_balance(employee, leave_type, fiscal_year): leave_all = webnotes.conn.sql("""select total_leaves_allocated from `tabLeave Allocation` where employee = %s and leave_type = %s and fiscal_year = %s and docstatus = 1""", (employee, leave_type, fiscal_year)) - + leave_all = leave_all and flt(leave_all[0][0]) or 0 leave_app = webnotes.conn.sql("""select SUM(total_leave_days) @@ -147,5 +146,24 @@ def get_approver_list(): return roles def is_lwp(leave_type): - lwp = sql("select is_lwp from `tabLeave Type` where name = %s", leave_type) - return lwp and cint(lwp[0][0]) or 0 \ No newline at end of file + lwp = webnotes.conn.sql("select is_lwp from `tabLeave Type` where name = %s", leave_type) + return lwp and cint(lwp[0][0]) or 0 + +test_records = [ + [{ + "doctype": "Leave Allocation", + "leave_type": "_Test Leave Type", + "fiscal_year": "_Test Fiscal Year", + "employee":"_T-Employee-0001", + "new_leaves_allocated": 15, + "docstatus": 1 + }], + [{ + "doctype": "Leave Application", + "leave_type": "_Test Leave Type", + "from_date": "2013-05-01", + "to_date": "2013-05-05", + "posting_date": "2013-01-02", + "fiscal_year": "_Test Fiscal Year", + "employee": "_T-Employee-0001" + }]] \ No newline at end of file diff --git a/hr/doctype/leave_application/test_leave_application.py b/hr/doctype/leave_application/test_leave_application.py new file mode 100644 index 0000000000..9d51bc1e24 --- /dev/null +++ b/hr/doctype/leave_application/test_leave_application.py @@ -0,0 +1,26 @@ +import sys +import unittest + +from hr.doctype.leave_application.leave_application import test_records, LeaveDayBlockedError + +class TestLeaveApplication(unittest.TestCase): + def setUp(self): + from webnotes.test_runner import make_test_records + make_test_records("Leave Application") + + def test_block_list(self): + import webnotes + webnotes.conn.set_value("Employee", "_T-Employee-0001", "department", + "_Test Department with Block List") + + application = webnotes.model_wrapper(test_records[1]) + application.doc.from_date = "2013-01-01" + application.doc.to_date = "2013-01-05" + self.assertRaises(LeaveDayBlockedError, application.insert) + +if __name__=="__main__": + sys.path.extend(["app", "lib"]) + import webnotes + webnotes.connect() + unittest.main() + \ No newline at end of file diff --git a/setup/doctype/currency/currency.py b/setup/doctype/currency/currency.py index 008175eea7..73076cbde2 100644 --- a/setup/doctype/currency/currency.py +++ b/setup/doctype/currency/currency.py @@ -21,5 +21,4 @@ class DocType: def __init__(self, d, dl): self.doc, self.doclist = d, dl -# already set test_records = [] \ No newline at end of file From 509f17c3a4a65fdf2ceb37aeec770538ddece832 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Wed, 6 Feb 2013 08:49:59 +0530 Subject: [PATCH 3/3] minor --- .../test_leave_application.py | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/hr/doctype/leave_application/test_leave_application.py b/hr/doctype/leave_application/test_leave_application.py index 9d51bc1e24..fac36857c4 100644 --- a/hr/doctype/leave_application/test_leave_application.py +++ b/hr/doctype/leave_application/test_leave_application.py @@ -1,26 +1,26 @@ -import sys -import unittest + import sys + import unittest -from hr.doctype.leave_application.leave_application import test_records, LeaveDayBlockedError + from hr.doctype.leave_application.leave_application import test_records, LeaveDayBlockedError -class TestLeaveApplication(unittest.TestCase): - def setUp(self): - from webnotes.test_runner import make_test_records - make_test_records("Leave Application") + class TestLeaveApplication(unittest.TestCase): + def setUp(self): + from webnotes.test_runner import make_test_records + make_test_records("Leave Application") - def test_block_list(self): - import webnotes - webnotes.conn.set_value("Employee", "_T-Employee-0001", "department", - "_Test Department with Block List") + def test_block_list(self): + import webnotes + webnotes.conn.set_value("Employee", "_T-Employee-0001", "department", + "_Test Department with Block List") - application = webnotes.model_wrapper(test_records[1]) - application.doc.from_date = "2013-01-01" - application.doc.to_date = "2013-01-05" - self.assertRaises(LeaveDayBlockedError, application.insert) + application = webnotes.model_wrapper(test_records[1]) + application.doc.from_date = "2013-01-01" + application.doc.to_date = "2013-01-05" + self.assertRaises(LeaveDayBlockedError, application.insert) -if __name__=="__main__": - sys.path.extend(["app", "lib"]) - import webnotes - webnotes.connect() - unittest.main() + if __name__=="__main__": + sys.path.extend(["app", "lib"]) + import webnotes + webnotes.connect() + unittest.main() \ No newline at end of file