Skip to content

Commit

Permalink
test: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
UvgenGen committed Sep 2, 2022
1 parent e41400b commit 0c6272a
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 40 deletions.
29 changes: 20 additions & 9 deletions cms/djangoapps/contentstore/tests/test_contentstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -1485,15 +1485,20 @@ def test_course_overview_view_with_course(self):
"""Test viewing the course overview page with an existing course"""
course = CourseFactory.create()
resp = self._show_course_overview(course.id)
self.assertContains(
resp,
'<article class="outline outline-complex outline-course" data-locator="{locator}" data-course-key="{course_key}">'.format( # lint-amnesty, pylint: disable=line-too-long
locator=str(course.location),
course_key=str(course.id),
),
status_code=200,
html=True
)

# course_handler raise 404 for old mongo course
if course.id.deprecated:
self.assertEqual(resp.status_code, 404)
else:
self.assertContains(
resp,
'<article class="outline outline-complex outline-course" data-locator="{locator}" data-course-key="{course_key}">'.format( # lint-amnesty, pylint: disable=line-too-long
locator=str(course.location),
course_key=str(course.id),
),
status_code=200,
html=True
)

def test_create_item(self):
"""Test creating a new xblock instance."""
Expand Down Expand Up @@ -1550,6 +1555,12 @@ def test_get_html(handler):
course_key = course_items[0].id

resp = self._show_course_overview(course_key)

# course_handler raise 404 for old mongo course
if course_key.deprecated:
self.assertEqual(resp.status_code, 404)
return

self.assertEqual(resp.status_code, 200)
self.assertContains(resp, 'Chapter 2')

Expand Down
11 changes: 7 additions & 4 deletions cms/djangoapps/contentstore/tests/test_course_listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ def test_staff_course_listing(self, default_store, mongo_calls):

# Fetch accessible courses list & verify their count
courses_list_by_staff, __ = get_courses_accessible_to_user(self.request)
self.assertEqual(len(list(courses_list_by_staff)), TOTAL_COURSES_COUNT)

if default_store is ModuleStoreEnum.Type.mongo:
self.assertEqual(len(list(courses_list_by_staff)), 0)
else:
self.assertEqual(len(list(courses_list_by_staff)), TOTAL_COURSES_COUNT)

# Verify fetched accessible courses list is a list of CourseSummery instances
self.assertTrue(all(isinstance(course, CourseSummary) for course in courses_list_by_staff))
Expand All @@ -194,7 +198,7 @@ def test_staff_course_listing(self, default_store, mongo_calls):
with check_mongo_calls(mongo_calls):
list(_accessible_courses_summary_iter(self.request))

@ddt.data(ModuleStoreEnum.Type.split, ModuleStoreEnum.Type.mongo)
@ddt.data(ModuleStoreEnum.Type.split)
def test_get_course_list_with_invalid_course_location(self, store):
"""
Test getting courses with invalid course location (course deleted from modulestore).
Expand Down Expand Up @@ -246,8 +250,7 @@ def test_get_course_list_with_invalid_course_location(self, store):
)

@ddt.data(
(ModuleStoreEnum.Type.split, 1, 2),
(ModuleStoreEnum.Type.mongo, 1, 2),
(ModuleStoreEnum.Type.split, 2, 3),
)
@ddt.unpack
def test_course_listing_performance(self, store, courses_list_from_group_calls, courses_list_calls):
Expand Down
28 changes: 23 additions & 5 deletions cms/djangoapps/contentstore/views/tests/test_course_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import datetime
import json
from unittest import mock
from unittest import SkipTest, mock, skip
from unittest.mock import patch

import ddt
Expand Down Expand Up @@ -58,15 +58,15 @@ def setUp(self):
display_name='dotted.course.name-2',
)

def check_courses_on_index(self, authed_client):
def check_courses_on_index(self, authed_client, expected_course_tab_len):
"""
Test that the React course listing is present.
"""
index_url = '/home/'
index_response = authed_client.get(index_url, {}, HTTP_ACCEPT='text/html')
parsed_html = lxml.html.fromstring(index_response.content)
courses_tab = parsed_html.find_class('react-course-listing')
self.assertEqual(len(courses_tab), 1)
self.assertEqual(len(courses_tab), expected_course_tab_len)

def test_libraries_on_index(self):
"""
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_is_staff_access(self):
"""
Test that people with is_staff see the courses and can navigate into them
"""
self.check_courses_on_index(self.client)
self.check_courses_on_index(self.client, 0)

