Add last tests for utils.
This commit is contained in:
parent
34fd36a503
commit
743493cee3
@ -18,7 +18,7 @@ import os
|
|||||||
import ConfigParser
|
import ConfigParser
|
||||||
|
|
||||||
|
|
||||||
CONFIG = {}
|
CONFIG = None
|
||||||
curdir = os.path.abspath(os.path.dirname(__file__))
|
curdir = os.path.abspath(os.path.dirname(__file__))
|
||||||
INIFILE = os.path.abspath(os.path.join(curdir, '..', 'etc', "config.ini"))
|
INIFILE = os.path.abspath(os.path.join(curdir, '..', 'etc', "config.ini"))
|
||||||
|
|
||||||
@ -27,24 +27,25 @@ class ConfigurationError(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def parse_ini(inifile):
|
def parse_ini(inicfg=INIFILE):
|
||||||
if os.path.exists(inifile):
|
if hasattr(inicfg, 'read'):
|
||||||
fp = open(inifile)
|
fp = inicfg
|
||||||
elif type(inifile) is file:
|
elif os.path.exists(inicfg):
|
||||||
fp = inifile
|
fp = open(inicfg)
|
||||||
else:
|
else:
|
||||||
raise ConfigurationError("Cannot found inifile")
|
raise ConfigurationError("Cannot found inicfg")
|
||||||
|
|
||||||
config = ConfigParser.RawConfigParser()
|
config = ConfigParser.RawConfigParser()
|
||||||
config.readfp(fp)
|
config.readfp(fp)
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
def get_config(section, option, default=None):
|
def get_config(section, option, default=None, _config=None):
|
||||||
"""Get section/option from ConfigParser or print default if specified"""
|
"""Get section/option from ConfigParser or print default if specified"""
|
||||||
global CONFIG
|
global CONFIG
|
||||||
|
if _config:
|
||||||
if not CONFIG:
|
CONFIG = _config
|
||||||
|
elif not CONFIG:
|
||||||
CONFIG = parse_ini()
|
CONFIG = parse_ini()
|
||||||
|
|
||||||
if not CONFIG.has_section(section):
|
if not CONFIG.has_section(section):
|
||||||
|
72
tests/test_utils.py
Normal file
72
tests/test_utils.py
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
|
||||||
|
#
|
||||||
|
# Author: Chmouel Boudjnah <chmouel@enovance.com>
|
||||||
|
#
|
||||||
|
# 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 ConfigParser
|
||||||
|
from cStringIO import StringIO
|
||||||
|
|
||||||
|
import base as test_base
|
||||||
|
import swsync.utils
|
||||||
|
|
||||||
|
|
||||||
|
class TestAccount(test_base.TestCase):
|
||||||
|
def test_parse_ini_file_not_found(self):
|
||||||
|
self.stubs.Set(swsync.utils.os.path, 'exists',
|
||||||
|
lambda x: False)
|
||||||
|
self.assertRaises(swsync.utils.ConfigurationError,
|
||||||
|
swsync.utils.parse_ini, "/tmp/foo")
|
||||||
|
|
||||||
|
def test_parse_ini_bad_file(self):
|
||||||
|
s = StringIO("foo=bar")
|
||||||
|
self.assertRaises(ConfigParser.MissingSectionHeaderError,
|
||||||
|
swsync.utils.parse_ini, s)
|
||||||
|
|
||||||
|
def test_parse_ini(self):
|
||||||
|
s = StringIO("[foo]\nfoo=bar")
|
||||||
|
self.assertIsInstance(swsync.utils.parse_ini(s),
|
||||||
|
ConfigParser.RawConfigParser)
|
||||||
|
|
||||||
|
def test_get_config(self):
|
||||||
|
s = StringIO("[foo]\nkey=bar")
|
||||||
|
cfg = swsync.utils.parse_ini(s)
|
||||||
|
self.assertEqual(swsync.utils.get_config('foo', 'key', _config=cfg),
|
||||||
|
'bar')
|
||||||
|
|
||||||
|
def test_get_config_no_section(self):
|
||||||
|
s = StringIO("[pasla]\nkey=bar")
|
||||||
|
cfg = swsync.utils.parse_ini(s)
|
||||||
|
self.assertRaises(swsync.utils.ConfigurationError,
|
||||||
|
swsync.utils.get_config,
|
||||||
|
'foo', 'key', _config=cfg)
|
||||||
|
|
||||||
|
def test_get_config_with_default(self):
|
||||||
|
s = StringIO("[foo]\n")
|
||||||
|
cfg = swsync.utils.parse_ini(s)
|
||||||
|
self.assertEqual(swsync.utils.get_config('foo', 'key', default='MEME',
|
||||||
|
_config=cfg),
|
||||||
|
'MEME')
|
||||||
|
|
||||||
|
def test_get_config_auto_parsed(self):
|
||||||
|
s = StringIO("[foo]\nkey=bar")
|
||||||
|
cfg = swsync.utils.parse_ini(s)
|
||||||
|
self.stubs.Set(swsync.utils, 'CONFIG', cfg)
|
||||||
|
self.assertEqual(swsync.utils.get_config('foo', 'key'), 'bar')
|
||||||
|
|
||||||
|
def test_get_config_no_value(self):
|
||||||
|
s = StringIO("[foo]\n")
|
||||||
|
cfg = swsync.utils.parse_ini(s)
|
||||||
|
self.assertRaises(swsync.utils.ConfigurationError,
|
||||||
|
swsync.utils.get_config,
|
||||||
|
'foo', 'key', _config=cfg)
|
Loading…
x
Reference in New Issue
Block a user