Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bjodah committed Jan 16, 2025
1 parent ac4cabf commit 54e7789
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 18 deletions.
5 changes: 4 additions & 1 deletion .ci/ci-01-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ done

$PYTHON -m pip install $INSTALL_PIP_FLAGS -e .[all]
$PYTHON -c "import pycvodes; import pyodesys; import pygslodeiv2"
git fetch -tq
#git fetch -tq
$PYTHON setup.py sdist # test pip installable sdist (checks MANIFEST.in)
git archive -o dist/chempy-head.zip HEAD # test pip installable zip (symlinks break)
mkdir -p deploy/public_html/branches/${CI_COMMIT_BRANCH}
cp dist/chempy-* deploy/public_html/branches/${CI_COMMIT_BRANCH}/

set +e
[[ $($PYTHON setup.py --version) =~ ^[0-9]+.* ]]
./scripts/run_tests.sh --cov chempy --cov-report html
bash
./scripts/coverage_badge.py htmlcov/ htmlcov/coverage.svg

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ sundials-*.tar.gz
!.jupyter/jupyter_notebook_config.py
tmp/
.env
.coverage
14 changes: 7 additions & 7 deletions .woodpecker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ when:
steps:

- name: restore-cache
image: cont-reg.bjodah.se:443/bjodah/triceratops-3:24
image: cont-reg.bjodah.se:443/bjodah/triceratops-4:16
commands:
- curl ftp://chempy:$${ARTIFACTS_PASS}@$${FTP_SERVER}/cache/cache-ci.tar | tar x
secrets: [ ARTIFACTS_PASS, FTP_SERVER ]
Expand All @@ -13,7 +13,7 @@ steps:
repo: bjodah/chempy

- name: install
image: cont-reg.bjodah.se:443/bjodah/triceratops-3:24
image: cont-reg.bjodah.se:443/bjodah/triceratops-4:16
environment:
- CPLUS_INCLUDE_PATH=/opt-3/boost-1.87.0/include
- SUNDBASE=/opt-3/sundials-6.7.0-release
Expand All @@ -25,7 +25,7 @@ steps:
- .ci/ci-01-install.sh

- name: test-suite
image: cont-reg.bjodah.se:443/bjodah/triceratops-3:24
image: cont-reg.bjodah.se:443/bjodah/triceratops-4:16
environment:
- CPLUS_INCLUDE_PATH=/opt-3/boost-1.87.0/include
- SUNDBASE=/opt-3/sundials-6.7.0-release
Expand All @@ -51,7 +51,7 @@ steps:
- install

- name: render-notebooks
image: cont-reg.bjodah.se:443/bjodah/triceratops-3:24
image: cont-reg.bjodah.se:443/bjodah/triceratops-4:16
environment:
- CHEMPY_DEPRECATION_FILTER=ignore
- SUNDBASE=/opt-3/sundials-6.7.0-release
Expand All @@ -69,7 +69,7 @@ steps:
- install

- name: compile-documentation
image: cont-reg.bjodah.se:443/bjodah/triceratops-3:24
image: cont-reg.bjodah.se:443/bjodah/triceratops-4:16
environment:
- CHEMPY_DEPRECATION_FILTER=ignore
- SUNDBASE=/opt-3/sundials-6.7.0-release
Expand All @@ -86,7 +86,7 @@ steps:
- render-notebooks

- name: rebuild-cache
image: cont-reg.bjodah.se:443/bjodah/triceratops-3:24
image: cont-reg.bjodah.se:443/bjodah/triceratops-4:16
commands:
- find ./cache-ci/ -type f -mtime +90 -exec rm {} \;
- tar cf cache-ci.tar ./cache-ci/
Expand All @@ -99,7 +99,7 @@ steps:
- compile-documentation

- name: deploy-public-html
image: cont-reg.bjodah.se:443/bjodah/triceratops-3:24
image: cont-reg.bjodah.se:443/bjodah/triceratops-4:16
commands:
- tar czf chempy-${CI_COMMIT_BRANCH}.tar.gz ./deploy/public_html
- curl -T chempy-${CI_COMMIT_BRANCH}.tar.gz ftp://chempy:$${ARTIFACTS_PASS}@$${FTP_SERVER}/public_html/
Expand Down
9 changes: 6 additions & 3 deletions chempy/_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,24 @@ def __setitem__(self, key, value):
raise ValueError("entry for %s (%s) is not compatible with %s" % (key, value, self.units))
super(QuantityDict, self).__setitem__(key, value)