def test_negative_conditions(self):
"""
Expand Down Expand Up @@ -128,9 +128,10 @@ def test_course_staff_access(self):
)

# test access
self.check_courses_on_index(course_staff_client)
self.check_courses_on_index(course_staff_client, 0)

def test_json_responses(self):

outline_url = reverse_course_url('course_handler', self.course.id)
chapter = ItemFactory.create(parent_location=self.course.location, category='chapter', display_name="Week 1")
lesson = ItemFactory.create(parent_location=chapter.location, category='sequential', display_name="Lesson 1")
Expand All @@ -142,6 +143,11 @@ def test_json_responses(self):
ItemFactory.create(parent_location=subsection.location, category="video", display_name="My Video")

resp = self.client.get(outline_url, HTTP_ACCEPT='application/json')

if self.course.id.deprecated:
self.assertEqual(resp.status_code, 404)
return

json_response = json.loads(resp.content.decode('utf-8'))

# First spot check some values in the root response
Expand Down Expand Up @@ -307,6 +313,11 @@ def test_course_outline_with_display_course_number_as_none(self):
course_outline_url = reverse_course_url('course_handler', updated_course.id)
response = self.client.get_html(course_outline_url)

# course_handler raise 404 for old mongo course
if self.course.id.deprecated:
self.assertEqual(response.status_code, 404)
return

# Assert that response code is 200.
self.assertEqual(response.status_code, 200)

Expand Down Expand Up @@ -401,6 +412,7 @@ def check_index_page(self, separate_archived_courses, org):
archived_course_tab = parsed_html.find_class('archived-courses')
self.assertEqual(len(archived_course_tab), 1 if separate_archived_courses else 0)

@skip('Skip test for old mongo course')
@ddt.data(
# Staff user has course staff access
(True, 'staff', None, 0, 20),
Expand Down Expand Up @@ -469,6 +481,10 @@ def test_json_responses(self, is_concise):
outline_url = reverse_course_url('course_handler', self.course.id)
outline_url = outline_url + '?format=concise' if is_concise else outline_url
resp = self.client.get(outline_url, HTTP_ACCEPT='application/json')
if self.course.id.deprecated:
self.assertEqual(resp.status_code, 404)
return

json_response = json.loads(resp.content.decode('utf-8'))

# First spot check some values in the root response
Expand Down Expand Up @@ -613,6 +629,8 @@ def test_proctoring_link_is_visible(self, mock_validate_proctoring_settings):
"""
Test to check proctored exam settings mfe url is rendering properly
"""
if self.course.id.deprecated:
raise SkipTest("Skip test for old mongo course")
mock_validate_proctoring_settings.return_value = [
{
'key': 'proctoring_provider',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Exam Settings View Tests
"""

from unittest import SkipTest
from unittest.mock import patch

import ddt
Expand Down Expand Up @@ -104,6 +104,10 @@ def test_exam_settings_alert_with_exam_settings_enabled(self, page_handler):
self.course.enable_proctored_exams = True
self.save_course()

# course_handler raise 404 for old mongo course
if self.course.id.deprecated:
raise SkipTest("course_handler raise 404 for old mongo course")

url = reverse_course_url(page_handler, self.course.id)
resp = self.client.get(url, HTTP_ACCEPT='text/html')
alert_text = self._get_exam_settings_alert_text(resp.content)
Expand Down Expand Up @@ -139,6 +143,9 @@ def test_exam_settings_alert_with_exam_settings_disabled(self, page_handler):
self.course.enable_proctored_exams = True
self.save_course()

