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


Python mocking.mock函数代码示例

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


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

示例1: test_mirror_mirror_on_the_wall_1

  def test_mirror_mirror_on_the_wall_1(self):
    def tutorial_example():
      from stem.control import Controller

      with Controller.from_port(control_port = 9051) as controller:
        controller.authenticate()

        for desc in controller.get_network_statuses():
          print "found relay %s (%s)" % (desc.nickname, desc.fingerprint)

    controller = mocking.get_object(Controller, {
      'authenticate': mocking.no_op(),
      'close': mocking.no_op(),
      'get_network_statuses': mocking.return_value(
        [mocking.get_router_status_entry_v2()],
      ),
    })

    mocking.mock(
      Controller.from_port, mocking.return_value(controller),
      target_module = Controller,
      is_static = True,
    )

    tutorial_example()
    self.assertEqual("found relay caerSidi (A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB)\n", self.stdout.getvalue())
开发者ID:ankitmodi,项目名称:Projects,代码行数:26,代码来源:tutorial.py

示例2: test_get_pid_by_name_lsof

  def test_get_pid_by_name_lsof(self):
    """
    Tests the get_pid_by_name function with a lsof response.
    """

    runner = test.runner.get_runner()
    if self.is_extra_tor_running:
      test.runner.skip(self, "(multiple tor instances)")
      return
    elif not stem.util.system.is_available("lsof"):
      test.runner.skip(self, "(lsof unavailable)")
      return
    elif not runner.is_ptraceable():
      test.runner.skip(self, "(DisableDebuggerAttachment is set)")
      return

    lsof_prefix = stem.util.system.GET_PID_BY_NAME_LSOF % ""
    mocking.mock(stem.util.system.call, filter_system_call([lsof_prefix]))

    our_tor_pid = test.runner.get_runner().get_pid()

    all_tor_pids = stem.util.system.get_pid_by_name("tor", multiple = True)

    if len(all_tor_pids) == 1:
      self.assertEquals(our_tor_pid, all_tor_pids[0])
开发者ID:saturn597,项目名称:stem,代码行数:25,代码来源:system.py

示例3: test_load_processed_files

    def test_load_processed_files(self):
        """
    Successful load of content.
    """

        test_lines = (
            "/dir/ 0",
            "/dir/file 12345",
            "/dir/file with spaces 7138743",
            "  /dir/with extra space 12345   ",
            "   \t   ",
            "",
            "/dir/after empty line 12345",
        )

        expected_value = {
            "/dir/": 0,
            "/dir/file": 12345,
            "/dir/file with spaces": 7138743,
            "/dir/with extra space": 12345,
            "/dir/after empty line": 12345,
        }

        test_content = StringIO.StringIO("\n".join(test_lines))
        mocking.support_with(test_content)
        mocking.mock(open, mocking.return_value(test_content))
        self.assertEquals(expected_value, stem.descriptor.reader.load_processed_files(""))
开发者ID:ndanger000,项目名称:stem,代码行数:27,代码来源:reader.py

示例4: test_get_protocolinfo

  def test_get_protocolinfo(self):
    """
    Exercises the get_protocolinfo() method.
    """

    # Use the handy mocked protocolinfo response.
    mocking.mock(stem.connection.get_protocolinfo, mocking.return_value(
      mocking.get_protocolinfo_response()
    ))
    # Compare the str representation of these object, because the class
    # does not have, nor need, a direct comparison operator.
    self.assertEqual(str(mocking.get_protocolinfo_response()), str(self.controller.get_protocolinfo()))

    # Raise an exception in the stem.connection.get_protocolinfo() call.
    mocking.mock(stem.connection.get_protocolinfo, mocking.raise_exception(ProtocolError))

    # Get a default value when the call fails.

    self.assertEqual(
      "default returned",
      self.controller.get_protocolinfo(default = "default returned")
    )

    # No default value, accept the error.
    self.assertRaises(ProtocolError, self.controller.get_protocolinfo)
开发者ID:gsathya,项目名称:stem,代码行数:25,代码来源:controller.py

