INCR-286 Run python-modernize on lms/djangoapps/course_api/blocks/tests (#20593)

* run python modernize

* run isort

* Fix quality
This commit is contained in:
Stu Young
2019-05-21 11:24:39 -04:00
committed by Christie Rice
parent cea9390cdc
commit b0e6870271
5 changed files with 49 additions and 38 deletions

View File

@@ -2,6 +2,8 @@
Helper functions for unit tests
"""
from __future__ import absolute_import
from opaque_keys.edx.keys import UsageKey

View File

@@ -2,11 +2,14 @@
Tests for Blocks api.py
"""
from __future__ import absolute_import
from itertools import product
from mock import patch
import ddt
import six
from django.test.client import RequestFactory
from mock import patch
from openedx.core.djangoapps.content.block_structure.api import clear_course_from_cache
from openedx.core.djangoapps.content.block_structure.config import STORAGE_BACKING_FOR_CACHE, waffle
@@ -16,7 +19,6 @@ from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import SampleCourseFactory, check_mongo_calls
from xmodule.modulestore.tests.sample_courses import BlockInfo
from ..api import get_blocks
@@ -44,15 +46,15 @@ class TestGetBlocks(SharedModuleStoreTestCase):
def test_basic(self):
blocks = get_blocks(self.request, self.course.location, self.user)
self.assertEquals(blocks['root'], unicode(self.course.location))
self.assertEquals(blocks['root'], six.text_type(self.course.location))
# subtract for (1) the orphaned course About block and (2) the hidden Html block
self.assertEquals(len(blocks['blocks']), len(self.store.get_items(self.course.id)) - 2)
self.assertNotIn(unicode(self.html_block.location), blocks['blocks'])
self.assertNotIn(six.text_type(self.html_block.location), blocks['blocks'])
def test_no_user(self):
blocks = get_blocks(self.request, self.course.location)
self.assertIn(unicode(self.html_block.location), blocks['blocks'])
self.assertIn(six.text_type(self.html_block.location), blocks['blocks'])
def test_access_before_api_transformer_order(self):
"""
@@ -63,16 +65,16 @@ class TestGetBlocks(SharedModuleStoreTestCase):
vertical_block = self.store.get_item(self.course.id.make_usage_key('vertical', 'vertical_x1a'))
problem_block = self.store.get_item(self.course.id.make_usage_key('problem', 'problem_x1a_1'))
vertical_descendants = blocks['blocks'][unicode(vertical_block.location)]['descendants']
vertical_descendants = blocks['blocks'][six.text_type(vertical_block.location)]['descendants']
self.assertIn(unicode(problem_block.location), vertical_descendants)
self.assertNotIn(unicode(self.html_block.location), vertical_descendants)
self.assertIn(six.text_type(problem_block.location), vertical_descendants)
self.assertNotIn(six.text_type(self.html_block.location), vertical_descendants)
def test_sub_structure(self):
sequential_block = self.store.get_item(self.course.id.make_usage_key('sequential', 'sequential_y1'))
blocks = get_blocks(self.request, sequential_block.location, self.user)
self.assertEquals(blocks['root'], unicode(sequential_block.location))
self.assertEquals(blocks['root'], six.text_type(sequential_block.location))
self.assertEquals(len(blocks['blocks']), 5)
for block_type, block_name, is_inside_of_structure in (
@@ -83,9 +85,9 @@ class TestGetBlocks(SharedModuleStoreTestCase):
):
block = self.store.get_item(self.course.id.make_usage_key(block_type, block_name))
if is_inside_of_structure:
self.assertIn(unicode(block.location), blocks['blocks'])
self.assertIn(six.text_type(block.location), blocks['blocks'])
else:
self.assertNotIn(unicode(block.location), blocks['blocks'])
self.assertNotIn(six.text_type(block.location), blocks['blocks'])
def test_filtering_by_block_types(self):
sequential_block = self.store.get_item(self.course.id.make_usage_key('sequential', 'sequential_y1'))
@@ -94,7 +96,7 @@ class TestGetBlocks(SharedModuleStoreTestCase):
blocks = get_blocks(self.request, sequential_block.location, self.user, requested_fields=['type'])
self.assertEquals(len(blocks['blocks']), 5)
found_not_problem = False
for block in blocks['blocks'].itervalues():
for block in six.itervalues(blocks['blocks']):
if block['type'] != 'problem':
found_not_problem = True
self.assertTrue(found_not_problem)
@@ -103,7 +105,7 @@ class TestGetBlocks(SharedModuleStoreTestCase):
blocks = get_blocks(self.request, sequential_block.location, self.user,
block_types_filter=['problem'], requested_fields=['type'])
self.assertEquals(len(blocks['blocks']), 3)
for block in blocks['blocks'].itervalues():
for block in six.itervalues(blocks['blocks']):
self.assertEqual(block['type'], 'problem')

View File

@@ -1,9 +1,11 @@
"""
Tests for Course Blocks forms
"""
from urllib import urlencode
from __future__ import absolute_import
import ddt
import six
from six.moves.urllib.parse import urlencode # pylint: disable=import-error
from django.http import Http404, QueryDict
from opaque_keys.edx.locator import CourseLocator
from rest_framework.exceptions import PermissionDenied
@@ -46,7 +48,7 @@ class TestBlockListGetForm(FormTestMixin, SharedModuleStoreTestCase):
self.form_data = QueryDict(
urlencode({
'username': self.student.username,
'usage_key': unicode(usage_key),
'usage_key': six.text_type(usage_key),
}),
mutable=True,
)

View File

@@ -1,6 +1,9 @@
"""
Tests for Course Blocks serializers
"""
from __future__ import absolute_import
import six
from mock import MagicMock
from lms.djangoapps.course_blocks.api import get_course_block_access_transformers, get_course_blocks
@@ -66,7 +69,7 @@ class TestBlockSerializerBase(SharedModuleStoreTestCase):
serialized_block['type'],
)
self.assertEquals(
set(serialized_block.iterkeys()),
set(six.iterkeys(serialized_block)),
{'id', 'block_id', 'type', 'lms_web_url', 'student_view_url'},
)
@@ -100,7 +103,7 @@ class TestBlockSerializerBase(SharedModuleStoreTestCase):
'lti_url',
'visible_to_staff_only',
},
set(serialized_block.iterkeys()),
set(six.iterkeys(serialized_block)),
)
# video blocks should have student_view_data
@@ -142,7 +145,7 @@ class TestBlockSerializerBase(SharedModuleStoreTestCase):
"""
Test fields accessed by a staff user
"""
if serialized_block['id'] == unicode(self.html_block.location):
if serialized_block['id'] == six.text_type(self.html_block.location):
self.assertTrue(serialized_block['visible_to_staff_only'])
else:
self.assertFalse(serialized_block['visible_to_staff_only'])
@@ -208,10 +211,10 @@ class TestBlockDictSerializer(TestBlockSerializerBase):
serializer = self.create_serializer()
# verify root
self.assertEquals(serializer.data['root'], unicode(self.block_structure.root_block_usage_key))
self.assertEquals(serializer.data['root'], six.text_type(self.block_structure.root_block_usage_key))
# verify blocks
for block_key_string, serialized_block in serializer.data['blocks'].iteritems():
for block_key_string, serialized_block in six.iteritems(serializer.data['blocks']):
self.assertEquals(serialized_block['id'], block_key_string)
self.assert_basic_block(block_key_string, serialized_block)
self.assertEquals(len(serializer.data['blocks']), 28)
@@ -219,7 +222,7 @@ class TestBlockDictSerializer(TestBlockSerializerBase):
def test_additional_requested_fields(self):
self.add_additional_requested_fields()
serializer = self.create_serializer()
for serialized_block in serializer.data['blocks'].itervalues():
for serialized_block in six.itervalues(serializer.data['blocks']):
self.assert_extended_block(serialized_block)
self.assertEquals(len(serializer.data['blocks']), 28)
@@ -230,7 +233,7 @@ class TestBlockDictSerializer(TestBlockSerializerBase):
context = self.create_staff_context()
self.add_additional_requested_fields(context)
serializer = self.create_serializer(context)
for serialized_block in serializer.data['blocks'].itervalues():
for serialized_block in six.itervalues(serializer.data['blocks']):
self.assert_extended_block(serialized_block)
self.assert_staff_fields(serialized_block)
self.assertEquals(len(serializer.data['blocks']), 29)

View File

@@ -1,11 +1,13 @@
"""
Tests for Blocks Views
"""
from __future__ import absolute_import
from datetime import datetime
from string import join
from urllib import urlencode
from urlparse import urlunparse
import six
from six.moves.urllib.parse import urlencode, urlunparse # pylint: disable=import-error
from django.urls import reverse
from opaque_keys.edx.locator import CourseLocator
@@ -37,7 +39,7 @@ class TestBlocksView(SharedModuleStoreTestCase):
cls.course_usage_key = cls.store.make_course_usage_key(cls.course_key)
cls.non_orphaned_block_usage_keys = set(
unicode(item.location)
six.text_type(item.location)
for item in cls.store.get_items(cls.course_key)
# remove all orphaned items in the course, except for the root 'course' block
if cls.store.get_parent_location(item.location) or item.category == 'course'
@@ -54,7 +56,7 @@ class TestBlocksView(SharedModuleStoreTestCase):
# default values for url and query_params
self.url = reverse(
'blocks_in_block_tree',
kwargs={'usage_key_string': unicode(self.course_usage_key)}
kwargs={'usage_key_string': six.text_type(self.course_usage_key)}
)
self.query_params = {'depth': 'all', 'username': self.user.username}
@@ -93,7 +95,7 @@ class TestBlocksView(SharedModuleStoreTestCase):
Verify that the response contains the expected blocks
"""
self.assertSetEqual(
set(response.data['blocks'].iterkeys()),
set(six.iterkeys(response.data['blocks'])),
self.non_orphaned_block_usage_keys,
)
@@ -102,7 +104,7 @@ class TestBlocksView(SharedModuleStoreTestCase):
Verify the response has the expected structure
"""
self.verify_response_block_dict(response)
for block_key_string, block_data in response.data['blocks'].iteritems():
for block_key_string, block_data in six.iteritems(response.data['blocks']):
block_key = deserialize_usage_key(block_key_string, self.course_key)
xblock = self.store.get_item(block_key)
@@ -115,7 +117,7 @@ class TestBlocksView(SharedModuleStoreTestCase):
if xblock.has_children:
self.assertSetEqual(
set(unicode(child.location) for child in xblock.get_children()),
set(six.text_type(child.location) for child in xblock.get_children()),
set(block_data['children']),
)
@@ -159,7 +161,7 @@ class TestBlocksView(SharedModuleStoreTestCase):
usage_key = self.store.make_course_usage_key(CourseLocator('non', 'existent', 'course'))
url = reverse(
'blocks_in_block_tree',
kwargs={'usage_key_string': unicode(usage_key)}
kwargs={'usage_key_string': six.text_type(usage_key)}
)
self.verify_response(403, url=url)
@@ -180,9 +182,9 @@ class TestBlocksView(SharedModuleStoreTestCase):
def test_basic(self):
response = self.verify_response()
self.assertEquals(response.data['root'], unicode(self.course_usage_key))
self.assertEquals(response.data['root'], six.text_type(self.course_usage_key))
self.verify_response_block_dict(response)
for block_key_string, block_data in response.data['blocks'].iteritems():
for block_key_string, block_data in six.iteritems(response.data['blocks']):
block_key = deserialize_usage_key(block_key_string, self.course_key)
self.assertEquals(block_data['id'], block_key_string)
self.assertEquals(block_data['type'], block_key.block_type)
@@ -195,7 +197,7 @@ class TestBlocksView(SharedModuleStoreTestCase):
def test_block_counts_param(self):
response = self.verify_response(params={'block_counts': ['course', 'chapter']})
self.verify_response_block_dict(response)
for block_data in response.data['blocks'].itervalues():
for block_data in six.itervalues(response.data['blocks']):
self.assertEquals(
block_data['block_counts']['course'],
1 if block_data['type'] == 'course' else 0,
@@ -214,7 +216,7 @@ class TestBlocksView(SharedModuleStoreTestCase):
'student_view_data': self.BLOCK_TYPES_WITH_STUDENT_VIEW_DATA + ['chapter']
})
self.verify_response_block_dict(response)
for block_data in response.data['blocks'].itervalues():
for block_data in six.itervalues(response.data['blocks']):
self.assert_in_iff(
'student_view_data',
block_data,
@@ -224,7 +226,7 @@ class TestBlocksView(SharedModuleStoreTestCase):
def test_navigation_param(self):
response = self.verify_response(params={'nav_depth': 10})
self.verify_response_block_dict(response)
for block_data in response.data['blocks'].itervalues():
for block_data in six.itervalues(response.data['blocks']):
self.assertIn('descendants', block_data)
def test_requested_fields_param(self):
@@ -234,7 +236,7 @@ class TestBlocksView(SharedModuleStoreTestCase):
self.verify_response_with_requested_fields(response)
def test_with_list_field_url(self):
query = urlencode(self.query_params.items() + [
query = urlencode(list(self.query_params.items()) + [
('requested_fields', self.requested_fields[0]),
('requested_fields', self.requested_fields[1]),
('requested_fields', join(self.requested_fields[1:], ',')),
@@ -254,7 +256,7 @@ class TestBlocksInCourseView(TestBlocksView):
def setUp(self):
super(TestBlocksInCourseView, self).setUp()
self.url = reverse('blocks_in_course')
self.query_params['course_id'] = unicode(self.course_key)
self.query_params['course_id'] = six.text_type(self.course_key)
def test_no_course_id(self):
self.query_params.pop('course_id')
@@ -264,4 +266,4 @@ class TestBlocksInCourseView(TestBlocksView):
self.verify_response(400, params={'course_id': 'invalid_course_id'})
def test_non_existent_course(self):
self.verify_response(403, params={'course_id': unicode(CourseLocator('non', 'existent', 'course'))})
self.verify_response(403, params={'course_id': six.text_type(CourseLocator('non', 'existent', 'course'))})