def _copy_of_items(self):
return [(k, v.copy()) for k, v in self.items()]

def copy(self):
return self.__class__(self.units, copy.deepcopy(list(self.items())))
return self.__class__(self.units, self._copy_of_items())

def __repr__(self):
return "{}({}, {})".format(self.__class__.__name__,
repr(self.units),
dict(self))

def __mul__(self, other):
d = dict(copy.deepcopy(list(self.items())))
d = dict(self._copy_of_items())
_imul(d, other)
return self.__class__(self.units * getattr(other, 'units', 1), d)

def __truediv__(self, other):
d = dict(copy.deepcopy(list(self.items())))
d = dict(self._copy_of_items())
_itruediv(d, other)
return self.__class__(self.units / getattr(other, 'units', 1), d)

Expand Down
5 changes: 4 additions & 1 deletion chempy/tests/test_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ def test_QuantityDict():
# QuantityDict * scalar_quantity
c = QuantityDict(u.molar, {})
c['H2O'] = 55.4 * u.molar
assert to_unitless(c['H2O'], u.mol/u.m3) == 55400.0
with pytest.raises(ValueError):
c['HCl'] = 3 * u.kg

with pytest.raises(ValueError):
QuantityDict(u.molar, {'a': u.mole})

V = .4*u.dm3
assert to_unitless(c['H2O'], u.mol/u.m3) == 55400.0
n = c*V
assert to_unitless(c['H2O'], u.mol/u.m3) == 55400.0
assert isinstance(n, QuantityDict)

# For the following to work major changes to quantities needs to be made:
Expand Down Expand Up @@ -70,4 +73,4 @@ def test_Solution__withdraw():
s4 = s3.withdraw(.2 * u.dm3)
assert s4 == s3
assert s4 == (s1 + s2).withdraw(.2 * u.dm3)
assert s4 != s1 + s2
assert s4 != (s1 + s2)
10 changes: 6 additions & 4 deletions chempy/tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@
)



def test_dimensionality():
assert mass + 2*length - 2*time == energy
assert amount - 3*length == concentration
assert 3*length == volume



@requires(units_library)
def test_default_units():
u.metre
Expand Down Expand Up @@ -63,11 +61,15 @@ def test_default_units():
def test_rescale():
fourtytwo_km = 42*u.km
assert rescale(fourtytwo_km, u.m) == 42e3

@pytest.mark.xfail(reason="regression since numpy 2?")
def test_rescale__symbolic():
fourtytwo_km = 42*u.km
sy = SymPyDeDim()
l_m = np.array(sy.Symbol('l_m', real=True, nonnegative=True),
dtype=object)
lngth = l_m * u.m
assert rescale(fourtytwo_km/lngth, 1)*l_m - 42e3 == 0.0
assert rescale(fourtytwo_km/lngth, 1, dtype=object)*l_m - 42e3 == 0.0


@requires(units_library)
Expand Down Expand Up @@ -207,7 +209,7 @@ def test_to_unitless():
sy.exp(Ea_over_RT_uncert)

T_K_sym = np.array(sy.Symbol('T_K', real=True), dtype=object) * u.K
assert to_unitless(T_K_sym/(298*u.K)).tolist() == [sy.Symbol('T_K', real=True)]
assert to_unitless(T_K_sym/(298*u.K)) == sy.Symbol('T_K', real=True)/298.0
rescale(simplified(Ea/dc.molar_gas_constant)/T_K_sym, 1)
Ea_over_R_uncert = UncertainQuantity(Ea, unit_of(Ea), 0.1*Ea)/dc.molar_gas_constant
to_unitless(Ea_over_R_uncert/(298*u.K))
Expand Down
4 changes: 2 additions & 2 deletions chempy/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,12 @@ def unit_of(expr, simplify=False):
return 1


def rescale(value, unit):
def rescale(value, unit, dtype=None):
literal_integer_one = 1
if unit is literal_integer_one:
unit = pq.dimensionless
try:
return value.rescale(unit)
return value.rescale(unit, dtype=dtype)
except AttributeError:
if unit == 1:
return value
Expand Down

0 comments on commit 54e7789

Please sign in to comment.