11from __future__ import annotations
22
3+ import pytest
34from django .db import models
45from django .test import TestCase
56
@@ -53,70 +54,74 @@ def setUp(self):
5354 self .model = SimpleBlogPost ()
5455
5556 def test_initial_state_instantiated (self ):
56- self .assertEqual ( self . model .state , "new" )
57+ assert self .model .state == "new"
5758
5859 def test_known_transition_should_succeed (self ):
59- self . assertTrue ( can_proceed (self .model .publish ) )
60+ assert can_proceed (self .model .publish )
6061 self .model .publish ()
61- self .assertEqual ( self . model .state , "published" )
62+ assert self .model .state == "published"
6263
63- self . assertTrue ( can_proceed (self .model .hide ) )
64+ assert can_proceed (self .model .hide )
6465 self .model .hide ()
65- self .assertEqual ( self . model .state , "hidden" )
66+ assert self .model .state == "hidden"
6667
6768 def test_unknown_transition_fails (self ):
68- self .assertFalse (can_proceed (self .model .hide ))
69- self .assertRaises (TransitionNotAllowed , self .model .hide )
69+ assert not can_proceed (self .model .hide )
70+ with pytest .raises (TransitionNotAllowed ):
71+ self .model .hide ()
7072
7173 def test_state_non_changed_after_fail (self ):
72- self .assertTrue (can_proceed (self .model .remove ))
73- self .assertRaises (Exception , self .model .remove )
74- self .assertEqual (self .model .state , "new" )
74+ assert can_proceed (self .model .remove )
75+ with pytest .raises (Exception , match = "Upss" ):
76+ self .model .remove ()
77+ assert self .model .state == "new"
7578
7679 def test_allowed_null_transition_should_succeed (self ):
7780 self .model .publish ()
7881 self .model .notify_all ()
79- self .assertEqual ( self . model .state , "published" )
82+ assert self .model .state == "published"
8083
8184 def test_unknown_null_transition_should_fail (self ):
82- self .assertRaises (TransitionNotAllowed , self .model .notify_all )
83- self .assertEqual (self .model .state , "new" )
85+ with pytest .raises (TransitionNotAllowed ):
86+ self .model .notify_all ()
87+ assert self .model .state == "new"
8488
8589 def test_multiple_source_support_path_1_works (self ):
8690 self .model .publish ()
8791 self .model .steal ()
88- self .assertEqual ( self . model .state , "stolen" )
92+ assert self .model .state == "stolen"
8993
9094 def test_multiple_source_support_path_2_works (self ):
9195 self .model .publish ()
9296 self .model .hide ()
9397 self .model .steal ()
94- self .assertEqual ( self . model .state , "stolen" )
98+ assert self .model .state == "stolen"
9599
96100 def test_star_shortcut_succeed (self ):
97- self . assertTrue ( can_proceed (self .model .moderate ) )
101+ assert can_proceed (self .model .moderate )
98102 self .model .moderate ()
99- self .assertEqual ( self . model .state , "moderated" )
103+ assert self .model .state == "moderated"
100104
101105 def test_plus_shortcut_succeeds_for_other_source (self ):
102106 """Tests that the '+' shortcut succeeds for a source
103107 other than the target.
104108 """
105- self . assertTrue ( can_proceed (self .model .block ) )
109+ assert can_proceed (self .model .block )
106110 self .model .block ()
107- self .assertEqual ( self . model .state , "blocked" )
111+ assert self .model .state == "blocked"
108112
109113 def test_plus_shortcut_fails_for_same_source (self ):
110114 """Tests that the '+' shortcut fails if the source
111115 equals the target.
112116 """
113117 self .model .block ()
114- self .assertFalse (can_proceed (self .model .block ))
115- self .assertRaises (TransitionNotAllowed , self .model .block )
118+ assert not can_proceed (self .model .block )
119+ with pytest .raises (TransitionNotAllowed ):
120+ self .model .block ()
116121
117122 def test_empty_string_target (self ):
118123 self .model .empty ()
119- self .assertEqual ( self . model .state , "" )
124+ assert self .model .state == ""
120125
121126
122127class StateSignalsTests (TestCase ):
@@ -128,22 +133,23 @@ def setUp(self):
128133 post_transition .connect (self .on_post_transition , sender = SimpleBlogPost )
129134
130135 def on_pre_transition (self , sender , instance , name , source , target , ** kwargs ):
131- self . assertEqual ( instance .state , source )
136+ assert instance .state == source
132137 self .pre_transition_called = True
133138
134139 def on_post_transition (self , sender , instance , name , source , target , ** kwargs ):
135- self . assertEqual ( instance .state , target )
140+ assert instance .state == target
136141 self .post_transition_called = True
137142
138143 def test_signals_called_on_valid_transition (self ):
139144 self .model .publish ()
140- self .assertTrue ( self . pre_transition_called )
141- self .assertTrue ( self . post_transition_called )
145+ assert self .pre_transition_called
146+ assert self .post_transition_called
142147
143148 def test_signals_not_called_on_invalid_transition (self ):
144- self .assertRaises (TransitionNotAllowed , self .model .hide )
145- self .assertFalse (self .pre_transition_called )
146- self .assertFalse (self .post_transition_called )
149+ with pytest .raises (TransitionNotAllowed ):
150+ self .model .hide ()
151+ assert not self .pre_transition_called
152+ assert not self .post_transition_called
147153
148154
149155class TestFieldTransitionsInspect (TestCase ):
@@ -154,8 +160,8 @@ def test_in_operator_for_available_transitions(self):
154160 # store the generator in a list, so we can reuse the generator and do multiple asserts
155161 transitions = list (self .model .get_available_state_transitions ())
156162
157- self . assertIn ( "publish" , transitions )
158- self . assertNotIn ( "xyz" , transitions )
163+ assert "publish" in transitions
164+ assert "xyz" not in transitions
159165
160166 # inline method for faking the name of the transition
161167 def publish ():
@@ -171,13 +177,13 @@ def publish():
171177 custom = "" ,
172178 )
173179
174- self . assertTrue ( obj in transitions )
180+ assert obj in transitions
175181
176182 def test_available_conditions_from_new (self ):
177183 transitions = self .model .get_available_state_transitions ()
178184 actual = {(transition .source , transition .target ) for transition in transitions }
179185 expected = {("*" , "moderated" ), ("new" , "published" ), ("new" , "removed" ), ("*" , "" ), ("+" , "blocked" )}
180- self . assertEqual ( actual , expected )
186+ assert actual == expected
181187
182188 def test_available_conditions_from_published (self ):
183189 self .model .publish ()
@@ -191,37 +197,37 @@ def test_available_conditions_from_published(self):
191197 ("*" , "" ),
192198 ("+" , "blocked" ),
193199 }
194- self . assertEqual ( actual , expected )
200+ assert actual == expected
195201
196202 def test_available_conditions_from_hidden (self ):
197203 self .model .publish ()
198204 self .model .hide ()
199205 transitions = self .model .get_available_state_transitions ()
200206 actual = {(transition .source , transition .target ) for transition in transitions }
201207 expected = {("*" , "moderated" ), ("hidden" , "stolen" ), ("*" , "" ), ("+" , "blocked" )}
202- self . assertEqual ( actual , expected )
208+ assert actual == expected
203209
204210 def test_available_conditions_from_stolen (self ):
205211 self .model .publish ()
206212 self .model .steal ()
207213 transitions = self .model .get_available_state_transitions ()
208214 actual = {(transition .source , transition .target ) for transition in transitions }
209215 expected = {("*" , "moderated" ), ("*" , "" ), ("+" , "blocked" )}
210- self . assertEqual ( actual , expected )
216+ assert actual == expected
211217
212218 def test_available_conditions_from_blocked (self ):
213219 self .model .block ()
214220 transitions = self .model .get_available_state_transitions ()
215221 actual = {(transition .source , transition .target ) for transition in transitions }
216222 expected = {("*" , "moderated" ), ("*" , "" )}
217- self . assertEqual ( actual , expected )
223+ assert actual == expected
218224
219225 def test_available_conditions_from_empty (self ):
220226 self .model .empty ()
221227 transitions = self .model .get_available_state_transitions ()
222228 actual = {(transition .source , transition .target ) for transition in transitions }
223229 expected = {("*" , "moderated" ), ("*" , "" ), ("+" , "blocked" )}
224- self . assertEqual ( actual , expected )
230+ assert actual == expected
225231
226232 def test_all_conditions (self ):
227233 transitions = self .model .get_all_state_transitions ()
@@ -238,4 +244,4 @@ def test_all_conditions(self):
238244 ("*" , "" ),
239245 ("+" , "blocked" ),
240246 }
241- self . assertEqual ( actual , expected )
247+ assert actual == expected
0 commit comments