当前位置: 首页>>代码示例>>Python>>正文


Python scaffold.mock函数代码示例

本文整理汇总了Python中scaffold.mock函数的典型用法代码示例。如果您正苦于以下问题:Python mock函数的具体用法?Python mock怎么用?Python mock使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了mock函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: setUp

    def setUp(self):
        """ Set up test fixtures. """
        self.mock_tracker = scaffold.MockTracker()

        pidlockfile_scenarios = make_pidlockfile_scenarios()
        self.pidlockfile_scenario = pidlockfile_scenarios['simple']
        pidfile_path = self.pidlockfile_scenario['path']

        scaffold.mock(
            "pidlockfile.PIDLockFile.__init__",
            tracker=self.mock_tracker)
        scaffold.mock(
            "pidlockfile.PIDLockFile.acquire",
            tracker=self.mock_tracker)

        self.scenario = {
            'pidfile_path': self.pidlockfile_scenario['path'],
            'acquire_timeout': object(),
            }

        self.test_kwargs = dict(
            path=self.scenario['pidfile_path'],
            acquire_timeout=self.scenario['acquire_timeout'],
            )
        self.test_instance = pidlockfile.TimeoutPIDLockFile(**self.test_kwargs)
开发者ID:AlliedSecurityTrust,项目名称:python-daemon,代码行数:25,代码来源:test_pidlockfile.py

示例2: setup_lockfile_method_mocks

def setup_lockfile_method_mocks(testcase, scenario, class_name):
    """ Set up common mock methods for lockfile class. """

    def mock_read_pid():
        return scenario["pidfile_pid"]

    def mock_is_locked():
        return scenario["locking_pid"] is not None

    def mock_i_am_locking():
        return scenario["locking_pid"] == scenario["pid"]

    def mock_acquire(timeout=None):
        if scenario["locking_pid"] is not None:
            raise lockfile.AlreadyLocked()
        scenario["locking_pid"] = scenario["pid"]

    def mock_release():
        if scenario["locking_pid"] is None:
            raise lockfile.NotLocked()
        if scenario["locking_pid"] != scenario["pid"]:
            raise lockfile.NotMyLock()
        scenario["locking_pid"] = None

    def mock_break_lock():
        scenario["locking_pid"] = None

    for func_name in ["read_pid", "is_locked", "i_am_locking", "acquire", "release", "break_lock"]:
        mock_func = vars()["mock_%(func_name)s" % vars()]
        lockfile_func_name = "%(class_name)s.%(func_name)s" % vars()
        mock_lockfile_func = scaffold.Mock(lockfile_func_name, returns_func=mock_func, tracker=testcase.mock_tracker)
        try:
            scaffold.mock(lockfile_func_name, mock_obj=mock_lockfile_func, tracker=testcase.mock_tracker)
        except NameError:
            pass
开发者ID:philtweir,项目名称:python-daemon,代码行数:35,代码来源:test_pidlockfile.py

示例3: setUp

    def setUp(self):
        """ Set up test fixtures. """
        setup_pidfile_fixtures(self)
        self.scenario = self.scenarios['exist-currentpid']

        scaffold.mock(
            "os.remove",
            tracker=self.mock_tracker)
开发者ID:getsomeual,项目名称:pylockfile,代码行数:8,代码来源:test_pidlockfile.py

示例4: setUp

    def setUp(self):
        """ Set up test fixtures. """
        setup_runner_fixtures(self)
        set_runner_scenario(self, 'simple')

        scaffold.mock(
            "daemon.runner.DaemonRunner._usage_exit",
            raises=NotImplementedError,
            tracker=self.mock_tracker)
开发者ID:LukeCarrier,项目名称:py3k-daemon,代码行数:9,代码来源:test_runner.py

示例5: setup_pidlockfile_fixtures

def setup_pidlockfile_fixtures(testcase, scenario_name=None):
    """ Set up common fixtures for PIDLockFile test cases. """

    setup_pidfile_fixtures(testcase)

    scaffold.mock("pidlockfile.write_pid_to_pidfile", tracker=testcase.mock_tracker)
    scaffold.mock("pidlockfile.remove_existing_pidfile", tracker=testcase.mock_tracker)

    if scenario_name is not None:
        set_pidlockfile_scenario(testcase, scenario_name, clear_tracker=False)
开发者ID:philtweir,项目名称:python-daemon,代码行数:10,代码来源:test_pidlockfile.py

示例6: test_should_parse_system_argv_by_default

 def test_should_parse_system_argv_by_default(self):
     """ Should parse sys.argv by default. """
     instance = self.test_instance
     expect_action = 'start'
     argv = self.valid_argv_params['start']
     scaffold.mock(
         "sys.argv",
         mock_obj=argv,
         tracker=self.mock_tracker)
     instance.parse_args()
     self.failUnlessEqual(expect_action, instance.action)
