From 8a2aa92389f22798b3ea7b640e1dc7b356dd0eaf Mon Sep 17 00:00:00 2001 From: gruener Date: Fri, 9 Aug 2024 05:57:07 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20Fixes=20mariadb=20orm=20to=20return=20li?= =?UTF-8?q?st=20instead=20of=20tuple=20as=20the=20typisat=E2=80=A6=20(#271?= =?UTF-8?q?79)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Fixes mariadb orm to return list instead of tuple as the typisation suggests it * fix: inverted fix for pg: Expect tuple as data_type for _transform_result * fix: Fixed failing upstream spec due to data_type change --- frappe/database/database.py | 2 +- frappe/database/postgres/database.py | 3 +++ frappe/tests/test_db.py | 28 +++++++++++++++++++++------- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/frappe/database/database.py b/frappe/database/database.py index 1edd61926d..c7ce29de6a 100644 --- a/frappe/database/database.py +++ b/frappe/database/database.py @@ -144,7 +144,7 @@ class Database: def _transform_query(self, query: Query, values: QueryValues) -> tuple: return query, values - def _transform_result(self, result: list[tuple]) -> list[tuple]: + def _transform_result(self, result: list[tuple] | tuple[tuple]) -> tuple[tuple]: return result def _clean_up(self): diff --git a/frappe/database/postgres/database.py b/frappe/database/postgres/database.py index 56ae71a9ad..aa3f808a79 100644 --- a/frappe/database/postgres/database.py +++ b/frappe/database/postgres/database.py @@ -225,6 +225,9 @@ class PostgresDatabase(PostgresExceptionUtil, Database): ) return db_size[0].get("database_size") + def _transform_result(self, result: list[tuple] | tuple[tuple]) -> tuple[tuple]: + return tuple(result) if isinstance(result, list) else result + # pylint: disable=W0221 def sql(self, query, values=EmptyQueryValues, *args, **kwargs): return super().sql(modify_query(query), modify_values(values), *args, **kwargs) diff --git a/frappe/tests/test_db.py b/frappe/tests/test_db.py index 63c0dba7da..6d2994dd90 100644 --- a/frappe/tests/test_db.py +++ b/frappe/tests/test_db.py @@ -572,6 +572,20 @@ class TestDB(FrappeTestCase): frappe.db.rollback() + def test_get_list_return_value_data_type(self): + frappe.db.delete("Note") + + frappe.get_doc(doctype="Note", title="note1", content="something").insert() + frappe.get_doc(doctype="Note", title="note2", content="someting else").insert() + + note_docs = frappe.db.sql("select * from `tabNote`") + + # should return both records + self.assertEqual(len(note_docs), 2) + + # data-type should be list + self.assertIsInstance(note_docs, tuple) + @run_only_if(db_type_is.POSTGRES) def test_modify_query(self): from frappe.database.postgres.database import modify_query @@ -1111,9 +1125,9 @@ class TestPostgresSchemaQueryIndependence(ExtFrappeTestCase): if frappe.db.sql( """SELECT 1 - FROM information_schema.schemata - WHERE schema_name = 'alt_schema' - limit 1 """ + FROM information_schema.schemata + WHERE schema_name = 'alt_schema' + LIMIT 1 """ ): self.cleanup() @@ -1244,19 +1258,19 @@ class TestPostgresSchemaQueryIndependence(ExtFrappeTestCase): rows = frappe.db.sql(f'select * from "tab{self.test_table_name}"') self.assertEqual( rows, - [ + ( ( "a", "b", - ) - ], + ), + ), ) # there should be a single row in the public table # when schema is changed to alt_schema, the alt_schema tables should be addressed by search path frappe.conf["db_schema"] = "alt_schema" frappe.db.connect() rows = frappe.db.sql(f'select * from "tab{self.test_table_name}"') - self.assertEqual(rows, []) # there are no records in the alt_schema table + self.assertEqual(rows, ()) # there are no records in the alt_schema table del frappe.conf["db_schema"]