def test_validate_flowsheet(models): # these have a type error since they are not iterable at all pytest.raises(TypeError, validate_flowsheet, None) pytest.raises(TypeError, validate_flowsheet, 123) # these are missing the top-level keys (but are sort of iterable, so no type error) assert validate_flowsheet("hello")[0] is False assert validate_flowsheet([])[0] is False # empty one fails assert validate_flowsheet({})[0] is False # the minimal ones we manually constructed will pass for model in models.values(): assert validate_flowsheet(model)[0] # now try tweaks on the minimal ones m = models[2]["model"] # remove image image = m["unit_models"]["U1"]["image"] del m["unit_models"]["U1"]["image"] assert validate_flowsheet(m)[0] is False m["unit_models"]["U1"]["image"] = image # restore it # mess up a unit model ID m["unit_models"]["U-FOO"] = m["unit_models"]["U1"] del m["unit_models"]["U1"] assert validate_flowsheet(m)[0] is False m["unit_models"]["U1"] = m["unit_models"]["U-FOO"] del m["unit_models"]["U-FOO"] # mess up an arc ID m["arcs"]["A-FOO"] = m["arcs"]["A1"] del m["arcs"]["A1"] assert validate_flowsheet(m)[0] is False m["arcs"]["A1"] = m["arcs"]["A-FOO"] del m["arcs"]["A-FOO"]
def test_visualize(flash_model, tmp_path): from pathlib import Path flowsheet = flash_model.fs # Start the visualization server result = fsvis.visualize(flowsheet, "Flash", browser=False, save_dir=tmp_path) # Get the model resp = requests.get(f"http://127.0.0.1:{result.port}/fs?id=Flash") data = resp.json() # Validate the model ok, msg = validate_flowsheet(data) assert ok, f"Invalid flowsheet returned: {msg}" assert data["model"]["id"] == "Flash" assert data["model"]["unit_models"]["flash"]["type"] == "flash" assert len(data["cells"]) == 7 units = [x for x in data["cells"] if x["type"] == "standard.Image"] assert len(units) == 4 unit_images = [Path(x["attrs"]["image"]["xlinkHref"]).name for x in units] unit_images.sort() assert unit_images == [ "feed.svg", "flash.svg", "product.svg", "product.svg" ] # Modify the model by deleting its one and only component flowsheet.del_component("flash") # Get the model (again) resp = requests.get(f"http://127.0.0.1:{result.port}/fs?id=Flash") data = resp.json() # Validate the modified model expected = { "model": { "id": "Flash", "stream_table": { "columns": ["", "Variable"], "data": [], "index": [] }, "unit_models": {}, "arcs": {}, }, "cells": [], } assert data == expected
def test_visualize(flash_model): from pathlib import Path flowsheet = flash_model.fs # Start the visualization server port = fsvis.visualize(flowsheet, "Flash", browser=False, save_as=None) # Get the model resp = requests.get(f"http://127.0.0.1:{port}/fs?id=Flash") data = resp.json() # Validate the model ok, msg = validate_flowsheet(data) assert ok, f"Invalid flowsheet returned: {msg}" assert data["model"]["id"] == "Flash" assert data["model"]["unit_models"]["flash"]["type"] == "flash" assert len(data["cells"]) == 7 units = [x for x in data["cells"] if x["type"] == "standard.Image"] assert len(units) == 4 unit_images = [Path(x["attrs"]["image"]["xlinkHref"]).name for x in units] unit_images.sort() assert unit_images == [ "feed.svg", "flash.svg", "product.svg", "product.svg" ] # Modify the model by deleting its one and only component flowsheet.del_component("flash") # Get the model (again) resp = requests.get(f"http://127.0.0.1:{port}/fs?id=Flash") data = resp.json() # Validate the modified model expected = { 'model': { 'id': 'Flash', 'unit_models': {}, 'arcs': {} }, 'cells': [] } assert data == expected