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

21 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 00:02 +0000

1from unittest.mock import patch 1ghijkl

2 

3from sqlalchemy import inspect 1ghijkl

4from sqlalchemy.engine.reflection import Inspector 1ghijkl

5from sqlmodel import create_engine 1ghijkl

6 

7from ...conftest import get_testing_print_function 1ghijkl

8 

9 

10def test_tutorial(clear_sqlmodel): 1ghijkl

11 from docs_src.tutorial.indexes import tutorial001 as mod 1fabcde

12 

13 mod.sqlite_url = "sqlite://" 1fabcde

14 mod.engine = create_engine(mod.sqlite_url) 1fabcde

15 calls = [] 1fabcde

16 

17 new_print = get_testing_print_function(calls) 1fabcde

18 

19 with patch("builtins.print", new=new_print): 1fabcde

20 mod.main() 1fabcde

21 assert calls == [ 1fabcde

22 [{"secret_name": "Dive Wilson", "age": None, "id": 1, "name": "Deadpond"}] 

23 ] 

24 

25 insp: Inspector = inspect(mod.engine) 1fabcde

26 indexes = insp.get_indexes(str(mod.Hero.__tablename__)) 1fabcde

27 expected_indexes = [ 1abcde

28 { 

29 "name": "ix_hero_name", 

30 "dialect_options": {}, 

31 "column_names": ["name"], 

32 "unique": 0, 

33 }, 

34 { 

35 "name": "ix_hero_age", 

36 "dialect_options": {}, 

37 "column_names": ["age"], 

38 "unique": 0, 

39 }, 

40 ] 

41 for index in expected_indexes: 1fabcde

42 assert index in indexes, "This expected index should be in the indexes in DB" 1fabcde

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

44 indexes.pop(indexes.index(index)) 1fabcde

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