def setup_dir(request: SubRequest) -> None: os.mkdir('test-dir', mode=0o777) def teardown_dir() -> None: os.rmdir('test-dir-1') request.addfinalizer(teardown_dir)
def gradebook(request: SubRequest) -> Gradebook: gb = api.Gradebook("sqlite:///:memory:") def fin() -> None: gb.close() request.addfinalizer(fin) return gb
def block_network(request: SubRequest, record_mode: str, vcr_markers: List[Mark]) -> Iterator[None]: """Block network access in tests except for "none" VCR recording mode.""" marker = request.node.get_closest_marker(name="block_network") if marker is not None: validate_block_network_mark(marker) # If network blocking is enabled there is one exception - if VCR is in recording mode (any mode except "none") default_block = marker or request.config.getoption("--block-network") allowed_hosts = getattr( marker, "kwargs", {}).get("allowed_hosts") or request.config.getoption("--allowed-hosts") if isinstance(allowed_hosts, str): allowed_hosts = allowed_hosts.split(",") if vcr_markers: # Take `record_mode` with the most priority: # - Explicit CLI option # - The `vcr_config` fixture # - The `vcr` mark config = request.getfixturevalue("vcr_config") merged_config = merge_kwargs(config, vcr_markers) # If `--record-mode` was not explicitly passed in CLI, then take one from the merged config if request.config.getoption("--record-mode") is None: record_mode = merged_config.get("record_mode", "none") if default_block and (not request.getfixturevalue("vcr_markers") or record_mode == "none"): with network.blocking_context(allowed_hosts=allowed_hosts): yield else: yield
def course_dir(request: SubRequest) -> str: path = tempfile.mkdtemp(prefix='tmp-coursedir-') def fin() -> None: rmtree(path) request.addfinalizer(fin) return path
def empty_dir(request: SubRequest) -> str: dirpath = tempfile.NamedTemporaryFile().name def _remove_empty_dir(): shutil.rmtree(dirpath) os.makedirs(dirpath) request.addfinalizer(_remove_empty_dir) return dirpath
def db(request: SubRequest) -> str: path = tempfile.mkdtemp(prefix='tmp-dbdir-') dbpath = os.path.join(path, "nbgrader_test.db") def fin() -> None: rmtree(path) request.addfinalizer(fin) return "sqlite:///" + dbpath
def user_fx(request: SubRequest, db_fx: database.Database) -> dict: params_data = {'login': '******', 'password': get_password_hash('test')} db_fx[DbCollection.USER].insert_one(params_data.copy()) def remove_user(): db_fx[DbCollection.USER].delete_one(params_data) request.addfinalizer(remove_user) return {'login': '******', 'password': '******'}
def env(request: SubRequest) -> dict: old_env = os.environ.copy() def fin() -> None: os.environ = old_env request.addfinalizer(fin) return os.environ
def app(request: SubRequest) -> Flask: app = create_app("testing") ctx = app.app_context() ctx.push() def teardown(): ctx.pop() request.addfinalizer(teardown) return app
def session(app: Flask, request: SubRequest) -> scoped_session: def teardown(): _db.session.rollback() _db.drop_all() _db.session.commit() _db.app = app _db.drop_all() _db.create_all() request.addfinalizer(teardown) return _db.session
async def event_catcher_with_namespace(app_test_client: TestClientProtocol, request: SubRequest): """ catch events as they happen in the background """ _catcher = EventsCatcher(app_test_client=app_test_client, blueprint_path='/events', namespace='ns1') def teardown(): _catcher._stop.set() request.addfinalizer(teardown) async with _catcher: yield _catcher
def record_stdout( disable_recording: bool, record_stdout_markers: List[Mark], record_mode: str, request: SubRequest, ): marker = request.node.get_closest_marker("record_stdout") if disable_recording: yield None elif marker: # SETUP TEST DETAILS module_dir = request.node.fspath.dirname module_name = request.node.fspath.purebasename test_name = request.node.name # FORMAT MARKER'S KEYWORD ARGUMENTS formatted_kwargs = record_stdout_format_kwargs( test_name=test_name, record_mode=record_mode, record_stdout_markers=record_stdout_markers, ) # SETUP RECORDER path_template = PathTemplate( module_dir=module_dir, module_name=module_name, test_name=formatted_kwargs["record_name"], ) recorder = Recorder(path_template=path_template, record_mode=formatted_kwargs["record_mode"]) # CAPTURE STDOUT capture = request.config.getoption("--capture") if capture == "no": global_capturing = MultiCapture(in_=SysCapture(0), out=SysCapture(1), err=SysCapture(2)) global_capturing.start_capturing() yield recorder.capture( captured=global_capturing.readouterr().out, strip=formatted_kwargs["strip"], ) global_capturing.stop_capturing() else: capsys = request.getfixturevalue("capsys") yield recorder.capture(captured=capsys.readouterr().out, strip=formatted_kwargs["strip"]) # SAVE/CHECK RECORD if formatted_kwargs["save_record"]: recorder.persist() recorder.assert_equal() recorder.assert_in_list(in_list=formatted_kwargs["assert_in_list"]) else: recorder.assert_in_list(in_list=formatted_kwargs["assert_in_list"]) else: yield None
def auth_token_fx(request: SubRequest, db_fx: database.Database, user_fx: dict) -> dict: auth_collection: collection.Collection = db_fx[DbCollection.AUTH] login: str = user_fx.get('login') token = str(uuid4()) auth_collection.replace_one({'login': login}, { 'login': login, 'token': token, 'expire': datetime.now() + timedelta(10) }, upsert=True) def remove_auth() -> None: db_fx[DbCollection.AUTH].delete_one({'login': login}) request.addfinalizer(remove_auth) return {'Authorization': token}
def temp_cwd(request: SubRequest, course_dir: str) -> str: orig_dir = os.getcwd() path = tempfile.mkdtemp(prefix='tmp-cwd-') os.chdir(path) with open("nbgrader_config.py", "w") as fh: fh.write(dedent( """ c = get_config() c.CourseDirectory.root = r"{}" """.format(course_dir) )) def fin() -> None: os.chdir(orig_dir) rmtree(path) request.addfinalizer(fin) return path
def vcr( # pylint: disable=too-many-arguments request: SubRequest, vcr_markers: List[Mark], vcr_cassette_dir: str, record_mode: str, disable_recording: bool, pytestconfig: Config, ) -> Iterator[Optional[Cassette]]: """Install a cassette if a test is marked with `pytest.mark.vcr`.""" if disable_recording: yield None elif vcr_markers: config = request.getfixturevalue("vcr_config") default_cassette = request.getfixturevalue("default_cassette_name") with use_cassette(default_cassette, vcr_cassette_dir, record_mode, vcr_markers, config, pytestconfig) as cassette: yield cassette else: yield None
async def quart_events_catcher(app_test_client: TestClientProtocol, request: SubRequest): def _getini(name, default=None): """ getini returns an empty string instead of None; this helper fixes that """ _val = request.config.getini(name) return _val if len(_val) > 0 else default """ catch events from quart-events as they are generated in the background """ _catcher = EventsCatcher(app_test_client=app_test_client, blueprint_path=_getini('quart_events_path', default='/events'), namespace=_getini('quart_events_namespace', default=None)) def teardown(): _catcher._stop.set() request.addfinalizer(teardown) async with _catcher: yield _catcher
def xfail_if_unseeded_model_chosen(request: SubRequest): """Adds a xfail mark on tests that relate to seeding when non-seeded model is chosen.""" # TODO: Can't refer to the tests by reference because of `parametrize_this`: their `__name__` # becomes "method". If/when we rework `parametrize_this` or change the name of tests, it'll # be important to update this as well. # NOTE: Also, normally I'd add a fixture to only these tests, but that's not currently possible: # It seems like the signature of these tests can't be changed, because of `@phase` and/or # `parametrize_this`. tests_that_check_seeding = [ TestHEBO.test_seed_rng, TestHEBO.test_seed_rng_init, TestHEBO.test_state_dict, ] model_name: str = request.getfixturevalue("model_name") if model_name in properly_seeded_models: return # Don't add any mark, the test is expected to pass. # NOTE: We need to detect the phase. The reason for this is so we can avoid having a # bunch of tests XPASS when the test is ran in the random phase (where some do work). if "num" not in request.fixturenames: return # One of the tests that doesn't involve the phase. in_random_phase: bool = request.getfixturevalue("num") == 0 if in_random_phase: return # NOTE: Also can't use `request.function` because of `parametrize_this`, since it points # to the local closure inside `parametrize_this`. # if request.function in test_that_check_seeding: if any(func == request.function for func in tests_that_check_seeding): request.node.add_marker( pytest.mark.xfail( reason=f"This model name {model_name} is not properly seeded.", ))
def block_network(request: SubRequest, record_mode: str) -> Iterator[None]: """Block network access in tests except for "none" VCR recording mode.""" marker = request.node.get_closest_marker(name="block_network") # If network blocking is enabled there is one exception - if VCR is in recording mode (any mode except "none") default_block = marker or request.config.getoption("--block-network") allowed_hosts = getattr( marker, "kwargs", {}).get("allowed_hosts") or request.config.getoption("--allowed-hosts") if isinstance(allowed_hosts, str): allowed_hosts = allowed_hosts.split(",") if default_block and (not request.getfixturevalue("vcr_markers") or record_mode == "none"): with network.blocking_context(allowed_hosts=allowed_hosts): yield else: yield
def test_after_processing( self, vws_client: VWS, request: SubRequest, image_fixture_name: str, expected_status: TargetStatuses, ) -> None: """ After processing is completed, the tracking rating is in the range of 0 to 5. The documentation says: > Note: tracking_rating and reco_rating are provided only when > status = success. However, this shows that ``tracking_rating`` is given when the status is not success. It also shows that ``reco_rating`` is not provided even when the status is success. """ image_file = request.getfixturevalue(image_fixture_name) target_id = vws_client.add_target( name='example', width=1, image=image_file, active_flag=True, application_metadata=None, ) # The tracking rating may change during processing. # Therefore we wait until processing ends. vws_client.wait_for_target_processed(target_id=target_id) report = vws_client.get_target_summary_report(target_id=target_id) target_details = vws_client.get_target_record(target_id=target_id) tracking_rating = target_details.target_record.tracking_rating assert report.tracking_rating == tracking_rating assert report.tracking_rating in range(6) assert report.status == expected_status assert report.total_recos == 0 assert report.current_month_recos == 0 assert report.previous_month_recos == 0
def auto_convert_schema(self, request: SubRequest) -> None: if hasattr(self, "schema") and isinstance(getattr(self, "schema"), str): setattr(self, "schema", request.getfixturevalue(getattr(self, "schema")))
def next_level_repo(request: SubRequest) -> str: next_level_repo = TestRepo(RepoType.Local, Template.NextDirWithGitRemoteUpstream) request.addfinalizer(next_level_repo.destroy) return next_level_repo.setup().repo_path
def single_collection_repo(request: SubRequest): single_collection_repo = TestRepo(RepoType.Local, Template.SingleCollection) request.addfinalizer(single_collection_repo.destroy) return single_collection_repo.setup().repo_path
def metadata_rich_notebooks(request: SubRequest) -> str: metadata_rich_repo = TestRepo(RepoType.Local, Template.MetadataRichNotebooks) request.addfinalizer(metadata_rich_repo.destroy) return metadata_rich_repo.setup().repo_path
def multi_notebook_category(request: SubRequest) -> str: multi_notebook_repo = TestRepo(RepoType.Local, Template.MultiNotebookCategory) request.addfinalizer(multi_notebook_repo.destroy) return multi_notebook_repo.setup().repo_path
def multi_collection_repo(request: SubRequest): multi_collection_repo = TestRepo(RepoType.Local, Template.MultiCollection) request.addfinalizer(multi_collection_repo.destroy) return multi_collection_repo.setup().repo_path
def quick_build_collection(request: SubRequest) -> str: quick_build_repo = TestRepo(RepoType.Local, Template.QuickBuild) request.addfinalizer(quick_build_repo.destroy) return quick_build_repo.setup().repo_path
def single_collection_repo__nth_categories(request: SubRequest) -> str: nth_categories_repo = TestRepo(RepoType.Local, Template.SingleCollectionNthCategories) request.addfinalizer(nth_categories_repo.destroy) return nth_categories_repo.setup().repo_path
def single_collection_repo__immediate_categories(request: SubRequest) -> str: immediate_categories_repo = TestRepo(RepoType.Local, Template.SingleCollectionImmediateCategories) request.addfinalizer(immediate_categories_repo.destroy) return immediate_categories_repo.setup().repo_path
def executed_notebook_collection(request: SubRequest) -> str: executed_notebook_repo = TestRepo(RepoType.Local, Template.ExecutedCollection) request.addfinalizer(executed_notebook_repo.destroy) return executed_notebook_repo.setup().repo_path
def immediate_level_repo(request: SubRequest) -> str: immediate_level_repo = TestRepo(RepoType.Local, Template.EmptyDirWithGitRemoteUpstream) request.addfinalizer(immediate_level_repo.destroy) return immediate_level_repo.setup().repo_path