1
1
"""Test Workbench Runtime"""
2
2
3
3
4
+ from unittest import TestCase , mock
4
5
5
- from unittest import TestCase
6
-
7
- from unittest import mock
8
6
import pytest
9
7
from xblock .fields import Scope
10
8
from xblock .reference .user_service import UserService
@@ -34,47 +32,59 @@ def test_should_increment(self):
34
32
35
33
def test_slug_support (self ):
36
34
self .assertEqual (
37
- self .id_mgr .create_definition ("my_block" , "my_slug" ),
38
- ".my_block.my_slug.d0"
35
+ self .id_mgr .create_definition ("my_block" , "my_slug" ), ".my_block.my_slug.d0"
39
36
)
40
37
self .assertEqual (
41
- self .id_mgr .create_definition ("my_block" , "my_slug" ),
42
- ".my_block.my_slug.d1"
38
+ self .id_mgr .create_definition ("my_block" , "my_slug" ), ".my_block.my_slug.d1"
43
39
)
44
40
45
41
def test_scenario_support (self ):
46
42
self .test_should_increment ()
47
43
48
44
# Now that we have a scenario, our definition numbering starts over again.
49
45
self .id_mgr .set_scenario ("my_scenario" )
50
- self .assertEqual (self .id_mgr .create_definition ("my_block" ), "my_scenario.my_block.d0" )
51
- self .assertEqual (self .id_mgr .create_definition ("my_block" ), "my_scenario.my_block.d1" )
46
+ self .assertEqual (
47
+ self .id_mgr .create_definition ("my_block" ), "my_scenario.my_block.d0"
48
+ )
49
+ self .assertEqual (
50
+ self .id_mgr .create_definition ("my_block" ), "my_scenario.my_block.d1"
51
+ )
52
52
53
53
self .id_mgr .set_scenario ("another_scenario" )
54
- self .assertEqual (self .id_mgr .create_definition ("my_block" ), "another_scenario.my_block.d0" )
54
+ self .assertEqual (
55
+ self .id_mgr .create_definition ("my_block" ), "another_scenario.my_block.d0"
56
+ )
55
57
56
58
def test_usages (self ):
57
59
# Now make sure our usages are attached to definitions
58
60
self .assertIsNone (self .id_mgr .last_created_usage_id ())
59
61
self .assertEqual (
60
62
self .id_mgr .create_usage ("my_scenario.my_block.d0" ),
61
- "my_scenario.my_block.d0.u0"
63
+ "my_scenario.my_block.d0.u0" ,
62
64
)
63
65
self .assertEqual (
64
66
self .id_mgr .create_usage ("my_scenario.my_block.d0" ),
65
- "my_scenario.my_block.d0.u1"
67
+ "my_scenario.my_block.d0.u1" ,
68
+ )
69
+ self .assertEqual (
70
+ self .id_mgr .last_created_usage_id (), "my_scenario.my_block.d0.u1"
66
71
)
67
- self .assertEqual (self .id_mgr .last_created_usage_id (), "my_scenario.my_block.d0.u1" )
68
72
69
73
def test_asides (self ):
70
- definition_id = self .id_mgr .create_definition (' my_block' )
74
+ definition_id = self .id_mgr .create_definition (" my_block" )
71
75
usage_id = self .id_mgr .create_usage (definition_id )
72
76
73
- aside_definition , aside_usage = self .id_mgr .create_aside (definition_id , usage_id , 'my_aside' )
77
+ aside_definition , aside_usage = self .id_mgr .create_aside (
78
+ definition_id , usage_id , "my_aside"
79
+ )
74
80
75
- self .assertEqual (self .id_mgr .get_aside_type_from_definition (aside_definition ), 'my_aside' )
76
- self .assertEqual (self .id_mgr .get_definition_id_from_aside (aside_definition ), definition_id )
77
- self .assertEqual (self .id_mgr .get_aside_type_from_usage (aside_usage ), 'my_aside' )
81
+ self .assertEqual (
82
+ self .id_mgr .get_aside_type_from_definition (aside_definition ), "my_aside"
83
+ )
84
+ self .assertEqual (
85
+ self .id_mgr .get_definition_id_from_aside (aside_definition ), definition_id
86
+ )
87
+ self .assertEqual (self .id_mgr .get_aside_type_from_usage (aside_usage ), "my_aside" )
78
88
self .assertEqual (self .id_mgr .get_usage_id_from_aside (aside_usage ), usage_id )
79
89
80
90
@@ -88,24 +98,29 @@ def test_lti_consumer_xblock_requirements(self):
88
98
The LTI Consumer XBlock expects a lot of values from the LMS Runtime,
89
99
this test ensures that those requirements fulfilled.
90
100
"""
91
- runtime = WorkbenchRuntime ('test_user' )
92
- assert runtime .get_real_user (object ()), 'The LTI Consumer XBlock needs this method.'
93
- assert runtime .hostname , 'The LTI Consumer XBlock needs this property.'
94
- assert runtime .anonymous_student_id , 'The LTI Consumer XBlock needs this property.'
101
+ runtime = WorkbenchRuntime ("test_user" )
102
+ assert runtime .get_real_user (
103
+ object ()
104
+ ), "The LTI Consumer XBlock needs this method."
105
+ assert runtime .hostname , "The LTI Consumer XBlock needs this property."
106
+ assert (
107
+ runtime .anonymous_student_id
108
+ ), "The LTI Consumer XBlock needs this property."
95
109
96
110
97
111
class TestKVStore (TestCase ):
98
112
"""
99
113
Test the Workbench KVP Store
100
114
"""
115
+
101
116
def setUp (self ):
102
117
super ().setUp ()
103
118
self .kvs = WorkbenchDjangoKeyValueStore ()
104
119
self .key = KeyValueStore .Key (
105
120
scope = Scope .content ,
106
121
user_id = "rusty" ,
107
122
block_scope_id = "my_scenario.my_block.d0" ,
108
- field_name = "age"
123
+ field_name = "age" ,
109
124
)
110
125
111
126
@pytest .mark .django_db
@@ -119,11 +134,12 @@ def test_storage(self):
119
134
120
135
121
136
class StubService :
122
- """Empty service to test loading additional services. """
137
+ """Empty service to test loading additional services."""
123
138
124
139
125
140
class ExceptionService :
126
- """Stub service that raises an exception on init. """
141
+ """Stub service that raises an exception on init."""
142
+
127
143
def __init__ (self ):
128
144
raise Exception ("Kaboom!" )
129
145
@@ -138,55 +154,58 @@ def setUp(self):
138
154
self .xblock = mock .Mock ()
139
155
140
156
def test_default_services (self ):
141
- runtime = WorkbenchRuntime (' test_user' )
157
+ runtime = WorkbenchRuntime (" test_user" )
142
158
self ._assert_default_services (runtime )
143
159
144
- @mock .patch .dict (settings .WORKBENCH ['services' ], {
145
- 'stub' : 'workbench.test.test_runtime.StubService'
146
- })
160
+ @mock .patch .dict (
161
+ settings .WORKBENCH ["services" ],
162
+ {"stub" : "workbench.test.test_runtime.StubService" },
163
+ )
147
164
def test_settings_adds_services (self ):
148
- runtime = WorkbenchRuntime (' test_user' )
165
+ runtime = WorkbenchRuntime (" test_user" )
149
166
150
167
# Default services should still be available
151
168
self ._assert_default_services (runtime )
152
169
153
170
# An additional service should be provided
154
- self ._assert_service (runtime , ' stub' , StubService )
171
+ self ._assert_service (runtime , " stub" , StubService )
155
172
156
173
# Check that the service has the runtime attribute set
157
- service = runtime .service (self .xblock , ' stub' )
174
+ service = runtime .service (self .xblock , " stub" )
158
175
self .assertIs (service .runtime , runtime )
159
176
160
- @mock .patch .dict (settings .WORKBENCH ['services' ], {
161
- 'not_found' : 'workbench.test.test_runtime.NotFoundService'
162
- })
177
+ @mock .patch .dict (
178
+ settings .WORKBENCH ["services" ],
179
+ {"not_found" : "workbench.test.test_runtime.NotFoundService" },
180
+ )
163
181
def test_could_not_find_service (self ):
164
- runtime = WorkbenchRuntime (' test_user' )
182
+ runtime = WorkbenchRuntime (" test_user" )
165
183
166
184
# Default services should still be available
167
185
self ._assert_default_services (runtime )
168
186
169
187
# The additional service should NOT be available
170
- self .assertIs (runtime .service (self .xblock , ' not_found' ), None )
188
+ self .assertIs (runtime .service (self .xblock , " not_found" ), None )
171
189
172
- @mock .patch .dict (settings .WORKBENCH ['services' ], {
173
- 'exception' : 'workbench.test.test_runtime.ExceptionService'
174
- })
190
+ @mock .patch .dict (
191
+ settings .WORKBENCH ["services" ],
192
+ {"exception" : "workbench.test.test_runtime.ExceptionService" },
193
+ )
175
194
def test_runtime_service_initialization_failed (self ):
176
- runtime = WorkbenchRuntime (' test_user' )
195
+ runtime = WorkbenchRuntime (" test_user" )
177
196
178
197
# Default services should still be available
179
198
self ._assert_default_services (runtime )
180
199
181
200
# The additional service should NOT be available
182
- self .assertIs (runtime .service (self .xblock , ' exception' ), None )
201
+ self .assertIs (runtime .service (self .xblock , " exception" ), None )
183
202
184
203
def _assert_default_services (self , runtime ):
185
- """Check that the default services are available. """
186
- self ._assert_service (runtime , ' field-data' , KvsFieldData )
187
- self ._assert_service (runtime , ' user' , UserService )
204
+ """Check that the default services are available."""
205
+ self ._assert_service (runtime , " field-data" , KvsFieldData )
206
+ self ._assert_service (runtime , " user" , UserService )
188
207
189
208
def _assert_service (self , runtime , service_name , service_class ):
190
- """Check that a service is loaded. """
209
+ """Check that a service is loaded."""
191
210
service_instance = runtime .service (self .xblock , service_name )
192
211
self .assertIsInstance (service_instance , service_class )
0 commit comments