Skip to content
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

Issue 303 gdal failed import #94

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/tm2py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import argparse

# this is mysterious but appears to resolve DLL load errors that otherwise occur
# why import gdal first: https://github.com/BayAreaMetro/tm2py/blob/7a563f0c5cea2125f28bfaedc50205e70c532094/README.md?plain=1#L57
import gdal

from tm2py import RunController
Expand Down
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
Personal travel demand generated by household members.

::: tm2py.components.demand.household
::: tm2py.config.HouseholdConfig
::: tm2py.HouseholdConfig

#### Air Passenger Demand

Expand Down
4 changes: 0 additions & 4 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,10 @@ extra:
extra_javascript:
- https://unpkg.com/[email protected]/dist/mermaid.min.js

extra_css:
- https://use.fontawesome.com/releases/v5.13.0/css/all.css

markdown_extensions:
- admonition
- codehilite:
linenums: true
- fontawesome_markdown
- meta
- pymdownx.inlinehilite
- pymdownx.tasklist:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mkdocs-awesome-pages-plugin
mkdocs-jupyter
mkdocs-macros-plugin
mkdocs-material
mkdocstrings
mkdocstrings >= 0.18
mkdocstrings-python
notebook
numpy
Expand Down
86 changes: 70 additions & 16 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import os
import sys
from pathlib import Path

import pytest
# why import gdal first: https://github.com/BayAreaMetro/tm2py/blob/7a563f0c5cea2125f28bfaedc50205e70c532094/README.md?plain=1#L57
import gdal

print("CONFTEST LOADED")

Expand Down Expand Up @@ -50,20 +49,21 @@ def temp_dir():
def pytest_addoption(parser):
"""Parse command line arguments."""
parser.addoption("--inro", action="store", default="notmock")
print('pytest_addoption')
print("pytest_addoption")


def mocked_inro_context():
import unittest.mock

"""Mocking of modules which need to be mocked for tests."""
sys.modules["inro.emme.database.emmebank"] = unittest.mock.MagicMock()
sys.modules["inro.emme.network"] = unittest.mock.MagicMock()
sys.modules["inro.emme.database.scenario"] = unittest.mock.MagicMock()
sys.modules["inro.emme.database.matrix"] = unittest.mock.MagicMock()
sys.modules["inro.emme.network.node"] = unittest.mock.MagicMock()
sys.modules["inro.emme.desktop.app"] = unittest.mock.MagicMock()
sys.modules["inro"] = unittest.mock.MagicMock()
sys.modules["inro.modeller"] = unittest.mock.MagicMock()
sys.modules["inro.emme.database.emmebank"] = unittest.mock.MagicMock()
sys.modules["inro.emme.network"] = unittest.mock.MagicMock()
sys.modules["inro.emme.database.scenario"] = unittest.mock.MagicMock()
sys.modules["inro.emme.database.matrix"] = unittest.mock.MagicMock()
sys.modules["inro.emme.network.node"] = unittest.mock.MagicMock()
sys.modules["inro.emme.desktop.app"] = unittest.mock.MagicMock()
sys.modules["inro"] = unittest.mock.MagicMock()
sys.modules["inro.modeller"] = unittest.mock.MagicMock()


@pytest.fixture(scope="session")
Expand All @@ -73,24 +73,78 @@ def inro_context(pytestconfig):
try:
# obey command line option
_inro = pytestconfig.getoption("inro")
print('_inro = [{}]'.format(_inro))
print("_inro = [{}]".format(_inro))
if _inro.lower() == "mock":
print("Mocking inro environment.")
mocked_inro_context()
return "mock"
else:
# why import gdal first: https://github.com/BayAreaMetro/tm2py/blob/7a563f0c5cea2125f28bfaedc50205e70c532094/README.md?plain=1#L57
import gdal
import inro.emme.database.emmebank

print("Using inro environment.")
except Exception as inst:
print(type(inst)) # the exception instance
print(inst.args) # arguments stored in .args
print(inst) # __str__ allows args to be printed directly,
return "inro"
except Exception as inst:
print(type(inst)) # the exception instance
print(inst.args) # arguments stored in .args
print(inst) # __str__ allows args to be printed directly,

# if commandline option fails, try using Emme and then failing that, using Mock
try:
# why import gdal first: https://github.com/BayAreaMetro/tm2py/blob/7a563f0c5cea2125f28bfaedc50205e70c532094/README.md?plain=1#L57
import gdal
import inro.emme.database.emmebank

print("Using inro environment.")
return "inro"
except ModuleNotFoundError:
print("Mocking inro environment.")
mocked_inro_context()
return "mock"

@pytest.fixture(scope="session")
def ctramp_context(pytestconfig):
"""Identifies if ctramp is available."""
import subprocess
try:
from tm2py.tools import run_process
commands = [
"CALL CTRAMP\\runtime\\CTRampEnv.bat",
"set PATH=%CD%\\CTRAMP\runtime;C:\\Windows\\System32;%JAVA_PATH%\bin;"
"%TPP_PATH%;%PYTHON_PATH%;%PYTHON_PATH%\\condabin;%PYTHON_PATH%\\envs",
'CALL CTRAMP\runtime\runMtxMgr.cmd %HOST_IP_ADDRESS% "%JAVA_PATH%"',
]
run_process(commands, name="start_matrix_manager")

print("Using ctramp environment.")
return "ctramp"
except subprocess.CalledProcessError:
print("No ctramp environment.")
return None


@pytest.fixture(scope="session")
def union_city(examples_dir, root_dir, inro_context):
"""Union City model run testing fixture."""
from tm2py.controller import RunController
from tm2py.examples import get_example

