From d03601544c63895b0f9625cbae28b6a002989d8a Mon Sep 17 00:00:00 2001 From: Sebastian Kalinowski Date: Thu, 24 Sep 2015 14:03:39 +0200 Subject: [PATCH] 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 --- fuelmenu/settings.py | 37 ++++--------------------- fuelmenu/tests/test_settings.py | 48 +++++++++++++++++++++++++++++++++ requirements.txt | 5 ---- test-requirements.txt | 3 ++- tox.ini | 5 ++-- 5 files changed, 58 insertions(+), 40 deletions(-) create mode 100644 fuelmenu/tests/test_settings.py delete mode 100644 requirements.txt diff --git a/fuelmenu/settings.py b/fuelmenu/settings.py index c5e285a..51716cb 100644 --- a/fuelmenu/settings.py +++ b/fuelmenu/settings.py @@ -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()) diff --git a/fuelmenu/tests/test_settings.py b/fuelmenu/tests/test_settings.py new file mode 100644 index 0000000..4589616 --- /dev/null +++ b/fuelmenu/tests/test_settings.py @@ -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) diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 10b6e20..0000000 --- a/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -netaddr>=0.7.5 -OrderedDict>=1.1 -PyYAML>=3.10 -netifaces>=0.5 -urwid>=1.1.1 diff --git a/test-requirements.txt b/test-requirements.txt index 5041deb..9ab8c65 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1 +1,2 @@ -hacking==0.7 \ No newline at end of file +pytest==2.8.0 +mock==1.3.0 diff --git a/tox.ini b/tox.ini index fe79a88..4fab6b6 100644 --- a/tox.ini +++ b/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