Merge "Enable writing tests for fuelmenu"
This commit is contained in:
commit
2265e95320
@ -13,11 +13,9 @@
|
||||
# under the License.
|
||||
|
||||
import collections
|
||||
try:
|
||||
from collections import OrderedDict
|
||||
except Exception:
|
||||
# python 2.6 or earlier use backport
|
||||
from ordereddict import OrderedDict
|
||||
import logging
|
||||
|
||||
from ordereddict import OrderedDict
|
||||
import yaml
|
||||
|
||||
|
||||
@ -77,9 +75,7 @@ yaml.representer.Representer.add_representer(OrderedDict, yaml.representer.
|
||||
SafeRepresenter.represent_dict)
|
||||
|
||||
|
||||
class Settings():
|
||||
def __init__(self):
|
||||
pass
|
||||
class Settings(object):
|
||||
|
||||
def read(self, yamlfile):
|
||||
try:
|
||||
@ -88,8 +84,7 @@ class Settings():
|
||||
return settings
|
||||
except Exception:
|
||||
if yamlfile is not None:
|
||||
import logging
|
||||
logging.error("Unable to read YAML: %s" % yamlfile)
|
||||
logging.error("Unable to read YAML: %s", yamlfile)
|
||||
return OrderedDict()
|
||||
|
||||
def write(self, newvalues, tree=None, defaultsfile='settings.yaml',
|
||||
@ -101,25 +96,3 @@ class Settings():
|
||||
yaml.dump(settings, outfile, default_style='"',
|
||||
default_flow_style=False)
|
||||
return True
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
sample = """
|
||||
one:
|
||||
two: fish
|
||||
red: fish
|
||||
blue: fish
|
||||
two:
|
||||
a: yes
|
||||
b: no
|
||||
c: null
|
||||
"""
|
||||
infile = file('settings.yaml', 'r')
|
||||
data = yaml.load(infile)
|
||||
#data = yaml.load(infile, OrderedDictYAMLLoader)
|
||||
#data = yaml.load(textwrap.dedent(sample), OrderedDictYAMLLoader)
|
||||
outfile = file("testout", 'w')
|
||||
yaml.dump(data, outfile, default_flow_style=False)
|
||||
|
||||
#assert type(data) is OrderedDict
|
||||
print(data.items())
|
||||
|
48
fuelmenu/tests/test_settings.py
Normal file
48
fuelmenu/tests/test_settings.py
Normal file
@ -0,0 +1,48 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2015 Mirantis, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import mock
|
||||
import ordereddict
|
||||
|
||||
from fuelmenu import settings
|
||||
|
||||
|
||||
def test_read_settings(tmpdir):
|
||||
yaml_file = tmpdir.join("yamlfile.yaml")
|
||||
yaml_file.write("""
|
||||
sample:
|
||||
- one
|
||||
- a: b
|
||||
c: d
|
||||
""")
|
||||
data = settings.Settings().read(yaml_file.strpath)
|
||||
assert data == {
|
||||
'sample': [
|
||||
'one',
|
||||
{
|
||||
'a': 'b',
|
||||
'c': 'd',
|
||||
}
|
||||
]
|
||||
}
|
||||
assert isinstance(data, ordereddict.OrderedDict)
|
||||
|
||||
|
||||
@mock.patch('fuelmenu.settings.file', side_effect=Exception('Error'))
|
||||
def test_read_settings_with_error(_):
|
||||
data = settings.Settings().read('some_path')
|
||||
assert data == {}
|
||||
assert isinstance(data, ordereddict.OrderedDict)
|
@ -1,5 +0,0 @@
|
||||
netaddr>=0.7.5
|
||||
OrderedDict>=1.1
|
||||
PyYAML>=3.10
|
||||
netifaces>=0.5
|
||||
urwid>=1.1.1
|
@ -1 +1,2 @@
|
||||
hacking==0.7
|
||||
pytest==2.8.0
|
||||
mock==1.3.0
|
||||
|
5
tox.ini
5
tox.ini
@ -7,9 +7,10 @@ envlist = py26,py27,pep8
|
||||
usedevelop = True
|
||||
install_command = pip install --allow-external -U {opts} {packages}
|
||||
setenv = VIRTUAL_ENV={envdir}
|
||||
deps = -r{toxinidir}/requirements.txt
|
||||
-r{toxinidir}/test-requirements.txt
|
||||
deps =
|
||||
-r{toxinidir}/test-requirements.txt
|
||||
commands =
|
||||
py.test -vv {posargs:fuelmenu/tests}
|
||||
|
||||
[tox:jenkins]
|
||||
downloadcache = ~/cache/pip
|
||||
|
Loading…
Reference in New Issue
Block a user