From 3db9c6be2de00331c5f4e520d2822c0eb13c7280 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Mon, 27 Oct 2025 10:56:49 -0400 Subject: [PATCH] try to improve coverage a bit use pyproject.toml so the options are safe from "rm .coverage*" types of commands omit some files add test coverage for a big section of cache-key Change-Id: Ia540c632d91ec09284e187e3edeb8ccf0214e1a6 --- .coveragerc | 7 ----- noxfile.py | 4 +++ pyproject.toml | 13 ++++++++++ test/sql/test_compare.py | 55 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 7 deletions(-) delete mode 100644 .coveragerc diff --git a/.coveragerc b/.coveragerc deleted file mode 100644 index e279765ddf..0000000000 --- a/.coveragerc +++ /dev/null @@ -1,7 +0,0 @@ -[paths] -source=lib/ - -[run] -relative_files=True - - diff --git a/noxfile.py b/noxfile.py index 6b593b06fb..a5afe31673 100644 --- a/noxfile.py +++ b/noxfile.py @@ -192,6 +192,10 @@ def _tests( timing_intensive: bool = True, coverage: bool = False, ) -> None: + + # ensure external PYTHONPATH not interfering + session.env["PYTHONPATH"] = "" + # PYTHONNOUSERSITE - this *MUST* be set so that the ./lib/ import # set up explicitly in test/conftest.py is *disabled*, so that # when SQLAlchemy is built into the .nox area, we use that and not the diff --git a/pyproject.toml b/pyproject.toml index caeaf47c6e..8179aa4832 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -196,6 +196,19 @@ mypy = [ ] +[tool.coverage.paths] +source="lib/" + +[tool.coverage.run] +relative_files = true +omit = [ + "lib/sqlalchemy/testing/plugin/*", + "lib/sqlalchemy/testing/fixtures/mypy.py", + "lib/sqlalchemy/testing/profiling.py", + "lib/sqlalchemy/util/tool_support.py", +] + + [tool.setuptools] include-package-data = true diff --git a/test/sql/test_compare.py b/test/sql/test_compare.py index 774f9792f4..94f34ddfff 100644 --- a/test/sql/test_compare.py +++ b/test/sql/test_compare.py @@ -49,6 +49,7 @@ from sqlalchemy.sql.base import DialectKWArgs from sqlalchemy.sql.base import HasCacheKey from sqlalchemy.sql.base import SingletonConstant from sqlalchemy.sql.base import SyntaxExtension +from sqlalchemy.sql.cache_key import CacheKey from sqlalchemy.sql.elements import _label_reference from sqlalchemy.sql.elements import _textual_label_reference from sqlalchemy.sql.elements import BindParameter @@ -2315,3 +2316,57 @@ class TypesTest(fixtures.TestBase): eq_(c1, c2) ne_(c1, c3) + + +class TestWhatsDifferentUtil(fixtures.TestBase): + """Test the CacheKey._whats_different() utility method + + Note: The _whats_different() method has a limitation where it can only + properly handle nested tuple structures. It was designed to work with + real cache keys which are always nested tuples. + """ + + def test_nested_tuple_difference(self): + """Test difference detection in nested tuples""" + k1 = CacheKey(key=((1, (2, 3, 4), 5),), bindparams=[]) + k2 = CacheKey(key=((1, (2, 7, 4), 5),), bindparams=[]) + + eq_(list(k1._whats_different(k2)), ["key[0][1][1]: 3 != 7"]) + + def test_deeply_nested_tuple_difference(self): + """Test difference detection in deeply nested tuples""" + k1 = CacheKey(key=((1, (2, (3, 4, 5), 6), 7),), bindparams=[]) + k2 = CacheKey(key=((1, (2, (3, 9, 5), 6), 7),), bindparams=[]) + + eq_(list(k1._whats_different(k2)), ["key[0][1][1][1]: 4 != 9"]) + + def test_multiple_differences_nested(self): + """Test detection of multiple differences in nested structure""" + k1 = CacheKey(key=((1, (2, 3), 4),), bindparams=[]) + k2 = CacheKey(key=((1, (5, 7), 4),), bindparams=[]) + + eq_( + list(k1._whats_different(k2)), + ["key[0][1][0]: 2 != 5", "key[0][1][1]: 3 != 7"], + ) + + def test_diff_method(self): + """Test the _diff() method that returns a comma-separated string""" + k1 = CacheKey(key=((1, (2, 3)),), bindparams=[]) + k2 = CacheKey(key=((1, (5, 7)),), bindparams=[]) + + eq_(k1._diff(k2), "key[0][1][0]: 2 != 5, key[0][1][1]: 3 != 7") + + def test_with_string_differences(self): + """Test detection of string differences""" + k1 = CacheKey(key=(("name", ("x", "value")),), bindparams=[]) + k2 = CacheKey(key=(("name", ("y", "value")),), bindparams=[]) + + eq_(list(k1._whats_different(k2)), ["key[0][1][0]: x != y"]) + + def test_with_mixed_types(self): + """Test detection of differences with mixed types""" + k1 = CacheKey(key=(("id", 1, ("nested", 100)),), bindparams=[]) + k2 = CacheKey(key=(("id", 1, ("nested", 200)),), bindparams=[]) + + eq_(list(k1._whats_different(k2)), ["key[0][2][1]: 100 != 200"]) -- 2.47.3