seitime-frappe/frappe/tests/test_assign.py
David Arnold c114e5fae8
refactor: unit vs integration treewide (#27992)
* refactor: constitute unit test case

* fix: docs and type hints

* refactor: mark presumed integration test cases explicitly

At time of writing, we now have at least two base test classes:

- frappe.tests.UnitTestCase
- frappe.tests.IntegrationTestCase

They load in their perspective priority queue during execution.

Probably more to come for more efficient queing and scheduling.

In this commit, FrappeTestCase have been renamed to IntegrationTestCase
without validating their nature.

* feat: Move test-related functions from test_runner.py to tests/utils.py

* refactor: add bare UnitTestCase to all doctype tests

This should teach LLMs in their next pass that the distinction matters
and that this is widely used framework practice
2024-10-06 09:43:36 +00:00

112 lines
3.3 KiB
Python

# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
import frappe
import frappe.desk.form.assign_to
from frappe.automation.doctype.assignment_rule.test_assignment_rule import (
TEST_DOCTYPE,
_make_test_record,
create_test_doctype,
)
from frappe.desk.form.load import get_assignments
from frappe.desk.listview import get_group_by_count
from frappe.tests import IntegrationTestCase
class TestAssign(IntegrationTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
create_test_doctype(TEST_DOCTYPE)
def test_assign(self):
todo = frappe.get_doc({"doctype": "ToDo", "description": "test"}).insert()
if not frappe.db.exists("User", "test@example.com"):
frappe.get_doc({"doctype": "User", "email": "test@example.com", "first_name": "Test"}).insert()
self._test_basic_assign_on_document(todo)
def _test_basic_assign_on_document(self, doc):
added = assign(doc, "test@example.com")
self.assertTrue("test@example.com" in [d.owner for d in added])
frappe.desk.form.assign_to.remove(doc.doctype, doc.name, "test@example.com")
# assignment is cleared
assignments = frappe.desk.form.assign_to.get(dict(doctype=doc.doctype, name=doc.name))
self.assertEqual(len(assignments), 0)
def test_assign_single(self):
c = frappe.get_doc("Contact Us Settings")
self._test_basic_assign_on_document(c)
def test_assignment_count(self):
frappe.db.delete("ToDo")
if not frappe.db.exists("User", "test_assign1@example.com"):
frappe.get_doc(
{
"doctype": "User",
"email": "test_assign1@example.com",
"first_name": "Test",
"roles": [{"role": "System Manager"}],
}
).insert()
if not frappe.db.exists("User", "test_assign2@example.com"):
frappe.get_doc(
{
"doctype": "User",
"email": "test_assign2@example.com",
"first_name": "Test",
"roles": [{"role": "System Manager"}],
}
).insert()
note = _make_test_record()
assign(note, "test_assign1@example.com")
note = _make_test_record(public=1)
assign(note, "test_assign2@example.com")
note = _make_test_record(public=1)
assign(note, "test_assign2@example.com")
note = _make_test_record()
assign(note, "test_assign2@example.com")
data = {d.name: d.count for d in get_group_by_count(TEST_DOCTYPE, "[]", "assigned_to")}
self.assertTrue("test_assign1@example.com" in data)
self.assertEqual(data["test_assign1@example.com"], 1)
self.assertEqual(data["test_assign2@example.com"], 3)
data = {d.name: d.count for d in get_group_by_count(TEST_DOCTYPE, '[{"public": 1}]', "assigned_to")}
self.assertFalse("test_assign1@example.com" in data)
self.assertEqual(data["test_assign2@example.com"], 2)
frappe.db.rollback()
def test_assignment_removal(self):
todo = frappe.get_doc({"doctype": "ToDo", "description": "test"}).insert()
if not frappe.db.exists("User", "test@example.com"):
frappe.get_doc({"doctype": "User", "email": "test@example.com", "first_name": "Test"}).insert()
new_todo = assign(todo, "test@example.com")
# remove assignment
frappe.db.set_value("ToDo", new_todo[0].name, "allocated_to", "")
self.assertFalse(get_assignments("ToDo", todo.name))
def assign(doc, user):
return frappe.desk.form.assign_to.add(
{
"assign_to": [user],
"doctype": doc.doctype,
"name": doc.name,
"description": "test",
}
)