Skip to content

Commit

Permalink
more adjustments on the monkeypatching
Browse files Browse the repository at this point in the history
  • Loading branch information
gtfierro committed Jun 28, 2024
1 parent 13af3de commit 7617f8c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
31 changes: 14 additions & 17 deletions tests/library/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

import pytest
from pytest import MonkeyPatch

import buildingmotif
Expand Down Expand Up @@ -37,19 +36,6 @@ def mock_building_motif():
return instances[name]


# this fixture is automatically applied to all tests. It monkeypatches the building motif module to ignore the singleton pattern.
@pytest.fixture(autouse=True, scope="function")
def patch_bmotif(monkeypatch):
monkeypatch.setattr(buildingmotif.building_motif.singleton, "Singleton", type)
monkeypatch.setattr(buildingmotif.building_motif.building_motif, "Singleton", type)
monkeypatch.setattr(
buildingmotif.building_motif.building_motif,
"get_building_motif",
mock_building_motif,
)
monkeypatch.setattr(buildingmotif, "get_building_motif", mock_building_motif)


def library_load(**kwargs):
with MonkeyPatch().context() as m:
m.setattr(
Expand All @@ -71,9 +57,6 @@ def library_load(**kwargs):
return Library.load(**kwargs)


# create a contextmanager that patches the building motif module to ignore the singleton pattern.


class PatchBuildingMotif:
def __enter__(self):
print("patching")
Expand All @@ -94,6 +77,20 @@ def __enter__(self):
"get_building_motif",
mock_building_motif,
)
self.monkeypatch.setattr(
buildingmotif.building_motif.singleton, "Singleton", type
)
self.monkeypatch.setattr(
buildingmotif.building_motif.building_motif, "Singleton", type
)
self.monkeypatch.setattr(
buildingmotif.building_motif.building_motif,
"get_building_motif",
mock_building_motif,
)
self.monkeypatch.setattr(
buildingmotif, "get_building_motif", mock_building_motif
)
return self.monkeypatch

def __exit__(self, *args):
Expand Down
4 changes: 2 additions & 2 deletions tests/library/test_223p_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ def setup_building_motif_s223() -> Tuple[BuildingMOTIF, Library]:
instances[__file__] = bm
# bm = get_building_motif()
bm.setup_tables()
brick = Library.load(
s223 = Library.load(
ontology_graph="libraries/ashrae/223p/ontology/223p.ttl",
run_shacl_inference=False,
)
bm.session.commit()
return bm, brick
return bm, s223


def plug_223_connection_points(g: Graph):
Expand Down
33 changes: 17 additions & 16 deletions tests/library/test_brick_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,23 @@ def setup_building_motif_brick() -> Tuple[BuildingMOTIF, Library]:

def test_brick_template(bm, brick, library, template):
# set the module to this file; this helps the monkeypatch determine which BuildingMOTIF instance to use
os.environ["bmotif_module"] = __file__
try:
MODEL = Namespace("urn:ex/")
m = Model.create(MODEL)
_, g = template.inline_dependencies().fill(MODEL, include_optional=False)
assert isinstance(g, Graph), "was not a graph"
bind_prefixes(g)
m.add_graph(g)
ctx = m.validate(
[library.get_shape_collection(), brick.get_shape_collection()],
error_on_missing_imports=False,
)
except Exception as e:
bm.session.rollback()
raise e
assert ctx.valid, ctx.report_string
with PatchBuildingMotif():
os.environ["bmotif_module"] = __file__
try:
MODEL = Namespace("urn:ex/")
m = Model.create(MODEL)
_, g = template.inline_dependencies().fill(MODEL, include_optional=False)
assert isinstance(g, Graph), "was not a graph"
bind_prefixes(g)
m.add_graph(g)
ctx = m.validate(
[brick.get_shape_collection()],
error_on_missing_imports=False,
)
except Exception as e:
bm.session.rollback()
raise e
assert ctx.valid, ctx.report_string


def pytest_generate_tests(metafunc):
Expand Down

0 comments on commit 7617f8c

Please sign in to comment.