示例5: test_the_little_relay_that_could

  def test_the_little_relay_that_could(self):
    def tutorial_example():
      from stem.control import Controller

      with Controller.from_port(control_port = 9051) as controller:
        controller.authenticate()  # provide the password here if you set one

        bytes_read = controller.get_info("traffic/read")
        bytes_written = controller.get_info("traffic/written")

        print "My Tor relay has read %s bytes and written %s." % (bytes_read, bytes_written)

    controller = mocking.get_object(Controller, {
      'authenticate': mocking.no_op(),
      'close': mocking.no_op(),
      'get_info': mocking.return_for_args({
        ('traffic/read',): '33406',
        ('traffic/written',): '29649',
      }, is_method = True),
    })

    mocking.mock(
      Controller.from_port, mocking.return_value(controller),
      target_module = Controller,
      is_static = True,
    )

    tutorial_example()
    self.assertEqual("My Tor relay has read 33406 bytes and written 29649.\n", self.stdout.getvalue())
开发者ID:ankitmodi,项目名称:Projects,代码行数:29,代码来源:tutorial.py

示例6: test_mirror_mirror_on_the_wall_4

  def test_mirror_mirror_on_the_wall_4(self):
    def tutorial_example():
      from stem.control import Controller
      from stem.util import str_tools

      # provides a mapping of observed bandwidth to the relay nicknames
      def get_bw_to_relay():
        bw_to_relay = {}

        with Controller.from_port(control_port = 9051) as controller:
          controller.authenticate()

          for desc in controller.get_server_descriptors():
            if desc.exit_policy.is_exiting_allowed():
              bw_to_relay.setdefault(desc.observed_bandwidth, []).append(desc.nickname)

        return bw_to_relay

      # prints the top fifteen relays

      bw_to_relay = get_bw_to_relay()
      count = 1

      for bw_value in sorted(bw_to_relay.keys(), reverse = True):
        for nickname in bw_to_relay[bw_value]:
          print "%i. %s (%s/s)" % (count, nickname, str_tools.get_size_label(bw_value, 2))
          count += 1

          if count > 15:
            return

    exit_descriptor = mocking.get_relay_server_descriptor({
      'router': 'speedyexit 149.255.97.109 9001 0 0'
    }, content = True).replace(b'reject *:*', b'accept *:*')

    exit_descriptor = mocking.sign_descriptor_content(exit_descriptor)
    exit_descriptor = RelayDescriptor(exit_descriptor)

    controller = mocking.get_object(Controller, {
      'authenticate': mocking.no_op(),
      'close': mocking.no_op(),
      'get_server_descriptors': mocking.return_value([
        exit_descriptor,
        mocking.get_relay_server_descriptor(),  # non-exit
        exit_descriptor,
        exit_descriptor,
      ])
    })

    mocking.mock(
      Controller.from_port, mocking.return_value(controller),
      target_module = Controller,
      is_static = True,
    )

    tutorial_example()
    self.assertEqual(MIRROR_MIRROR_OUTPUT, self.stdout.getvalue())
开发者ID:ankitmodi,项目名称:Projects,代码行数:57,代码来源:tutorial.py

示例7: test_get_pid_by_open_file_lsof

  def test_get_pid_by_open_file_lsof(self):
    """
    Tests the get_pid_by_open_file function with a lsof response.
    """

    lsof_query = system.GET_PID_BY_FILE_LSOF % "/tmp/foo"
    mocking.mock(system.call, mock_call(lsof_query, ["4762"]))
    self.assertEquals(4762, system.get_pid_by_open_file("/tmp/foo"))
    self.assertEquals(None, system.get_pid_by_open_file("/tmp/somewhere_else"))
开发者ID:bwrichte,项目名称:TorFinalProject,代码行数:9,代码来源:system.py

示例8: test_get_pid_by_port_sockstat

  def test_get_pid_by_port_sockstat(self):
    """
    Tests the get_pid_by_port function with a sockstat response.
    """

    mocking.mock(system.call, mock_call(system.GET_PID_BY_PORT_SOCKSTAT % 9051, GET_PID_BY_PORT_SOCKSTAT_RESULTS))
    self.assertEquals(4397, system.get_pid_by_port(9051))
    self.assertEquals(4397, system.get_pid_by_port("9051"))
    self.assertEquals(None, system.get_pid_by_port(123))
