Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bccheung committed Dec 20, 2024
1 parent 9b666f4 commit fa81183
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
51 changes: 49 additions & 2 deletions pixi.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ dev = [
"twine>=6.0.1,<7",
"sphinx>=8.1.3,<9",
"sphinx-favicon>=1.0.1,<2",
"pydata-sphinx-theme>=0.16.1,<0.17"
"pydata-sphinx-theme>=0.16.1,<0.17",
"pytest>=8.3.4,<9"
]

[tool.setuptools.packages.find]
Expand All @@ -38,6 +39,12 @@ where = ["src"]
[tool.setuptools_scm]
version_file = "src/wsp_jacquard/_version.py"

[tool.pytest.ini_options]
addopts = [
"--import-mode=importlib",
]
pythonpath = "src"

[tool.pixi.project]
channels = ["conda-forge"]
platforms = ["linux-64", "osx-arm64", "osx-64", "win-64"]
Expand Down
82 changes: 82 additions & 0 deletions tests/test_jacquard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from __future__ import annotations

import json
from pathlib import Path
from tempfile import TemporaryDirectory

from wsp_jacquard import Jacquard

config_dict = {
"traffic_assignment": {
"iterations": 100,
"best_relative_gap": 0.001,
"demand_matrix": (Path("..").resolve() / 'demand.mdf').as_posix(),
"consider_background_traffic": True
},
"world": {
"scenario_name": "2016 Base",
"scenario_number": 501,
"transit_modes": [
"b",
"r",
"w"
],
"random_seed": None
},
"vdf": {
"10": "fd1",
"11": "fd2"
}
}


def test_loading():
from_dict = Jacquard.from_dict(config_dict).serialize()
from_str = Jacquard.from_string(json.dumps(config_dict)).serialize()
with TemporaryDirectory() as tempdir:
fp = Path(tempdir) / 'config.json'
with open(fp, mode='w') as f:
json.dump(config_dict, f)
from_file = Jacquard.from_file(fp).serialize()
assert json.dumps(from_dict) == json.dumps(from_str)
assert json.dumps(from_dict) == json.dumps(from_file)


def test_as_dict():
config = Jacquard.from_dict(config_dict)
assert json.dumps(config.vdf.as_dict(value_type=str)) == json.dumps(config_dict['vdf'])


def test_as_bool():
config = Jacquard.from_dict(config_dict)
assert config.traffic_assignment.consider_background_traffic.as_bool() == config_dict['traffic_assignment']['consider_background_traffic']


def test_as_int():
config = Jacquard.from_dict(config_dict)
assert config.traffic_assignment.iterations.as_int() == config_dict['traffic_assignment']['iterations']


def test_as_float():
config = Jacquard.from_dict(config_dict)
assert config.traffic_assignment.best_relative_gap.as_float() == config_dict['traffic_assignment']['best_relative_gap']


def test_as_str():
config = Jacquard.from_dict(config_dict)
assert config.world.scenario_name.as_str() == config_dict['world']['scenario_name']


def test_as_list():
config = Jacquard.from_dict(config_dict)
assert json.dumps(config.world.transit_modes.as_list(sub_type=str)) == json.dumps(config_dict['world']['transit_modes'])


def test_as_path():
config = Jacquard.from_dict(config_dict)
assert config.traffic_assignment.demand_matrix.as_path().as_posix() == config_dict['traffic_assignment']['demand_matrix']


def test_as_set():
config = Jacquard.from_dict(config_dict)
assert set(config.world.transit_modes.as_list(sub_type=str)) == set(config_dict['world']['transit_modes'])

0 comments on commit fa81183

Please sign in to comment.