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


Python MagicMock.__enter__方法代码示例

本文整理汇总了Python中mock.mock.MagicMock.__enter__方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.__enter__方法的具体用法?Python MagicMock.__enter__怎么用?Python MagicMock.__enter__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mock.mock.MagicMock的用法示例。


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

示例1: test_action_create_simple_xml_config

# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import __enter__ [as 别名]
  def test_action_create_simple_xml_config(self,
                                           time_asctime_mock,
                                           os_path_isdir_mock,
                                           os_path_exists_mock,
                                           open_mock,
                                           ensure_mock):
    """
    Tests if 'create' action - creates new non existent xml file and write proper data
    where configurations={"Some conf":"Some value"}
    """
    os_path_isdir_mock.side_effect = [False, True]
    os_path_exists_mock.return_value = False
    time_asctime_mock.return_value = 'Wed 2014-02'

    result_file = MagicMock()
    open_mock.return_value = result_file

    with Environment('/') as env:
      XmlConfig('file.xml',
                conf_dir='/dir/conf',
                configurations={'property1': 'value1'}
      )

    open_mock.assert_called_with('/dir/conf/file.xml', 'wb')
    result_file.__enter__().write.assert_called_with(u'<!--Wed 2014-02-->\n    <configuration>\n    \n    <property>\n      <name>property1</name>\n      <value>value1</value>\n    </property>\n    \n  </configuration>\n')
开发者ID:Altiscale,项目名称:incubator-slider,代码行数:27,代码来源:TestXmlConfigResource.py

示例2: test_action_create_empty_properties_with_dir

# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import __enter__ [as 别名]
  def test_action_create_empty_properties_with_dir(self,
                                                   time_asctime_mock,
                                                   os_path_isdir_mock,
                                                   os_path_exists_mock,
                                                   open_mock,
                                                   ensure_mock):
    """
    Tests if 'action_create' - creates new non existent file and write proper data
    1) properties={}
    2) dir='Some directory that exist '
    """
    os_path_isdir_mock.side_effect = [False, True]
    os_path_exists_mock.return_value = False
    time_asctime_mock.return_value = 'Some other day'

    result_file = MagicMock()
    open_mock.return_value = result_file

    with Environment('/') as env:
      PropertiesFile('file.txt',
                     dir="/dir/and/dir",
                     properties={},
      )

    open_mock.assert_called_with('/dir/and/dir/file.txt', 'wb')
    result_file.__enter__().write.assert_called_with(u'# Generated by Apache Ambari. Some other day\n    \n    \n')
    self.assertEqual(open_mock.call_count, 1)
    ensure_mock.assert_called()
开发者ID:duxia,项目名称:ambari,代码行数:30,代码来源:TestPropertiesFileResource.py

示例3: test_action_create_xml_config_with_metacharacters

# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import __enter__ [as 别名]
  def test_action_create_xml_config_with_metacharacters(self,
                                                        time_asctime_mock,
                                                        os_path_isdir_mock,
                                                        os_path_exists_mock,
                                                        open_mock,
                                                        ensure_mock):
    """
    Tests if 'create' action - creates new non existent xml file and write proper data
    where configurations={"Some conf":"Some metacharacters"}
    """
    os_path_isdir_mock.side_effect = [False, True]
    os_path_exists_mock.return_value = False
    time_asctime_mock.return_value = 'Wed 2014-02'

    result_file = MagicMock()
    open_mock.return_value = result_file

    with Environment('/') as env:
      XmlConfig('file.xml',
                conf_dir='/dir/conf',
                configurations={"": "",
                                "prop.1": "'.'yyyy-MM-dd-HH",
                                "prop.3": "%d{ISO8601} %5p %c{1}:%L - %m%n",
                                "prop.2": "INFO, openjpa",
                                "prop.4": "${oozie.log.dir}/oozie.log",
                                "prop.empty": "",
                },
      )

    open_mock.assert_called_with('/dir/conf/file.xml', 'wb')
    result_file.__enter__().write.assert_called_with(u'<!--Wed 2014-02-->\n    <configuration>\n    \n    <property>\n      <name></name>\n      <value></value>\n    </property>\n    \n    <property>\n      <name>prop.empty</name>\n      <value></value>\n    </property>\n    \n    <property>\n      <name>prop.3</name>\n      <value>%d{ISO8601} %5p %c{1}:%L - %m%n</value>\n    </property>\n    \n    <property>\n      <name>prop.2</name>\n      <value>INFO, openjpa</value>\n    </property>\n    \n    <property>\n      <name>prop.1</name>\n      <value>&#39;.&#39;yyyy-MM-dd-HH</value>\n    </property>\n    \n    <property>\n      <name>prop.4</name>\n      <value>${oozie.log.dir}/oozie.log</value>\n    </property>\n    \n  </configuration>\n')