# course_handler raise 404 for old mongo course
if self.course.id.deprecated and page_handler == 'course_handler':
raise SkipTest("course_handler raise 404 for old mongo course")
url = reverse_course_url(page_handler, self.course.id)
resp = self.client.get(url, HTTP_ACCEPT='text/html')
alert_text = self._get_exam_settings_alert_text(resp.content)
Expand All @@ -163,6 +170,9 @@ def test_exam_settings_alert_not_shown(self, page_handler):
"""
Alert should not be visible if no proctored exam setting error exists
"""
# course_handler raise 404 for old mongo course
if self.course.id.deprecated and page_handler == 'course_handler':
raise SkipTest("course_handler raise 404 for old mongo course")
url = reverse_course_url(page_handler, self.course.id)
resp = self.client.get(url, HTTP_ACCEPT='text/html')
parsed_html = lxml.html.fromstring(resp.content)
Expand Down
14 changes: 13 additions & 1 deletion cms/djangoapps/contentstore/views/tests/test_header_menu.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Course Header Menu Tests.
"""

from unittest import SkipTest

from django.conf import settings
from django.test.utils import override_settings
Expand Down Expand Up @@ -37,6 +37,9 @@ def test_header_menu_without_web_certs_enabled(self):
Tests course header menu should not have `Certificates` menu item
if course has not web/HTML certificates enabled.
"""
# course_handler raise 404 for old mongo course
if self.course.id.deprecated:
raise SkipTest("course_handler raise 404 for old mongo course")
self.course.cert_html_view_enabled = False
self.save_course()
outline_url = reverse_course_url('course_handler', self.course.id)
Expand All @@ -49,6 +52,9 @@ def test_header_menu_with_web_certs_enabled(self):
Tests course header menu should have `Certificates` menu item
if course has web/HTML certificates enabled.
"""
# course_handler raise 404 for old mongo course
if self.course.id.deprecated:
raise SkipTest("course_handler raise 404 for old mongo course")
outline_url = reverse_course_url('course_handler', self.course.id)
resp = self.client.get(outline_url, HTTP_ACCEPT='text/html')
self.assertEqual(resp.status_code, 200)
Expand All @@ -60,6 +66,9 @@ def test_header_menu_without_exam_settings_enabled(self):
Tests course header menu should not have `Exam Settings` menu item
if course does not have the Exam Settings view enabled.
"""
# course_handler raise 404 for old mongo course
if self.course.id.deprecated:
raise SkipTest("course_handler raise 404 for old mongo course")
outline_url = reverse_course_url('course_handler', self.course.id)
resp = self.client.get(outline_url, HTTP_ACCEPT='text/html')
self.assertEqual(resp.status_code, 200)
Expand All @@ -71,6 +80,9 @@ def test_header_menu_with_exam_settings_enabled(self):
Tests course header menu should have `Exam Settings` menu item
if course does have Exam Settings view enabled.
"""
# course_handler raise 404 for old mongo course
if self.course.id.deprecated:
raise SkipTest("course_handler raise 404 for old mongo course")
outline_url = reverse_course_url('course_handler', self.course.id)
resp = self.client.get(outline_url, HTTP_ACCEPT='text/html')
self.assertEqual(resp.status_code, 200)
Expand Down
2 changes: 1 addition & 1 deletion lms/djangoapps/course_api/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def test_too_many_courses(self):
self.setup_user(self.audit_user)

# These query counts were found empirically
query_counts = [50, 46, 46, 46, 46, 46, 46, 46, 46, 46, 16]
query_counts = [51, 47, 47, 47, 47, 47, 47, 47, 47, 47, 17]
ordered_course_ids = sorted([str(cid) for cid in (course_ids + [c.id for c in self.courses])])

self.clear_caches()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def test_dump_course_ids_with_filter(self):

def test_dump_course_ids(self):
output = self.call_command('dump_course_ids')
dumped_courses = output.strip().split('\n')
course_ids = {str(course_id) for course_id in self.loaded_courses}
dumped_courses = (output.strip() or []) and output.strip().split('\n')
course_ids = {str(course_id) for course_id in self.loaded_courses if not course_id.deprecated}
dumped_ids = set(dumped_courses)
assert course_ids == dumped_ids

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ def setUpClass(cls):
"""
super().setUpClass()
cls.command = Command()
# org.0/course_0/Run_0
cls.course_key_1 = CourseFactory.create(default_store=ModuleStoreEnum.Type.mongo).id
# course-v1:org.1+course_1+Run_1
cls.course_key_2 = CourseFactory.create(default_store=ModuleStoreEnum.Type.split).id
cls.course_key_1 = CourseFactory.create(default_store=ModuleStoreEnum.Type.split).id
# course-v1:org.2+course_2+Run_2
cls.course_key_3 = CourseFactory.create(default_store=ModuleStoreEnum.Type.split).id
cls.course_key_2 = CourseFactory.create(default_store=ModuleStoreEnum.Type.split).id

