Merge branch 'feature/testing' into develop
Ab sofort können sinnvoll Tests geschrieben werden. Da das Projekt weiter wächst, ist das auch notwendig. Hiermit wird die notwendige Basiskonfiguration bereitgestellt, sowie ein erster Satz an Tests.
This commit is contained in:
commit
5c6f7783d2
|
@ -166,3 +166,5 @@ fabric.properties
|
|||
[Ss]cripts
|
||||
pyvenv.cfg
|
||||
pip-selfcheck.json
|
||||
|
||||
.pytest_cache/
|
|
@ -0,0 +1,15 @@
|
|||
language: python
|
||||
|
||||
python:
|
||||
- 3.5
|
||||
- 3.6
|
||||
- 3.7-dev
|
||||
|
||||
cache: pip
|
||||
|
||||
install:
|
||||
pip install -r src/requirements-test.txt
|
||||
|
||||
script:
|
||||
- cd src
|
||||
- py.test --cov --cov-report term-missing --cov-config .coveragerc test/
|
|
@ -1,3 +1,8 @@
|
|||
| Branch | Build Status |
|
||||
|---------|----------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| master | [![Build Status master](https://travis-ci.org/juergen-rocks/raumklima.svg?branch=master)](https://travis-ci.org/juergen-rocks/raumklima) |
|
||||
| develop | [![Build Status develop](https://travis-ci.org/juergen-rocks/raumklima.svg?branch=develop)](https://travis-ci.org/juergen-rocks/raumklima) |
|
||||
|
||||
# ELV Raumklimastation RS 500 unter Linux auslesen
|
||||
|
||||
Der Elektronikversender [ELV](https://www.elv.de/) bietet unter der Artikelnummer [68-12 87 19](https://www.elv.de/elv-raumklimastation-rs500-inkl-5-sensoren-messwertespeicher-und-pc-auswertesoftware.html) eine Raumklimastation mit 5 Sensoren an, die sich über einen USB-Anschluss am PC auswerten lassen soll. Dazu liegt die proprietäre Windows-Software "EasyTemp" bei. Eine Unterstützung für Linux wird nicht angeboten.
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
[run]
|
||||
omit =
|
||||
test/*
|
||||
*/venv/*
|
||||
*/pyvenv/*
|
||||
*/JetBrains/*
|
||||
*/virtualenv/*
|
|
@ -0,0 +1,2 @@
|
|||
pytest>=3.7.2,<3.8
|
||||
pytest-cov>=2.5.1,<2.6
|
|
@ -1,7 +1,7 @@
|
|||
import configparser
|
||||
from os import getenv
|
||||
from os.path import join, exists, isfile
|
||||
from pathlib import Path
|
||||
import os.path
|
||||
import pathlib
|
||||
|
||||
|
||||
class ConfigProvider(object):
|
||||
|
@ -9,7 +9,8 @@ class ConfigProvider(object):
|
|||
def __init__(self, file: str):
|
||||
self.__config = configparser.ConfigParser()
|
||||
self.__config.optionxform = str
|
||||
self.__config.read(file)
|
||||
with open(file, 'r') as fp:
|
||||
self.__config.read_file(fp)
|
||||
|
||||
def get_config(self) -> configparser.ConfigParser:
|
||||
return self.__config
|
||||
|
@ -17,19 +18,19 @@ class ConfigProvider(object):
|
|||
|
||||
def discover_config_file_by_name(filename: str, script_dir: str=None, env_var: str='RS500_CONFIG_PATH') -> str:
|
||||
if script_dir is not None:
|
||||
candidate = join(script_dir, filename)
|
||||
if exists(candidate) and isfile(candidate):
|
||||
candidate = os.path.join(script_dir, filename)
|
||||
if os.path.exists(candidate) and os.path.isfile(candidate):
|
||||
return candidate
|
||||
if env_var is not None:
|
||||
env_var_value = getenv(env_var, None)
|
||||
if env_var_value is not None:
|
||||
candidate = join(env_var_value, filename)
|
||||
if exists(candidate) and isfile(candidate):
|
||||
candidate = os.path.join(env_var_value, filename)
|
||||
if os.path.exists(candidate) and os.path.isfile(candidate):
|
||||
return candidate
|
||||
candidate = join(str(Path.home().absolute()), '.rs500', filename)
|
||||
if exists(candidate) and isfile(candidate):
|
||||
candidate = os.path.join(str(pathlib.Path.home().absolute()), '.rs500', filename)
|
||||
if os.path.exists(candidate) and os.path.isfile(candidate):
|
||||
return candidate
|
||||
candidate = join('/etc', filename)
|
||||
if exists(candidate) and isfile(candidate):
|
||||
candidate = os.path.join('/etc', filename)
|
||||
if os.path.exists(candidate) and os.path.isfile(candidate):
|
||||
return candidate
|
||||
raise FileNotFoundError('Unable to find configuration file "{}"'.format(filename))
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
from os.path import join, dirname, exists
|
||||
|
||||
import pytest
|
||||
|
||||
from rs500common.configuration import ConfigProvider
|
||||
|
||||
|
||||
def test_config_provider_smoke_error():
|
||||
with pytest.raises(FileNotFoundError):
|
||||
ConfigProvider('/does/not/exist.ini')
|
||||
|
||||
|
||||
def test_config_provider_on_existing_file():
|
||||
file = join(dirname(__file__), '..', 'check_rs500.ini')
|
||||
assert exists(file)
|
||||
cf = ConfigProvider(file)
|
||||
keys = cf.get_config().keys()
|
||||
assert len(keys) > 0
|
|
@ -0,0 +1,46 @@
|
|||
import os.path
|
||||
import pathlib
|
||||
|
||||
import pytest
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
|
||||
from rs500common.configuration import discover_config_file_by_name
|
||||
|
||||
|
||||
def test_discovery_file_in_folder(monkeypatch: MonkeyPatch):
|
||||
with monkeypatch.context() as m:
|
||||
m.setattr('os.path.isfile', lambda path: True)
|
||||
m.setattr('os.path.exists', lambda path: True)
|
||||
result = discover_config_file_by_name('test.ini', '/foo/bar')
|
||||
assert result == os.path.join('/foo/bar', 'test.ini')
|
||||
|
||||
|
||||
def test_discovery_via_env_var(monkeypatch: MonkeyPatch):
|
||||
with monkeypatch.context() as m:
|
||||
m.setenv('RS500_CONFIG_PATH', '/rs500_config_path/here/we/are')
|
||||
m.setattr('os.path.isfile', lambda path: True)
|
||||
m.setattr('os.path.exists', lambda path: True)
|
||||
result = discover_config_file_by_name('test.ini', script_dir=None)
|
||||
assert result == os.path.join('/rs500_config_path/here/we/are', 'test.ini')
|
||||
|
||||
|
||||
def test_discovery_via_user_home(monkeypatch: MonkeyPatch):
|
||||
with monkeypatch.context() as m:
|
||||
m.setattr('os.path.isfile', lambda path: True)
|
||||
m.setattr('os.path.exists', lambda path: True)
|
||||
monkeypatch.setattr(pathlib.Path, 'absolute', lambda x: '/user/home/path/test')
|
||||
result = discover_config_file_by_name('test.ini', script_dir=None)
|
||||
assert result == os.path.join('/user/home/path/test', '.rs500', 'test.ini')
|
||||
|
||||
|
||||
def test_discovery_in_etc(monkeypatch: MonkeyPatch):
|
||||
with monkeypatch.context() as m:
|
||||
m.setattr('os.path.isfile', lambda path: path.startswith('/etc'))
|
||||
m.setattr('os.path.exists', lambda path: path.startswith('/etc'))
|
||||
result = discover_config_file_by_name('test.ini')
|
||||
assert result == os.path.join('/etc', 'test.ini')
|
||||
|
||||
|
||||
def test_no_hit():
|
||||
with pytest.raises(FileNotFoundError):
|
||||
discover_config_file_by_name('does-not-exist.nothing', 'bla-blubb')
|
Loading…
Reference in New Issue