Coverage for tests/test_tutorial/test_indexes/test_tutorial002_py310.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-27 00:03 +0000

1from unittest.mock import patch 1ijklmnopqr

2 

3from sqlalchemy import inspect 1ijklmnopqr

4from sqlalchemy.engine.reflection import Inspector 1ijklmnopqr

5from sqlmodel import create_engine 1ijklmnopqr

6 

7from ...conftest import get_testing_print_function, needs_py310 1ijklmnopqr

8 

9 

10@needs_py310 1ijklmnopqr

11def test_tutorial(clear_sqlmodel): 1ijklmnopqr

12 from docs_src.tutorial.indexes import tutorial002_py310 as mod 1abcdefgh

13 

14 mod.sqlite_url = "sqlite://" 1abcdefgh

15 mod.engine = create_engine(mod.sqlite_url) 1abcdefgh

16 calls = [] 1abcdefgh

17 

18 new_print = get_testing_print_function(calls) 1abcdefgh

19 

20 with patch("builtins.print", new=new_print): 1abcdefgh

21 mod.main() 1abcdefgh

22 assert calls == [ 1abcdefgh

23 [{"name": "Tarantula", "secret_name": "Natalia Roman-on", "age": 32, "id": 4}], 

24 [{"name": "Black Lion", "secret_name": "Trevor Challa", "age": 35, "id": 5}], 

25 ] 

26 

27 insp: Inspector = inspect(mod.engine) 1abcdefgh

28 indexes = insp.get_indexes(str(mod.Hero.__tablename__)) 1abcdefgh

29 expected_indexes = [ 1abcdefgh

30 { 

31 "name": "ix_hero_name", 

32 "dialect_options": {}, 

33 "column_names": ["name"], 

34 "unique": 0, 

35 }, 

36 { 

37 "name": "ix_hero_age", 

38 "dialect_options": {}, 

39 "column_names": ["age"], 

40 "unique": 0, 

41 }, 

42 ] 

43 for index in expected_indexes: 1abcdefgh

44 assert index in indexes, "This expected index should be in the indexes in DB" 1abcdefgh

45 # Now that this index was checked, remove it from the list of indexes 

46 indexes.pop(indexes.index(index)) 1abcdefgh

47 assert len(indexes) == 0, "The database should only have the expected indexes" 1abcdefgh