# Buggy code
def enroll_random_student(current_date):
    random_student = get_random("Student Applicant", {"application_status": "Approved"})
    if random_student:
        enrollment = enroll_student(random_student)
        enrollment.academic_year = get_random("Academic Year")
        enrollment.enrollment_date = current_date
        fee_schedule = get_fee_schedule(enrollment.program)
        for fee in fee_schedule:
            enrollment.append("fees", fee)
        enrollment.submit()
        frappe.db.commit()
        assign_student_group(enrollment.student, enrollment.program)


# Traceback (frappe.exceptions.LinkValidationError)
➜  frappe-bench bench --site school.erpnext.dev execute erpnext.demo.demo.make --args Education
Complete Setup...
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 79, in <module>
main()
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 16, in main
click.Group(commands=commands)(prog_name='bench')
File "/Users/rushabh/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 716, in __call__
return self.main(*args, **kwargs)
File "/Users/rushabh/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 696, in main
rv = self.invoke(ctx)
File "/Users/rushabh/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 1060, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/Users/rushabh/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 1060, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/Users/rushabh/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 889, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Users/rushabh/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 534, in invoke
return callback(*args, **kwargs)
File "/Users/rushabh/frappe/frappe-bench/env/lib/python2.7/site-packages/click/decorators.py", line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/commands/__init__.py", line 24, in _func
ret = f(frappe._dict(ctx.obj), *args, **kwargs)
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/commands/utils.py", line 110, in execute
ret = frappe.get_attr(method)(*args, **kwargs)
File "/Users/rushabh/frappe/frappe-bench/apps/erpnext/erpnext/demo/demo.py", line 32, in make
education.setup_data()
File "/Users/rushabh/frappe/frappe-bench/apps/erpnext/erpnext/demo/setup/education.py", line 12, in setup_data
make_masters()
File "/Users/rushabh/frappe/frappe-bench/apps/erpnext/erpnext/demo/setup/education.py", line 26, in make_masters
import_json("Program")
File "/Users/rushabh/frappe/frappe-bench/apps/erpnext/erpnext/demo/setup/setup_data.py", line 356, in import_json
doc.insert()
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 213, in insert
self._validate()
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 388, in _validate
self._validate_links()
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 630, in _validate_links
frappe.LinkValidationError)
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 302, in throw
msgprint(msg, raise_exception=exc, title=title, indicator='red')
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 295, in msgprint
_raise_exception()
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 268, in _raise_exception
raise raise_exception, encode(msg)
frappe.exceptions.LinkValidationError: Could not find Row #1: Academic Term: Semester 1, Row #2: Academic Term: Semester 1, Row #3: Academic Term: Semester 2

# Fixed code
def enroll_random_student(current_date):
    batch = ["Section-A", "Section-B"]
    random_student = get_random("Student Applicant", {"application_status": "Approved"})
    if random_student:
        enrollment = enroll_student(random_student)
        enrollment.academic_year = get_random("Academic Year")
        enrollment.enrollment_date = current_date
        enrollment.student_batch_name = batch[weighted_choice([9, 3])]
        fee_schedule = get_fee_schedule(enrollment.program)
        for fee in fee_schedule:
            enrollment.append("fees", fee)
        enrolled_courses = get_course(enrollment.program)
        for course in enrolled_courses:
            enrollment.append("courses", course)
        enrollment.submit()
        frappe.db.commit()
        assign_student_group(
            enrollment.student,
            enrollment.student_name,
            enrollment.program,
            enrolled_courses,
            enrollment.student_batch_name,
        )