def test_grouping_laziness(self): """ Energy system `groups` should be fully lazy. `Node`s added to an energy system should only be tested for and put into their respective groups right before the `groups` property of an energy system is accessed. """ group = "Group" g = Nodes(key=group, filter=lambda n: getattr(n, "group", False)) self.es = es.EnergySystem(groupings=[g]) buses = [Bus("Grouped"), Bus("Ungrouped one"), Bus("Ungrouped two")] self.es.add(buses[0]) buses[0].group = True self.es.add(*buses[1:]) ok_( group in self.es.groups, "\nExpected to find\n\n `{!r}`\n\nin `es.groups`.\nGot:\n\n `{}`" .format( group, "\n ".join(pformat(set(self.es.groups.keys())).split("\n")), ), ) ok_( buses[0] in self.es.groups[group], "\nExpected\n\n `{}`\n\nin `es.groups['{}']`:\n\n `{}`".format( "\n ".join(pformat(buses[0]).split("\n")), group, "\n ".join(pformat(self.es.groups[group]).split("\n"))), )
def test_node_registration(self): eq_(Node.registry, self.es) b1 = Bus(label='<B1>') eq_(self.es.entities[0], b1) b2 = Bus(label='<B2>') eq_(self.es.entities[1], b2) t1 = Transformer(label='<TF1>', inputs=[b1], outputs=[b2]) ok_(t1 in self.es.entities)
def test_entity_registration(self): bus = Bus(label='bus-uid', type='bus-type') eq_(self.es.nodes[0], bus) bus2 = Bus(label='bus-uid2', type='bus-type') eq_(self.es.nodes[1], bus2) t1 = Transformer(label='pp_gas', inputs=[bus], outputs=[bus2]) ok_(t1 in self.es.nodes) self.es.timeindex = self.timeindex ok_(len(self.es.timeindex) == 5)
def test_that_nodes_do_not_get_undead_flows(self): """ Newly created nodes should only have flows assigned to them. A new node `n`, which re-used a previously used label `l`, retained the flows of those nodes which where labeled `l` before `n`. This incorrect behaviour is a problem if somebody wants to use different nodes with the same label in multiple energy systems. While this feature currently isn't used, it also lead to weird behaviour when running tests. This test ensures that new nodes only have those flows which are assigned to them on construction. """ # We don't want a registry as we are re-using a label on purpose. # Having a registry would just throw and error generated by the DEFAULT # grouping in this case. Node.registry = None flow = object() old = Node(label="A reused label") bus = Bus(label="bus", inputs={old: flow}) eq_(bus.inputs[old].flow, flow, ("\n Expected: {}" + "\n Got : {} instead").format( flow, bus.inputs[old].flow)) eq_(old.outputs[bus].flow, flow, ("\n Expected: {}" + "\n Got : {} instead").format( flow, old.outputs[bus].flow)) new = Node(label="A reused label") eq_(new.outputs, {}, ("\n Expected an empty dictionary of outputs." + "\n Got: {} instead").format(new.outputs))
def test_modifying_inputs_after_construction(self): """ One should be able to add and delete inputs of a node. """ node = Node("N1") bus = Bus("N2") flow = "flow" eq_(node.inputs, {}, ("\n Expected an empty dictionary of inputs." + "\n Got: {} (== {}) instead").format(node.inputs, dict(node.inputs))) node.inputs[bus] = flow eq_(node.inputs, {bus: flow}, ("\n Expected {} as `node.inputs`." + "\n Got : {} (== {}) instead").format({bus: flow}, node.inputs, dict(node.inputs))) eq_(node.inputs[bus], flow, ("\n Expected {} as `node.inputs[bus]`." + "\n Got : {} instead").format(flow, node.inputs[bus])) del node.inputs[bus] eq_(node.inputs, {}, ("\n Expected an empty dictionary of inputs." + "\n Got: {} (== {}) instead").format(node.inputs, dict(node.inputs)))
def test_flows(self): key = object() ensys = es.EnergySystem(groupings=[Flows(key)]) Node.registry = ensys flows = (object(), object()) bus = Bus(label="A Bus") Node(label="A Node", inputs={bus: flows[0]}, outputs={bus: flows[1]}) eq_(ensys.groups[key], set(flows))
def test_flows_with_nodes(self): key = object() ensys = es.EnergySystem(groupings=[FWNs(key)]) Node.registry = ensys flows = (object(), object()) bus = Bus(label="A Bus") node = Node(label="A Node", inputs={bus: flows[0]}, outputs={bus: flows[1]}) eq_(ensys.groups[key], {(bus, node, flows[0]), (node, bus, flows[1])})
def test_that_nodes_is_a_proper_alias_for_entities(self): b1, b2 = Bus(label="B1"), Bus(label="B2") eq_(self.es.nodes, [b1, b2]) empty = [] self.es.nodes = empty ok_(self.es.entities is empty)
def test_entity_grouping_on_construction(self): bus = Bus(label="test bus") ensys = es.EnergySystem(entities=[bus]) ok_(ensys.groups[bus.label] is bus)