1
1
from __future__ import annotations
2
2
3
+ import pytest
3
4
from django .db import models
4
5
from django .test import TestCase
5
6
@@ -53,70 +54,74 @@ def setUp(self):
53
54
self .model = SimpleBlogPost ()
54
55
55
56
def test_initial_state_instantiated (self ):
56
- self .assertEqual ( self . model .state , "new" )
57
+ assert self .model .state == "new"
57
58
58
59
def test_known_transition_should_succeed (self ):
59
- self . assertTrue ( can_proceed (self .model .publish ) )
60
+ assert can_proceed (self .model .publish )
60
61
self .model .publish ()
61
- self .assertEqual ( self . model .state , "published" )
62
+ assert self .model .state == "published"
62
63
63
- self . assertTrue ( can_proceed (self .model .hide ) )
64
+ assert can_proceed (self .model .hide )
64
65
self .model .hide ()
65
- self .assertEqual ( self . model .state , "hidden" )
66
+ assert self .model .state == "hidden"
66
67
67
68
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 ()
70
72
71
73
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"
75
78
76
79
def test_allowed_null_transition_should_succeed (self ):
77
80
self .model .publish ()
78
81
self .model .notify_all ()
79
- self .assertEqual ( self . model .state , "published" )
82
+ assert self .model .state == "published"
80
83
81
84
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"
84
88
85
89
def test_multiple_source_support_path_1_works (self ):
86
90
self .model .publish ()
87
91
self .model .steal ()
88
- self .assertEqual ( self . model .state , "stolen" )
92
+ assert self .model .state == "stolen"
89
93
90
94
def test_multiple_source_support_path_2_works (self ):
91
95
self .model .publish ()
92
96
self .model .hide ()
93
97
self .model .steal ()
94
- self .assertEqual ( self . model .state , "stolen" )
98
+ assert self .model .state == "stolen"
95
99
96
100
def test_star_shortcut_succeed (self ):
97
- self . assertTrue ( can_proceed (self .model .moderate ) )
101
+ assert can_proceed (self .model .moderate )
98
102
self .model .moderate ()
99
- self .assertEqual ( self . model .state , "moderated" )
103
+ assert self .model .state == "moderated"
100
104
101
105
def test_plus_shortcut_succeeds_for_other_source (self ):
102
106
"""Tests that the '+' shortcut succeeds for a source
103
107
other than the target.
104
108
"""
105
- self . assertTrue ( can_proceed (self .model .block ) )
109
+ assert can_proceed (self .model .block )
106
110
self .model .block ()
107
- self .assertEqual ( self . model .state , "blocked" )
111
+ assert self .model .state == "blocked"
108
112
109
113
def test_plus_shortcut_fails_for_same_source (self ):
110
114
"""Tests that the '+' shortcut fails if the source
111
115
equals the target.
112
116
"""
113
117
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 ()
116
121
117
122
def test_empty_string_target (self ):
118
123
self .model .empty ()
119
- self .assertEqual ( self . model .state , "" )
124
+ assert self .model .state == ""
120
125
121
126
122
127
class StateSignalsTests (TestCase ):
@@ -128,22 +133,23 @@ def setUp(self):
128
133
post_transition .connect (self .on_post_transition , sender = SimpleBlogPost )
129
134
130
135
def on_pre_transition (self , sender , instance , name , source , target , ** kwargs ):
131
- self . assertEqual ( instance .state , source )
136
+ assert instance .state == source
132
137
self .pre_transition_called = True
133
138
134
139
def on_post_transition (self , sender , instance , name , source , target , ** kwargs ):
135
- self . assertEqual ( instance .state , target )
140
+ assert instance .state == target
136
141
self .post_transition_called = True
137
142
138
143
def test_signals_called_on_valid_transition (self ):
139
144
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
142
147
143
148
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
147
153
148
154
149
155
class TestFieldTransitionsInspect (TestCase ):
@@ -154,8 +160,8 @@ def test_in_operator_for_available_transitions(self):
154
160
# store the generator in a list, so we can reuse the generator and do multiple asserts
155
161
transitions = list (self .model .get_available_state_transitions ())
156
162
157
- self . assertIn ( "publish" , transitions )
158
- self . assertNotIn ( "xyz" , transitions )
163
+ assert "publish" in transitions
164
+ assert "xyz" not in transitions
159
165
160
166
# inline method for faking the name of the transition
161
167
def publish ():
@@ -171,13 +177,13 @@ def publish():
171
177
custom = "" ,
172
178
)
173
179
174
- self . assertTrue ( obj in transitions )
180
+ assert obj in transitions
175
181
176
182
def test_available_conditions_from_new (self ):
177
183
transitions = self .model .get_available_state_transitions ()
178
184
actual = {(transition .source , transition .target ) for transition in transitions }
179
185
expected = {("*" , "moderated" ), ("new" , "published" ), ("new" , "removed" ), ("*" , "" ), ("+" , "blocked" )}
180
- self . assertEqual ( actual , expected )
186
+ assert actual == expected
181
187
182
188
def test_available_conditions_from_published (self ):
183
189
self .model .publish ()
@@ -191,37 +197,37 @@ def test_available_conditions_from_published(self):
191
197
("*" , "" ),
192
198
("+" , "blocked" ),
193
199
}
194
- self . assertEqual ( actual , expected )
200
+ assert actual == expected
195
201
196
202
def test_available_conditions_from_hidden (self ):
197
203
self .model .publish ()
198
204
self .model .hide ()
199
205
transitions = self .model .get_available_state_transitions ()
200
206
actual = {(transition .source , transition .target ) for transition in transitions }
201
207
expected = {("*" , "moderated" ), ("hidden" , "stolen" ), ("*" , "" ), ("+" , "blocked" )}
202
- self . assertEqual ( actual , expected )
208
+ assert actual == expected
203
209
204
210
def test_available_conditions_from_stolen (self ):
205
211
self .model .publish ()
206
212
self .model .steal ()
207
213
transitions = self .model .get_available_state_transitions ()
208
214
actual = {(transition .source , transition .target ) for transition in transitions }
209
215
expected = {("*" , "moderated" ), ("*" , "" ), ("+" , "blocked" )}
210
- self . assertEqual ( actual , expected )
216
+ assert actual == expected
211
217
212
218
def test_available_conditions_from_blocked (self ):
213
219
self .model .block ()
214
220
transitions = self .model .get_available_state_transitions ()
215
221
actual = {(transition .source , transition .target ) for transition in transitions }
216
222
expected = {("*" , "moderated" ), ("*" , "" )}
217
- self . assertEqual ( actual , expected )
223
+ assert actual == expected
218
224
219
225
def test_available_conditions_from_empty (self ):
220
226
self .model .empty ()
221
227
transitions = self .model .get_available_state_transitions ()
222
228
actual = {(transition .source , transition .target ) for transition in transitions }
223
229
expected = {("*" , "moderated" ), ("*" , "" ), ("+" , "blocked" )}
224
- self . assertEqual ( actual , expected )
230
+ assert actual == expected
225
231
226
232
def test_all_conditions (self ):
227
233
transitions = self .model .get_all_state_transitions ()
@@ -238,4 +244,4 @@ def test_all_conditions(self):
238
244
("*" , "" ),
239
245
("+" , "blocked" ),
240
246
}
241
- self . assertEqual ( actual , expected )
247
+ assert actual == expected
0 commit comments