Exemple #1
0
def test_from_json(from_json):
    json_path = os.path.join(
        os.path.dirname(__file__), "models", "demand_saving2_with_variables.json"
    )

    if from_json:
        json_dict = pywr_json_to_d3_json(json_path, attributes=True)
    else:
        model = load_model("demand_saving2_with_variables.json")
        json_dict = pywr_model_to_d3_json(model, attributes=True)

    assert "nodes" in json_dict.keys()
    assert "links" in json_dict.keys()

    node_names = ["Inflow", "Reservoir", "Demand", "Spill"]
    for node in json_dict["nodes"]:
        assert node["name"] in node_names

        if node["name"] == "Reservoir":
            assert_array_equal(node["position"], [1, 1])

    demand = get_node(json_dict["nodes"], "Demand")
    demand_max_flow = get_node_attribute(demand, "max_flow")

    assert demand_max_flow["value"] == "demand_max_flow - AggregatedParameter"
Exemple #2
0
def test_from_model():

    json_path = os.path.join(os.path.dirname(__file__), "models",
                             "river1.json")
    model = Model.load(json_path)
    json_dict = pywr_model_to_d3_json(model, attributes=True)

    assert "nodes" in json_dict.keys()
    assert "links" in json_dict.keys()

    node_names = ["catchment1", "river1", "abs1", "link1", "term1", "demand1"]
    for node in json_dict["nodes"]:
        assert node["name"] in node_names

    catchment = get_node(json_dict["nodes"], "catchment1")
    catchment_max_flow = get_node_attribute(catchment, "max_flow")
    assert catchment_max_flow["value"] == "5.0"
Exemple #3
0
def test_d3_data():
    """Test returned by `pywr_json_to_d3_json` and `pywr_model_to_d3_json` is similar.

    These return graph data from a JSON file and Model instance respectively. Here we test that each returns the
    same node names and number on links. The data won't match exactly due to differences in node ordering.
    """
    json_path = os.path.join(os.path.dirname(__file__), "models",
                             "demand_saving2_with_variables.json")
    model = load_model("demand_saving2_with_variables.json")

    d3_data_from_json = pywr_json_to_d3_json(json_path)
    d3_data_from_model = pywr_model_to_d3_json(model)

    json_nodes = {n["name"]: n for n in d3_data_from_json["nodes"]}
    model_nodes = {n["name"]: n for n in d3_data_from_model["nodes"]}

    assert json_nodes == model_nodes
    assert len(d3_data_from_json["links"]) == len(d3_data_from_model["links"])