How to use query_count method in autotest

Best Python code snippet using autotest_python

test_field_override_performance.py

Source:test_field_override_performance.py Github

copy

Full Screen

1"""2Performance tests for field overrides.3"""4import itertools5from datetime import datetime6from unittest import mock7import ddt8import pytest9from ccx_keys.locator import CCXLocator10from django.conf import settings11from django.contrib.messages.storage.fallback import FallbackStorage12from django.core.cache import caches13from django.db import connections14from django.test.client import RequestFactory15from django.test.utils import override_settings16from edx_django_utils.cache import RequestCache17from opaque_keys.edx.keys import CourseKey18from pytz import UTC19from xblock.core import XBlock20from common.djangoapps.student.models import CourseEnrollment21from common.djangoapps.student.tests.factories import UserFactory22from lms.djangoapps.ccx.tests.factories import CcxFactory23from lms.djangoapps.courseware.field_overrides import OverrideFieldData24from lms.djangoapps.courseware.testutils import FieldOverrideTestMixin25from lms.djangoapps.courseware.views.views import progress26from openedx.core.djangoapps.content.block_structure.api import get_course_in_cache27from openedx.core.djangoapps.waffle_utils.testutils import WAFFLE_TABLES28from openedx.features.content_type_gating.models import ContentTypeGatingConfig29from xmodule.modulestore.tests.django_utils import (30 TEST_DATA_MONGO_MODULESTORE,31 TEST_DATA_SPLIT_MODULESTORE,32 ModuleStoreTestCase33)34from xmodule.modulestore.tests.factories import CourseFactory, check_mongo_calls, check_sum_of_calls35from xmodule.modulestore.tests.utils import ProceduralCourseTestMixin36QUERY_COUNT_TABLE_BLACKLIST = WAFFLE_TABLES37@mock.patch.dict(38 'django.conf.settings.FEATURES',39 {40 'ENABLE_XBLOCK_VIEW_ENDPOINT': True,41 }42)43@ddt.ddt44class FieldOverridePerformanceTestCase(FieldOverrideTestMixin, ProceduralCourseTestMixin, ModuleStoreTestCase):45 """46 Base class for instrumenting SQL queries and Mongo reads for field override47 providers.48 """49 __test__ = False50 # Tell Django to clean out all databases, not just default51 databases = set(connections)52 # TEST_DATA must be overridden by subclasses53 TEST_DATA = None54 def setUp(self):55 """56 Create a test client, course, and user.57 """58 super().setUp()59 self.request_factory = RequestFactory()60 self.student = UserFactory.create()61 self.request = self.request_factory.get("foo")62 self.request.session = {}63 self.request.user = self.student64 messages = FallbackStorage(self.request)65 self.request._messages = messages # pylint: disable=protected-access66 patcher = mock.patch('common.djangoapps.edxmako.request_context.get_current_request', return_value=self.request)67 patcher.start()68 self.addCleanup(patcher.stop)69 self.course = None70 self.ccx = None71 def setup_course(self, size, enable_ccx, view_as_ccx):72 """73 Build a gradable course where each node has `size` children.74 """75 grading_policy = {76 "GRADER": [77 {78 "drop_count": 2,79 "min_count": 12,80 "short_label": "HW",81 "type": "Homework",82 "weight": 0.1583 },84 {85 "drop_count": 2,86 "min_count": 12,87 "type": "Lab",88 "weight": 0.1589 },90 {91 "drop_count": 0,92 "min_count": 1,93 "short_label": "Midterm",94 "type": "Midterm Exam",95 "weight": 0.396 },97 {98 "drop_count": 0,99 "min_count": 1,100 "short_label": "Final",101 "type": "Final Exam",102 "weight": 0.4103 }104 ],105 "GRADE_CUTOFFS": {106 "Pass": 0.5107 }108 }109 self.course = CourseFactory.create(110 graded=True,111 start=datetime.now(UTC),112 grading_policy=grading_policy,113 enable_ccx=enable_ccx,114 )115 self.populate_course(size)116 course_key = self.course.id117 if enable_ccx:118 self.ccx = CcxFactory.create(course_id=self.course.id)119 if view_as_ccx:120 course_key = CCXLocator.from_course_locator(self.course.id, self.ccx.id)121 CourseEnrollment.enroll(122 self.student,123 course_key124 )125 return CourseKey.from_string(str(course_key))126 def grade_course(self, course_key):127 """128 Renders the progress page for the given course.129 """130 return progress(131 self.request,132 course_id=str(course_key),133 student_id=self.student.id134 )135 def assertMongoCallCount(self, calls):136 """137 Assert that mongodb is queried ``calls`` times in the surrounded138 context.139 """140 return check_mongo_calls(calls)141 def assertXBlockInstantiations(self, instantiations):142 """143 Assert that exactly ``instantiations`` XBlocks are instantiated in144 the surrounded context.145 """146 return check_sum_of_calls(XBlock, ['__init__'], instantiations, instantiations, include_arguments=False)147 def instrument_course_progress_render(148 self, course_width, enable_ccx, view_as_ccx,149 sql_queries, mongo_reads,150 ):151 """152 Renders the progress page, instrumenting Mongo reads and SQL queries.153 """154 course_key = self.setup_course(course_width, enable_ccx, view_as_ccx)155 # Switch to published-only mode to simulate the LMS156 with self.settings(MODULESTORE_BRANCH='published-only'):157 # Clear all caches before measuring158 for cache in settings.CACHES:159 caches[cache].clear()160 # Refill the metadata inheritance cache161 get_course_in_cache(course_key)162 # We clear the request cache to simulate a new request in the LMS.163 RequestCache.clear_all_namespaces()164 # Reset the list of provider classes, so that our django settings changes165 # can actually take affect.166 OverrideFieldData.provider_classes = None167 with self.assertNumQueries(sql_queries, using='default', table_blacklist=QUERY_COUNT_TABLE_BLACKLIST):168 with self.assertNumQueries(0, using='student_module_history'):169 with self.assertMongoCallCount(mongo_reads):170 with self.assertXBlockInstantiations(1):171 self.grade_course(course_key)172 @ddt.data(*itertools.product(('no_overrides', 'ccx'), list(range(1, 4)), (True, False), (True, False)))173 @ddt.unpack174 @override_settings(175 XBLOCK_FIELD_DATA_WRAPPERS=[],176 MODULESTORE_FIELD_OVERRIDE_PROVIDERS=[],177 )178 def test_field_overrides(self, overrides, course_width, enable_ccx, view_as_ccx):179 """180 Test without any field overrides.181 """182 ContentTypeGatingConfig.objects.create(183 enabled=True,184 enabled_as_of=datetime(2018, 1, 1),185 )186 providers = {187 'no_overrides': (),188 'ccx': ('lms.djangoapps.ccx.overrides.CustomCoursesForEdxOverrideProvider',)189 }190 if overrides == 'no_overrides' and view_as_ccx:191 pytest.skip("Can't view a ccx course if field overrides are disabled.")192 if not enable_ccx and view_as_ccx:193 pytest.skip("Can't view a ccx course if ccx is disabled on the course")194 if self.MODULESTORE == TEST_DATA_MONGO_MODULESTORE and view_as_ccx:195 pytest.skip("Can't use a MongoModulestore test as a CCX course")196 with self.settings(197 XBLOCK_FIELD_DATA_WRAPPERS=['lms.djangoapps.courseware.field_overrides:OverrideModulestoreFieldData.wrap'],198 MODULESTORE_FIELD_OVERRIDE_PROVIDERS=providers[overrides],199 ):200 sql_queries, mongo_reads = self.TEST_DATA[ # lint-amnesty, pylint: disable=unsubscriptable-object201 (overrides, course_width, enable_ccx, view_as_ccx)202 ]203 self.instrument_course_progress_render(204 course_width, enable_ccx, view_as_ccx, sql_queries, mongo_reads,205 )206class TestFieldOverrideMongoPerformance(FieldOverridePerformanceTestCase):207 """208 Test cases for instrumenting field overrides against the Mongo modulestore.209 """210 MODULESTORE = TEST_DATA_MONGO_MODULESTORE211 __test__ = True212 # TODO: decrease query count as part of REVO-28213 QUERY_COUNT = 34214 TEST_DATA = {215 # (providers, course_width, enable_ccx, view_as_ccx): (216 # # of sql queries to default,217 # # of mongo queries,218 # )219 ('no_overrides', 1, True, False): (QUERY_COUNT, 1),220 ('no_overrides', 2, True, False): (QUERY_COUNT, 1),221 ('no_overrides', 3, True, False): (QUERY_COUNT, 1),222 ('ccx', 1, True, False): (QUERY_COUNT, 1),223 ('ccx', 2, True, False): (QUERY_COUNT, 1),224 ('ccx', 3, True, False): (QUERY_COUNT, 1),225 ('no_overrides', 1, False, False): (QUERY_COUNT, 1),226 ('no_overrides', 2, False, False): (QUERY_COUNT, 1),227 ('no_overrides', 3, False, False): (QUERY_COUNT, 1),228 ('ccx', 1, False, False): (QUERY_COUNT, 1),229 ('ccx', 2, False, False): (QUERY_COUNT, 1),230 ('ccx', 3, False, False): (QUERY_COUNT, 1),231 }232class TestFieldOverrideSplitPerformance(FieldOverridePerformanceTestCase):233 """234 Test cases for instrumenting field overrides against the Split modulestore.235 """236 MODULESTORE = TEST_DATA_SPLIT_MODULESTORE237 __test__ = True238 # TODO: decrease query count as part of REVO-28239 QUERY_COUNT = 34240 TEST_DATA = {241 ('no_overrides', 1, True, False): (QUERY_COUNT, 3),242 ('no_overrides', 2, True, False): (QUERY_COUNT, 3),243 ('no_overrides', 3, True, False): (QUERY_COUNT, 3),244 ('ccx', 1, True, False): (QUERY_COUNT, 3),245 ('ccx', 2, True, False): (QUERY_COUNT, 3),246 ('ccx', 3, True, False): (QUERY_COUNT, 3),247 ('ccx', 1, True, True): (QUERY_COUNT + 2, 3),248 ('ccx', 2, True, True): (QUERY_COUNT + 2, 3),249 ('ccx', 3, True, True): (QUERY_COUNT + 2, 3),250 ('no_overrides', 1, False, False): (QUERY_COUNT, 3),251 ('no_overrides', 2, False, False): (QUERY_COUNT, 3),252 ('no_overrides', 3, False, False): (QUERY_COUNT, 3),253 ('ccx', 1, False, False): (QUERY_COUNT, 3),254 ('ccx', 2, False, False): (QUERY_COUNT, 3),255 ('ccx', 3, False, False): (QUERY_COUNT, 3),...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful