Пример #1
0
def test_exception_if_glotaran_is_missing(monkeypatch: MonkeyPatch):
    """Raise Exception if glotaran isn't installed."""

    monkeypatch.setitem(sys.modules, "glotaran", None)

    with pytest.raises(ImportError, match=r"you need to install pyglotaran"):
        import pyglotaran  # noqa:  F401
Пример #2
0
def test_setitem_deleted_meanwhile():
    d = {}
    monkeypatch = MonkeyPatch()
    monkeypatch.setitem(d, "x", 2)
    del d["x"]
    monkeypatch.undo()
    assert not d
Пример #3
0
def test_setitem_deleted_meanwhile() -> None:
    d = {}  # type: Dict[str, object]
    monkeypatch = MonkeyPatch()
    monkeypatch.setitem(d, "x", 2)
    del d["x"]
    monkeypatch.undo()
    assert not d
Пример #4
0
def test_setitem_deleted_meanwhile():
    d = {}
    monkeypatch = MonkeyPatch()
    monkeypatch.setitem(d, "x", 2)
    del d["x"]
    monkeypatch.undo()
    assert not d
Пример #5
0
 def test_check_filepath_consistency(self, monkeypatch: MonkeyPatch,
                                     tmp_path: Path) -> None:
     name = "pointsback123"
     p = tmp_path.joinpath(name + ".py")
     p.touch()
     for ending in (".pyc", ".pyo"):
         mod = ModuleType(name)
         pseudopath = tmp_path.joinpath(name + ending)
         pseudopath.touch()
         mod.__file__ = str(pseudopath)
         monkeypatch.setitem(sys.modules, name, mod)
         newmod = import_path(p)
         assert mod == newmod
     monkeypatch.undo()
     mod = ModuleType(name)
     pseudopath = tmp_path.joinpath(name + "123.py")
     pseudopath.touch()
     mod.__file__ = str(pseudopath)
     monkeypatch.setitem(sys.modules, name, mod)
     with pytest.raises(ImportPathMismatchError) as excinfo:
         import_path(p)
     modname, modfile, orig = excinfo.value.args
     assert modname == name
     assert modfile == str(pseudopath)
     assert orig == p
     assert issubclass(ImportPathMismatchError, ImportError)
Пример #6
0
def master_access():
    from pkuphysu_wechat import settings

    mpatch = MonkeyPatch()
    mpatch.setitem(settings["WECHAT"], "MASTER_IDS", "developmentopenid")
    yield
    mpatch.undo()
Пример #7
0
    def app(self, create_app):
        monkeypatch = MonkeyPatch()
        monkeypatch.setitem(ProjectSettingsService.config_override,
                            "ui.authentication", True)

        yield create_app()

        monkeypatch.undo()
