def test_unicode_string(self, monkeypatch: MonkeyPatch) -> None: with monkeypatch.context() as mp: mp.setattr("_pytest.config.Config", 42) import _pytest assert _pytest.config.Config == 42 # type: ignore mp.delattr("_pytest.config.Config")
def test_run_collector(monkeypatch: MonkeyPatch) -> None: model = Mock() model.type = CollectorType.Python with pytest.raises(NotImplementedError): run_collector(model) model.type = CollectorType.Command with monkeypatch.context() as m: ret = Mock() ret.stdout = "blubb" run = Mock(return_value=ret) m.setattr(subprocess, "run", run) with pytest.raises(CollectorError): run_collector(model) exp = CollectorResult( metrics=[ Metric(name="some.value", value=42.42, unit="plumbus"), Metric(name="some.value2", value=42.42, unit="plumbus"), ] ) ret.stdout = exp.json() act = run_collector(ret) assert exp == act
def test_import_prepend_append( self, pytester: Pytester, monkeypatch: MonkeyPatch ) -> None: root1 = pytester.mkdir("root1") root2 = pytester.mkdir("root2") root1.joinpath("x456.py").touch() root2.joinpath("x456.py").touch() p = root2.joinpath("test_x456.py") monkeypatch.syspath_prepend(str(root1)) p.write_text( textwrap.dedent( """\ import x456 def test(): assert x456.__file__.startswith({!r}) """.format( str(root2) ) ) ) with monkeypatch.context() as mp: mp.chdir(root2) reprec = pytester.inline_run("--import-mode=append") reprec.assertoutcome(passed=0, failed=1) reprec = pytester.inline_run() reprec.assertoutcome(passed=1)
def test_discovery_via_user_home(monkeypatch: MonkeyPatch): with monkeypatch.context() as m: # type: MonkeyPatch m.setattr('os.path.isfile', lambda path: True) m.setattr('os.path.exists', lambda path: True) monkeypatch.setattr(pathlib.Path, 'absolute', lambda x: '/user/home/path/test') result = discover_config_file_by_name('test.ini', script_dir=None) assert os.path.join('/user/home/path/test', '.rs500', 'test.ini') == result
def mock_redis(): """Monkey patch service cache with mocked redis.""" from renku.service.cache.base import BaseCache from renku.service.cache.models.user import User from renku.service.cache.models.job import Job from renku.service.cache.models.file import File from renku.service.cache.models.project import Project from renku.service.jobs.queues import WorkerQueues monkey_patch = MonkeyPatch() with monkey_patch.context() as m: fake_redis = fakeredis.FakeRedis() fake_model_db = Database(connection_pool=fake_redis.connection_pool) m.setattr(WorkerQueues, 'connection', fake_redis) m.setattr(BaseCache, 'cache', fake_redis) m.setattr(BaseCache, 'model_db', fake_model_db) m.setattr(Job, '__database__', fake_model_db) m.setattr(User, '__database__', fake_model_db) m.setattr(File, '__database__', fake_model_db) m.setattr(Project, '__database__', fake_model_db) yield monkey_patch.undo()
def test_discovery_via_env_var(monkeypatch: MonkeyPatch): with monkeypatch.context() as m: # type: MonkeyPatch m.setenv('RS500_CONFIG_PATH', '/rs500_config_path/here/we/are') m.setattr('os.path.isfile', lambda path: True) m.setattr('os.path.exists', lambda path: True) result = discover_config_file_by_name('test.ini', script_dir=None) assert os.path.join('/rs500_config_path/here/we/are', 'test.ini') == result
def test_is_interactive(self): from _pytest.monkeypatch import MonkeyPatch # noqa: F401, F403 monkey = MonkeyPatch() with monkey.context() as c: # Set `sys.ps1` to immitate an interactive session c.setattr(sys, 'ps1', 'rfm>>> ', raising=False) assert os_ext.is_interactive()
def test_pull_request_exists(monkeypatch: MonkeyPatch, branch: str) -> None: """It matches the branch in the process output.""" def stub(*args: Any, **kwargs: Any) -> Any: return pretend.stub(stdout="topic") with monkeypatch.context() as m: m.setattr("subprocess.run", stub) assert github.pull_request_exists(branch) is (branch == "topic")
def test_context_classmethod() -> None: class A: x = 1 with MonkeyPatch.context() as m: m.setattr(A, "x", 2) assert A.x == 2 assert A.x == 1
def test_create_pull_request(monkeypatch: MonkeyPatch) -> None: """It runs a subprocess.""" stub = pretend.call_recorder(lambda *args, **kwargs: None) with monkeypatch.context() as m: m.setattr("subprocess.run", stub) github.create_pull_request("title", "body") assert stub.calls
def mock_edb(monkeypatch) -> EDBClientFactory: import mongomock # NOTE since mongomock clients store data in memory, # the same MongoClient instance must be kept and used for the lifetime of the fixture # to be able to access the EDB data for e.g. assertions client_factory = EDBClientFactory(mongomock.MongoClient()) with MonkeyPatch.context() as mp: mp.setattr("watertap.edb.db_api.MongoClient", client_factory) yield client_factory
def test_context(): monkeypatch = MonkeyPatch() import functools import inspect with monkeypatch.context() as m: m.setattr(functools, "partial", 3) assert not inspect.isclass(functools.partial) assert inspect.isclass(functools.partial)
def mocked_env(monkeypatch: MonkeyPatch) -> Iterator[Dict[str, str]]: env_vars: Dict[str, str] = { "DYNAMIC_SIDECAR_IMAGE": "local/dynamic-sidecar:MOCK", } with monkeypatch.context() as m: for key, value in env_vars.items(): m.setenv(key, value) yield env_vars
def test_context(): monkeypatch = MonkeyPatch() import functools import inspect with monkeypatch.context() as m: m.setattr(functools, "partial", 3) assert not inspect.isclass(functools.partial) assert inspect.isclass(functools.partial)
def test_platform_non_linux(monkeypatch: MonkeyPatch) -> None: from platformdirs import unix try: with monkeypatch.context() as context: context.setattr(sys, "platform", "magic") monkeypatch.delenv("XDG_RUNTIME_DIR", raising=False) importlib.reload(unix) with pytest.raises(RuntimeError, match="should only be used on Linux"): unix.Unix().user_runtime_dir finally: importlib.reload(unix)
def test_sql_tracker_store_with_login_db_race_condition( postgres_login_db_connection: sa.engine.Connection, caplog: LogCaptureFixture, monkeypatch: MonkeyPatch, ): original_execute = sa.engine.Connection.execute def mock_execute(self, *args, **kwargs): # this simulates a race condition if kwargs == {"database_name": POSTGRES_TRACKER_STORE_DB}: original_execute( self.execution_options(isolation_level="AUTOCOMMIT"), f"CREATE DATABASE {POSTGRES_TRACKER_STORE_DB}", ) return Mock(rowcount=0) else: return original_execute(self, *args, **kwargs) with monkeypatch.context() as mp: mp.setattr(sa.engine.Connection, "execute", mock_execute) with caplog.at_level(logging.ERROR): tracker_store = SQLTrackerStore( dialect="postgresql", host=POSTGRES_HOST, port=POSTGRES_PORT, username=POSTGRES_USER, password=POSTGRES_PASSWORD, db=POSTGRES_TRACKER_STORE_DB, login_db=POSTGRES_LOGIN_DB, ) # IntegrityError has been caught and we log the error assert any( [ f"Could not create database '{POSTGRES_TRACKER_STORE_DB}'" in record.message for record in caplog.records ] ) matching_rows = ( postgres_login_db_connection.execution_options(isolation_level="AUTOCOMMIT") .execute( sa.text( "SELECT 1 FROM pg_catalog.pg_database WHERE datname = :database_name" ), database_name=POSTGRES_TRACKER_STORE_DB, ) .rowcount ) assert matching_rows == 1 tracker_store.engine.dispose()
def test_terminalwriter_dumb_term_no_markup(monkeypatch: MonkeyPatch) -> None: monkeypatch.setattr(os, "environ", {"TERM": "dumb", "PATH": ""}) class MyFile: closed = False def isatty(self): return True with monkeypatch.context() as m: m.setattr(sys, "stdout", MyFile()) assert sys.stdout.isatty() tw = terminalwriter.TerminalWriter() assert not tw.hasmarkup
def monkeypatch(monkeypatch: MonkeyPatch, request_context: None) -> Iterator[MonkeyPatch]: """Makes patch/undo of request globals possible In the GUI we often use the monkeypatch for patching request globals (e.g. cmk.gui.globals.config). To be able to undo all these patches, we need to be within the request context while monkeypatch.undo is running. However, with the default "monkeypatch" fixture the undo would be executed after leaving the application and request context. What we do here is to override the default monkeypatch fixture of pytest for the GUI tests: See also: https://github.com/pytest-dev/pytest/blob/main/src/_pytest/monkeypatch.py. The drawback here may be that we create some unnecessary application / request context objects for some tests. If you have an idea for a cleaner approach, let me know. """ with monkeypatch.context() as m: yield m
def test_samefile_false_negatives(tmp_path: Path, monkeypatch: MonkeyPatch) -> None: """ import_file() should not raise ImportPathMismatchError if the paths are exactly equal on Windows. It seems directories mounted as UNC paths make os.path.samefile return False, even when they are clearly equal. """ module_path = tmp_path.joinpath("my_module.py") module_path.write_text("def foo(): return 42") monkeypatch.syspath_prepend(tmp_path) with monkeypatch.context() as mp: # Forcibly make os.path.samefile() return False here to ensure we are comparing # the paths too. Using a context to narrow the patch as much as possible given # this is an important system function. mp.setattr(os.path, "samefile", lambda x, y: False) module = import_path(module_path) assert getattr(module, "foo")() == 42
def test_appends_when_entry_does_not_exist(monkeypatch: MonkeyPatch, cache_file: LocalPath, hash_file: LocalPath): def raise_assertionerror(*args, **kwargs): raise AssertionError() with monkeypatch.context() as m: # Throw an AssertionError when append is called. m.setattr(syphon.hash._OpenHashFile, "append", value=raise_assertionerror) cache_file.write(rand_string()) populate_hash_file(hash_file) entry = syphon.hash.HashEntry(cache_file) with pytest.raises(AssertionError): with syphon.hash.HashFile(hash_file) as hashfile: hashfile.update(entry)
def test_scheme_spec_deprecations( monkeypatch: MonkeyPatch, scheme_yml_str: str, expected_key: str, expected_value: Any, ): """Warning gets emitted by load_model""" return_dicts = [] with monkeypatch.context() as m: m.setattr(yml_module, "fromdict", lambda _, spec, *args, **kwargs: return_dicts.append(spec)) record, _ = deprecation_warning_on_call_test_helper( load_scheme, args=(scheme_yml_str, ), kwargs={"format_name": "yml_str"}) return_dict = return_dicts[0] assert expected_key in return_dict assert return_dict[expected_key] == expected_value assert len(record) == 1
def test_check_false_oserror_hash_file(capsys: CaptureFixture, monkeypatch: MonkeyPatch, tmpdir: LocalPath, verbose: bool): with monkeypatch.context() as m: # Update HashFile.__enter__ to always raise an OSError. m.setattr(syphon.hash, "HashFile", value=HashFileOSError) # Generate cache files and their respective hash entries. new_hashfile: LocalPath = make_hash_entries(tmpdir, 1, 0, 0, None) assert new_hashfile.size() > 0 # Collect all generated cache files. cache_files = glob(str(new_hashfile.dirpath("*.csv"))) assert len(cache_files) >= 0 for cache in cache_files: # Try to check the cache file. assert not syphon.check(cache, verbose=verbose) captured: CaptureResult = capsys.readouterr() assert_captured_outerr(captured, verbose, False) if verbose: assert_matches_outerr(captured, [str(new_hashfile)], [])
def test_model_spec_deprecations( monkeypatch: MonkeyPatch, model_yml_str: str, expected_nr_of_warnings: int, expected_key: str, expected_value: Any, ): """Warning gets emitted by load_model""" return_dicts = [] with monkeypatch.context() as m: m.setattr(yml_module, "sanitize_yaml", lambda spec: return_dicts.append(spec)) record, _ = deprecation_warning_on_call_test_helper( load_model, args=(model_yml_str, ), kwargs={"format_name": "yml_str"}) return_dict = return_dicts[0] assert expected_key in return_dict assert return_dict[expected_key] == expected_value assert len(record) == expected_nr_of_warnings
def test_discovery_in_etc(monkeypatch: MonkeyPatch): with monkeypatch.context() as m: # type: MonkeyPatch m.setattr('os.path.isfile', lambda path: path.startswith('/etc')) m.setattr('os.path.exists', lambda path: path.startswith('/etc')) result = discover_config_file_by_name('test.ini') assert os.path.join('/etc', 'test.ini') == result
class TestVcexport(unittest.TestCase): def setUp(self): self.monkeypatch = MonkeyPatch() self.lambdaMock = Mock() self.lambdaMock.run_lambda.return_value = { "tarfile": "s3://dr-dummy/239482934-37482abc.tgz", "stdout": "OK", "stderr": "" } self.monkeypatch.setattr('mztools.vcexport.run_lambda', lambda a,b: self.lambdaMock.run_lambda()) self.monkeypatch.setattr('mztools.vcexport.untar_bytes', lambda a,b: True) self.monkeypatch.setattr('mztools.vcexport.s3_fetch_bytes', lambda a: "") self.monkeypatch.setattr('mztools.vcexport.s3_delete_path', lambda a: True) @patch('sys.stderr') @patch('os.listdir') def test_warning_when_not_all_folders_exported(self, listdir, mock_stderr): args = Namespace(**{ "environment": ["test"], "directory": "mocktmpdir", "overwrite": False, "excludesysdata": False, "includemeta": True, "user": "******", "folders": [ "Examples", "Defaults" ] }) listdir.return_value = [] with self.monkeypatch.context() as m: m.setattr('mztools.vcexport.listdir_nohidden', lambda a: [ "Examples" ]) result = vcexport.run_vcexport(args) mock_stderr.assert_has_calls([ unittest.mock.call.write('Warning: folder "Defaults" was not found in the export' + "\n") ]) @patch('getpass.getpass') @patch('os.listdir') @patch('glob.glob') def test_asks_for_password_if_not_in_arguments(self, glob, listdir, getpass): args = Namespace(**{ "environment": ["test"], "directory": ".", "overwrite": False, "excludesysdata": False, "includemeta": True, "user": "******", "folders": [ "Examples" ] }) listdir.return_value = [] getpass.return_value = "thepass" glob.return_value = [ "Examples" ] result = vcexport.run_vcexport(args) self.assertEqual(getpass.called, True) self.assertEqual(result,True) @patch('getpass.getpass') @patch('os.listdir') @patch('glob.glob') def test_does_not_ask_for_passwd_if_given(self, glob, listdir, getpass): args = Namespace(**{ "environment": ["test"], "directory": ".", "overwrite": False, "excludesysdata": False, "includemeta": True, "user": "******", "folders": [ "Examples" ] }) listdir.return_value = [] getpass.return_value = "thepass" glob.return_value = [ "Examples" ] self.lambdaMock.reset_mock() result = vcexport.run_vcexport(args) self.assertEqual(self.lambdaMock.run_lambda.called, True) self.assertEqual(getpass.called, False) self.assertEqual(result,True) @patch('os.listdir') def test_aborts_when_exportdir_not_empty(self, listdir): args = Namespace(**{ "environment": ["test"], "directory": ".", "overwrite": False, "excludesysdata": False, "includemeta": True, "user": "******", "folders": [ "Examples" ] }) listdir.return_value = ["Afile"] self.lambdaMock.reset_mock() result = vcexport.run_vcexport(args) self.lambdaMock.run_lambda.assert_not_called() self.assertEqual(result,False) @patch('os.path.isdir') @patch('os.mkdir') def test_create_exportdir_ifnotexist(self, mkdir, isdir): args = Namespace(**{ "environment": ["test"], "directory": "doesnotexist", "overwrite": False, "excludesysdata": False, "includemeta": True, "user": "******", "folders": [ "Examples" ] }) isdir.return_value = False result = vcexport.run_vcexport(args) self.assertEqual(mkdir.called, True)
def test_discovery_file_in_folder(monkeypatch: MonkeyPatch): with monkeypatch.context() as m: # type: MonkeyPatch m.setattr('os.path.isfile', lambda path: True) m.setattr('os.path.exists', lambda path: True) result = discover_config_file_by_name('test.ini', '/foo/bar') assert os.path.join('/foo/bar', 'test.ini') == result
def test_string_expression(self, monkeypatch: MonkeyPatch) -> None: with monkeypatch.context() as mp: mp.setattr("os.path.abspath", lambda x: "hello2") assert os.path.abspath("123") == "hello2"
def test_string_expression_class(self, monkeypatch: MonkeyPatch) -> None: with monkeypatch.context() as mp: mp.setattr("_pytest.config.Config", 42) import _pytest assert _pytest.config.Config == 42 # type: ignore
def test_unknown_attr_non_raising(self, monkeypatch: MonkeyPatch) -> None: # https://github.com/pytest-dev/pytest/issues/746 with monkeypatch.context() as mp: mp.setattr("os.path.qweqwe", 42, raising=False) assert os.path.qweqwe == 42 # type: ignore
def test_delattr(self, monkeypatch: MonkeyPatch) -> None: with monkeypatch.context() as mp: mp.delattr("os.path.abspath") assert not hasattr(os.path, "abspath") mp.undo() assert os.path.abspath