seitime-frappe/frappe/tests/test_reportview.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

89 lines
3 KiB
Python

# Copyright (c) 2019, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
import frappe
from frappe.desk.reportview import export_query, extract_fieldnames
from frappe.tests import IntegrationTestCase
class TestReportview(IntegrationTestCase):
def test_csv(self):
from csv import QUOTE_ALL, QUOTE_MINIMAL, QUOTE_NONE, QUOTE_NONNUMERIC, DictReader
from io import StringIO
frappe.local.form_dict = frappe._dict(
doctype="DocType",
file_format_type="CSV",
fields=("name", "module", "issingle"),
filters={"issingle": 1, "module": "Core"},
)
for delimiter in (",", ";", "\t", "|"):
frappe.local.form_dict.csv_delimiter = delimiter
for quoting in (QUOTE_ALL, QUOTE_MINIMAL, QUOTE_NONE, QUOTE_NONNUMERIC):
frappe.local.form_dict.csv_quoting = quoting
export_query()
self.assertTrue(frappe.response["filename"].endswith(".csv"))
self.assertEqual(frappe.response["type"], "binary")
with StringIO(frappe.response["filecontent"].decode("utf-8")) as result:
reader = DictReader(result, delimiter=delimiter, quoting=quoting)
for row in reader:
self.assertEqual(int(row["Is Single"]), 1)
self.assertEqual(row["Module"], "Core")
def test_extract_fieldname(self):
self.assertEqual(
extract_fieldnames("count(distinct `tabPhoto`.name) as total_count")[0], "tabPhoto.name"
)
self.assertEqual(extract_fieldnames("owner")[0], "owner")
self.assertEqual(extract_fieldnames("from")[0], "from")
self.assertEqual(extract_fieldnames("module")[0], "module")
self.assertEqual(extract_fieldnames("count(`tabPhoto`.name) as total_count")[0], "tabPhoto.name")
self.assertEqual(extract_fieldnames("count(distinct `tabPhoto`.name)")[0], "tabPhoto.name")
self.assertEqual(extract_fieldnames("count(`tabPhoto`.name)")[0], "tabPhoto.name")
self.assertEqual(
extract_fieldnames("count(distinct `tabJob Applicant`.name) as total_count")[0],
"tabJob Applicant.name",
)
self.assertEqual(
extract_fieldnames("(1 / nullif(locate('a', `tabAddress`.`name`), 0)) as `_relevance`")[0],
"tabAddress.name",
)
self.assertEqual(
extract_fieldnames("(1 / nullif(locate('(a)', `tabAddress`.`name`), 0)) as `_relevance`")[0],
"tabAddress.name",
)
self.assertEqual(extract_fieldnames("EXTRACT(MONTH FROM date_column) AS month")[0], "date_column")
self.assertEqual(extract_fieldnames("COUNT(*) AS count")[0], "*")
self.assertEqual(
extract_fieldnames("first_name + ' ' + last_name AS full_name"), ["first_name", "last_name"]
)
self.assertEqual(
extract_fieldnames("CONCAT(first_name, ' ', last_name) AS full_name"),
["first_name", "last_name"],
)
self.assertEqual(
extract_fieldnames("CONCAT(id, '/', name, '/', age, '/', marks) AS student"),
["id", "name", "age", "marks"],
)
self.assertEqual(extract_fieldnames("tablefield.fiedname")[0], "tablefield.fiedname")
self.assertEqual(extract_fieldnames("`tabChild DocType`.`fiedname`")[0], "tabChild DocType.fiedname")
self.assertEqual(extract_fieldnames("sum(1)"), [])