开发者ID:Altiscale,项目名称:incubator-slider,代码行数:33,代码来源:TestXmlConfigResource.py

示例4: test_action_create_properties_rewrite_content

# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import __enter__ [as 别名]
  def test_action_create_properties_rewrite_content(self,
                                                    time_asctime_mock,
                                                    os_path_isdir_mock,
                                                    os_path_exists_mock,
                                                    open_mock,
                                                    ensure_mock):
    """
    Tests if 'action_create' - rewrite file that exist
    1) properties={"Some property":"Some value"}
    2) dir="Some dir"
    """
    os_path_isdir_mock.side_effect = [False, True]
    os_path_exists_mock.return_value = True
    time_asctime_mock.return_value = 777

    result_file = MagicMock()
    result_file.read.return_value = 'old-content'
    open_mock.return_value = result_file

    with Environment('/') as env:
      PropertiesFile('new_file',
                     dir='/dir1',
                     properties={'property_1': 'value1'},
      )

    result_file.read.assert_called()
    open_mock.assert_called_with('/dir1/new_file', 'wb')
    result_file.__enter__().write.assert_called_with(u'# Generated by Apache Ambari. 777\n    \nproperty_1=value1\n    \n')
    self.assertEqual(open_mock.call_count, 2)
    ensure_mock.assert_called()
开发者ID:duxia,项目名称:ambari,代码行数:32,代码来源:TestPropertiesFileResource.py

示例5: test_action_create_empty_properties_without_dir

# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import __enter__ [as 别名]
  def test_action_create_empty_properties_without_dir(self,
                                                      time_asctime_mock,
                                                      os_path_isdir_mock,
                                                      os_path_exists_mock,
                                                      open_mock,
                                                      ensure_mock):
    """
    Tests if 'action_create' - creates new non existent file and write proper data
    1) properties={}
    2) dir=None
    """
    os_path_isdir_mock.side_effect = [False, True]
    os_path_exists_mock.return_value = False
    time_asctime_mock.return_value = 'Today is Wednesday'

    result_file = MagicMock()
    open_mock.return_value = result_file

    with Environment('/') as env:
      PropertiesFile('/somewhere_in_system/one_file.properties',
                     dir=None,
                     properties={}
      )

    open_mock.assert_called_with('/somewhere_in_system/one_file.properties', 'wb')
    result_file.__enter__().write.assert_called_with( u'# Generated by Apache Ambari. Today is Wednesday\n    \n    \n')
    self.assertEqual(open_mock.call_count, 1)
    ensure_mock.assert_called()
开发者ID:duxia,项目名称:ambari,代码行数:30,代码来源:TestPropertiesFileResource.py

示例6: test_action_create_properties_with_metacharacters

# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import __enter__ [as 别名]
  def test_action_create_properties_with_metacharacters(self,
                                                        time_asctime_mock,
                                                        os_path_isdir_mock,
                                                        os_path_exists_mock,
                                                        open_mock,
                                                        ensure_mock):
    """
    Tests if 'action_create' - creates new non existent file and write proper data
    1) properties={"":"", "Some property":"Metacharacters: -%{} ${a.a}/"}
    2) dir=None
    """
    os_path_isdir_mock.side_effect = [False, True]
    os_path_exists_mock.return_value = False
    time_asctime_mock.return_value = 777

    result_file = MagicMock()
    open_mock.return_value = result_file

    with Environment('/') as env:
      PropertiesFile('/dir/new_file',
                     properties={"": "",
                                 "prop.1": "'.'yyyy-MM-dd-HH",
                                 "prop.3": "%d{ISO8601} %5p %c{1}:%L - %m%n",
                                 "prop.2": "INFO, openjpa",
                                 "prop.4": "${oozie.log.dir}/oozie.log",
                                 "prop.empty": "",
                     },
      )

    open_mock.assert_called_with('/dir/new_file','wb')
    result_file.__enter__().write.assert_called_with(u"# Generated by Apache Ambari. 777\n    \n=\nprop.1='.'yyyy-MM-dd-HH\nprop.2=INFO, openjpa\nprop.3=%d{ISO8601} %5p %c{1}:%L - %m%n\nprop.4=${oozie.log.dir}/oozie.log\nprop.empty=\n    \n")
    self.assertEqual(open_mock.call_count, 1)
    ensure_mock.assert_called()
开发者ID:duxia,项目名称:ambari,代码行数:35,代码来源:TestPropertiesFileResource.py

示例7: test_action_create_properties_simple

# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import __enter__ [as 别名]
  def test_action_create_properties_simple(self,
                                           time_asctime_mock,
                                           os_path_isdir_mock,
                                           os_path_exists_mock,
                                           open_mock,
                                           ensure_mock):
    """
    Tests if 'action_create' - creates new non existent file and write proper data
    1) properties={"Some property":"Some value"}
    2) dir=None
    """

    os_path_isdir_mock.side_effect = [False, True]
    os_path_exists_mock.return_value = False
    time_asctime_mock.return_value = 777

    result_file = MagicMock()
    open_mock.return_value = result_file

    with Environment('/') as env:
      PropertiesFile('/dir/new_file',
                     properties={'property1': 'value1'},
      )

    open_mock.assert_called_with('/dir/new_file',
                                 'wb')
    result_file.__enter__().write.assert_called_with(u'# Generated by Apache Ambari. 777\n    \nproperty1=value1\n    \n')
    self.assertEqual(open_mock.call_count, 1)
    ensure_mock.assert_called()
开发者ID:duxia,项目名称:ambari,代码行数:31,代码来源:TestPropertiesFileResource.py

示例8: test_action_create_xml_config_sorted_by_key

# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import __enter__ [as 别名]
  def test_action_create_xml_config_sorted_by_key(self,
                                                  time_asctime_mock,
                                                  os_path_isdir_mock,
                                                  os_path_exists_mock,
                                                  open_mock,
                                                  ensure_mock):
    """
    Tests if 'create' action - creates new non existent xml file and writes proper data
    where configurations={"Key":"Value"} are stored in sorted by key order
    """
    os_path_isdir_mock.side_effect = [False, True]
    os_path_exists_mock.return_value = False
    time_asctime_mock.return_value = 'Wed 2014-02'

    result_file = MagicMock()
    open_mock.return_value = result_file

    with Environment('/') as env:
      XmlConfig('file.xml',
                conf_dir='/dir/conf',
                configurations={"": "",
                                "third": "should be third",
                                "first": "should be first",
                                "z_last": "should be last",
                                "second": "should be second",
                                },
                configuration_attributes={}
                )

    open_mock.assert_called_with('/dir/conf/file.xml', 'wb')
    result_file.__enter__().write.assert_called_with(u'<!--Wed 2014-02-->\n    <configuration>\n    \n    <property>\n      <name></name>\n      <value></value>\n    </property>\n    \n    <property>\n      <name>first</name>\n      <value>should be first</value>\n    </property>\n    \n    <property>\n      <name>second</name>\n      <value>should be second</value>\n    </property>\n    \n    <property>\n      <name>third</name>\n      <value>should be third</value>\n    </property>\n    \n    <property>\n      <name>z_last</name>\n      <value>should be last</value>\n    </property>\n    \n  </configuration>\n')
开发者ID:duxia,项目名称:ambari,代码行数:33,代码来源:TestXmlConfigResource.py

示例9: test_action_create_non_existent_file

# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import __enter__ [as 别名]
  def test_action_create_non_existent_file(self, isdir_mock, exists_mock, open_mock, ensure_mock):
    """
    Tests if 'create' action create new non existent file and write proper data
    """
    isdir_mock.side_effect = [False, True]
    exists_mock.return_value = False
    new_file = MagicMock()
    open_mock.return_value = new_file
    with Environment('/') as env:
      File('/directory/file',
           action='create',
           mode=0777,
           content='file-content'
      )
    

    open_mock.assert_called_with('/directory/file', 'wb')
    new_file.__enter__().write.assert_called_with('file-content')
    self.assertEqual(open_mock.call_count, 1)
    ensure_mock.assert_called()
开发者ID:Altiscale,项目名称:incubator-slider,代码行数:22,代码来源:TestFileResource.py

示例10: test_action_create_encoding

# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import __enter__ [as 别名]
  def test_action_create_encoding(self, isdir_mock, exists_mock, open_mock, get_content_mock ,ensure_mock):

    isdir_mock.side_effect = [False, True]
    exists_mock.return_value = True
    content_mock = MagicMock()
    old_content_mock = MagicMock()
    get_content_mock.return_value = content_mock
    new_file = MagicMock()
    open_mock.return_value = new_file
    enter_file_mock = MagicMock()
    enter_file_mock.read = MagicMock(return_value=old_content_mock)
    new_file.__enter__ = MagicMock(return_value=enter_file_mock)
    with Environment('/') as env:
      File('/directory/file',
           action='create',
           mode=0777,
           content='file-content',
           encoding = "UTF-8"
      )


    open_mock.assert_called_with('/directory/file', 'wb')
    content_mock.encode.assert_called_with('UTF-8')
    old_content_mock.decode.assert_called_with('UTF-8')
开发者ID:duxia,项目名称:ambari,代码行数:26,代码来源:TestFileResource.py


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