开发者ID:bwrichte,项目名称:TorFinalProject,代码行数:9,代码来源:system.py

示例9: test_load_processed_files_empty

    def test_load_processed_files_empty(self):
        """
    Tests the load_processed_files() function with an empty file.
    """

        test_content = StringIO.StringIO("")
        mocking.support_with(test_content)
        mocking.mock(open, mocking.return_value(test_content))
        self.assertEquals({}, stem.descriptor.reader.load_processed_files(""))
开发者ID:ndanger000,项目名称:stem,代码行数:9,代码来源:reader.py

示例10: _mock_open

def _mock_open(content):
  test_content = StringIO.StringIO(content)
  mocking.support_with(test_content)

  if stem.prereq.is_python_3():
    import builtins
    mocking.mock(builtins.open, mocking.return_value(test_content), builtins)
  else:
    mocking.mock(open, mocking.return_value(test_content))
开发者ID:ankitmodi,项目名称:Projects,代码行数:9,代码来源:reader.py

示例11: test_get_pid_by_port_lsof

  def test_get_pid_by_port_lsof(self):
    """
    Tests the get_pid_by_port function with a lsof response.
    """

    mocking.mock(system.call, mock_call(system.GET_PID_BY_PORT_LSOF, GET_PID_BY_PORT_LSOF_RESULTS))
    self.assertEquals(1745, system.get_pid_by_port(9051))
    self.assertEquals(1745, system.get_pid_by_port("9051"))
    self.assertEquals(329, system.get_pid_by_port(80))
    self.assertEquals(None, system.get_pid_by_port(123))
开发者ID:bwrichte,项目名称:TorFinalProject,代码行数:10,代码来源:system.py

示例12: test_get_pid_by_port_netstat

  def test_get_pid_by_port_netstat(self):
    """
    Tests the get_pid_by_port function with a netstat response.
    """

    mocking.mock(system.call, mock_call(system.GET_PID_BY_PORT_NETSTAT, GET_PID_BY_PORT_NETSTAT_RESULTS))
    self.assertEquals(1641, system.get_pid_by_port(9051))
    self.assertEquals(1641, system.get_pid_by_port("9051"))
    self.assertEquals(None, system.get_pid_by_port(631))
    self.assertEquals(None, system.get_pid_by_port(123))
开发者ID:bwrichte,项目名称:TorFinalProject,代码行数:10,代码来源:system.py

示例13: test_get_pid_by_name_ps_bsd

  def test_get_pid_by_name_ps_bsd(self):
    """
    Tests the get_pid_by_name function with the bsd variant of ps.
    """

    mocking.mock(system.is_bsd, mocking.return_true())
    mocking.mock(system.call, mock_call(system.GET_PID_BY_NAME_PS_BSD, GET_PID_BY_NAME_PS_BSD))
    self.assertEquals(1, system.get_pid_by_name("launchd"))
    self.assertEquals(11, system.get_pid_by_name("DirectoryService"))
    self.assertEquals(None, system.get_pid_by_name("blarg"))
开发者ID:bwrichte,项目名称:TorFinalProject,代码行数:10,代码来源:system.py

示例14: test_load_processed_files_malformed_timestamp

    def test_load_processed_files_malformed_timestamp(self):
        """
    Tests the load_processed_files() function content that is malformed because
    it has a non-numeric timestamp.
    """

        test_content = StringIO.StringIO("/dir/file 123a")
        mocking.support_with(test_content)
        mocking.mock(open, mocking.return_value(test_content))
        self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, "")
开发者ID:ndanger000,项目名称:stem,代码行数:10,代码来源:reader.py

示例15: test_get_cwd

  def test_get_cwd(self):
    """
    Tests the get_cwd function with a given pid.
    """

    mocking.mock(os.readlink, mocking.return_for_args({
      ('/proc/24019/cwd',): '/home/directory/TEST'
    }), os)

    self.assertEquals('/home/directory/TEST', proc.get_cwd(24019))
开发者ID:ankitmodi,项目名称:Projects,代码行数:10,代码来源:proc.py


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