-
Notifications
You must be signed in to change notification settings - Fork 2
Add tests for constants and sequence utilities #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5861312
Add tests for constants and sequence utilities
dprada 4b828df
Update tests/test_constants.py
dprada 107f749
Fix parse and quantity tests when optional libraries absent
dprada b05bd14
Update tests/helpers.py
dprada ed38c4e
Update tests/helpers.py
dprada f962afc
Merge pull request #48 from uibcdf/codex/fix-ci-issue
dprada File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Test suite package for pyunitwizard.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| from contextlib import contextmanager | ||
|
|
||
| import pyunitwizard as puw | ||
|
|
||
|
|
||
| @contextmanager | ||
| def loaded_libraries(libraries): | ||
| """ | ||
| Context manager to temporarily load the requested libraries for a test case. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| libraries : list of str | ||
| List of library names to load temporarily for the duration of the context. | ||
|
|
||
| Behavior | ||
| -------- | ||
| - Saves the current loaded libraries, default form, and default parser. | ||
| - Resets the configuration and loads the specified libraries. | ||
| - After the context, restores the previous configuration. | ||
| - If no previous libraries were loaded, attempts to load a default set | ||
| ('pint', 'openmm.unit', 'unyt'), ignoring failures. | ||
|
|
||
| Exceptions | ||
| ---------- | ||
| - Any exception raised by `puw.configure.load_library` when loading the requested libraries | ||
| will propagate unless caught internally. | ||
| - Exceptions when loading default libraries after the context are caught and ignored. | ||
| """ | ||
| previous_libraries = list(puw.configure.get_libraries_loaded()) | ||
| previous_default_form = puw.configure.get_default_form() | ||
| previous_default_parser = puw.configure.get_default_parser() | ||
|
|
||
| puw.configure.reset() | ||
| puw.configure.load_library(libraries) | ||
| try: | ||
| yield | ||
| finally: | ||
| puw.configure.reset() | ||
| if previous_libraries: | ||
| puw.configure.load_library(previous_libraries) | ||
| else: | ||
| for library in ['pint', 'openmm.unit', 'unyt']: | ||
| try: | ||
| puw.configure.load_library(library) | ||
| except (ImportError, ModuleNotFoundError): | ||
| continue | ||
| if previous_default_form is not None: | ||
| puw.configure.set_default_form(previous_default_form) | ||
| if previous_default_parser is not None: | ||
| puw.configure.set_default_parser(previous_default_parser) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import pyunitwizard as puw | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def configure_pint(): | ||
| loaded_libraries = list(puw.configure.get_libraries_loaded()) | ||
| default_form = puw.configure.get_default_form() | ||
| default_parser = puw.configure.get_default_parser() | ||
| standard_units = list(puw.configure.get_standard_units().keys()) | ||
|
|
||
| puw.configure.reset() | ||
| puw.configure.load_library('pint') | ||
| puw.configure.set_standard_units(['nm', 'ps', 'kcal', 'mole']) | ||
|
|
||
| yield | ||
|
|
||
| puw.configure.reset() | ||
|
|
||
| if loaded_libraries: | ||
| puw.configure.load_library(loaded_libraries) | ||
|
|
||
| if standard_units: | ||
| puw.configure.set_standard_units(standard_units) | ||
|
|
||
| if default_form is not None: | ||
| puw.configure.set_default_form(default_form) | ||
|
|
||
| if default_parser is not None: | ||
| puw.configure.set_default_parser(default_parser) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def constants_module(monkeypatch): | ||
| from pyunitwizard.constants import constants as const_mod | ||
|
|
||
| monkeypatch.setattr(const_mod, 'quantity', puw.quantity, raising=False) | ||
| monkeypatch.setattr(const_mod, 'convert', puw.convert, raising=False) | ||
|
|
||
| return const_mod | ||
|
|
||
|
|
||
| def test_get_constant_synonym_and_conversion(constants_module): | ||
| universal = constants_module.get_constant('R', to_unit='kJ/(mole*kelvin)') | ||
| value, unit = puw.get_value_and_unit(universal, to_form='string') | ||
|
|
||
| assert value == pytest.approx(0.00813446, rel=1e-6) | ||
| assert unit == 'kilojoule / kelvin / mole' | ||
|
|
||
|
|
||
| def test_get_constant_standardized_string_form(constants_module): | ||
| raw = constants_module.get_constant('Avogadro') | ||
| standardized = constants_module.get_constant('NA', standardized=True) | ||
|
|
||
| assert puw.are_equal(standardized, puw.standardize(raw)) | ||
|
|
||
| string_form = constants_module.get_constant('NA', to_form='string') | ||
| assert string_form == '6.02214076e+23 / mole' | ||
|
|
||
|
|
||
| def test_get_constant_unknown_raises(constants_module): | ||
| with pytest.raises(ValueError): | ||
| constants_module.get_constant('Unknown constant') | ||
|
|
||
|
|
||
| def test_show_constants_lists_synonyms(constants_module): | ||
| registry = constants_module.show_constants() | ||
|
|
||
| assert ('Avogadro', 'NA') in registry | ||
| assert registry[('Universal gas', 'R', 'Molar gas')] == '8.13446261815324 J/(kelvin*mole)' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.