예제 #1
0
    def test_context_exception():
        """Test that exception during update does not update file."""
        test_file = tempfile.NamedTemporaryFile()
        with pytest.raises(ValueError):
            with YAMLFile(test_file.name) as yaml_file:
                yaml_file['property1'] = 'value1'
                raise ValueError('Test')

        assert open(test_file.name, 'r').read() == ''
예제 #2
0
    def test_update_empty_yaml_file(self):
        """
        Update an empty YAML file with content.
        """
        fp = tempfile.NamedTemporaryFile()
        conf = {'property1': self.kv_pair}

        with YAMLFile(fp.name) as file_conf:
            for key in conf.keys():
                file_conf[key] = conf[key]

        with open(fp.name, 'r') as retrieved_conf:
            assert retrieved_conf.read() == ruamel.yaml.round_trip_dump(conf)
예제 #3
0
    def test_update_empty_yaml_file(self):
        """
        Update an empty YAML file with content.
        """
        test_file = tempfile.NamedTemporaryFile()
        conf = {'property1': self.kv_pair}

        with YAMLFile(test_file.name) as file_conf:
            for key in conf:
                file_conf[key] = conf[key]

        with open(test_file.name, 'r') as retrieved_conf:
            buffer = StringIO()
            self.yaml.dump(conf, buffer)
            assert retrieved_conf.read() == buffer.getvalue()
예제 #4
0
    def test_update_non_empty_yaml_file(self):
        """
        Update a non-empty YAML file with modifications
        """
        test_file = tempfile.NamedTemporaryFile()

        with open(test_file.name, 'w') as conf_file:
            self.yaml.dump({'property1': self.kv_pair}, conf_file)

        with YAMLFile(test_file.name) as file_conf:
            file_conf['property2'] = self.kv_pair

        with open(test_file.name, 'r') as retrieved_conf:
            file_conf = self.yaml.load(retrieved_conf)
            assert file_conf == {
                'property1': self.kv_pair,
                'property2': self.kv_pair
            }
예제 #5
0
    def test_update_non_empty_yaml_file(self):
        """
        Update a non-empty YAML file with modifications
        """
        fp = tempfile.NamedTemporaryFile()

        with open(fp.name, 'w') as conf_file:
            conf_file.write(
                ruamel.yaml.round_trip_dump({
                    'property1': self.kv_pair
                }))

        with YAMLFile(fp.name) as file_conf:
            file_conf['property2'] = self.kv_pair

        with open(fp.name, 'r') as retrieved_conf:
            file_conf = ruamel.yaml.round_trip_load(retrieved_conf)
            assert file_conf == {'property1': self.kv_pair,
                                 'property2': self.kv_pair}