Пример #8
0
def test_should_do_markup_PY_COLORS_eq_0(monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setitem(os.environ, "PY_COLORS", "0")
    f = io.StringIO()
    f.isatty = lambda: True  # type: ignore
    tw = terminalwriter.TerminalWriter(file=f)
    assert not tw.hasmarkup
    tw.line("hello", bold=True)
    s = f.getvalue()
    assert s == "hello\n"
Пример #9
0
 def test_restore_reloaded(self, monkeypatch: MonkeyPatch) -> None:
     assert self.key not in sys.modules
     monkeypatch.setitem(sys.modules, self.key, ModuleType("something"))
     assert self.key in sys.modules
     original = dict(sys.modules)
     snapshot = SysModulesSnapshot()
     sys.modules[self.key] = ModuleType("something else")
     snapshot.restore()
     assert sys.modules == original
Пример #10
0
def test_rule_config(rule_config: Dict[str, Any],
                     monkeypatch: MonkeyPatch) -> None:
    rule_id = 'rule-0'
    monkeypatch.setattr(AnsibleLintRule, 'id', rule_id)
    monkeypatch.setitem(options.rules, rule_id, rule_config)

    rule = AnsibleLintRule()
    assert set(rule.rule_config.items()) == set(rule_config.items())
    assert all(rule.get_config(k) == v for k, v in rule_config.items())
Пример #11
0
def test_should_do_markup_PY_COLORS_eq_1(monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setitem(os.environ, "PY_COLORS", "1")
    file = io.StringIO()
    tw = terminalwriter.TerminalWriter(file)
    assert tw.hasmarkup
    tw.line("hello", bold=True)
    s = file.getvalue()
    assert len(s) > len("hello\n")
    assert "\x1b[1m" in s
    assert "\x1b[0m" in s
Пример #12
0
def test_setup_database_drop_tables_dsx_write_not_on_production(
        cli_runner: CliRunner, caplog: LogCaptureFixture, mock_drop_all: Mock,
        monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setitem(master_config.database_dsn, DatabaseType.dsx_write,
                        "THIS_MIGHT_BE_PRODUCTION")

    result = cli_runner.invoke(setup_database, ["dsx-write", "--drop-tables"])
    assert result.exit_code == 1
    assert "Cannot drop tables on external production dsx_write database." == str(
        result.exception)

    mock_drop_all.assert_not_called()
Пример #13
0
def test_delitem():
    d = {"x": 1}
    monkeypatch = MonkeyPatch()
    monkeypatch.delitem(d, "x")
    assert "x" not in d
    monkeypatch.delitem(d, "y", raising=False)
    pytest.raises(KeyError, "monkeypatch.delitem(d, 'y')")
    assert not d
    monkeypatch.setitem(d, "y", 1700)
    assert d["y"] == 1700
    d["hello"] = "world"
    monkeypatch.setitem(d, "x", 1500)
    assert d["x"] == 1500
    monkeypatch.undo()
    assert d == {"hello": "world", "x": 1}
Пример #14
0
def test_delitem():
    d = {'x': 1}
    monkeypatch = MonkeyPatch()
    monkeypatch.delitem(d, 'x')
    assert 'x' not in d
    monkeypatch.delitem(d, 'y', raising=False)
    pytest.raises(KeyError, "monkeypatch.delitem(d, 'y')")
    assert not d
    monkeypatch.setitem(d, 'y', 1700)
    assert d['y'] == 1700
    d['hello'] = 'world'
    monkeypatch.setitem(d, 'x', 1500)
    assert d['x'] == 1500
    monkeypatch.undo()
    assert d == {'hello': 'world', 'x': 1}
Пример #15
0
def test_lambda_cloudwatch_insights_query_results_timeout(
        monkeypatch: MonkeyPatch) -> None:
    client = FakeCloudwatchClientLogsClient()
    # disable warning: "Argument 1 to "setitem" of "MonkeyPatch" has incompatible type "QueryResults"; expected "MutableMapping[str, str]"mypy(error)"
    monkeypatch.setitem(
        FAKE_CLOUDWATCH_CLIENT_LOGS_CLIENT_DEFAULT_RESPONSE,  # type: ignore
        "status",
        "Running",
    )
    assert (LambdaCloudwatchInsights.query_results(
        client=client,
        query_id="FakeQueryId",
        timeout_seconds=0.001,
        sleep_duration=0.001,
    ) == None)
Пример #16
0
def test_delitem():
    d = {"x": 1}
    monkeypatch = MonkeyPatch()
    monkeypatch.delitem(d, "x")
    assert "x" not in d
    monkeypatch.delitem(d, "y", raising=False)
    pytest.raises(KeyError, "monkeypatch.delitem(d, 'y')")
    assert not d
    monkeypatch.setitem(d, "y", 1700)
    assert d["y"] == 1700
    d["hello"] = "world"
    monkeypatch.setitem(d, "x", 1500)
    assert d["x"] == 1500
    monkeypatch.undo()
    assert d == {"hello": "world", "x": 1}
Пример #17
0
def test_setup_database_drop_tables_dsx_write(
        cli_runner: CliRunner, caplog: LogCaptureFixture, mock_drop_all: Mock,
        monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setitem(master_config.database_dsn, DatabaseType.dsx_write,
                        "ML_Internal_DB")

    result = cli_runner.invoke(setup_database, ["dsx-write", "--drop-tables"])
    assert result.exit_code == 0
    assert "Successfully connected to dsx_write database" in caplog.messages
    assert "Found schema: ml_dsx_write in dsx_write database" in caplog.messages
    assert "Found existing dsx_write database tables: {'ml_dsx_write.STG_Import_Periodic_ML'}" in caplog.messages
    assert "Now existing tables: ['ml_dsx_write.STG_Import_Periodic_ML']" in caplog.messages

    assert "Dropping own database tables: ['ml_dsx_write.STG_Import_Periodic_ML']" in caplog.messages
    mock_drop_all.assert_called_once()
Пример #18
0
def test_delitem() -> None:
    d = {"x": 1}  # type: Dict[str, object]
    monkeypatch = MonkeyPatch()
    monkeypatch.delitem(d, "x")
    assert "x" not in d
    monkeypatch.delitem(d, "y", raising=False)
    pytest.raises(KeyError, monkeypatch.delitem, d, "y")
    assert not d
    monkeypatch.setitem(d, "y", 1700)
    assert d["y"] == 1700
    d["hello"] = "world"
    monkeypatch.setitem(d, "x", 1500)
    assert d["x"] == 1500
    monkeypatch.undo()
    assert d == {"hello": "world", "x": 1}
Пример #19
0
def test_delitem():
    d = {'x': 1}
    monkeypatch = MonkeyPatch()
    monkeypatch.delitem(d, 'x')
    assert 'x' not in d
    monkeypatch.delitem(d, 'y', raising=False)
    pytest.raises(KeyError, "monkeypatch.delitem(d, 'y')")
    assert not d
    monkeypatch.setitem(d, 'y', 1700)
    assert d['y'] == 1700
    d['hello'] = 'world'
    monkeypatch.setitem(d, 'x', 1500)
    assert d['x'] == 1500
    monkeypatch.undo()
    assert d == {'hello': 'world', 'x': 1}
Пример #20
0
 def test_consider_env_plugin_instantiation(
     self,
     pytester: Pytester,
     monkeypatch: MonkeyPatch,
     pytestpm: PytestPluginManager,
 ) -> None:
     pytester.syspathinsert()
     pytester.makepyfile(xy123="#")
     monkeypatch.setitem(os.environ, "PYTEST_PLUGINS", "xy123")
     l1 = len(pytestpm.get_plugins())
     pytestpm.consider_env()
     l2 = len(pytestpm.get_plugins())
     assert l2 == l1 + 1
     assert pytestpm.get_plugin("xy123")
     pytestpm.consider_env()
     l3 = len(pytestpm.get_plugins())
     assert l2 == l3
Пример #21
0
    def test_preserve_modules(self, monkeypatch: MonkeyPatch) -> None:
        key = [self.key + str(i) for i in range(3)]
        assert not any(k in sys.modules for k in key)
        for i, k in enumerate(key):
            mod = ModuleType("something" + str(i))
            monkeypatch.setitem(sys.modules, k, mod)
        original = dict(sys.modules)

        def preserve(name):
            return name in (key[0], key[1], "some-other-key")

        snapshot = SysModulesSnapshot(preserve=preserve)
        sys.modules[key[0]] = original[key[0]] = ModuleType("something else0")
        sys.modules[key[1]] = original[key[1]] = ModuleType("something else1")
        sys.modules[key[2]] = ModuleType("something else2")
        snapshot.restore()
        assert sys.modules == original
Пример #22
0
def test_setitem():
    d = {'x': 1}
    monkeypatch = MonkeyPatch()
    monkeypatch.setitem(d, 'x', 2)
    monkeypatch.setitem(d, 'y', 1700)
    monkeypatch.setitem(d, 'y', 1700)
    assert d['x'] == 2
    assert d['y'] == 1700
    monkeypatch.setitem(d, 'x', 3)
    assert d['x'] == 3
    monkeypatch.undo()
    assert d['x'] == 1
    assert 'y' not in d
    d['x'] = 5
    monkeypatch.undo()
    assert d['x'] == 5
Пример #23
0
def test_setitem():
    d = {'x': 1}
    monkeypatch = MonkeyPatch()
    monkeypatch.setitem(d, 'x', 2)
    monkeypatch.setitem(d, 'y', 1700)
    monkeypatch.setitem(d, 'y', 1700)
    assert d['x'] == 2
    assert d['y'] == 1700
    monkeypatch.setitem(d, 'x', 3)
    assert d['x'] == 3
    monkeypatch.undo()
    assert d['x'] == 1
    assert 'y' not in d
    d['x'] = 5
    monkeypatch.undo()
    assert d['x'] == 5
Пример #24
0
def test_setitem():
    d = {"x": 1}
    monkeypatch = MonkeyPatch()
    monkeypatch.setitem(d, "x", 2)
    monkeypatch.setitem(d, "y", 1700)
    monkeypatch.setitem(d, "y", 1700)
    assert d["x"] == 2
    assert d["y"] == 1700
    monkeypatch.setitem(d, "x", 3)
    assert d["x"] == 3
    monkeypatch.undo()
    assert d["x"] == 1
    assert "y" not in d
    d["x"] = 5
    monkeypatch.undo()
    assert d["x"] == 5
Пример #25
0
def test_setitem():
    d = {"x": 1}
    monkeypatch = MonkeyPatch()
    monkeypatch.setitem(d, "x", 2)
    monkeypatch.setitem(d, "y", 1700)
    monkeypatch.setitem(d, "y", 1700)
    assert d["x"] == 2
    assert d["y"] == 1700
    monkeypatch.setitem(d, "x", 3)
    assert d["x"] == 3
    monkeypatch.undo()
    assert d["x"] == 1
    assert "y" not in d
    d["x"] = 5
    monkeypatch.undo()
    assert d["x"] == 5
Пример #26
0
def test_should_do_markup_FORCE_COLOR(monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setitem(os.environ, "FORCE_COLOR", "1")
    assert_color_set()
Пример #27
0
def pytest_generate_tests(metafunc):
    monkeypatch = MonkeyPatch()
    monkeypatch.setitem(os.environ, 'SLACK_SECRET_NAME', 'slack-security-bot')
    create_secret()
Пример #28
0
def test_extend_conflicting_plugin_key_with_fixed_entry(
        monkeypatch: MonkeyPatch):
    """Keys get proper postfix if there isn't a fixed key already."""
    monkeypatch.setitem(mock_registry_data_io, "sdt_gta", SdtDataIo("sdt"))
    assert (extend_conflicting_plugin_key(
        "sdt", SdtDataIo("sdt"), mock_registry_data_io) == "sdt_gta_1")
Пример #29
0
def test_should_not_do_markup_NO_COLOR_and_FORCE_COLOR(
    monkeypatch: MonkeyPatch, ) -> None:  # noqa
    monkeypatch.setitem(os.environ, "NO_COLOR", "1")
    monkeypatch.setitem(os.environ, "FORCE_COLOR", "1")
    assert_color_not_set()
Пример #30
0
def test_should_not_do_markup_PY_COLORS_eq_0(monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setitem(os.environ, "PY_COLORS", "0")
    assert_color_not_set()