Example #1
0
 def test_topo_sort_knows_what_cycles_are(self):
     """Test that topo_sort fails on cyclic graphs."""
     d = DepGraph()
     d.add_edge('a', 'b')
     d.add_edge('b', 'c')
     d.add_edge('c', 'a')
     self.assertRaises(HasACycle, d.topo_sort)
     assert not d.acyclic
    def _check_cycle(self, collection):
        """Raise an exception if there's a relationship cycle."""
        d = DepGraph()
        for _, fixture in collection:
            for dependency, _ in fixture.extract_relationships():
                d.add_edge(dependency, fixture.key)

        # This does nothing except raise an error if there's a cycle
        d.topo_sort()
        return d
Example #3
0
    def _load_fixtures(self, filenames):
        """Pre-load the fixtures.

        :param list or str filenames: files that hold the fixture data
        """

        if isinstance(filenames, _compat.string_types):
            globbed_filenames = glob(filenames)
        else:
            globbed_filenames = list(
                chain.from_iterable(glob(f) for f in filenames)
            )

        if len(globbed_filenames) == 1:
            content = load_file(filenames, self.use_unicode)
        else:
            content = {}

            for filename in globbed_filenames:
                namespace = self._get_namespace_from_filename(filename)
                content[namespace] = {
                    "objects": load_file(filename, self.use_unicode)
                }

        fixtures = {}
        for k, v in _compat.iteritems(content):

            if "objects" in v:
                # It's a collection of fictures.
                fixtures[k] = self._handle_collection(
                    namespace=k,
                    definition=v,
                    objects=v["objects"],
                )

            # Named fixtures
            else:
                if "id" in v:
                    # Renaming id because it's a Python builtin function
                    v["id_"] = v["id"]
                    del v["id"]

                fixtures[k] = Fixture(key=k, fixture_manager=self, **v)

        d = DepGraph()
        for fixture in fixtures.values():
            for dependency, _ in fixture.extract_relationships():
                d.add_edge(dependency, fixture.key)

        # This does nothing except raise an error if there's a cycle
        d.topo_sort()
        return fixtures, d
Example #4
0
 def test_has_edge_between(self):
     """Test the has_edge_between function."""
     d = DepGraph()
     #
     #  a    b
     #  |    |
     #  c -- d    e
     d.add_edge('a', 'c')
     d.add_edge('b', 'd')
     d.add_edge('c', 'd')
     d.add_node('e')
     assert d.has_edge_between('a', 'c')
     assert not d.has_edge_between('c', 'a'), 'should not be commutative'
     assert not d.has_edge_between('a', 'b'), 'should be edges, not paths'
     assert not d.has_edge_between('e', 'd')
Example #5
0
 def test_ancestors_of(self):
     """Test the ancestors_of function in DepGraph."""
     d = DepGraph()
     #
     #   a    b
     #    \  /
     #     c
     #    / \
     #   d   e
     #   |
     #   f
     d.add_edge('a', 'c')
     d.add_edge('b', 'c')
     d.add_edge('c', 'd')
     d.add_edge('c', 'e')
     d.add_edge('d', 'f')
     l = d.ancestors_of('d')
     self.assertCountEqual(l, ['a', 'b', 'c'])
Example #6
0
 def test_topo_sort_knows_what_cycles_are(self):
     """Test that topo_sort fails on cyclic graphs."""
     d = DepGraph()
     d.add_edge('a', 'b')
     d.add_edge('b', 'c')
     d.add_edge('c', 'a')
     self.assertRaises(HasACycle, d.topo_sort)
     assert not d.acyclic
Example #7
0
 def test_topo_sort(self):
     """Test the topo_sort function of DepGraph."""
     d = DepGraph()
     #
     #      a    b
     #       \  /
     #        c
     #        |
     #        d
     #       /  \
     #      e    f
     d.add_edge('a', 'c')
     d.add_edge('b', 'c')
     d.add_edge('c', 'd')
     d.add_edge('d', 'e')
     d.add_edge('d', 'f')
     l = d.topo_sort()
     self.assertCountEqual(l, ['a', 'b', 'c', 'd', 'e', 'f'])
     assert d.acyclic
Example #8
0
    def _check_cycle(self, collection):
        """Raise an exception if there's a relationship cycle."""
        d = DepGraph()
        for _, fixture in collection:
            for dependency, _ in fixture.extract_relationships():
                d.add_edge(dependency, fixture.key)

        # This does nothing except raise an error if there's a cycle
        d.topo_sort()
        return d
Example #9
0
 def test_ancestors_of(self):
     """Test the ancestors_of function in DepGraph."""
     d = DepGraph()
     #
     #   a    b
     #    \  /
     #     c
     #    / \
     #   d   e
     #   |
     #   f
     d.add_edge('a', 'c')
     d.add_edge('b', 'c')
     d.add_edge('c', 'd')
     d.add_edge('c', 'e')
     d.add_edge('d', 'f')
     l = d.ancestors_of('d')
     self.assertCountEqual(l, ['a', 'b', 'c'])
Example #10
0
 def test_topo_sort(self):
     """Test the topo_sort function of DepGraph."""
     d = DepGraph()
     #
     #      a    b
     #       \  /
     #        c
     #        |
     #        d
     #       /  \
     #      e    f
     d.add_edge('a', 'c')
     d.add_edge('b', 'c')
     d.add_edge('c', 'd')
     d.add_edge('d', 'e')
     d.add_edge('d', 'f')
     l = d.topo_sort()
     self.assertCountEqual(l, ['a', 'b', 'c', 'd', 'e', 'f'])
     assert d.acyclic
Example #11
0
 def test_has_edge_between(self):
     """Test the has_edge_between function."""
     d = DepGraph()
     #
     #  a    b
     #  |    |
     #  c -- d    e
     d.add_edge('a', 'c')
     d.add_edge('b', 'd')
     d.add_edge('c', 'd')
     d.add_node('e')
     assert d.has_edge_between('a', 'c')
     assert not d.has_edge_between('c', 'a'), 'should not be commutative'
     assert not d.has_edge_between('a', 'b'), 'should be edges, not paths'
     assert not d.has_edge_between('e', 'd')