开发者ID:LukeCarrier,项目名称:py3k-daemon,代码行数:11,代码来源:test_runner.py

示例7: test_openid_store_created_with_datadir

 def test_openid_store_created_with_datadir(self):
     """ OpenIDStore should be created with specified datadir """
     params = self.valid_servers['datadir']
     datadir = params['datadir']
     scaffold.mock("server.OpenIDStore",
         outfile=self.mock_outfile)
     expect_mock_output = """\
         Called server.OpenIDStore(%(datadir)r)
         """ % vars()
     instance = self.server_class(**params['args'])
     self.failUnlessOutputCheckerMatch(
         expect_mock_output, self.mock_outfile.getvalue()
         )
开发者ID:petertodd,项目名称:gracie,代码行数:13,代码来源:test_server.py

示例8: test_parent_exits

 def test_parent_exits(self):
     """ become_daemon parent process should exit """
     parent_pid = 23
     scaffold.mock("os.fork", returns_iter=[parent_pid],
         outfile=self.mock_outfile)
     expect_mock_output = """\
         Called os.fork()
         Called os._exit(0)
         ...
         """
     server.become_daemon()
     self.failUnlessOutputCheckerMatch(
         expect_mock_output, self.mock_outfile.getvalue()
         )
开发者ID:petertodd,项目名称:gracie,代码行数:14,代码来源:test_server.py

示例9: test_requests_stop_then_start

 def test_requests_stop_then_start(self):
     """ Should request stop, then start. """
     instance = self.test_instance
     scaffold.mock(
         "daemon.runner.DaemonRunner._start",
         tracker=self.mock_tracker)
     scaffold.mock(
         "daemon.runner.DaemonRunner._stop",
         tracker=self.mock_tracker)
     expect_mock_output = """\
         Called daemon.runner.DaemonRunner._stop()
         Called daemon.runner.DaemonRunner._start()
         """
     instance.do_action()
     self.failUnlessMockCheckerMatch(expect_mock_output)
开发者ID:LukeCarrier,项目名称:py3k-daemon,代码行数:15,代码来源:test_runner.py

示例10: setUp

    def setUp(self):
        """ Set up test fixtures """
        self.mock_outfile = StringIO()

        test_pids = [0, 0]
        scaffold.mock("os.fork", returns_iter=test_pids,
            outfile=self.mock_outfile)
        scaffold.mock("os.setsid",
            outfile=self.mock_outfile)
        scaffold.mock("os._exit",
            outfile=self.mock_outfile)
        scaffold.mock("sys.exit",
            outfile=self.mock_outfile)
        scaffold.mock("server.remove_standard_files",
            outfile=self.mock_outfile)
开发者ID:petertodd,项目名称:gracie,代码行数:15,代码来源:test_server.py

示例11: setUp

    def setUp(self):
        """ Set up test fixtures. """
        self.mock_tracker = scaffold.MockTracker()

        pidlockfile_scenarios = make_pidlockfile_scenarios()
        self.pidlockfile_scenario = pidlockfile_scenarios["simple"]
        pidfile_path = self.pidlockfile_scenario["path"]

        scaffold.mock("pidlockfile.PIDLockFile.__init__", tracker=self.mock_tracker)
        scaffold.mock("pidlockfile.PIDLockFile.acquire", tracker=self.mock_tracker)

        self.scenario = {"pidfile_path": self.pidlockfile_scenario["path"], "acquire_timeout": object()}

        self.test_kwargs = dict(path=self.scenario["pidfile_path"], acquire_timeout=self.scenario["acquire_timeout"])
        self.test_instance = pidlockfile.TimeoutPIDLockFile(**self.test_kwargs)
开发者ID:philtweir,项目名称:python-daemon,代码行数:15,代码来源:test_pidlockfile.py

示例12: test_child_forks_next_parent_exits

 def test_child_forks_next_parent_exits(self):
     """ become_daemon should fork, then exit if parent """
     test_pids = [0, 42]
     scaffold.mock("os.fork", returns_iter=test_pids,
         outfile=self.mock_outfile)
     expect_mock_output = """\
         Called os.fork()
         Called os.setsid()
         Called os.fork()
         Called os._exit(0)
         ...
         """
     server.become_daemon()
     self.failUnlessOutputCheckerMatch(
         expect_mock_output, self.mock_outfile.getvalue()
         )
开发者ID:petertodd,项目名称:gracie,代码行数:16,代码来源:test_server.py