def setUp(self):
"""
Expand Down Expand Up @@ -108,12 +106,11 @@ def test_specific_courses(self):
"""Test sending only to specific courses."""
self.command.handle(
**self.options(
courses=[str(self.course_key_1), str(self.course_key_2)]
courses=[str(self.course_key_1)]
)
)
assert self.course_key_1 in self.received_1
assert self.course_key_2 in self.received_1
assert self.course_key_3 not in self.received_1
assert self.course_key_2 not in self.received_1
assert self.received_1 == self.received_2

def test_specific_receivers(self):
Expand All @@ -125,7 +122,6 @@ def test_specific_receivers(self):
)
assert self.course_key_1 in self.received_1
assert self.course_key_2 in self.received_1
assert self.course_key_3 in self.received_1
assert not self.received_2

def test_course_overviews(self):
Expand All @@ -139,7 +135,7 @@ def test_course_overviews(self):
]
)
)
assert CourseOverview.objects.all().count() == 3
assert CourseOverview.objects.all().count() == 2
assert not self.received_1
assert not self.received_2

Expand Down
7 changes: 3 additions & 4 deletions openedx/core/djangoapps/content/course_overviews/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,9 @@ def get_all_courses(cls, orgs=None, filter_=None):
# make sure the "publish" signal was emitted when the course was
# created. For tests using CourseFactory, use emit_signals=True.
course_overviews = CourseOverview.objects.all()
# Exclude old mongo courses
deprecated_course_ids = [id for id in course_overviews.values_list('id', flat=True) if id.deprecated]
course_overviews = course_overviews.exclude(id__in=deprecated_course_ids)

if orgs:
# In rare cases, courses belonging to the same org may be accidentally assigned
Expand All @@ -673,10 +676,6 @@ def get_all_courses(cls, orgs=None, filter_=None):
if filter_:
course_overviews = course_overviews.filter(**filter_)

# Exclude old mongo courses
deprecated_course_ids = [id for id in course_overviews.values_list('id', flat=True) if id.deprecated]
course_overviews = course_overviews.exclude(id__in=deprecated_course_ids)

return course_overviews

@classmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
class BatchedAsyncCourseOverviewUpdateTests(ModuleStoreTestCase): # lint-amnesty, pylint: disable=missing-class-docstring
def setUp(self):
super().setUp()
self.course_1 = CourseFactory.create(default_store=ModuleStoreEnum.Type.mongo)
self.course_2 = CourseFactory.create(default_store=ModuleStoreEnum.Type.mongo)
self.course_1 = CourseFactory.create(default_store=ModuleStoreEnum.Type.split)
self.course_2 = CourseFactory.create(default_store=ModuleStoreEnum.Type.split)
self.course_3 = CourseFactory.create(default_store=ModuleStoreEnum.Type.mongo)

@mock.patch('openedx.core.djangoapps.content.course_overviews.models.CourseOverview.update_select_courses')
Expand All @@ -25,7 +25,7 @@ def test_enqueue_all_courses_in_single_batch(self, mock_update_courses):
)

called_args, called_kwargs = mock_update_courses.call_args_list[0]
assert sorted([self.course_1.id, self.course_2.id, self.course_3.id]) == sorted(called_args[0])
assert sorted([self.course_1.id, self.course_2.id]) == sorted(called_args[0]) # Only split courses updated
assert {'force_update': True} == called_kwargs
assert 1 == mock_update_courses.call_count

Expand Down
2 changes: 1 addition & 1 deletion xmodule/modulestore/mixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def get_course_summaries(self, **kwargs):
# Skip course if it's from old mongo modulestore.
elif course_id.deprecated:
log.warning(
"Course %s from %s; skipping from result.", course_id, store
"Course %s from %s; skipping from result.", course_id, store
)
else:
course_summaries[course_id] = course_summary
Expand Down
9 changes: 9 additions & 0 deletions xmodule/modulestore/tests/test_semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,18 @@ def test_update_asides(self, block_type):
self.ASIDE_DATA_FIELD.field_name, self.ASIDE_DATA_FIELD.updated)


@ddt.ddt
class TestMongoDirectOnlyCategorySemantics(DirectOnlyCategorySemantics):
"""
Verify DIRECT_ONLY_CATEGORY semantics against the MongoModulestore
"""
MODULESTORE = TEST_DATA_MONGO_MODULESTORE
__test__ = True

@ddt.data(ModuleStoreEnum.Branch.draft_preferred, ModuleStoreEnum.Branch.published_only)
def test_course_summaries(self, branch):
""" Test that `get_course_summaries` method in modulestore work as expected. """
with self.store.branch_setting(branch_setting=branch):
course_summaries = self.store.get_course_summaries()
# get_course_summaries skip old mongo courses
assert len(course_summaries) == 0

0 comments on commit 0c6272a

Please sign in to comment.