EXAMPLE = "UnionCity"
_example_root = examples_dir / EXAMPLE

get_example(example_name="UnionCity", root_dir=root_dir)
controller = RunController(
[
os.path.join(examples_dir, "scenario_config.toml"),
os.path.join(examples_dir, "model_config.toml"),
],
run_dir=_example_root,
)

#------Below this line, need Inro's Emme installed-----
if inro_context != "inro": return
#------------------------------------------------------

controller.run()
return controller
5 changes: 4 additions & 1 deletion tests/test_air_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
from tm2py.examples import get_example


@pytest.mark.menow
def test_air_pax_model(inro_context, examples_dir, root_dir):
"Tests that airport access model be run."
from tools import test_component

get_example(example_name="UnionCity", root_dir=root_dir)

my_run = test_component(examples_dir, "air_passenger")

#------Below this line, need Inro's Emme installed-----
if inro_context != "inro": return

my_run.run_next()

# TODO write assert
3 changes: 0 additions & 3 deletions tests/test_externals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@

from tm2py.examples import get_example


# @pytest.mark.menow
def test_external_travel(examples_dir, root_dir):
"Tests that internal/external travel component can be run."
from tools import test_component

get_example(example_name="UnionCity", root_dir=root_dir)

my_run = test_component(examples_dir, "internal_external")
my_run.run_next()

# TODO write assert
22 changes: 20 additions & 2 deletions tests/test_highway.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def test_prepare_highway_network(inro_context, examples_dir):
from tools import test_component

my_run = test_component(examples_dir, "prepare_network_highway")

#------Below this line, need Inro's Emme installed-----
if inro_context != "inro": return
my_run.run_next()

# TODO write assert
Expand All @@ -21,6 +24,11 @@ def test_highway_assign(inro_context, examples_dir):
from tools import test_component

my_run = test_component(examples_dir, "highway")

#------Below this line, need Inro's Emme installed-----
if inro_context != "inro": return
print("!!!!!!!INRO CONTEXT ",inro_context)

my_run.run_next()

# TODO write assert
Expand All @@ -31,6 +39,10 @@ def test_highway_maz_assign(inro_context, examples_dir):
from tools import test_component

my_run = test_component(examples_dir, "highway_maz_assign")

#------Below this line, need Inro's Emme installed-----
if inro_context != "inro": return

my_run.run_next()

# TODO write assert
Expand All @@ -45,9 +57,12 @@ def test_highway_maz_skim(inro_context, examples_dir):

# TODO write assert


@pytest.mark.menow
def test_highway_skims(inro_context, union_city):
"""Test that the OMX highway skims match the reference."""
#------Below this line, need Inro's Emme installed-----
if inro_context != "inro": return

run_dir = union_city.run_dir

ref_dir_hwy_skims = os.path.join(run_dir, "ref_skim_matrices", "highway")
Expand Down Expand Up @@ -76,9 +91,12 @@ def test_highway_skims(inro_context, union_city):
assert len(missing_skims) == 0, f"Missing skims: {missing_skims}"
assert len(different_skims) == 0, f"Different skims: {different_skims}"


@pytest.mark.menow
def test_maz_da_skims(inro_context, union_city):
"""Test that the DA MAZ skims match the reference."""
#------Below this line, need Inro's Emme installed-----
if inro_context != "inro": return

run_dir = union_city.run_dir

ref_dir_hwy_skims = os.path.join(run_dir, "ref_skim_matrices", "highway")
Expand Down
5 changes: 4 additions & 1 deletion tests/test_household.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@


@pytest.mark.skipci
def test_household_travel(examples_dir):
def test_household_travel(ctramp_context,examples_dir):
"Tests that household travel component can be run."
from tools import test_component

my_run = test_component(examples_dir, "household")

#------Below this line, need Inro's Emme installed-----
if ctramp_context != "ctramp": return
my_run.run_next()

# TODO write assert
5 changes: 5 additions & 0 deletions tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ def test_get_blended_skim(inro_context):

# TODO
pass


#------Below this line, need Inro's Emme installed-----
if inro_context != "inro": return

21 changes: 0 additions & 21 deletions tests/test_run_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,6 @@ def test_example_download(examples_dir, root_dir, inro_context):
assert not (os.path.exists(os.path.join(example_root, "test_data.zip")))


@pytest.fixture(scope="session")
def union_city(examples_dir, root_dir, inro_context):
"""Union City model run testing fixture."""
from tm2py.controller import RunController
from tm2py.examples import get_example

EXAMPLE = "UnionCity"
_example_root = os.path.join(examples_dir, EXAMPLE)

get_example(example_name="UnionCity", root_dir=root_dir)
controller = RunController(
[
os.path.join(examples_dir, "scenario_config.toml"),
os.path.join(examples_dir, "model_config.toml"),
],
run_dir=_example_root,
)
controller.run()
return controller


def test_validate_input_fail(examples_dir, inro_context, temp_dir):
"""Test that validate_input fails when required inputs are missing."""
import toml
Expand Down
5 changes: 5 additions & 0 deletions tests/test_trucks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"Test commercial vehicle model."

import os

import pytest

from tm2py.examples import get_example


Expand All @@ -12,6 +14,9 @@ def test_commercial_vehicle(inro_context, examples_dir, root_dir):
get_example(example_name="UnionCity", root_dir=root_dir)

my_run = test_component(examples_dir, "truck")

#------Below this line, need Inro's Emme installed-----
if inro_context != "inro": return
my_run.run_next()

# TODO write assert