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:
Sebastian Kalinowski 2015-09-24 14:03:39 +02:00 committed by Sebastian Kalinowski
parent fa484f497b
commit d03601544c
5 changed files with 58 additions and 40 deletions

View File

@ -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())

View 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)

View File

@ -1,5 +0,0 @@
netaddr>=0.7.5
OrderedDict>=1.1
PyYAML>=3.10
netifaces>=0.5
urwid>=1.1.1

View File

@ -1 +1,2 @@
hacking==0.7
pytest==2.8.0
mock==1.3.0

View File

@ -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