示例13: test_server_creates_http_server

 def test_server_creates_http_server(self):
     """ GracieServer should create an HTTP server """
     params = self.valid_servers['simple']
     args = params['args']
     opts = params['opts']
     server_address = (opts.host, opts.port)
     scaffold.mock("server.HTTPServer",
         outfile=self.mock_outfile)
     expect_mock_output = """\
         Called server.HTTPServer(
             %(server_address)r,
             <class '...HTTPRequestHandler'>,
             <gracie.server.GracieServer object ...>)
         """ % vars()
     instance = self.server_class(**params['args'])
     self.failUnlessOutputCheckerMatch(
         expect_mock_output, self.mock_outfile.getvalue()
         )
开发者ID:petertodd,项目名称:gracie,代码行数:18,代码来源:test_server.py

示例14: setup_pidlockfile_fixtures

def setup_pidlockfile_fixtures(testcase):
    """ Set up common fixtures for PIDLockFile test cases. """

    setup_pidfile_fixtures(testcase)

    testcase.pidlockfile_args = dict(
        path=testcase.scenario['path'],
        )

    testcase.test_instance = pidlockfile.PIDLockFile(
        **testcase.pidlockfile_args)

    scaffold.mock(
        "pidlockfile.write_pid_to_pidfile",
        tracker=testcase.mock_tracker)
    scaffold.mock(
        "pidlockfile.remove_existing_pidfile",
        tracker=testcase.mock_tracker)
开发者ID:getsomeual,项目名称:pylockfile,代码行数:18,代码来源:test_pidlockfile.py

示例15: setup_pidfile_fixtures

def setup_pidfile_fixtures(testcase):
    """ Set up common fixtures for PID file test cases. """
    testcase.mock_tracker = scaffold.MockTracker()

    mock_current_pid = 235
    mock_other_pid = 8642
    mock_pidfile_current = FakeFileDescriptorStringIO(
        "%(mock_current_pid)d\n" % vars())
    mock_pidfile_other = FakeFileDescriptorStringIO(
        "%(mock_other_pid)d\n" % vars())
    mock_pidfile_path = tempfile.mktemp()

    scaffold.mock(
        "os.getpid",
        returns=mock_current_pid,
        tracker=testcase.mock_tracker)

    def mock_pidfile_open_nonexist(filename, mode, buffering):
        if 'r' in mode:
            raise IOError("No such file %(filename)r" % vars())
        else:
            result = testcase.scenario['pidfile']
        return result

    def mock_pidfile_open_exist(filename, mode, buffering):
        result = testcase.scenario['pidfile']
        return result

    def mock_open(filename, mode='r', buffering=None):
        if filename == testcase.scenario['path']:
            result = testcase.scenario['open_func'](
                filename, mode, buffering)
        else:
            result = FakeFileDescriptorStringIO()
        return result

    scaffold.mock(
        "__builtin__.open",
        returns_func=mock_open,
        tracker=testcase.mock_tracker)

    def mock_pidfile_os_open_nonexist(filename, flags, mode):
        if (flags & os.O_CREAT):
            result = testcase.scenario['pidfile'].fileno()
        else:
            raise OSError(errno.ENOENT, "No such file", filename)
        return result

    def mock_pidfile_os_open_exist(filename, flags, mode):
        result = testcase.scenario['pidfile'].fileno()
        return result

    def mock_os_open(filename, flags, mode=None):
        if filename == testcase.scenario['path']:
            result = testcase.scenario['os_open_func'](
                filename, flags, mode)
        else:
            result = FakeFileDescriptorStringIO().fileno()
        return result

    scaffold.mock(
        "os.open",
        returns_func=mock_os_open,
        tracker=testcase.mock_tracker)

    def mock_os_fdopen(fd, mode='r', buffering=None):
        if fd == testcase.scenario['pidfile'].fileno():
            result = testcase.scenario['pidfile']
        else:
            raise OSError(errno.EBADF, "Bad file descriptor")
        return result

    scaffold.mock(
        "os.fdopen",
        returns_func=mock_os_fdopen,
        tracker=testcase.mock_tracker)

    def mock_os_path_exists(path):
        if path == testcase.scenario['path']:
            result = testcase.scenario['path_exists_func']()
        else:
            result = False
        return result

    scaffold.mock(
        "os.path.exists",
        mock_obj=mock_os_path_exists)

    testcase.scenarios = {
        'nonexist': {
            'path': mock_pidfile_path,
            'pidfile': None,
            'pidfile_pid': None,
            'path_exists_func': (lambda: False),
            'os_open_func': mock_pidfile_os_open_nonexist,
            'open_func': mock_pidfile_open_nonexist,
            },
        'exist-currentpid': {
            'path': mock_pidfile_path,
            'pidfile': mock_pidfile_current,
#.........这里部分代码省略.........
开发者ID:getsomeual,项目名称:pylockfile,代码行数:101,代码来源:test_pidlockfile.py


注:本文中的scaffold.mock函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。