config becomes object-oriented and pytests have been added
This commit is contained in:
parent
5bf6040d2c
commit
9b578637ba
2 changed files with 101 additions and 41 deletions
|
@ -4,59 +4,62 @@
|
||||||
import profig
|
import profig
|
||||||
|
|
||||||
# constants
|
# constants
|
||||||
FLASK_APP = 'flask.app'
|
FLASK_APP = "flask.app"
|
||||||
|
|
||||||
DB_URL = 'main.db_url'
|
DB_URL = "main.db_url"
|
||||||
DB_FILE = 'main.db_file'
|
DB_BACKUP_JSON_FILE = "main.db_backup_json_file"
|
||||||
LANG = 'main.lang'
|
LANG = "main.lang"
|
||||||
COMMENT_POLLING = 'main.newcomment_polling'
|
COMMENT_POLLING = "main.newcomment_polling"
|
||||||
|
|
||||||
HTTP_HOST = 'http.host'
|
HTTP_HOST = "http.host"
|
||||||
HTTP_PORT = 'http.port'
|
HTTP_PORT = "http.port"
|
||||||
|
|
||||||
RSS_PROTO = 'rss.proto'
|
RSS_PROTO = "rss.proto"
|
||||||
RSS_FILE = 'rss.file'
|
RSS_FILE = "rss.file"
|
||||||
|
|
||||||
IMAP_POLLING = 'imap.polling'
|
IMAP_POLLING = "imap.polling"
|
||||||
IMAP_SSL = 'imap.ssl'
|
IMAP_SSL = "imap.ssl"
|
||||||
IMAP_HOST = 'imap.host'
|
IMAP_HOST = "imap.host"
|
||||||
IMAP_PORT = 'imap.port'
|
IMAP_PORT = "imap.port"
|
||||||
IMAP_LOGIN = 'imap.login'
|
IMAP_LOGIN = "imap.login"
|
||||||
IMAP_PASSWORD = 'imap.password'
|
IMAP_PASSWORD = "imap.password"
|
||||||
|
|
||||||
SMTP_STARTTLS = 'smtp.starttls'
|
SMTP_STARTTLS = "smtp.starttls"
|
||||||
SMTP_HOST = 'smtp.host'
|
SMTP_HOST = "smtp.host"
|
||||||
SMTP_PORT = 'smtp.port'
|
SMTP_PORT = "smtp.port"
|
||||||
SMTP_LOGIN = 'smtp.login'
|
SMTP_LOGIN = "smtp.login"
|
||||||
SMTP_PASSWORD = 'smtp.password'
|
SMTP_PASSWORD = "smtp.password"
|
||||||
|
|
||||||
SITE_NAME = 'site.name'
|
SITE_NAME = "site.name"
|
||||||
SITE_URL = 'site.url'
|
SITE_URL = "site.url"
|
||||||
SITE_TOKEN = 'site.token'
|
SITE_TOKEN = "site.token"
|
||||||
SITE_ADMIN_EMAIL = 'site.admin_email'
|
SITE_ADMIN_EMAIL = "site.admin_email"
|
||||||
|
|
||||||
# variable
|
|
||||||
params = dict()
|
|
||||||
|
|
||||||
|
|
||||||
def initialize(config_pathname, flask_app):
|
class Config:
|
||||||
cfg = profig.Config(config_pathname)
|
def __init__(self):
|
||||||
cfg.sync()
|
self._params = dict()
|
||||||
params.update(cfg)
|
|
||||||
params.update({FLASK_APP: flask_app})
|
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def load(cls, config_pathname):
|
||||||
|
cfg = profig.Config(config_pathname)
|
||||||
|
cfg.sync()
|
||||||
|
config = cls()
|
||||||
|
config._params.update(cfg)
|
||||||
|
return config
|
||||||
|
|
||||||
def get(key):
|
def exists(self, key):
|
||||||
return params[key]
|
return key in self._params
|
||||||
|
|
||||||
|
def get(self, key):
|
||||||
|
return self._params[key] if key in self._params else None
|
||||||
|
|
||||||
def get_int(key):
|
def put(self, key, value):
|
||||||
return int(params[key])
|
self._params[key] = value
|
||||||
|
|
||||||
|
def get_int(self, key):
|
||||||
|
return int(self._params[key])
|
||||||
|
|
||||||
def get_bool(key):
|
def get_bool(self, key):
|
||||||
return params[key].lower() in ('yes', 'true', '1')
|
return self._params[key].lower() in ("yes", "true")
|
||||||
|
|
||||||
|
|
||||||
def flaskapp():
|
|
||||||
return params[FLASK_APP]
|
|
||||||
|
|
57
tests/test_config.py
Normal file
57
tests/test_config.py
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: UTF-8 -*-
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import stacosys.conf.config as config
|
||||||
|
|
||||||
|
EXPECTED_DB_URL = "sqlite:///db.sqlite"
|
||||||
|
EXPECTED_HTTP_PORT = 8080
|
||||||
|
EXPECTED_IMAP_PORT = "5000"
|
||||||
|
EXPECTED_IMAP_LOGIN = "user"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def conf():
|
||||||
|
conf = config.Config()
|
||||||
|
conf.put(config.DB_URL, EXPECTED_DB_URL)
|
||||||
|
conf.put(config.HTTP_PORT, EXPECTED_HTTP_PORT)
|
||||||
|
conf.put(config.IMAP_PORT, EXPECTED_IMAP_PORT)
|
||||||
|
conf.put(config.SMTP_STARTTLS, "yes")
|
||||||
|
conf.put(config.IMAP_SSL, "false")
|
||||||
|
return conf
|
||||||
|
|
||||||
|
|
||||||
|
def test_exists(conf):
|
||||||
|
assert conf is not None
|
||||||
|
assert conf.exists(config.DB_URL)
|
||||||
|
assert not conf.exists(config.IMAP_HOST)
|
||||||
|
|
||||||
|
|
||||||
|
def test_get(conf):
|
||||||
|
assert conf is not None
|
||||||
|
assert conf.get(config.DB_URL) == EXPECTED_DB_URL
|
||||||
|
assert conf.get(config.HTTP_PORT) == EXPECTED_HTTP_PORT
|
||||||
|
assert conf.get(config.HTTP_HOST) is None
|
||||||
|
assert conf.get(config.HTTP_PORT) == EXPECTED_HTTP_PORT
|
||||||
|
assert conf.get(config.IMAP_PORT) == EXPECTED_IMAP_PORT
|
||||||
|
assert conf.get_int(config.IMAP_PORT) == int(EXPECTED_IMAP_PORT)
|
||||||
|
try:
|
||||||
|
conf.get_int(config.HTTP_PORT)
|
||||||
|
assert False
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
assert conf.get_bool(config.SMTP_STARTTLS)
|
||||||
|
assert not conf.get_bool(config.IMAP_SSL)
|
||||||
|
try:
|
||||||
|
conf.get_bool(config.DB_URL)
|
||||||
|
assert False
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def test_put(conf):
|
||||||
|
assert conf is not None
|
||||||
|
assert not conf.exists(config.IMAP_LOGIN)
|
||||||
|
conf.put(config.IMAP_LOGIN, EXPECTED_IMAP_LOGIN)
|
||||||
|
assert conf.exists(config.IMAP_LOGIN)
|
||||||
|
assert conf.get(config.IMAP_LOGIN) == EXPECTED_IMAP_LOGIN
|
Loading…
Add table
Reference in a new issue