Commit cd0ab27b authored by M. Huang's avatar M. Huang
Browse files

getConfig dir from envir CONF_DIR; refactor fixtures to pass test5,1,2

parent f6ed019e
......@@ -141,7 +141,7 @@ versiontag:
PYTEST = python3 -m pytest
TESTLOG = /tmp/fdi-tests.log
OPT = -r P -v -l --pdb #--log-file=$(TESTLOG)
OPT = -r P -v -l --pdb -s --show-capture=all #--log-file=$(TESTLOG)
T =
test: test1 test2
......
# -*- coding: utf-8 -*-
from os.path import join, expanduser, expandvars, is_dir
from os.path import join, expanduser, expandvars, isdir
import functools
import sys
import importlib
import logging
# create logger
......@@ -41,26 +42,42 @@ def getConfig(name=None, conf='pns'):
config = {}
epath = expandvars('$CONF_DIR')
if is_dir(epath):
if isdir(epath):
confp = epath
else:
env = expanduser(epath)
# environment variable CONFIG_DIR is not set
env = expanduser(expandvars('$HOME'))
# apache wsgi will return '$HOME' with no expansion
if env == '$HOME':
env = '/root'
confp = join(env, '.config')
sys.path.insert(0, confp)
# this is the var_name part of filename and the name of the returned dict
var_name = conf+'config'
module_name = conf+'local'
file_name = module_name + '.py'
logger.info('Reading from configuration file %s/%s.py' %
(confp, file_name))
filep = join(confp, file_name)
absolute_name = importlib.util.resolve_name(module_name, None)
logger.debug('Reading from configuration file %s/%s. absolute mod name %s' %
(confp, file_name, absolute_name))
# if sys.path[0] != confp:
# sys.path.insert(0, confp)
# print(sys.path)
# for finder in sys.meta_path:
# spec = finder.find_spec(absolute_name, filep)
# print(spec) # if spec is not None:
try:
c = __import__(module_name, globals(), locals(), [stem], 0)
logger.debug('Reading %s/%s.py done.' % (confp, file_name))
config.update(c.__dict__[var_name])
spec = importlib.util.spec_from_file_location(absolute_name, filep)
#print('zz', spec)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
sys.modules[module_name] = module
# the following suffers from non-updating loader
# importlib.invalidate_caches()
# module = importlib.import_module(module_name)
# modul = __import__(module_name, globals(), locals(), [var_name], 0)
config.update(getattr(module, var_name))
logger.debug('Reading %s/%s done.' % (confp, file_name))
except ModuleNotFoundError as e:
logger.warning(str(
e) + '. Use default config in the package, such as fdi/pns/config.py. Copy it to ~/.config/[package]local.py and make persistent customization there.')
......
......@@ -232,6 +232,10 @@ type ArrayDataset string None Arra
Name of class.
unit lyr string None None B Unit of every element
.
shape () tuple None () Number of elements in
each dimension. Quic
k changers to the rig
ht.
typecode UNKNOWN string None UNKNOWN B Python internal stora
ge code.
version 0.1 string None 0.1 B Version of dataset
......@@ -297,15 +301,15 @@ ArrayDataset-dataset =
level 1, repr
=== ArrayDataset () ===
meta= {
-------------------------- ------------------ ----------------
-------------------------- ------------------ -----------------
description= toString test type= ArrayDataset unit= lyr
er AD
typecode= UNKNOWN version= 0.1 FORMATV= 1.6.0.1
a= 3.4 b= xy (2019-02-19 c= Invalid (IJK)
01:02:03.456789
1929229323456789)
d= off (0b00)
-------------------------- ------------------ ----------------
shape= () typecode= UNKNOWN version= 0.1
FORMATV= 1.6.0.1 a= 3.4 b= xy (2019-02-19
01:02:03.456789
1929229323456789)
c= Invalid (IJK) d= off (0b00)
-------------------------- ------------------ -----------------
MetaData-listeners = ListnerSet{}
}
ArrayDataset-dataset =
......@@ -353,7 +357,7 @@ ArrayDataset-dataset =
level 2,
ArrayDataset([[[[0, 0, 0, ...0]]], [[[0, 0, 0, ...0]]]] description: toString tester AD, type: ArrayDataset, unit: lyr, typecode: UNKNOWN, version: 0.1, FORMATV: 1.6.0.1, a: 3.4, b: FineTime{2019-02-19T01:02:03.456789 TAI(1929229323456789) fmt=%Y-%m-%dT%H:%M:%S.%f UTC}, c: IJK, d: 1)"""
ArrayDataset([[[[0, 0, 0, ...0]]], [[[0, 0, 0, ...0]]]] description: toString tester AD, type: ArrayDataset, unit: lyr, shape: (), typecode: UNKNOWN, version: 0.1, FORMATV: 1.6.0.1, a: 3.4, b: FineTime{2019-02-19T01:02:03.456789 TAI(1929229323456789) fmt=%Y-%m-%dT%H:%M:%S.%f UTC}, c: IJK, d: 1)"""
out_TableDataset = """
level 0
......@@ -455,6 +459,10 @@ type ArrayDataset string None ArrayDataset B Type i
Name of class.
unit ev string None None B Unit of every element
.
shape () tuple None () Number of elements in
each dimension. Quic
k changers to the rig
ht.
typecode UNKNOWN string None UNKNOWN B Python internal stora
ge code.
version 0.1 string None 0.1 B Version of dataset
......@@ -493,10 +501,11 @@ MetaData-listeners = ListnerSet{}
CompositeDataset-datasets =
<ODict === ArrayDataset () ===
meta= {
------------------------ ------------------ ----------------
------------------------ ------------------ ------------
description= arraydset 1 type= ArrayDataset unit= ev
typecode= UNKNOWN version= 0.1 FORMATV= 1.6.0.1
------------------------ ------------------ ----------------
shape= () typecode= UNKNOWN version= 0.1
FORMATV= 1.6.0.1
------------------------ ------------------ ------------
MetaData-listeners = ListnerSet{}
}
ArrayDataset-dataset =
......@@ -520,7 +529,7 @@ TableDataset-dataset =
meta
CompositeDataset-datasets =
<ODict ArrayDataset([768, 4.4, 5400.0] description: arraydset 1, type: ArrayDataset, unit: ev, typecode: UNKNOWN, version: 0.1, FORMATV: 1.6.0.1) === TableDataset () ===
<ODict ArrayDataset([768, 4.4, 5400.0] description: arraydset 1, type: ArrayDataset, unit: ev, shape: (), typecode: UNKNOWN, version: 0.1, FORMATV: 1.6.0.1) === TableDataset () ===
meta
TableDataset-dataset =
Time Energy
......
......@@ -14,15 +14,6 @@ from urllib.error import HTTPError
logger = logging.getLogger(__name__)
@pytest.fixture(scope="package")
def pc():
""" get configuration.
"""
pc = getconfig.getConfig()
return pc
def checkserver(aburl):
""" make sure the server is running when tests start
"""
......@@ -70,23 +61,6 @@ def setup(pc):
del aburl, headers
@pytest.fixture
def local_pools_dir(pc):
""" this is a path in the local OS, where the server runs.
the path is used to directly access pool server's internals.
return: has no trailing '/'
"""
# http server pool
schm = 'server'
#basepath = pc['server_local_pools_dir']
basepath = PoolManager.PlacePaths[schm]
local_pools_dir = os.path.join(basepath, pc['api_version'])
return local_pools_dir
@pytest.fixture(scope="package")
def userpass(pc):
auth_user = pc['auth_user']
......
......@@ -59,7 +59,7 @@ else:
Classes.updateMapping()
# make format output in /tmp/output.py
mko = 1
mko = 0
if __name__ == '__main__' and __package__ is None:
# run by python3 tests/test_dataset.py
......
# -*- coding: utf-8 -*-
import datetime
import traceback
import copy
import sys
import os
from fdi.dataset.annotatable import Annotatable
from fdi.dataset.copyable import Copyable
from fdi.dataset.odict import ODict
from fdi.dataset.eq import deepcmp
from fdi.dataset.classes import Classes
from fdi.dataset.deserialize import deserialize
from fdi.dataset.quantifiable import Quantifiable
from fdi.dataset.listener import EventSender, DatasetBaseListener, EventTypes, EventType, EventTypeOf
from fdi.dataset.composite import Composite
from fdi.dataset.metadata import Parameter, MetaData, make_jsonable
from fdi.dataset.numericparameter import NumericParameter
from fdi.dataset.stringparameter import StringParameter
from fdi.dataset.dateparameter import DateParameter
from fdi.dataset.datatypes import DataTypes, DataTypeNames
from fdi.dataset.attributable import Attributable
from fdi.dataset.abstractcomposite import AbstractComposite
from fdi.dataset.datawrapper import DataWrapper, DataWrapperMapper
from fdi.dataset.dataset import TableDataset, CompositeDataset
from fdi.dataset.arraydataset import Column, ArrayDataset
from fdi.dataset.ndprint import ndprint
from fdi.dataset.datatypes import Vector, Quaternion
from fdi.dataset.finetime import FineTime, FineTime1, utcobj
from fdi.dataset.history import History
from fdi.dataset.baseproduct import BaseProduct
from fdi.dataset.deserialize import Class_Look_Up
from fdi.dataset.product import Product
from fdi.pal.urn import Urn
from fdi.utils.checkjson import checkjson
......@@ -39,28 +17,30 @@ from fdi.utils.common import fullname, l2t
from fdi.utils.options import opt
from fdi.utils.fetch import fetch
# import __builtins__
import datetime
import importlib
import traceback
import copy
import sys
import os
import os.path
import pytest
if sys.version_info[0] >= 3: # + 0.1 * sys.version_info[1] >= 3.3:
PY3 = True
else:
PY3 = False
Classes.updateMapping()
if __name__ == '__main__' and __package__ == 'tests':
# run by python -m tests.test_dataset
from outputs import nds2, nds3, out_TableDataset, out_CompositeDataset
pass
else:
# run by pytest
# This is to be able to test w/ or w/o installing the package
# https://docs.python-guide.org/writing/structure/
from .pycontext import fdi
from .outputs import nds2, nds3, out_TableDataset, out_CompositeDataset
from .pycontext import fdi
from .logdict import logdict
import logging
......@@ -360,4 +340,55 @@ def test_opt():
assert 0, 'failed to exit.'
def test_getConfig():
def check_conf(cfp, typ, getConfig):
cfn = typ + 'local.py'
cfp = os.path.expanduser(cfp)
filec = os.path.join(cfp, cfn)
os.system('rm -f ' + filec)
conf = 'import os; %sconfig={"jk":98, "m":os.path.abspath(__file__)}' % typ
with open(filec, 'w') as f:
f.write(conf)
# check conf file directory
w = getConfig(conf=typ)
assert w['jk'] == 98
pfile = w['m']
assert pfile.startswith(cfp)
os.system('rm -f ' + filec)
def test_getConfig_init(getConfig):
# no arg
v = getConfig()
from fdi.pns.config import pnsconfig
# v is a superset
assert all(n in v for n in pnsconfig)
def test_getConfig_noENV(getConfig):
# non-default conf type
typ = 'abc'
# environment variable not set
try:
del os.environ['CONF_DIR']
except:
pass
# default dir, there is nothing
# put mock in the default directory
check_conf('~/.config', typ, getConfig)
def test_getConfig_conf(getConfig):
# non-default conf type
typ = 'abc'
# specify directory
cp = '/tmp'
# environment variable
os.environ['CONF_DIR'] = cp
check_conf(cp, typ, getConfig)
# non-existing. the file has been deleted by the check_conf in the last line
with pytest.raises(FileNotFoundError):
w = getConfig(conf=typ)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment