def test_initialization_argument_checks(): """ `Grouping` constructor should raise `TypeError` on bad arguments. """ message = "\n`Grouping` constructor did not check mandatory arguments." with assert_raises(TypeError, msg=message): g = Grouping() message = "\n`Grouping` constructor did not check conflicting arguments." with assert_raises(TypeError, msg=message): g = Grouping(key=lambda x: x, constant_key='key')
def test_mutable_mapping_groups(): g = Grouping(key=lambda x: len(x), value=lambda x: {y: len([z for z in x if z == y]) for y in x}) groups = {} expected = {3: {'o': 2, 'f': 1}} g("foo", groups) eq_(groups, expected, "\n Expected: {} \n Got : {}".format(expected, groups))
def test_grouping_filter_parameter(self): g1 = Grouping(key=lambda e: "The Special One", filter=lambda e: "special" in e.uid) g2 = Nodes(key=lambda e: "A Subset", filter=lambda e: "subset" in e.uid) ES = es.EnergySystem(groupings=[g1, g2]) special = Entity(uid="special") subset = set(Entity(uid="subset: {}".format(i)) for i in range(10)) others = set(Entity(uid="other: {}".format(i)) for i in range(10)) eq_(ES.groups["The Special One"], special) eq_(ES.groups["A Subset"], subset)
def test_grouping_filter_parameter(self): g1 = Grouping(key=lambda e: "The Special One", filter=lambda e: "special" in str(e)) g2 = Nodes(key=lambda e: "A Subset", filter=lambda e: "subset" in str(e)) ES = es.EnergySystem(groupings=[g1, g2]) special = Node(label="special") subset = set(Node(label="subset: {}".format(i)) for i in range(10)) others = set(Node(label="other: {}".format(i)) for i in range(10)) ES.add(special, *subset) ES.add(*others) eq_(ES.groups["The Special One"], special) eq_(ES.groups["A Subset"], subset)
def test_notimplementederrors(): """ `Grouping` should raise an error when reaching unreachable code. """ message = "\n`Grouping.key` not overriden, but no error raised." with assert_raises(NotImplementedError, msg=message): g = Grouping(key="key") del g.key g.key("dummy argument") message = "\n`Grouping.filter` not overriden, but no error raised." with assert_raises(NotImplementedError, msg=message): g = Grouping(key="key") del g.filter g.filter("dummy argument")
def test_non_callable_group_keys(self): collect_everything = Nodes(key="everything") g1 = Grouping(key="The Special One", filter=lambda e: "special" in e.uid) g2 = Nodes(key="A Subset", filter=lambda e: "subset" in e.uid) ES = es.EnergySystem(groupings=[g1, g2, collect_everything]) special = Entity(uid="special") subset = set(Entity(uid="subset: {}".format(i)) for i in range(2)) others = set(Entity(uid="other: {}".format(i)) for i in range(2)) everything = subset.union(others) everything.add(special) eq_(ES.groups["The Special One"], special) eq_(ES.groups["A Subset"], subset) eq_(ES.groups["everything"], everything)