Enable writing tests for fuelmenu
Added running tests with py.test in fuelmenu Added 2 simple tests for settings and cleaned settings.py a little bit Ignored tests .cache dir Removed not needed requirements.txt since setup.py has it all Change-Id: Id6869407273b38c57f3d3e7c8dff635829178348 Partial-Bug: #1365391
This commit is contained in:
parent
fa484f497b
commit
d03601544c
|
@ -13,11 +13,9 @@
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import collections
|
import collections
|
||||||
try:
|
import logging
|
||||||
from collections import OrderedDict
|
|
||||||
except Exception:
|
from ordereddict import OrderedDict
|
||||||
# python 2.6 or earlier use backport
|
|
||||||
from ordereddict import OrderedDict
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
|
||||||
|
@ -77,9 +75,7 @@ yaml.representer.Representer.add_representer(OrderedDict, yaml.representer.
|
||||||
SafeRepresenter.represent_dict)
|
SafeRepresenter.represent_dict)
|
||||||
|
|
||||||
|
|
||||||
class Settings():
|
class Settings(object):
|
||||||
def __init__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def read(self, yamlfile):
|
def read(self, yamlfile):
|
||||||
try:
|
try:
|
||||||
|
@ -88,8 +84,7 @@ class Settings():
|
||||||
return settings
|
return settings
|
||||||
except Exception:
|
except Exception:
|
||||||
if yamlfile is not None:
|
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()
|
return OrderedDict()
|
||||||
|
|
||||||
def write(self, newvalues, tree=None, defaultsfile='settings.yaml',
|
def write(self, newvalues, tree=None, defaultsfile='settings.yaml',
|
||||||
|
@ -101,25 +96,3 @@ class Settings():
|
||||||
yaml.dump(settings, outfile, default_style='"',
|
yaml.dump(settings, outfile, default_style='"',
|
||||||
default_flow_style=False)
|
default_flow_style=False)
|
||||||
return True
|
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())
|
|
||||||
|
|
|
@ -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
|
usedevelop = True
|
||||||
install_command = pip install --allow-external -U {opts} {packages}
|
install_command = pip install --allow-external -U {opts} {packages}
|
||||||
setenv = VIRTUAL_ENV={envdir}
|
setenv = VIRTUAL_ENV={envdir}
|
||||||
deps = -r{toxinidir}/requirements.txt
|
deps =
|
||||||
-r{toxinidir}/test-requirements.txt
|
-r{toxinidir}/test-requirements.txt
|
||||||
commands =
|
commands =
|
||||||
|
py.test -vv {posargs:fuelmenu/tests}
|
||||||
|
|
||||||
[tox:jenkins]
|
[tox:jenkins]
|
||||||
downloadcache = ~/cache/pip
|
downloadcache = ~/cache/pip
|
||||||
|
|
Loading…
Reference in New Issue