2020-09-07 14:56:59 -07:00
|
|
|
from sqlite_utils import Database
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_tracer():
|
|
|
|
|
collected = []
|
|
|
|
|
db = Database(
|
|
|
|
|
memory=True, tracer=lambda sql, params: collected.append((sql, params))
|
|
|
|
|
)
|
2025-05-08 23:19:36 -07:00
|
|
|
dogs = db.table("dogs")
|
|
|
|
|
dogs.insert({"name": "Cleopaws"})
|
|
|
|
|
dogs.enable_fts(["name"])
|
|
|
|
|
dogs.search("Cleopaws")
|
2020-09-07 14:56:59 -07:00
|
|
|
assert collected == [
|
|
|
|
|
("PRAGMA recursive_triggers=on;", None),
|
|
|
|
|
("select name from sqlite_master where type = 'view'", None),
|
|
|
|
|
("select name from sqlite_master where type = 'table'", None),
|
2021-08-18 15:25:18 -07:00
|
|
|
("select name from sqlite_master where type = 'view'", None),
|
2025-05-08 23:19:36 -07:00
|
|
|
("select name from sqlite_master where type = 'view'", None),
|
2023-07-22 12:23:42 -07:00
|
|
|
("select name from sqlite_master where type = 'table'", None),
|
|
|
|
|
("select name from sqlite_master where type = 'view'", None),
|
2025-11-23 20:43:26 -08:00
|
|
|
('CREATE TABLE "dogs" (\n "name" TEXT\n);\n ', None),
|
2020-09-07 14:56:59 -07:00
|
|
|
("select name from sqlite_master where type = 'view'", None),
|
2025-11-23 20:43:26 -08:00
|
|
|
('INSERT INTO "dogs" ("name") VALUES (?)', ["Cleopaws"]),
|
2020-09-07 14:56:59 -07:00
|
|
|
(
|
2025-11-23 20:43:26 -08:00
|
|
|
'CREATE VIRTUAL TABLE "dogs_fts" USING FTS5 (\n "name",\n content="dogs"\n)',
|
2020-09-07 14:56:59 -07:00
|
|
|
None,
|
|
|
|
|
),
|
|
|
|
|
(
|
2025-11-23 20:43:26 -08:00
|
|
|
'INSERT INTO "dogs_fts" (rowid, "name")\n SELECT rowid, "name" FROM "dogs";',
|
2020-09-07 14:56:59 -07:00
|
|
|
None,
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_with_tracer():
|
|
|
|
|
collected = []
|
2021-06-22 18:22:08 -07:00
|
|
|
|
|
|
|
|
def tracer(sql, params):
|
|
|
|
|
return collected.append((sql, params))
|
2020-09-07 14:56:59 -07:00
|
|
|
|
|
|
|
|
db = Database(memory=True)
|
|
|
|
|
|
2025-05-08 23:19:36 -07:00
|
|
|
dogs = db.table("dogs")
|
|
|
|
|
|
|
|
|
|
dogs.insert({"name": "Cleopaws"})
|
|
|
|
|
dogs.enable_fts(["name"])
|
2020-09-07 14:56:59 -07:00
|
|
|
|
|
|
|
|
assert len(collected) == 0
|
|
|
|
|
|
|
|
|
|
with db.tracer(tracer):
|
2025-05-08 23:19:36 -07:00
|
|
|
list(dogs.search("Cleopaws"))
|
2020-09-07 14:56:59 -07:00
|
|
|
|
More type annotations (#697)
* Add comprehensive type annotations
- mypy.ini: expanded configuration with module-specific settings
- hookspecs.py: type annotations for hook functions
- plugins.py: typed get_plugins() return value
- recipes.py: full type annotations for parsedate, parsedatetime, jsonsplit
- utils.py: extensive type annotations including Row type alias,
TypeTracker, ValueTracker, and all utility functions
- db.py: type annotations for Database methods (__exit__,
ensure_autocommit_off, tracer, register_function, etc.) and
Queryable class methods
- tests/test_docs.py: updated to match new signature display format
* Fix type errors caught by ty check
- Add type: ignore comments for external library type stub limitations
(csv.reader, click.progressbar, IOBase.name, Callable.__name__)
- Change Iterable to Sequence for SQL where_args parameters
- Use db.table() instead of db[name] for proper Table return type
- Fix rebuild_fts return type from None to Table
- Update test_tracer to expect fewer queries (optimization side effect)
* Fix mypy type errors
- Add type: ignore comments for runtime-valid patterns mypy can't verify
- Fix new_column_types annotation to Dict[str, Set[type]]
- Add type: ignore for Default sentinel values passed to create_table
* mypy skip tests directory
* Fix CI: exclude typing imports from recipe docs, skip mypy on tests
- Add Callable and Optional to exclusion list in _generate_convert_help()
- Regenerate docs/cli-reference.rst with cog
- Add [mypy-tests.*] ignore_errors = True to skip test type errors
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-16 22:11:47 -08:00
|
|
|
assert len(collected) == 4
|
2020-09-07 14:56:59 -07:00
|
|
|
assert collected == [
|
2020-11-06 10:23:16 -08:00
|
|
|
(
|
2025-05-08 23:19:36 -07:00
|
|
|
"SELECT name FROM sqlite_master\n"
|
|
|
|
|
" WHERE rootpage = 0\n"
|
|
|
|
|
" AND (\n"
|
|
|
|
|
" sql LIKE :like\n"
|
|
|
|
|
" OR sql LIKE :like2\n"
|
|
|
|
|
" OR (\n"
|
|
|
|
|
" tbl_name = :table\n"
|
|
|
|
|
" AND sql LIKE '%VIRTUAL TABLE%USING FTS%'\n"
|
|
|
|
|
" )\n"
|
|
|
|
|
" )",
|
|
|
|
|
{
|
2026-06-21 16:29:32 -07:00
|
|
|
"like": "%VIRTUAL TABLE%USING FTS%content=[dogs]%",
|
2025-05-08 23:19:36 -07:00
|
|
|
"like2": '%VIRTUAL TABLE%USING FTS%content="dogs"%',
|
|
|
|
|
"table": "dogs",
|
|
|
|
|
},
|
2020-11-06 10:23:16 -08:00
|
|
|
),
|
|
|
|
|
("select name from sqlite_master where type = 'view'", None),
|
|
|
|
|
("select sql from sqlite_master where name = ?", ("dogs_fts",)),
|
2020-09-07 14:56:59 -07:00
|
|
|
(
|
2025-11-23 20:43:26 -08:00
|
|
|
'with "original" as (\n'
|
2025-05-08 23:19:36 -07:00
|
|
|
" select\n"
|
|
|
|
|
" rowid,\n"
|
|
|
|
|
" *\n"
|
2025-11-23 20:43:26 -08:00
|
|
|
' from "dogs"\n'
|
2025-05-08 23:19:36 -07:00
|
|
|
")\n"
|
|
|
|
|
"select\n"
|
2025-11-23 20:43:26 -08:00
|
|
|
' "original".*\n'
|
2025-05-08 23:19:36 -07:00
|
|
|
"from\n"
|
2025-11-23 20:43:26 -08:00
|
|
|
' "original"\n'
|
|
|
|
|
' join "dogs_fts" on "original".rowid = "dogs_fts".rowid\n'
|
2025-05-08 23:19:36 -07:00
|
|
|
"where\n"
|
2025-11-23 20:43:26 -08:00
|
|
|
' "dogs_fts" match :query\n'
|
2025-05-08 23:19:36 -07:00
|
|
|
"order by\n"
|
2025-11-23 20:43:26 -08:00
|
|
|
' "dogs_fts".rank',
|
2020-11-06 10:23:16 -08:00
|
|
|
{"query": "Cleopaws"},
|
2020-09-07 14:56:59 -07:00
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# Outside the with block collected should not be appended to
|
2025-05-08 23:19:36 -07:00
|
|
|
dogs.insert({"name": "Cleopaws"})
|
More type annotations (#697)
* Add comprehensive type annotations
- mypy.ini: expanded configuration with module-specific settings
- hookspecs.py: type annotations for hook functions
- plugins.py: typed get_plugins() return value
- recipes.py: full type annotations for parsedate, parsedatetime, jsonsplit
- utils.py: extensive type annotations including Row type alias,
TypeTracker, ValueTracker, and all utility functions
- db.py: type annotations for Database methods (__exit__,
ensure_autocommit_off, tracer, register_function, etc.) and
Queryable class methods
- tests/test_docs.py: updated to match new signature display format
* Fix type errors caught by ty check
- Add type: ignore comments for external library type stub limitations
(csv.reader, click.progressbar, IOBase.name, Callable.__name__)
- Change Iterable to Sequence for SQL where_args parameters
- Use db.table() instead of db[name] for proper Table return type
- Fix rebuild_fts return type from None to Table
- Update test_tracer to expect fewer queries (optimization side effect)
* Fix mypy type errors
- Add type: ignore comments for runtime-valid patterns mypy can't verify
- Fix new_column_types annotation to Dict[str, Set[type]]
- Add type: ignore for Default sentinel values passed to create_table
* mypy skip tests directory
* Fix CI: exclude typing imports from recipe docs, skip mypy on tests
- Add Callable and Optional to exclusion list in _generate_convert_help()
- Regenerate docs/cli-reference.rst with cog
- Add [mypy-tests.*] ignore_errors = True to skip test type errors
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-16 22:11:47 -08:00
|
|
|
assert len(collected) == 4
|