def test_data(django_db_blocker, seeded_database_loader, pytestconfig): """ Fixture that creates a login-enabled test user and backs up a seeded database to be loaded in between dashboard states and cleaned up at the end of the test run. """ user = None with django_db_blocker.unblock(): with connection.cursor() as cur: load_from_existing_db = should_load_from_existing_db( seeded_database_loader, cur, config=pytestconfig) if not load_from_existing_db: user = create_user_for_login(is_staff=True, username='******') call_command("seed_db") # Analog Learning program enrollment is being created here because # no programs are enrolled in the initial data snapshot. Setting this enrollment # ensures that the user will see the Analog Learning program in the dashboard. # Right now we manually delete this enrollment in scenarios where we want the user # to see the Digital Learning dashboard. ProgramEnrollment.objects.create( user=user, program=Program.objects.get(title='Analog Learning')) Role.objects.create( user=user, role=Staff.ROLE_ID, program=Program.objects.get(title='Analog Learning')) seeded_database_loader.create_backup(db_cursor=cur) if load_from_existing_db: with django_db_blocker.unblock(): terminate_db_connections() seeded_database_loader.load_backup() user = User.objects.get(username='******') yield dict(user=user)
def test_data(django_db_blocker, seeded_database_loader): """ Fixture that creates a login-enabled test user and backs up a seeded database to be loaded in between dashboard states and cleaned up at the end of the test run. """ user = None with django_db_blocker.unblock(): with connection.cursor() as cur: load_from_existing_db = should_load_from_existing_db(seeded_database_loader, cur) if not load_from_existing_db: user = create_user_for_login(is_staff=True, username='******') call_command("seed_db") # Analog Learning program enrollment is being created here because # no programs are enrolled in the initial data snapshot. Setting this enrollment # ensures that the user will see the Analog Learning program in the dashboard. # Right now we manually delete this enrollment in scenarios where we want the user # to see the Digital Learning dashboard. ProgramEnrollment.objects.create( user=user, program=Program.objects.get(title='Analog Learning') ) seeded_database_loader.create_backup(db_cursor=cur) if load_from_existing_db: with django_db_blocker.unblock(): terminate_db_connections() seeded_database_loader.load_backup() user = User.objects.get(username='******') yield dict(user=user)
def _use_db_loader(request, django_db_blocker, database_loader): """ Fixture that replaces the test database with the post-migration backup before each test case. NOTE: This should run *before* the 'django_db' marker code is executed. """ marker = request.keywords.get('django_db', None) if marker: with django_db_blocker.unblock(): terminate_db_connections() database_loader.load_backup()
def test_learners_states(browser, override_allowed_hosts, seeded_database_loader, django_db_blocker, test_data): """Iterate through all possible dashboard states and save screenshots/API results of each one""" output_directory = DASHBOARD_STATES_OPTIONS.get('output_directory') os.makedirs(output_directory, exist_ok=True) use_mobile = DASHBOARD_STATES_OPTIONS.get('mobile') if use_mobile: browser.driver.set_window_size(480, 854) learners_states = LearnersStates(test_data['user']) learners_state_iter = enumerate(learners_states) match = DASHBOARD_STATES_OPTIONS.get('match') if match is not None: learners_state_iter = filter( lambda scenario: match in make_filename(scenario[0], scenario[1][1] ), learners_state_iter) LoginPage(browser).log_in_via_admin(learners_states.user, DEFAULT_PASSWORD) recreate_index() # warm the cache browser.get("/learners") for num, (run_scenario, name) in learners_state_iter: skip_screenshot = False with django_db_blocker.unblock(): learners_states.user.refresh_from_db() filename = make_filename(num, name, output_directory=output_directory, use_mobile=use_mobile) new_url = run_scenario() if not skip_screenshot: browser.get(new_url) browser.wait_until_loaded(By.CSS_SELECTOR, '.sk-hits,.no-hits') browser.wait_until_loaded(By.CLASS_NAME, 'micromasters-title') try: browser.click_when_loaded( By.CSS_SELECTOR, '.filter--company_name .Select-arrow-zone', retries=0, ) except ElementNotVisibleException: # We are trying to make the work history visible, but if it doesn't exist # there's nothing to do pass # sometimes the browser scrolls down for some reason after clicking browser.driver.execute_script("window.scrollTo(0, 0)") browser.take_screenshot(filename=filename) with django_db_blocker.unblock(): terminate_db_connections() seeded_database_loader.load_backup()
def test_dashboard_states(browser, override_allowed_hosts, seeded_database_loader, django_db_blocker, test_data): """Iterate through all possible dashboard states and save screenshots/API results of each one""" output_directory = DASHBOARD_STATES_OPTIONS.get('output_directory') use_learner_page = DASHBOARD_STATES_OPTIONS.get('learner') os.makedirs(output_directory, exist_ok=True) use_mobile = DASHBOARD_STATES_OPTIONS.get('mobile') if use_mobile: browser.driver.set_window_size(480, 854) dashboard_states = DashboardStates(test_data['user']) dashboard_state_iter = enumerate(dashboard_states) match = DASHBOARD_STATES_OPTIONS.get('match') if match is not None: dashboard_state_iter = filter( lambda scenario: match in make_filename(scenario[0], scenario[1][1]), dashboard_state_iter ) LoginPage(browser).log_in_via_admin(dashboard_states.user, DEFAULT_PASSWORD) for num, (run_scenario, name) in dashboard_state_iter: skip_screenshot = False with django_db_blocker.unblock(): dashboard_states.user.refresh_from_db() if use_learner_page: for program in Program.objects.all(): Role.objects.create(role=Staff.ROLE_ID, user=dashboard_states.user, program=program) filename = make_filename(num, name, output_directory=output_directory, use_mobile=use_mobile) new_url = run_scenario() if new_url is None: if use_learner_page: new_url = '/learner' else: new_url = '/dashboard' elif use_learner_page: # the new_url is only for the dashboard page, skip skip_screenshot = True if not skip_screenshot: browser.get(new_url) browser.store_api_results( get_social_username(dashboard_states.user), filename=filename ) if use_learner_page: browser.wait_until_loaded(By.CLASS_NAME, 'user-page') else: browser.wait_until_loaded(By.CLASS_NAME, 'course-list') browser.take_screenshot(filename=filename) with django_db_blocker.unblock(): terminate_db_connections() seeded_database_loader.load_backup()
def django_db_setup_override(django_db_setup, django_db_blocker, database_loader, pytestconfig): """ Fixture provided by pytest-django to allow for custom Django database config. 'django_db_setup' exists in the arguments because we want to perform the normal pytest-django database setup before applying our own changes. """ with django_db_blocker.unblock(): with connection.cursor() as cur: load_from_existing_db = should_load_from_existing_db(database_loader, cur, config=pytestconfig) if not load_from_existing_db: # Drop a wagtail table due to a bug: https://github.com/wagtail/wagtail/issues/1824 cur.execute('DROP TABLE IF EXISTS wagtailsearch_editorspick CASCADE;') # Create the initial post-migration database backup to be restored before each test case database_loader.create_backup(db_cursor=cur) if load_from_existing_db: with django_db_blocker.unblock(): terminate_db_connections() database_loader.load_backup()
def django_db_setup_override(django_db_setup, django_db_blocker, database_loader): """ Fixture provided by pytest-django to allow for custom Django database config. 'django_db_setup' exists in the arguments because we want to perform the normal pytest-django database setup before applying our own changes. """ with django_db_blocker.unblock(): with connection.cursor() as cur: load_from_existing_db = should_load_from_existing_db(database_loader, cur) if not load_from_existing_db: # Drop a wagtail table due to a bug: https://github.com/wagtail/wagtail/issues/1824 cur.execute('DROP TABLE IF EXISTS wagtailsearch_editorspick CASCADE;') # Create the initial post-migration database backup to be restored before each test case database_loader.create_backup(db_cursor=cur) if load_from_existing_db: with django_db_blocker.unblock(): terminate_db_connections() database_loader.load_backup()