Coverage for tests/test_advanced/test_uuid/test_tutorial002.py: 100%

18 statements  

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

1from unittest.mock import patch 1klmnopqrst

2 

3from dirty_equals import IsUUID 1klmnopqrst

4from sqlmodel import create_engine 1klmnopqrst

5 

6from ...conftest import get_testing_print_function 1klmnopqrst

7 

8 

9def test_tutorial() -> None: 1klmnopqrst

10 from docs_src.advanced.uuid import tutorial002 as mod 1abcdefghij

11 

12 mod.sqlite_url = "sqlite://" 1abcdefghij

13 mod.engine = create_engine(mod.sqlite_url) 1abcdefghij

14 calls = [] 1abcdefghij

15 

16 new_print = get_testing_print_function(calls) 1abcdefghij

17 

18 with patch("builtins.print", new=new_print): 1abcdefghij

19 mod.main() 1abcdefghij

20 first_uuid = calls[1][0]["id"] 1abcdefghij

21 assert first_uuid == IsUUID(4) 1abcdefghij

22 

23 second_uuid = calls[7][0]["id"] 1abcdefghij

24 assert second_uuid == IsUUID(4) 1abcdefghij

25 

26 assert first_uuid != second_uuid 1abcdefghij

27 

28 assert calls == [ 1abcdefghij

29 ["The hero before saving in the DB"], 

30 [ 

31 { 

32 "name": "Deadpond", 

33 "secret_name": "Dive Wilson", 

34 "id": first_uuid, 

35 "age": None, 

36 } 

37 ], 

38 ["The hero ID was already set"], 

39 [first_uuid], 

40 ["After saving in the DB"], 

41 [ 

42 { 

43 "name": "Deadpond", 

44 "secret_name": "Dive Wilson", 

45 "age": None, 

46 "id": first_uuid, 

47 } 

48 ], 

49 ["Created hero:"], 

50 [ 

51 { 

52 "name": "Spider-Boy", 

53 "secret_name": "Pedro Parqueador", 

54 "age": None, 

55 "id": second_uuid, 

56 } 

57 ], 

58 ["Created hero ID:"], 

59 [second_uuid], 

60 ["Selected hero:"], 

61 [ 

62 { 

63 "name": "Spider-Boy", 

64 "secret_name": "Pedro Parqueador", 

65 "age": None, 

66 "id": second_uuid, 

67 } 

68 ], 

69 ["Selected hero ID:"], 

70 [second_uuid], 

71 ]