Coverage for tests/test_tutorial/test_indexes/test_tutorial002.py: 100%
21 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-27 00:03 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-27 00:03 +0000
1from unittest.mock import patch 1klmnopqrst
3from sqlalchemy import inspect 1klmnopqrst
4from sqlalchemy.engine.reflection import Inspector 1klmnopqrst
5from sqlmodel import create_engine 1klmnopqrst
7from ...conftest import get_testing_print_function 1klmnopqrst
10def test_tutorial(clear_sqlmodel): 1klmnopqrst
11 from docs_src.tutorial.indexes import tutorial002 as mod 1abcdefghij
13 mod.sqlite_url = "sqlite://" 1abcdefghij
14 mod.engine = create_engine(mod.sqlite_url) 1abcdefghij
15 calls = [] 1abcdefghij
17 new_print = get_testing_print_function(calls) 1abcdefghij
19 with patch("builtins.print", new=new_print): 1abcdefghij
20 mod.main() 1abcdefghij
21 assert calls == [ 1abcdefghij
22 [{"name": "Tarantula", "secret_name": "Natalia Roman-on", "age": 32, "id": 4}],
23 [{"name": "Black Lion", "secret_name": "Trevor Challa", "age": 35, "id": 5}],
24 ]
26 insp: Inspector = inspect(mod.engine) 1abcdefghij
27 indexes = insp.get_indexes(str(mod.Hero.__tablename__)) 1abcdefghij
28 expected_indexes = [ 1abcdefghij
29 {
30 "name": "ix_hero_name",
31 "dialect_options": {},
32 "column_names": ["name"],
33 "unique": 0,
34 },
35 {
36 "name": "ix_hero_age",
37 "dialect_options": {},
38 "column_names": ["age"],
39 "unique": 0,
40 },
41 ]
42 for index in expected_indexes: 1abcdefghij
43 assert index in indexes, "This expected index should be in the indexes in DB" 1abcdefghij
44 # Now that this index was checked, remove it from the list of indexes
45 indexes.pop(indexes.index(index)) 1abcdefghij
46 assert len(indexes) == 0, "The database should only have the expected indexes" 1abcdefghij