'Test feedback trigger'

def test_feedback_trigger(self):

    from frappe.www.feedback import accept
    frappe.delete_doc(u'Feedback    Trigger', u'ToDo')
    frappe.db.sql(u'delete    from    `tabEmail    Queue`')
    frappe.db.sql(u'delete    from    `tabFeedback    Request`')
    feedback_trigger = frappe.get_doc({u'enabled': 1, u'doctype': u'Feedback    Trigger', u'document_type': u'ToDo', u'email_field': u'assigned_by', u'email_fieldname': u'assigned_by', u'subject': u'{{    doc.name    }}    Task    Completed', u'condition': u"doc.status    ==    'Closed'", u'message': u'Task    {{    doc.name    }}    is    Completed    by    {{    doc.owner    }}.\n DCTB  DCTB  DCTB regarding    the    Task    {{    doc.name    }}'}).insert(ignore_permissions=True)
    todo = frappe.get_doc({u'doctype': u'ToDo', u'owner': u'test-feedback@example.com', u'assigned_by': u'test-feedback@example.com', u'description': u'Unable    To    Submit    Sales    Order    #SO-00001'}).insert(ignore_permissions=True)
    email_queue = frappe.db.sql(u"select    name    from    `tabEmail    Queue`    where\n DCTB  DCTB  DCTB reference_doctype='ToDo'    and    reference_name='{0}'".format(todo.name))
    self.assertFalse(email_queue)
    frappe.get_doc({u'reference_doctype': u'ToDo', u'reference_name': todo.name, u'communication_type': u'Communication', u'content': u'Test    Communication', u'subject': u'Test    Communication', u'doctype': u'Communication'}).insert(ignore_permissions=True)
    todo.status = u'Closed'
    todo.save(ignore_permissions=True)
    email_queue = frappe.db.sql(u"select    name    from    `tabEmail    Queue`    where\n DCTB  DCTB  DCTB reference_doctype='ToDo'    and    reference_name='{0}'".format(todo.name))
    self.assertTrue(email_queue)
    (feedback_request, request_key) = get_feedback_request(todo.name, feedback_trigger.name)
    self.assertTrue(feedback_request)
    todo.save(ignore_permissions=True)
    email_queue = frappe.db.sql(u"select    name    from    `tabEmail    Queue`    where\n DCTB  DCTB  DCTB reference_doctype='ToDo'    and    reference_name='{0}'".format(todo.name))
    self.assertTrue((len(email_queue) == 1))
    frappe.db.sql(u'delete    from    `tabEmail    Queue`')
    result = accept(request_key, u'test-feedback@example.com', u'ToDo', todo.name, u'Great    Work    !!', 4, fullname=u'Test    User')
    self.assertTrue(result)
    docname = frappe.db.get_value(u'Communication', {u'reference_doctype': u'ToDo', u'reference_name': todo.name, u'communication_type': u'Feedback', u'feedback_request': feedback_request})
    communication = frappe.get_doc(u'Communication', docname)
    self.assertEqual(communication.rating, 4)
    self.assertEqual(communication.content, u'Great    Work    !!')
    self.assertRaises(Exception, accept, key=request_key, sender=u'test-feedback@example.com', reference_doctype=u'ToDo', reference_name=todo.name, feedback=u'Thank    You    !!', rating=4, fullname=u'Test    User')
    todo.save(ignore_permissions=True)
    email_queue = frappe.db.sql(u"select    name    from    `tabEmail    Queue`    where\n DCTB  DCTB  DCTB reference_doctype='ToDo'    and    reference_name='{0}'".format(todo.name))
    self.assertFalse(email_queue)
    frappe.delete_doc(u'ToDo', todo.name)
    communications = frappe.get_all(u'Communication', {u'reference_doctype': u'ToDo', u'reference_name': todo.name, u'communication_type': u'Feedback'})
    self.assertFalse(communications)
    feedback_requests = frappe.get_all(u'Feedback    Request', {u'reference_doctype': u'ToDo', u'reference_name': todo.name, u'is_feedback_submitted': 0})
    self.assertFalse(feedback_requests)
