replaced unittest assertions pytest assertions (#26564)
This commit is contained in:
@@ -138,7 +138,7 @@ class ContentLibrariesRestApiTest(APITestCase):
|
||||
Like python 2's assertDictContainsSubset, but with the arguments in the
|
||||
correct order.
|
||||
"""
|
||||
self.assertGreaterEqual(big_dict.items(), subset_dict.items())
|
||||
assert big_dict.items() >= subset_dict.items()
|
||||
|
||||
# API helpers
|
||||
|
||||
@@ -147,10 +147,8 @@ class ContentLibrariesRestApiTest(APITestCase):
|
||||
Call a REST API
|
||||
"""
|
||||
response = getattr(self.client, method)(url, data, format="json")
|
||||
self.assertEqual(
|
||||
response.status_code, expect_response,
|
||||
"Unexpected response code {}:\n{}".format(response.status_code, getattr(response, 'data', '(no data)')),
|
||||
)
|
||||
assert response.status_code == expect_response,\
|
||||
'Unexpected response code {}:\n{}'.format(response.status_code, getattr(response, 'data', '(no data)'))
|
||||
return response.data
|
||||
|
||||
@contextmanager
|
||||
@@ -323,10 +321,8 @@ class ContentLibrariesRestApiTest(APITestCase):
|
||||
file_handle = BytesIO(content)
|
||||
url = URL_LIB_BLOCK_ASSET_FILE.format(block_key=block_key, file_name=file_name)
|
||||
response = self.client.put(url, data={"content": file_handle})
|
||||
self.assertEqual(
|
||||
response.status_code, expect_response,
|
||||
"Unexpected response code {}:\n{}".format(response.status_code, getattr(response, 'data', '(no data)')),
|
||||
)
|
||||
assert response.status_code == expect_response,\
|
||||
'Unexpected response code {}:\n{}'.format(response.status_code, getattr(response, 'data', '(no data)'))
|
||||
|
||||
def _delete_library_block_asset(self, block_key, file_name, expect_response=200):
|
||||
""" Delete a static asset file. """
|
||||
|
||||
@@ -131,17 +131,17 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
lib = self._create_library(
|
||||
slug=slug, title="A Test Library", description="Just Testing", library_type=start_type,
|
||||
)
|
||||
self.assertEqual(lib['type'], start_type)
|
||||
assert lib['type'] == start_type
|
||||
for block_type, block_slug in xblock_specs:
|
||||
self._add_block_to_library(lib['id'], block_type, block_slug)
|
||||
self._commit_library_changes(lib['id'])
|
||||
result = self._update_library(lib['id'], type=target_type, expect_response=expect_response)
|
||||
if expect_response == 200:
|
||||
self.assertEqual(result['type'], target_type)
|
||||
self.assertIn('type', result)
|
||||
assert result['type'] == target_type
|
||||
assert 'type' in result
|
||||
else:
|
||||
lib = self._get_library(lib['id'])
|
||||
self.assertEqual(lib['type'], start_type)
|
||||
assert lib['type'] == start_type
|
||||
|
||||
def test_no_convert_on_unpublished(self):
|
||||
"""
|
||||
@@ -153,7 +153,7 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
)
|
||||
self._add_block_to_library(lib['id'], "video", 'vid-block')
|
||||
result = self._update_library(lib['id'], type=VIDEO, expect_response=400)
|
||||
self.assertIn('type', result)
|
||||
assert 'type' in result
|
||||
|
||||
def test_no_convert_on_pending_deletes(self):
|
||||
"""
|
||||
@@ -167,7 +167,7 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
self._commit_library_changes(lib['id'])
|
||||
self._delete_library_block(block['id'])
|
||||
result = self._update_library(lib['id'], type=VIDEO, expect_response=400)
|
||||
self.assertIn('type', result)
|
||||
assert 'type' in result
|
||||
|
||||
def test_library_validation(self):
|
||||
"""
|
||||
@@ -195,30 +195,30 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
lib1['has_unpublished_deletes'] = lib2['has_unpublished_deletes'] = None
|
||||
|
||||
result = self._list_libraries()
|
||||
self.assertEqual(len(result), 2)
|
||||
self.assertIn(lib1, result)
|
||||
self.assertIn(lib2, result)
|
||||
assert len(result) == 2
|
||||
assert lib1 in result
|
||||
assert lib2 in result
|
||||
result = self._list_libraries({'pagination': 'true'})
|
||||
self.assertEqual(len(result['results']), 2)
|
||||
self.assertEqual(result['next'], None)
|
||||
assert len(result['results']) == 2
|
||||
assert result['next'] is None
|
||||
|
||||
# Create another library which causes number of libraries to exceed the page size
|
||||
self._create_library(slug="some-slug-3", title="Existing Library")
|
||||
# Verify that if `pagination` param isn't sent, API still honors the max page size.
|
||||
# This is for maintaining compatibility with older non pagination-aware clients.
|
||||
result = self._list_libraries()
|
||||
self.assertEqual(len(result), 2)
|
||||
assert len(result) == 2
|
||||
|
||||
# Pagination enabled:
|
||||
# Verify total elements and valid 'next' in page 1
|
||||
result = self._list_libraries({'pagination': 'true'})
|
||||
self.assertEqual(len(result['results']), 2)
|
||||
self.assertIn('page=2', result['next'])
|
||||
self.assertIn('pagination=true', result['next'])
|
||||
assert len(result['results']) == 2
|
||||
assert 'page=2' in result['next']
|
||||
assert 'pagination=true' in result['next']
|
||||
# Verify total elements and null 'next' in page 2
|
||||
result = self._list_libraries({'pagination': 'true', 'page': '2'})
|
||||
self.assertEqual(len(result['results']), 1)
|
||||
self.assertEqual(result['next'], None)
|
||||
assert len(result['results']) == 1
|
||||
assert result['next'] is None
|
||||
|
||||
@ddt.data(True, False)
|
||||
def test_library_filters(self, is_indexing_enabled):
|
||||
@@ -251,19 +251,17 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
org=f'org-test-{suffix}',
|
||||
)
|
||||
|
||||
self.assertEqual(len(self._list_libraries()), 5)
|
||||
self.assertEqual(len(self._list_libraries({'org': f'org-test-{suffix}'})), 2)
|
||||
self.assertEqual(len(self._list_libraries({'text_search': f'test-lib-filter-{suffix}'})), 2)
|
||||
self.assertEqual(len(self._list_libraries({'text_search': f'test-lib-filter-{suffix}', 'type': VIDEO})), 1)
|
||||
self.assertEqual(len(self._list_libraries({'text_search': f'library-title-{suffix}'})), 4)
|
||||
self.assertEqual(len(self._list_libraries({'text_search': f'library-title-{suffix}', 'type': VIDEO})), 2)
|
||||
self.assertEqual(len(self._list_libraries({'text_search': f'bar-{suffix}'})), 2)
|
||||
self.assertEqual(len(self._list_libraries({'text_search': f'org-test-{suffix}'})), 2)
|
||||
self.assertEqual(
|
||||
len(self._list_libraries({'org': f'org-test-{suffix}', 'text_search': f'library-title-{suffix}-4'})),
|
||||
1,
|
||||
)
|
||||
self.assertEqual(len(self._list_libraries({'type': VIDEO})), 3)
|
||||
assert len(self._list_libraries()) == 5
|
||||
assert len(self._list_libraries({'org': f'org-test-{suffix}'})) == 2
|
||||
assert len(self._list_libraries({'text_search': f'test-lib-filter-{suffix}'})) == 2
|
||||
assert len(self._list_libraries({'text_search': f'test-lib-filter-{suffix}', 'type': VIDEO})) == 1
|
||||
assert len(self._list_libraries({'text_search': f'library-title-{suffix}'})) == 4
|
||||
assert len(self._list_libraries({'text_search': f'library-title-{suffix}', 'type': VIDEO})) == 2
|
||||
assert len(self._list_libraries({'text_search': f'bar-{suffix}'})) == 2
|
||||
assert len(self._list_libraries({'text_search': f'org-test-{suffix}'})) == 2
|
||||
assert len(self._list_libraries({'org': f'org-test-{suffix}',
|
||||
'text_search': f'library-title-{suffix}-4'})) == 1
|
||||
assert len(self._list_libraries({'type': VIDEO})) == 3
|
||||
|
||||
# General Content Library XBlock tests:
|
||||
|
||||
@@ -274,10 +272,10 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
"""
|
||||
lib = self._create_library(slug="testlib1", title="A Test Library", description="Testing XBlocks")
|
||||
lib_id = lib["id"]
|
||||
self.assertEqual(lib["has_unpublished_changes"], False)
|
||||
assert lib['has_unpublished_changes'] is False
|
||||
|
||||
# A library starts out empty:
|
||||
self.assertEqual(self._get_library_blocks(lib_id), [])
|
||||
assert self._get_library_blocks(lib_id) == []
|
||||
|
||||
# Add a 'problem' XBlock to the library:
|
||||
block_data = self._add_block_to_library(lib_id, "problem", "problem1")
|
||||
@@ -290,23 +288,23 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
block_id = block_data["id"]
|
||||
# Confirm that the result contains a definition key, but don't check its value,
|
||||
# which for the purposes of these tests is an implementation detail.
|
||||
self.assertIn("def_key", block_data)
|
||||
assert 'def_key' in block_data
|
||||
|
||||
# now the library should contain one block and have unpublished changes:
|
||||
self.assertEqual(self._get_library_blocks(lib_id), [block_data])
|
||||
self.assertEqual(self._get_library(lib_id)["has_unpublished_changes"], True)
|
||||
assert self._get_library_blocks(lib_id) == [block_data]
|
||||
assert self._get_library(lib_id)['has_unpublished_changes'] is True
|
||||
|
||||
# Publish the changes:
|
||||
self._commit_library_changes(lib_id)
|
||||
self.assertEqual(self._get_library(lib_id)["has_unpublished_changes"], False)
|
||||
assert self._get_library(lib_id)['has_unpublished_changes'] is False
|
||||
# And now the block information should also show that block has no unpublished changes:
|
||||
block_data["has_unpublished_changes"] = False
|
||||
self.assertDictContainsEntries(self._get_library_block(block_id), block_data)
|
||||
self.assertEqual(self._get_library_blocks(lib_id), [block_data])
|
||||
assert self._get_library_blocks(lib_id) == [block_data]
|
||||
|
||||
# Now update the block's OLX:
|
||||
orig_olx = self._get_library_block_olx(block_id)
|
||||
self.assertIn("<problem", orig_olx)
|
||||
assert '<problem' in orig_olx
|
||||
new_olx = """
|
||||
<problem display_name="New Multi Choice Question" max_attempts="5">
|
||||
<multiplechoiceresponse>
|
||||
@@ -323,7 +321,7 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
""".strip()
|
||||
self._set_library_block_olx(block_id, new_olx)
|
||||
# now reading it back, we should get that exact OLX (no change to whitespace etc.):
|
||||
self.assertEqual(self._get_library_block_olx(block_id), new_olx)
|
||||
assert self._get_library_block_olx(block_id) == new_olx
|
||||
# And the display name and "unpublished changes" status of the block should be updated:
|
||||
self.assertDictContainsEntries(self._get_library_block(block_id), {
|
||||
"display_name": "New Multi Choice Question",
|
||||
@@ -332,27 +330,27 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
|
||||
# Now view the XBlock's student_view (including draft changes):
|
||||
fragment = self._render_block_view(block_id, "student_view")
|
||||
self.assertIn("resources", fragment)
|
||||
self.assertIn("Blockstore is designed to store.", fragment["content"])
|
||||
assert 'resources' in fragment
|
||||
assert 'Blockstore is designed to store.' in fragment['content']
|
||||
|
||||
# Also call a handler to make sure that's working:
|
||||
handler_url = self._get_block_handler_url(block_id, "xmodule_handler") + "problem_get"
|
||||
problem_get_response = self.client.get(handler_url)
|
||||
self.assertEqual(problem_get_response.status_code, 200)
|
||||
self.assertIn("You have used 0 of 5 attempts", problem_get_response.content.decode('utf-8'))
|
||||
assert problem_get_response.status_code == 200
|
||||
assert 'You have used 0 of 5 attempts' in problem_get_response.content.decode('utf-8')
|
||||
|
||||
# Now delete the block:
|
||||
self.assertEqual(self._get_library(lib_id)["has_unpublished_deletes"], False)
|
||||
assert self._get_library(lib_id)['has_unpublished_deletes'] is False
|
||||
self._delete_library_block(block_id)
|
||||
# Confirm it's deleted:
|
||||
self._render_block_view(block_id, "student_view", expect_response=404)
|
||||
self._get_library_block(block_id, expect_response=404)
|
||||
self.assertEqual(self._get_library(lib_id)["has_unpublished_deletes"], True)
|
||||
assert self._get_library(lib_id)['has_unpublished_deletes'] is True
|
||||
|
||||
# Now revert all the changes back until the last publish:
|
||||
self._revert_library_changes(lib_id)
|
||||
self.assertEqual(self._get_library(lib_id)["has_unpublished_deletes"], False)
|
||||
self.assertEqual(self._get_library_block_olx(block_id), orig_olx)
|
||||
assert self._get_library(lib_id)['has_unpublished_deletes'] is False
|
||||
assert self._get_library_block_olx(block_id) == orig_olx
|
||||
|
||||
# fin
|
||||
|
||||
@@ -370,24 +368,24 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
self._add_block_to_library(lib["id"], "problem", "problem2", parent_block=block2["id"])
|
||||
|
||||
result = self._get_library_blocks(lib["id"])
|
||||
self.assertEqual(len(result), 2)
|
||||
self.assertIn(block1, result)
|
||||
assert len(result) == 2
|
||||
assert block1 in result
|
||||
|
||||
result = self._get_library_blocks(lib["id"], {'pagination': 'true'})
|
||||
self.assertEqual(len(result['results']), 2)
|
||||
self.assertEqual(result['next'], None)
|
||||
assert len(result['results']) == 2
|
||||
assert result['next'] is None
|
||||
|
||||
self._add_block_to_library(lib["id"], "problem", "problem3")
|
||||
# Test pagination
|
||||
result = self._get_library_blocks(lib["id"])
|
||||
self.assertEqual(len(result), 3)
|
||||
assert len(result) == 3
|
||||
result = self._get_library_blocks(lib["id"], {'pagination': 'true'})
|
||||
self.assertEqual(len(result['results']), 2)
|
||||
self.assertIn('page=2', result['next'])
|
||||
self.assertIn('pagination=true', result['next'])
|
||||
assert len(result['results']) == 2
|
||||
assert 'page=2' in result['next']
|
||||
assert 'pagination=true' in result['next']
|
||||
result = self._get_library_blocks(lib["id"], {'pagination': 'true', 'page': '2'})
|
||||
self.assertEqual(len(result['results']), 1)
|
||||
self.assertEqual(result['next'], None)
|
||||
assert len(result['results']) == 1
|
||||
assert result['next'] is None
|
||||
|
||||
@ddt.data(True, False)
|
||||
def test_library_blocks_filters(self, is_indexing_enabled):
|
||||
@@ -404,19 +402,17 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
|
||||
self._set_library_block_olx(block1["id"], "<problem display_name=\"DisplayName\"></problem>")
|
||||
|
||||
self.assertEqual(len(self._get_library_blocks(lib["id"])), 5)
|
||||
self.assertEqual(len(self._get_library_blocks(lib["id"], {'text_search': 'Foo'})), 2)
|
||||
self.assertEqual(len(self._get_library_blocks(lib["id"], {'text_search': 'Display'})), 1)
|
||||
self.assertEqual(len(self._get_library_blocks(lib["id"], {'text_search': 'Video'})), 1)
|
||||
self.assertEqual(len(self._get_library_blocks(lib["id"], {'text_search': 'Foo', 'block_type': 'video'})), 0)
|
||||
self.assertEqual(len(self._get_library_blocks(lib["id"], {'text_search': 'Baz', 'block_type': 'video'})), 1)
|
||||
self.assertEqual(len(
|
||||
self._get_library_blocks(lib["id"], {'text_search': 'Baz', 'block_type': ['video', 'html']})),
|
||||
2,
|
||||
)
|
||||
self.assertEqual(len(self._get_library_blocks(lib["id"], {'block_type': 'video'})), 1)
|
||||
self.assertEqual(len(self._get_library_blocks(lib["id"], {'block_type': 'problem'})), 3)
|
||||
self.assertEqual(len(self._get_library_blocks(lib["id"], {'block_type': 'squirrel'})), 0)
|
||||
assert len(self._get_library_blocks(lib['id'])) == 5
|
||||
assert len(self._get_library_blocks(lib['id'], {'text_search': 'Foo'})) == 2
|
||||
assert len(self._get_library_blocks(lib['id'], {'text_search': 'Display'})) == 1
|
||||
assert len(self._get_library_blocks(lib['id'], {'text_search': 'Video'})) == 1
|
||||
assert len(self._get_library_blocks(lib['id'], {'text_search': 'Foo', 'block_type': 'video'})) == 0
|
||||
assert len(self._get_library_blocks(lib['id'], {'text_search': 'Baz', 'block_type': 'video'})) == 1
|
||||
assert len(self._get_library_blocks(lib['id'], {'text_search': 'Baz', 'block_type': ['video', 'html']})) ==\
|
||||
2
|
||||
assert len(self._get_library_blocks(lib['id'], {'block_type': 'video'})) == 1
|
||||
assert len(self._get_library_blocks(lib['id'], {'block_type': 'problem'})) == 3
|
||||
assert len(self._get_library_blocks(lib['id'], {'block_type': 'squirrel'})) == 0
|
||||
|
||||
@ddt.data(
|
||||
('video-problem', VIDEO, 'problem', 400),
|
||||
@@ -464,17 +460,14 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
""")
|
||||
|
||||
# Check the resulting OLX of the unit:
|
||||
self.assertEqual(self._get_library_block_olx(unit_block["id"]), (
|
||||
'<unit xblock-family="xblock.v1">\n'
|
||||
' <xblock-include definition="html/html1"/>\n'
|
||||
' <xblock-include definition="problem/problem1"/>\n'
|
||||
'</unit>\n'
|
||||
))
|
||||
assert self._get_library_block_olx(unit_block['id']) ==\
|
||||
'<unit xblock-family="xblock.v1">\n <xblock-include definition="html/html1"/>\n' \
|
||||
' <xblock-include definition="problem/problem1"/>\n</unit>\n'
|
||||
|
||||
# The unit can see and render its children:
|
||||
fragment = self._render_block_view(unit_block["id"], "student_view")
|
||||
self.assertIn("Hello world", fragment["content"])
|
||||
self.assertIn("What is an even number?", fragment["content"])
|
||||
assert 'Hello world' in fragment['content']
|
||||
assert 'What is an even number?' in fragment['content']
|
||||
|
||||
# We cannot add a duplicate ID to the library, either at the top level or as a child:
|
||||
self._add_block_to_library(lib_id, "problem", "problem1", expect_response=400)
|
||||
@@ -508,12 +501,12 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
lib = self._create_library(slug="permtest", title="Permission Test Library", description="Testing")
|
||||
lib_id = lib["id"]
|
||||
# By default, "public learning" and public read access are disallowed.
|
||||
self.assertEqual(lib["allow_public_learning"], False)
|
||||
self.assertEqual(lib["allow_public_read"], False)
|
||||
assert lib['allow_public_learning'] is False
|
||||
assert lib['allow_public_read'] is False
|
||||
|
||||
# By default, the creator of a new library is the only admin
|
||||
data = self._get_library_team(lib_id)
|
||||
self.assertEqual(len(data), 1)
|
||||
assert len(data) == 1
|
||||
self.assertDictContainsEntries(data[0], {
|
||||
"username": admin.username, "group_name": None, "access_level": "admin",
|
||||
})
|
||||
@@ -528,7 +521,7 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
self._set_group_access_level(lib_id, group.name, access_level="author")
|
||||
|
||||
team_response = self._get_library_team(lib_id)
|
||||
self.assertEqual(len(team_response), 4)
|
||||
assert len(team_response) == 4
|
||||
# We'll use this one later.
|
||||
reader_grant = {"username": reader.username, "group_name": None, "access_level": "read"}
|
||||
# The response should also always be sorted in a specific order (by username and group name):
|
||||
@@ -552,9 +545,9 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
with self.as_user(user):
|
||||
self._get_library(lib_id)
|
||||
data = self._get_library_team(lib_id)
|
||||
self.assertEqual(data, team_response)
|
||||
assert data == team_response
|
||||
data = self._get_user_access_level(lib_id, reader.username)
|
||||
self.assertEqual(data, {**reader_grant, 'username': 'Reader', 'email': 'reader@example.com'})
|
||||
assert data == {**reader_grant, 'username': 'Reader', 'email': 'reader@example.com'}
|
||||
|
||||
# A user with only read permission can get data about the library but not the team:
|
||||
with self.as_user(reader):
|
||||
@@ -586,8 +579,8 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
# Verify the permitted changes were made:
|
||||
with self.as_user(admin):
|
||||
data = self._get_library(lib_id)
|
||||
self.assertEqual(data["description"], "Revised description")
|
||||
self.assertEqual(data["title"], "New Library Title")
|
||||
assert data['description'] == 'Revised description'
|
||||
assert data['title'] == 'New Library Title'
|
||||
|
||||
# Library XBlock editing ###############################################
|
||||
|
||||
@@ -723,26 +716,26 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
|
||||
# The unit can see and render its children:
|
||||
fragment = self._render_block_view(unit_block["id"], "student_view")
|
||||
self.assertIn("What is an odd number?", fragment["content"])
|
||||
self.assertIn("What is an even number?", fragment["content"])
|
||||
self.assertIn("What holds this XBlock?", fragment["content"])
|
||||
assert 'What is an odd number?' in fragment['content']
|
||||
assert 'What is an even number?' in fragment['content']
|
||||
assert 'What holds this XBlock?' in fragment['content']
|
||||
|
||||
# Also check the API for retrieving links:
|
||||
links_created = self._get_library_links(lib_id)
|
||||
links_created.sort(key=lambda link: link["id"])
|
||||
self.assertEqual(len(links_created), 2)
|
||||
assert len(links_created) == 2
|
||||
|
||||
self.assertEqual(links_created[0]["id"], "problem_bank")
|
||||
self.assertEqual(links_created[0]["bundle_uuid"], bank_lib["bundle_uuid"])
|
||||
self.assertEqual(links_created[0]["version"], 2)
|
||||
self.assertEqual(links_created[0]["latest_version"], 2)
|
||||
self.assertEqual(links_created[0]["opaque_key"], bank_lib_id)
|
||||
assert links_created[0]['id'] == 'problem_bank'
|
||||
assert links_created[0]['bundle_uuid'] == bank_lib['bundle_uuid']
|
||||
assert links_created[0]['version'] == 2
|
||||
assert links_created[0]['latest_version'] == 2
|
||||
assert links_created[0]['opaque_key'] == bank_lib_id
|
||||
|
||||
self.assertEqual(links_created[1]["id"], "problem_bank_v1")
|
||||
self.assertEqual(links_created[1]["bundle_uuid"], bank_lib["bundle_uuid"])
|
||||
self.assertEqual(links_created[1]["version"], 1)
|
||||
self.assertEqual(links_created[1]["latest_version"], 2)
|
||||
self.assertEqual(links_created[1]["opaque_key"], bank_lib_id)
|
||||
assert links_created[1]['id'] == 'problem_bank_v1'
|
||||
assert links_created[1]['bundle_uuid'] == bank_lib['bundle_uuid']
|
||||
assert links_created[1]['version'] == 1
|
||||
assert links_created[1]['latest_version'] == 2
|
||||
assert links_created[1]['opaque_key'] == bank_lib_id
|
||||
|
||||
def test_library_blocks_limit(self):
|
||||
"""
|
||||
@@ -770,7 +763,7 @@ class ContentLibrariesTest(ContentLibrariesRestApiTest):
|
||||
lib = self._create_library(slug=slug, title='Test Block Types', library_type=library_type)
|
||||
types = self._get_library_block_types(lib['id'])
|
||||
if constrained:
|
||||
self.assertEqual(len(types), 1)
|
||||
self.assertEqual(types[0]['block_type'], library_type)
|
||||
assert len(types) == 1
|
||||
assert types[0]['block_type'] == library_type
|
||||
else:
|
||||
self.assertGreater(len(types), 1)
|
||||
assert len(types) > 1
|
||||
|
||||
@@ -38,15 +38,15 @@ class ContentLibraryIndexerTest(ContentLibrariesRestApiTest):
|
||||
library_key = LibraryLocatorV2.from_string(result['id'])
|
||||
response = ContentLibraryIndexer.get_items([library_key])[0]
|
||||
|
||||
self.assertEqual(response['id'], result['id'])
|
||||
self.assertEqual(response['title'], result['title'])
|
||||
self.assertEqual(response['description'], result['description'])
|
||||
self.assertEqual(response['uuid'], result['bundle_uuid'])
|
||||
self.assertEqual(response['num_blocks'], 0)
|
||||
self.assertEqual(response['version'], result['version'])
|
||||
self.assertEqual(response['last_published'], None)
|
||||
self.assertEqual(response['has_unpublished_changes'], False)
|
||||
self.assertEqual(response['has_unpublished_deletes'], False)
|
||||
assert response['id'] == result['id']
|
||||
assert response['title'] == result['title']
|
||||
assert response['description'] == result['description']
|
||||
assert response['uuid'] == result['bundle_uuid']
|
||||
assert response['num_blocks'] == 0
|
||||
assert response['version'] == result['version']
|
||||
assert response['last_published'] is None
|
||||
assert response['has_unpublished_changes'] is False
|
||||
assert response['has_unpublished_deletes'] is False
|
||||
|
||||
def test_schema_updates(self):
|
||||
"""
|
||||
@@ -56,15 +56,15 @@ class ContentLibraryIndexerTest(ContentLibrariesRestApiTest):
|
||||
new=0):
|
||||
result = self._create_library(slug="test-lib-schemaupdates-1", title="Title 1", description="Description")
|
||||
library_key = LibraryLocatorV2.from_string(result['id'])
|
||||
self.assertEqual(len(ContentLibraryIndexer.get_items([library_key])), 1)
|
||||
assert len(ContentLibraryIndexer.get_items([library_key])) == 1
|
||||
|
||||
with patch("openedx.core.djangoapps.content_libraries.libraries_index.ContentLibraryIndexer.SCHEMA_VERSION",
|
||||
new=1):
|
||||
self.assertEqual(len(ContentLibraryIndexer.get_items([library_key])), 0)
|
||||
assert len(ContentLibraryIndexer.get_items([library_key])) == 0
|
||||
|
||||
call_command("reindex_content_library", all=True, force=True)
|
||||
|
||||
self.assertEqual(len(ContentLibraryIndexer.get_items([library_key])), 1)
|
||||
assert len(ContentLibraryIndexer.get_items([library_key])) == 1
|
||||
|
||||
def test_remove_all_libraries(self):
|
||||
"""
|
||||
@@ -75,10 +75,10 @@ class ContentLibraryIndexerTest(ContentLibrariesRestApiTest):
|
||||
library_key1 = LibraryLocatorV2.from_string(lib1['id'])
|
||||
library_key2 = LibraryLocatorV2.from_string(lib2['id'])
|
||||
|
||||
self.assertEqual(len(ContentLibraryIndexer.get_items([library_key1, library_key2])), 2)
|
||||
assert len(ContentLibraryIndexer.get_items([library_key1, library_key2])) == 2
|
||||
|
||||
ContentLibraryIndexer.remove_all_items()
|
||||
self.assertEqual(len(ContentLibraryIndexer.get_items()), 0)
|
||||
assert len(ContentLibraryIndexer.get_items()) == 0
|
||||
|
||||
def test_update_libraries(self):
|
||||
"""
|
||||
@@ -91,18 +91,18 @@ class ContentLibraryIndexerTest(ContentLibrariesRestApiTest):
|
||||
|
||||
response = ContentLibraryIndexer.get_items([library_key])[0]
|
||||
|
||||
self.assertEqual(response['id'], lib['id'])
|
||||
self.assertEqual(response['title'], "New Title")
|
||||
self.assertEqual(response['description'], "New Title")
|
||||
self.assertEqual(response['uuid'], lib['bundle_uuid'])
|
||||
self.assertEqual(response['num_blocks'], 0)
|
||||
self.assertEqual(response['version'], lib['version'])
|
||||
self.assertEqual(response['last_published'], None)
|
||||
self.assertEqual(response['has_unpublished_changes'], False)
|
||||
self.assertEqual(response['has_unpublished_deletes'], False)
|
||||
assert response['id'] == lib['id']
|
||||
assert response['title'] == 'New Title'
|
||||
assert response['description'] == 'New Title'
|
||||
assert response['uuid'] == lib['bundle_uuid']
|
||||
assert response['num_blocks'] == 0
|
||||
assert response['version'] == lib['version']
|
||||
assert response['last_published'] is None
|
||||
assert response['has_unpublished_changes'] is False
|
||||
assert response['has_unpublished_deletes'] is False
|
||||
|
||||
self._delete_library(lib['id'])
|
||||
self.assertEqual(ContentLibraryIndexer.get_items([library_key]), [])
|
||||
assert ContentLibraryIndexer.get_items([library_key]) == []
|
||||
ContentLibraryIndexer.get_items([library_key])
|
||||
|
||||
def test_update_library_blocks(self):
|
||||
@@ -116,9 +116,9 @@ class ContentLibraryIndexerTest(ContentLibrariesRestApiTest):
|
||||
last_published = ContentLibraryIndexer.get_items([library_key])[0]['last_published']
|
||||
self._commit_library_changes(str(library_key))
|
||||
response = ContentLibraryIndexer.get_items([library_key])[0]
|
||||
self.assertEqual(response['has_unpublished_changes'], False)
|
||||
self.assertEqual(response['has_unpublished_deletes'], False)
|
||||
self.assertGreaterEqual(response['last_published'], last_published)
|
||||
assert response['has_unpublished_changes'] is False
|
||||
assert response['has_unpublished_deletes'] is False
|
||||
assert response['last_published'] >= last_published
|
||||
return response
|
||||
|
||||
def verify_uncommitted_libraries(library_key, has_unpublished_changes, has_unpublished_deletes):
|
||||
@@ -126,8 +126,8 @@ class ContentLibraryIndexerTest(ContentLibrariesRestApiTest):
|
||||
Verify uncommitted changes and deletes in the index
|
||||
"""
|
||||
response = ContentLibraryIndexer.get_items([library_key])[0]
|
||||
self.assertEqual(response['has_unpublished_changes'], has_unpublished_changes)
|
||||
self.assertEqual(response['has_unpublished_deletes'], has_unpublished_deletes)
|
||||
assert response['has_unpublished_changes'] == has_unpublished_changes
|
||||
assert response['has_unpublished_deletes'] == has_unpublished_deletes
|
||||
return response
|
||||
|
||||
lib = self._create_library(slug="test-lib-update-block", title="Title", description="Description")
|
||||
@@ -136,20 +136,20 @@ class ContentLibraryIndexerTest(ContentLibrariesRestApiTest):
|
||||
# Verify uncommitted new blocks
|
||||
block = self._add_block_to_library(lib['id'], "problem", "problem1")
|
||||
response = verify_uncommitted_libraries(library_key, True, False)
|
||||
self.assertEqual(response['last_published'], None)
|
||||
self.assertEqual(response['num_blocks'], 1)
|
||||
assert response['last_published'] is None
|
||||
assert response['num_blocks'] == 1
|
||||
# Verify committed new blocks
|
||||
self._commit_library_changes(lib['id'])
|
||||
response = verify_uncommitted_libraries(library_key, False, False)
|
||||
self.assertEqual(response['num_blocks'], 1)
|
||||
assert response['num_blocks'] == 1
|
||||
# Verify uncommitted deleted blocks
|
||||
self._delete_library_block(block['id'])
|
||||
response = verify_uncommitted_libraries(library_key, True, True)
|
||||
self.assertEqual(response['num_blocks'], 0)
|
||||
assert response['num_blocks'] == 0
|
||||
# Verify committed deleted blocks
|
||||
self._commit_library_changes(lib['id'])
|
||||
response = verify_uncommitted_libraries(library_key, False, False)
|
||||
self.assertEqual(response['num_blocks'], 0)
|
||||
assert response['num_blocks'] == 0
|
||||
|
||||
block = self._add_block_to_library(lib['id'], "problem", "problem1")
|
||||
self._commit_library_changes(lib['id'])
|
||||
@@ -201,17 +201,17 @@ class LibraryBlockIndexerTest(ContentLibrariesRestApiTest):
|
||||
block1 = self._add_block_to_library(lib['id'], "problem", "problem1")
|
||||
block2 = self._add_block_to_library(lib['id'], "problem", "problem2")
|
||||
|
||||
self.assertEqual(len(LibraryBlockIndexer.get_items()), 2)
|
||||
assert len(LibraryBlockIndexer.get_items()) == 2
|
||||
|
||||
for block in [block1, block2]:
|
||||
usage_key = LibraryUsageLocatorV2.from_string(block['id'])
|
||||
response = LibraryBlockIndexer.get_items([usage_key])[0]
|
||||
|
||||
self.assertEqual(response['id'], block['id'])
|
||||
self.assertEqual(response['def_key'], block['def_key'])
|
||||
self.assertEqual(response['block_type'], block['block_type'])
|
||||
self.assertEqual(response['display_name'], block['display_name'])
|
||||
self.assertEqual(response['has_unpublished_changes'], block['has_unpublished_changes'])
|
||||
assert response['id'] == block['id']
|
||||
assert response['def_key'] == block['def_key']
|
||||
assert response['block_type'] == block['block_type']
|
||||
assert response['display_name'] == block['display_name']
|
||||
assert response['has_unpublished_changes'] == block['has_unpublished_changes']
|
||||
|
||||
def test_schema_updates(self):
|
||||
"""
|
||||
@@ -221,15 +221,15 @@ class LibraryBlockIndexerTest(ContentLibrariesRestApiTest):
|
||||
with patch("openedx.core.djangoapps.content_libraries.libraries_index.LibraryBlockIndexer.SCHEMA_VERSION",
|
||||
new=0):
|
||||
block = self._add_block_to_library(lib['id'], "problem", "problem1")
|
||||
self.assertEqual(len(LibraryBlockIndexer.get_items([block['id']])), 1)
|
||||
assert len(LibraryBlockIndexer.get_items([block['id']])) == 1
|
||||
|
||||
with patch("openedx.core.djangoapps.content_libraries.libraries_index.LibraryBlockIndexer.SCHEMA_VERSION",
|
||||
new=1):
|
||||
self.assertEqual(len(LibraryBlockIndexer.get_items([block['id']])), 0)
|
||||
assert len(LibraryBlockIndexer.get_items([block['id']])) == 0
|
||||
|
||||
call_command("reindex_content_library", all=True, force=True)
|
||||
|
||||
self.assertEqual(len(LibraryBlockIndexer.get_items([block['id']])), 1)
|
||||
assert len(LibraryBlockIndexer.get_items([block['id']])) == 1
|
||||
|
||||
def test_remove_all_items(self):
|
||||
"""
|
||||
@@ -238,10 +238,10 @@ class LibraryBlockIndexerTest(ContentLibrariesRestApiTest):
|
||||
lib1 = self._create_library(slug="test-lib-rm-all", title="Title 1", description="Description")
|
||||
self._add_block_to_library(lib1['id'], "problem", "problem1")
|
||||
self._add_block_to_library(lib1['id'], "problem", "problem2")
|
||||
self.assertEqual(len(LibraryBlockIndexer.get_items()), 2)
|
||||
assert len(LibraryBlockIndexer.get_items()) == 2
|
||||
|
||||
LibraryBlockIndexer.remove_all_items()
|
||||
self.assertEqual(len(LibraryBlockIndexer.get_items()), 0)
|
||||
assert len(LibraryBlockIndexer.get_items()) == 0
|
||||
|
||||
def test_crud_block(self):
|
||||
"""
|
||||
@@ -253,29 +253,29 @@ class LibraryBlockIndexerTest(ContentLibrariesRestApiTest):
|
||||
# Update OLX, verify updates in index
|
||||
self._set_library_block_olx(block["id"], '<problem display_name="new_name"/>')
|
||||
response = LibraryBlockIndexer.get_items([block['id']])[0]
|
||||
self.assertEqual(response['display_name'], "new_name")
|
||||
self.assertEqual(response['has_unpublished_changes'], True)
|
||||
assert response['display_name'] == 'new_name'
|
||||
assert response['has_unpublished_changes'] is True
|
||||
|
||||
# Verify has_unpublished_changes after committing library
|
||||
self._commit_library_changes(lib['id'])
|
||||
response = LibraryBlockIndexer.get_items([block['id']])[0]
|
||||
self.assertEqual(response['has_unpublished_changes'], False)
|
||||
assert response['has_unpublished_changes'] is False
|
||||
|
||||
# Verify has_unpublished_changes after reverting library
|
||||
self._set_library_block_asset(block["id"], "whatever.png", b"data")
|
||||
response = LibraryBlockIndexer.get_items([block['id']])[0]
|
||||
self.assertEqual(response['has_unpublished_changes'], True)
|
||||
assert response['has_unpublished_changes'] is True
|
||||
|
||||
self._revert_library_changes(lib['id'])
|
||||
response = LibraryBlockIndexer.get_items([block['id']])[0]
|
||||
self.assertEqual(response['has_unpublished_changes'], False)
|
||||
assert response['has_unpublished_changes'] is False
|
||||
|
||||
# Verify that deleting block removes it from index
|
||||
self._delete_library_block(block['id'])
|
||||
self.assertEqual(LibraryBlockIndexer.get_items([block['id']]), [])
|
||||
assert LibraryBlockIndexer.get_items([block['id']]) == []
|
||||
|
||||
# Verify that deleting a library removes its blocks from index too
|
||||
self._add_block_to_library(lib['id'], "problem", "problem1")
|
||||
LibraryBlockIndexer.get_items([block['id']])
|
||||
self._delete_library(lib['id'])
|
||||
self.assertEqual(LibraryBlockIndexer.get_items([block['id']]), [])
|
||||
assert LibraryBlockIndexer.get_items([block['id']]) == []
|
||||
|
||||
@@ -99,11 +99,8 @@ class ContentLibraryRuntimeTest(ContentLibraryContentTestMixin, TestCase):
|
||||
# Load both blocks:
|
||||
unit_block = xblock_api.load_block(unit_block_key, self.student_a)
|
||||
unit_block2 = xblock_api.load_block(unit_block2_key, self.student_a)
|
||||
self.assertEqual(
|
||||
library_api.get_library_block_olx(unit_block_key),
|
||||
library_api.get_library_block_olx(unit_block2_key),
|
||||
)
|
||||
self.assertNotEqual(unit_block.children, unit_block2.children)
|
||||
assert library_api.get_library_block_olx(unit_block_key) == library_api.get_library_block_olx(unit_block2_key)
|
||||
assert unit_block.children != unit_block2.children
|
||||
|
||||
def test_has_score(self):
|
||||
"""
|
||||
@@ -116,10 +113,12 @@ class ContentLibraryRuntimeTest(ContentLibraryContentTestMixin, TestCase):
|
||||
unit_block = xblock_api.load_block(unit_block_key, self.student_a)
|
||||
problem_block = xblock_api.load_block(problem_block_key, self.student_a)
|
||||
|
||||
self.assertFalse(hasattr(UnitBlock, 'has_score')) # The block class doesn't declare 'has_score'
|
||||
self.assertEqual(unit_block.has_score, False) # But it gets added by the runtime and defaults to False
|
||||
assert not hasattr(UnitBlock, 'has_score')
|
||||
# The block class doesn't declare 'has_score'
|
||||
assert unit_block.has_score is False
|
||||
# But it gets added by the runtime and defaults to False
|
||||
# And problems do have has_score True:
|
||||
self.assertEqual(problem_block.has_score, True)
|
||||
assert problem_block.has_score is True
|
||||
|
||||
@skip_unless_cms # creating child blocks only works properly in Studio
|
||||
def test_xblock_metadata(self):
|
||||
@@ -154,23 +153,24 @@ class ContentLibraryRuntimeTest(ContentLibraryContentTestMixin, TestCase):
|
||||
URL_BLOCK_METADATA_URL.format(block_key=unit_block_key),
|
||||
{"include": "children,editable_children"},
|
||||
)
|
||||
self.assertEqual(metadata_view_result.data["children"], [str(problem_key)])
|
||||
self.assertEqual(metadata_view_result.data["editable_children"], [str(problem_key)])
|
||||
assert metadata_view_result.data['children'] == [str(problem_key)]
|
||||
assert metadata_view_result.data['editable_children'] == [str(problem_key)]
|
||||
|
||||
# Check the metadata API for the problem:
|
||||
metadata_view_result = client.get(
|
||||
URL_BLOCK_METADATA_URL.format(block_key=problem_key),
|
||||
{"include": "student_view_data,index_dictionary"},
|
||||
)
|
||||
self.assertEqual(metadata_view_result.data["block_id"], str(problem_key))
|
||||
self.assertEqual(metadata_view_result.data["display_name"], "New Multi Choice Question")
|
||||
self.assertNotIn("children", metadata_view_result.data)
|
||||
self.assertNotIn("editable_children", metadata_view_result.data)
|
||||
assert metadata_view_result.data['block_id'] == str(problem_key)
|
||||
assert metadata_view_result.data['display_name'] == 'New Multi Choice Question'
|
||||
assert 'children' not in metadata_view_result.data
|
||||
assert 'editable_children' not in metadata_view_result.data
|
||||
self.assertDictContainsSubset({
|
||||
"content_type": "CAPA",
|
||||
"problem_types": ["multiplechoiceresponse"],
|
||||
}, metadata_view_result.data["index_dictionary"])
|
||||
self.assertEqual(metadata_view_result.data["student_view_data"], None) # Capa doesn't provide student_view_data
|
||||
assert metadata_view_result.data['student_view_data'] is None
|
||||
# Capa doesn't provide student_view_data
|
||||
|
||||
|
||||
@requires_blockstore
|
||||
@@ -197,11 +197,11 @@ class ContentLibraryXBlockUserStateTest(ContentLibraryContentTestMixin, TestCase
|
||||
|
||||
block_alice = xblock_api.load_block(block_usage_key, self.student_a)
|
||||
|
||||
self.assertEqual(block_alice.scope_ids.user_id, self.student_a.id)
|
||||
self.assertEqual(block_alice.user_str, 'default value')
|
||||
self.assertEqual(block_alice.uss_str, 'default value')
|
||||
self.assertEqual(block_alice.pref_str, 'default value')
|
||||
self.assertEqual(block_alice.user_info_str, 'default value')
|
||||
assert block_alice.scope_ids.user_id == self.student_a.id
|
||||
assert block_alice.user_str == 'default value'
|
||||
assert block_alice.uss_str == 'default value'
|
||||
assert block_alice.pref_str == 'default value'
|
||||
assert block_alice.user_info_str == 'default value'
|
||||
|
||||
@XBlock.register_temp_plugin(UserStateTestBlock, UserStateTestBlock.BLOCK_TYPE)
|
||||
def test_modify_state_directly(self):
|
||||
@@ -226,30 +226,30 @@ class ContentLibraryXBlockUserStateTest(ContentLibraryContentTestMixin, TestCase
|
||||
# Now load it back and expect the same field data:
|
||||
block1_alice = xblock_api.load_block(block1_usage_key, self.student_a)
|
||||
|
||||
self.assertEqual(block1_alice.scope_ids.user_id, self.student_a.id)
|
||||
self.assertEqual(block1_alice.user_str, 'Alice was here')
|
||||
self.assertEqual(block1_alice.uss_str, 'Alice was here (USS)')
|
||||
self.assertEqual(block1_alice.pref_str, 'Alice was here (prefs)')
|
||||
self.assertEqual(block1_alice.user_info_str, 'Alice was here (user info)')
|
||||
assert block1_alice.scope_ids.user_id == self.student_a.id
|
||||
assert block1_alice.user_str == 'Alice was here'
|
||||
assert block1_alice.uss_str == 'Alice was here (USS)'
|
||||
assert block1_alice.pref_str == 'Alice was here (prefs)'
|
||||
assert block1_alice.user_info_str == 'Alice was here (user info)'
|
||||
|
||||
# Now load a different block for Alice:
|
||||
block2_alice = xblock_api.load_block(block2_usage_key, self.student_a)
|
||||
# User state should be default:
|
||||
self.assertEqual(block2_alice.user_str, 'default value')
|
||||
assert block2_alice.user_str == 'default value'
|
||||
# User state summary should be default:
|
||||
self.assertEqual(block2_alice.uss_str, 'default value')
|
||||
assert block2_alice.uss_str == 'default value'
|
||||
# But prefs and user info should be shared:
|
||||
self.assertEqual(block2_alice.pref_str, 'Alice was here (prefs)')
|
||||
self.assertEqual(block2_alice.user_info_str, 'Alice was here (user info)')
|
||||
assert block2_alice.pref_str == 'Alice was here (prefs)'
|
||||
assert block2_alice.user_info_str == 'Alice was here (user info)'
|
||||
|
||||
# Now load the first block, block1, for Bob:
|
||||
block1_bob = xblock_api.load_block(block1_usage_key, self.student_b)
|
||||
|
||||
self.assertEqual(block1_bob.scope_ids.user_id, self.student_b.id)
|
||||
self.assertEqual(block1_bob.user_str, 'default value')
|
||||
self.assertEqual(block1_bob.uss_str, 'Alice was here (USS)')
|
||||
self.assertEqual(block1_bob.pref_str, 'default value')
|
||||
self.assertEqual(block1_bob.user_info_str, 'default value')
|
||||
assert block1_bob.scope_ids.user_id == self.student_b.id
|
||||
assert block1_bob.user_str == 'default value'
|
||||
assert block1_bob.uss_str == 'Alice was here (USS)'
|
||||
assert block1_bob.pref_str == 'default value'
|
||||
assert block1_bob.user_info_str == 'default value'
|
||||
|
||||
@XBlock.register_temp_plugin(UserStateTestBlock, UserStateTestBlock.BLOCK_TYPE)
|
||||
def test_state_for_anonymous_users(self):
|
||||
@@ -273,7 +273,7 @@ class ContentLibraryXBlockUserStateTest(ContentLibraryContentTestMixin, TestCase
|
||||
url = url_result.data["handler_url"]
|
||||
data_json = json.dumps(data) if data else None
|
||||
response = getattr(client, method)(url, data_json, content_type="application/json")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
assert response.status_code == 200
|
||||
return response.json()
|
||||
|
||||
# Now client1 sets all the fields via a handler:
|
||||
@@ -286,33 +286,33 @@ class ContentLibraryXBlockUserStateTest(ContentLibraryContentTestMixin, TestCase
|
||||
|
||||
# Now load it back and expect the same data:
|
||||
data = call_handler(client1, block1_usage_key, "get_user_state", "get")
|
||||
self.assertEqual(data["user_str"], "1 was here")
|
||||
self.assertEqual(data["uss_str"], "1 was here (USS)")
|
||||
self.assertEqual(data["pref_str"], "1 was here (prefs)")
|
||||
self.assertEqual(data["user_info_str"], "1 was here (user info)")
|
||||
assert data['user_str'] == '1 was here'
|
||||
assert data['uss_str'] == '1 was here (USS)'
|
||||
assert data['pref_str'] == '1 was here (prefs)'
|
||||
assert data['user_info_str'] == '1 was here (user info)'
|
||||
|
||||
# Now load a different XBlock and expect only pref_str and user_info_str to be set:
|
||||
data = call_handler(client1, block2_usage_key, "get_user_state", "get")
|
||||
self.assertEqual(data["user_str"], "default value")
|
||||
self.assertEqual(data["uss_str"], "default value")
|
||||
self.assertEqual(data["pref_str"], "1 was here (prefs)")
|
||||
self.assertEqual(data["user_info_str"], "1 was here (user info)")
|
||||
assert data['user_str'] == 'default value'
|
||||
assert data['uss_str'] == 'default value'
|
||||
assert data['pref_str'] == '1 was here (prefs)'
|
||||
assert data['user_info_str'] == '1 was here (user info)'
|
||||
|
||||
# Now a different anonymous user loading the first block should see only the uss_str set:
|
||||
data = call_handler(client2, block1_usage_key, "get_user_state", "get")
|
||||
self.assertEqual(data["user_str"], "default value")
|
||||
self.assertEqual(data["uss_str"], "1 was here (USS)")
|
||||
self.assertEqual(data["pref_str"], "default value")
|
||||
self.assertEqual(data["user_info_str"], "default value")
|
||||
assert data['user_str'] == 'default value'
|
||||
assert data['uss_str'] == '1 was here (USS)'
|
||||
assert data['pref_str'] == 'default value'
|
||||
assert data['user_info_str'] == 'default value'
|
||||
|
||||
# The "user state summary" should not be shared between registered and anonymous users:
|
||||
client_registered = APIClient()
|
||||
client_registered.login(username=self.student_a.username, password='edx')
|
||||
data = call_handler(client_registered, block1_usage_key, "get_user_state", "get")
|
||||
self.assertEqual(data["user_str"], "default value")
|
||||
self.assertEqual(data["uss_str"], "default value")
|
||||
self.assertEqual(data["pref_str"], "default value")
|
||||
self.assertEqual(data["user_info_str"], "default value")
|
||||
assert data['user_str'] == 'default value'
|
||||
assert data['uss_str'] == 'default value'
|
||||
assert data['pref_str'] == 'default value'
|
||||
assert data['user_info_str'] == 'default value'
|
||||
|
||||
def test_views_for_anonymous_users(self):
|
||||
"""
|
||||
@@ -330,14 +330,14 @@ class ContentLibraryXBlockUserStateTest(ContentLibraryContentTestMixin, TestCase
|
||||
public_view_result = anon_client.get(
|
||||
URL_BLOCK_RENDER_VIEW.format(block_key=block_usage_key, view_name='public_view'),
|
||||
)
|
||||
self.assertEqual(public_view_result.status_code, 200)
|
||||
self.assertIn("Hello world", public_view_result.data["content"])
|
||||
assert public_view_result.status_code == 200
|
||||
assert 'Hello world' in public_view_result.data['content']
|
||||
|
||||
# Try to view the student_view:
|
||||
public_view_result = anon_client.get(
|
||||
URL_BLOCK_RENDER_VIEW.format(block_key=block_usage_key, view_name='student_view'),
|
||||
)
|
||||
self.assertEqual(public_view_result.status_code, 403)
|
||||
assert public_view_result.status_code == 403
|
||||
|
||||
@XBlock.register_temp_plugin(UserStateTestBlock, UserStateTestBlock.BLOCK_TYPE)
|
||||
def test_independent_instances(self):
|
||||
@@ -359,14 +359,14 @@ class ContentLibraryXBlockUserStateTest(ContentLibraryContentTestMixin, TestCase
|
||||
# block.
|
||||
|
||||
block_instance1.user_str = 'changed to this'
|
||||
self.assertNotEqual(block_instance1.user_str, block_instance2.user_str)
|
||||
assert block_instance1.user_str != block_instance2.user_str
|
||||
|
||||
block_instance1.save()
|
||||
self.assertNotEqual(block_instance1.user_str, block_instance2.user_str)
|
||||
assert block_instance1.user_str != block_instance2.user_str
|
||||
|
||||
block_instance2 = block_instance1.runtime.get_block(block_usage_key)
|
||||
# Now they should be equal, because we've saved and re-loaded instance2:
|
||||
self.assertEqual(block_instance1.user_str, block_instance2.user_str)
|
||||
assert block_instance1.user_str == block_instance2.user_str
|
||||
|
||||
@skip_unless_lms # Scores are only used in the LMS
|
||||
def test_scores_persisted(self):
|
||||
@@ -399,14 +399,14 @@ class ContentLibraryXBlockUserStateTest(ContentLibraryContentTestMixin, TestCase
|
||||
client.login(username=self.student_a.username, password='edx')
|
||||
student_view_result = client.get(URL_BLOCK_RENDER_VIEW.format(block_key=block_id, view_name='student_view'))
|
||||
problem_key = "input_{}_2_1".format(block_id)
|
||||
self.assertIn(problem_key, student_view_result.data["content"])
|
||||
assert problem_key in student_view_result.data['content']
|
||||
|
||||
# And submit a wrong answer:
|
||||
result = client.get(URL_BLOCK_GET_HANDLER_URL.format(block_key=block_id, handler_name='xmodule_handler'))
|
||||
problem_check_url = result.data["handler_url"] + 'problem_check'
|
||||
|
||||
submit_result = client.post(problem_check_url, data={problem_key: "choice_3"})
|
||||
self.assertEqual(submit_result.status_code, 200)
|
||||
assert submit_result.status_code == 200
|
||||
submit_data = json.loads(submit_result.content.decode('utf-8'))
|
||||
self.assertDictContainsSubset({
|
||||
"current_score": 0,
|
||||
@@ -417,12 +417,12 @@ class ContentLibraryXBlockUserStateTest(ContentLibraryContentTestMixin, TestCase
|
||||
# Now test that the score is also persisted in StudentModule:
|
||||
# If we add a REST API to get an individual block's score, that should be checked instead of StudentModule.
|
||||
sm = get_score(self.student_a, block_id)
|
||||
self.assertEqual(sm.grade, 0)
|
||||
self.assertEqual(sm.max_grade, 1)
|
||||
assert sm.grade == 0
|
||||
assert sm.max_grade == 1
|
||||
|
||||
# And submit a correct answer:
|
||||
submit_result = client.post(problem_check_url, data={problem_key: "choice_1"})
|
||||
self.assertEqual(submit_result.status_code, 200)
|
||||
assert submit_result.status_code == 200
|
||||
submit_data = json.loads(submit_result.content.decode('utf-8'))
|
||||
self.assertDictContainsSubset({
|
||||
"current_score": 1,
|
||||
@@ -432,8 +432,8 @@ class ContentLibraryXBlockUserStateTest(ContentLibraryContentTestMixin, TestCase
|
||||
# Now test that the score is also updated in StudentModule:
|
||||
# If we add a REST API to get an individual block's score, that should be checked instead of StudentModule.
|
||||
sm = get_score(self.student_a, block_id)
|
||||
self.assertEqual(sm.grade, 1)
|
||||
self.assertEqual(sm.max_grade, 1)
|
||||
assert sm.grade == 1
|
||||
assert sm.max_grade == 1
|
||||
|
||||
@skip_unless_lms
|
||||
def test_i18n(self):
|
||||
@@ -469,14 +469,14 @@ class ContentLibraryXBlockUserStateTest(ContentLibraryContentTestMixin, TestCase
|
||||
|
||||
# View the problem without specifying a language
|
||||
default_public_view = client.get(URL_BLOCK_RENDER_VIEW.format(block_key=block_id, view_name='public_view'))
|
||||
self.assertIn("Submit", default_public_view.data["content"])
|
||||
self.assertNotIn("Süßmït", default_public_view.data["content"])
|
||||
assert 'Submit' in default_public_view.data['content']
|
||||
assert 'Süßmït' not in default_public_view.data['content']
|
||||
|
||||
# View the problem and request the dummy language
|
||||
dummy_public_view = client.get(URL_BLOCK_RENDER_VIEW.format(block_key=block_id, view_name='public_view'),
|
||||
HTTP_ACCEPT_LANGUAGE='eo')
|
||||
self.assertIn("Süßmït", dummy_public_view.data["content"])
|
||||
self.assertNotIn("Submit", dummy_public_view.data["content"])
|
||||
assert 'Süßmït' in dummy_public_view.data['content']
|
||||
assert 'Submit' not in dummy_public_view.data['content']
|
||||
|
||||
|
||||
@requires_blockstore
|
||||
@@ -518,7 +518,7 @@ class ContentLibraryXBlockCompletionTest(ContentLibraryContentTestMixin, Complet
|
||||
return service.get_completions([block_id])[block_id]
|
||||
|
||||
# At first the block is not completed
|
||||
self.assertEqual(get_block_completion_status(), 0)
|
||||
assert get_block_completion_status() == 0
|
||||
|
||||
# Now call the 'publish_completion' handler:
|
||||
client = APIClient()
|
||||
@@ -528,7 +528,7 @@ class ContentLibraryXBlockCompletionTest(ContentLibraryContentTestMixin, Complet
|
||||
|
||||
# This will test the 'completion' service and the completion event handler:
|
||||
result2 = client.post(publish_completion_url, {"completion": 1.0}, format='json')
|
||||
self.assertEqual(result2.status_code, 200)
|
||||
assert result2.status_code == 200
|
||||
|
||||
# Now the block is completed
|
||||
self.assertEqual(get_block_completion_status(), 1)
|
||||
assert get_block_completion_status() == 1
|
||||
|
||||
@@ -47,7 +47,7 @@ class ContentLibrariesStaticAssetsTest(ContentLibrariesRestApiTest):
|
||||
file_name = "image.svg"
|
||||
|
||||
# A new block has no assets:
|
||||
self.assertEqual(self._get_library_block_assets(block_id), [])
|
||||
assert self._get_library_block_assets(block_id) == []
|
||||
self._get_library_block_asset(block_id, file_name, expect_response=404)
|
||||
|
||||
# Upload an asset file
|
||||
@@ -55,22 +55,22 @@ class ContentLibrariesStaticAssetsTest(ContentLibrariesRestApiTest):
|
||||
|
||||
# Get metadata about the uploaded asset file
|
||||
metadata = self._get_library_block_asset(block_id, file_name)
|
||||
self.assertEqual(metadata["path"], file_name)
|
||||
self.assertEqual(metadata["size"], len(SVG_DATA))
|
||||
assert metadata['path'] == file_name
|
||||
assert metadata['size'] == len(SVG_DATA)
|
||||
asset_list = self._get_library_block_assets(block_id)
|
||||
# We don't just assert that 'asset_list == [metadata]' because that may
|
||||
# break in the future if the "get asset" view returns more detail than
|
||||
# the "list assets" view.
|
||||
self.assertEqual(len(asset_list), 1)
|
||||
self.assertEqual(asset_list[0]["path"], metadata["path"])
|
||||
self.assertEqual(asset_list[0]["size"], metadata["size"])
|
||||
self.assertEqual(asset_list[0]["url"], metadata["url"])
|
||||
assert len(asset_list) == 1
|
||||
assert asset_list[0]['path'] == metadata['path']
|
||||
assert asset_list[0]['size'] == metadata['size']
|
||||
assert asset_list[0]['url'] == metadata['url']
|
||||
|
||||
# Download the file and check that it matches what was uploaded.
|
||||
# We need to download using requests since this is served by Blockstore,
|
||||
# which the django test client can't interact with.
|
||||
content_get_result = requests.get(metadata["url"])
|
||||
self.assertEqual(content_get_result.content, SVG_DATA)
|
||||
assert content_get_result.content == SVG_DATA
|
||||
|
||||
# Set some OLX referencing this asset:
|
||||
self._set_library_block_olx(block_id, """
|
||||
@@ -82,16 +82,16 @@ class ContentLibrariesStaticAssetsTest(ContentLibrariesRestApiTest):
|
||||
# served differently by Blockstore and we should test that too.
|
||||
self._commit_library_changes(library["id"])
|
||||
metadata = self._get_library_block_asset(block_id, file_name)
|
||||
self.assertEqual(metadata["path"], file_name)
|
||||
self.assertEqual(metadata["size"], len(SVG_DATA))
|
||||
assert metadata['path'] == file_name
|
||||
assert metadata['size'] == len(SVG_DATA)
|
||||
# Download the file from the new URL:
|
||||
content_get_result = requests.get(metadata["url"])
|
||||
self.assertEqual(content_get_result.content, SVG_DATA)
|
||||
assert content_get_result.content == SVG_DATA
|
||||
|
||||
# Check that the URL in the student_view gets rewritten:
|
||||
fragment = self._render_block_view(block_id, "student_view")
|
||||
self.assertNotIn("/static/image.svg", fragment["content"])
|
||||
self.assertIn(metadata["url"], fragment["content"])
|
||||
assert '/static/image.svg' not in fragment['content']
|
||||
assert metadata['url'] in fragment['content']
|
||||
|
||||
def test_asset_filenames(self):
|
||||
"""
|
||||
@@ -105,14 +105,14 @@ class ContentLibrariesStaticAssetsTest(ContentLibrariesRestApiTest):
|
||||
# Unicode names are allowed
|
||||
file_name = "🏕.svg" # (camping).svg
|
||||
self._set_library_block_asset(block_id, file_name, SVG_DATA)
|
||||
self.assertEqual(self._get_library_block_asset(block_id, file_name)["path"], file_name)
|
||||
self.assertEqual(self._get_library_block_asset(block_id, file_name)["size"], file_size)
|
||||
assert self._get_library_block_asset(block_id, file_name)['path'] == file_name
|
||||
assert self._get_library_block_asset(block_id, file_name)['size'] == file_size
|
||||
|
||||
# Subfolder names are allowed
|
||||
file_name = "transcripts/en.srt"
|
||||
self._set_library_block_asset(block_id, file_name, SVG_DATA)
|
||||
self.assertEqual(self._get_library_block_asset(block_id, file_name)["path"], file_name)
|
||||
self.assertEqual(self._get_library_block_asset(block_id, file_name)["size"], file_size)
|
||||
assert self._get_library_block_asset(block_id, file_name)['path'] == file_name
|
||||
assert self._get_library_block_asset(block_id, file_name)['size'] == file_size
|
||||
|
||||
# '../' is definitely not allowed
|
||||
file_name = "../definition.xml"
|
||||
@@ -148,8 +148,8 @@ class ContentLibrariesStaticAssetsTest(ContentLibrariesRestApiTest):
|
||||
"""
|
||||
url = transcript_handler_url + 'translation/en'
|
||||
response = self.client.get(url)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertIn("Welcome to edX", response.content.decode('utf-8'))
|
||||
assert response.status_code == 200
|
||||
assert 'Welcome to edX' in response.content.decode('utf-8')
|
||||
|
||||
def check_download():
|
||||
"""
|
||||
@@ -157,8 +157,8 @@ class ContentLibrariesStaticAssetsTest(ContentLibrariesRestApiTest):
|
||||
"""
|
||||
url = transcript_handler_url + 'download'
|
||||
response = self.client.get(url)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.content, TRANSCRIPT_DATA)
|
||||
assert response.status_code == 200
|
||||
assert response.content == TRANSCRIPT_DATA
|
||||
|
||||
check_sjson()
|
||||
check_download()
|
||||
|
||||
Reference in New Issue
Block a user