diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..c59dc9f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,11 @@ +language: python +python: + - "3" + +install: + - pip install tox-travis coveralls +script: + - tox + +after_success: + - coveralls diff --git a/Readme.md b/README.md similarity index 100% rename from Readme.md rename to README.md diff --git a/SecondaryValue/test_secondaryvalue.py b/SecondaryValue/test_secondaryvalue.py new file mode 100644 index 0000000..70c2a47 --- /dev/null +++ b/SecondaryValue/test_secondaryvalue.py @@ -0,0 +1,81 @@ +from SecondaryValue import SecondaryValue +import random +from pytest import approx, raises +import numpy + +class TestBasicUsage(): + def setup(self): + self.x = SecondaryValue('a*b+c') + + def test_no_uncertainties(self): + # no uncertainties + assert 1.2 * 7.9 + 10 == self.x(a=1.2, b=7.9, c=10) + + def test_get_symbols(self): + # the right symbols + assert self.x.get_symbols() == {'a': {}, 'b': {}, 'c': {}} + + def ret_deps_without_deps(self): + # retdeps returns a dict + assert self.x(a=1, b=1, c=1, retdeps=True)[1] == {} + + def test_uncertainties(self): + # dirty + for i in range(100): + a, b, c = [(random.random(), random.random()) for _ in range(3)] + assert (a[0]*b[0] + c[0], numpy.sqrt((a[0]*b[1])**2 + (a[1]*b[0])**2 + c[1]**2)) \ + == approx(self.x(a=a, b=b, c=c)) + + def test_error_missing(self): + with raises(RuntimeError): + self.x(u=1) + +class TestDependecies(): + def setup(self): + self.x = SecondaryValue('b') + self.y = SecondaryValue('a + x', dependencies=dict(x=self.x)) + + def test_overwrite(self): + result, deps = self.y(a=1, x=1, retdeps=True) + assert deps == {} + assert result == 2 + + def test_dep_calc(self): + result, deps = self.y(a=1, b=2, retdeps=True) + + assert result == 3 + assert deps == {'x': (2, {})} + + def test_dep_calc_err(self): + # no retdep + for i in range(100): + a, b = [(random.random(), random.random()) for _ in range(2)] + result, deps = self.y(a=a, b=b, retdeps=True) + + assert (a[0]+b[0], numpy.sqrt((b[1])**2 + (a[1])**2)) \ + == approx(result) + + assert (b[0], numpy.abs(b[1])) == approx(deps['x'][0]) + + +class TestDefaults(): + def setup(self): + self.y = SecondaryValue('a + b', defaults=dict(a=(1, 2))) + + def test_basics(self): + assert (2, 2) == self.y(b=1) + +class TestShapes(): + def setup(self): + self.x = SecondaryValue('b') + self.y = SecondaryValue('a + x', dependencies=dict(x=self.x)) + + def test_array_central_value(self): + # mixed + assert numpy.any(numpy.array([1,2,3]) == self.y(a=([1,2,3],), b=0)) + + # both + assert numpy.any(2*numpy.array([1,2,3]) \ + == self.y(a=([1,2,3],), b=([1,2,3],))) + +# def test_array_uncert(self): diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..05c2ac8 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[tool:pytest] +addopts = -v -x --ignore=setup.py --ignore=./SecondaryValue/__init__.py --cov=SecondaryValue diff --git a/setup.py b/setup.py index fd8687c..af83d18 100644 --- a/setup.py +++ b/setup.py @@ -1,8 +1,10 @@ from setuptools import setup - +# read the contents of your README file +from os import path +this_directory = path.abspath(path.dirname(__file__)) def readme(): - with open('Readme.md') as f: + with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f: return f.read() setup(name='SecondaryValue', diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..d4efa5f --- /dev/null +++ b/tox.ini @@ -0,0 +1,12 @@ +[tox] +envlist = py3 + +[testenv] +commands = pytest +deps = + pytest-pep8 + pytest-cov + pytest + coverage + numpy + sympy