Coverage for tests/test_tutorial/test_automatic_id_none_refresh/test_tutorial001_tutorial002.py: 100%

51 statements  

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

1from typing import Any, Dict, List, Union 1mnopqr

2from unittest.mock import patch 1mnopqr

3 

4from sqlmodel import create_engine 1mnopqr

5 

6from tests.conftest import get_testing_print_function 1mnopqr

7 

8 

9def check_calls(calls: List[List[Union[str, Dict[str, Any]]]]): 1mnopqr

10 assert calls[0] == ["Before interacting with the database"] 1abcdefghijkl

11 assert calls[1] == [ 1abcdefghijkl

12 "Hero 1:", 

13 { 

14 "id": None, 

15 "name": "Deadpond", 

16 "secret_name": "Dive Wilson", 

17 "age": None, 

18 }, 

19 ] 

20 assert calls[2] == [ 1abcdefghijkl

21 "Hero 2:", 

22 { 

23 "id": None, 

24 "name": "Spider-Boy", 

25 "secret_name": "Pedro Parqueador", 

26 "age": None, 

27 }, 

28 ] 

29 assert calls[3] == [ 1abcdefghijkl

30 "Hero 3:", 

31 { 

32 "id": None, 

33 "name": "Rusty-Man", 

34 "secret_name": "Tommy Sharp", 

35 "age": 48, 

36 }, 

37 ] 

38 assert calls[4] == ["After adding to the session"] 1abcdefghijkl

39 assert calls[5] == [ 1abcdefghijkl

40 "Hero 1:", 

41 { 

42 "id": None, 

43 "name": "Deadpond", 

44 "secret_name": "Dive Wilson", 

45 "age": None, 

46 }, 

47 ] 

48 assert calls[6] == [ 1abcdefghijkl

49 "Hero 2:", 

50 { 

51 "id": None, 

52 "name": "Spider-Boy", 

53 "secret_name": "Pedro Parqueador", 

54 "age": None, 

55 }, 

56 ] 

57 assert calls[7] == [ 1abcdefghijkl

58 "Hero 3:", 

59 { 

60 "id": None, 

61 "name": "Rusty-Man", 

62 "secret_name": "Tommy Sharp", 

63 "age": 48, 

64 }, 

65 ] 

66 assert calls[8] == ["After committing the session"] 1abcdefghijkl

67 assert calls[9] == ["Hero 1:", {}] 1abcdefghijkl

68 assert calls[10] == ["Hero 2:", {}] 1abcdefghijkl

69 assert calls[11] == ["Hero 3:", {}] 1abcdefghijkl

70 assert calls[12] == ["After committing the session, show IDs"] 1abcdefghijkl

71 assert calls[13] == ["Hero 1 ID:", 1] 1abcdefghijkl

72 assert calls[14] == ["Hero 2 ID:", 2] 1abcdefghijkl

73 assert calls[15] == ["Hero 3 ID:", 3] 1abcdefghijkl

74 assert calls[16] == ["After committing the session, show names"] 1abcdefghijkl

75 assert calls[17] == ["Hero 1 name:", "Deadpond"] 1abcdefghijkl

76 assert calls[18] == ["Hero 2 name:", "Spider-Boy"] 1abcdefghijkl

77 assert calls[19] == ["Hero 3 name:", "Rusty-Man"] 1abcdefghijkl

78 assert calls[20] == ["After refreshing the heroes"] 1abcdefghijkl

79 assert calls[21] == [ 1abcdefghijkl

80 "Hero 1:", 

81 { 

82 "id": 1, 

83 "name": "Deadpond", 

84 "secret_name": "Dive Wilson", 

85 "age": None, 

86 }, 

87 ] 

88 assert calls[22] == [ 1abcdefghijkl

89 "Hero 2:", 

90 { 

91 "id": 2, 

92 "name": "Spider-Boy", 

93 "secret_name": "Pedro Parqueador", 

94 "age": None, 

95 }, 

96 ] 

97 assert calls[23] == [ 1abcdefghijkl

98 "Hero 3:", 

99 { 

100 "id": 3, 

101 "name": "Rusty-Man", 

102 "secret_name": "Tommy Sharp", 

103 "age": 48, 

104 }, 

105 ] 

106 assert calls[24] == ["After the session closes"] 1abcdefghijkl

107 assert calls[21] == [ 1abcdefghijkl

108 "Hero 1:", 

109 { 

110 "id": 1, 

111 "name": "Deadpond", 

112 "secret_name": "Dive Wilson", 

113 "age": None, 

114 }, 

115 ] 

116 assert calls[22] == [ 1abcdefghijkl

117 "Hero 2:", 

118 { 

119 "id": 2, 

120 "name": "Spider-Boy", 

121 "secret_name": "Pedro Parqueador", 

122 "age": None, 

123 }, 

124 ] 

125 assert calls[23] == [ 1abcdefghijkl

126 "Hero 3:", 

127 { 

128 "id": 3, 

129 "name": "Rusty-Man", 

130 "secret_name": "Tommy Sharp", 

131 "age": 48, 

132 }, 

133 ] 

134 

135 

136def test_tutorial_001(clear_sqlmodel): 1mnopqr

137 from docs_src.tutorial.automatic_id_none_refresh import tutorial001 as mod 1acegik

138 

139 mod.sqlite_url = "sqlite://" 1acegik

140 mod.engine = create_engine(mod.sqlite_url) 1acegik

141 calls = [] 1acegik

142 

143 new_print = get_testing_print_function(calls) 1acegik

144 

145 with patch("builtins.print", new=new_print): 1acegik

146 mod.main() 1acegik

147 check_calls(calls) 1acegik

148 

149 

150def test_tutorial_002(clear_sqlmodel): 1mnopqr

151 from docs_src.tutorial.automatic_id_none_refresh import tutorial002 as mod 1bdfhjl

152 

153 mod.sqlite_url = "sqlite://" 1bdfhjl

154 mod.engine = create_engine(mod.sqlite_url) 1bdfhjl

155 calls = [] 1bdfhjl

156 

157 new_print = get_testing_print_function(calls) 1bdfhjl

158 

159 with patch("builtins.print", new=new_print): 1bdfhjl

160 mod.main() 1bdfhjl

161 check_calls(calls) 1bdfhjl