Example #1
0
    def test_does_nothing_if_module_is_already_squashed(self):
        graph = ImportGraph()
        graph.add_module("foo", is_squashed=True)
        graph.add_import(importer="foo", imported="bar")

        graph.squash_module("foo")

        assert graph.direct_import_exists(importer="foo", imported="bar")
Example #2
0
    def test_contracts_import_to_descendant(self):
        graph = ImportGraph()
        for module in [
                "foo",
                "foo.green",
                "bar.blue",
        ]:
            graph.add_module(module)
        graph.add_import(importer="bar.blue", imported="foo.green")

        graph.squash_module("foo")

        assert graph.direct_import_exists(importer="bar.blue", imported="foo")
Example #3
0
    def test_keeps_import_of_squashed_root(self):
        graph = ImportGraph()
        for module in [
                "foo",
                "foo.green",
                "bar.blue",
        ]:
            graph.add_module(module)
        graph.add_import(importer="bar.blue", imported="foo")

        graph.squash_module("foo")

        assert graph.direct_import_exists(importer="bar.blue", imported="foo")
Example #4
0
def test_direct_import_exists(importer, imported, as_packages,
                              expected_result):
    """
    Build a graph to analyse for chains. This is much easier to debug visually,
    so here is the dot syntax for the graph, which can be viewed using a dot file viewer.

        digraph {
            a;
            a_one;
            a_one_green;
            a_one_blue;
            a_two;
            a_two_green;
            a_two_blue;
            a_three;
            a_three_green;
            a_three_blue;
            a_four;
            a_four_green;
            a_four_green_alpha;
            a_five;

            a_one_green -> a_two_green;
            a_two -> a_two_green;
            a_two_green -> a_three_blue;
            a_five -> a_four_green_alpha;
            a_four_green_alpha -> a_two_green;
        }
    """
    graph = ImportGraph()
    all_modules = (
        a,
        a_one,
        a_one_green,
        a_one_blue,
        a_two,
        a_two_green,
        a_two_blue,
        a_three,
        a_three_green,
        a_three_blue,
        a_four,
        a_four_green,
        a_four_green_alpha,
        a_five,
    ) = (
        "a",
        "a.one",
        "a.one.green",
        "a.one.blue",
        "a.two",
        "a.two.green",
        "a.two.blue",
        "a.three",
        "a.three.green",
        "a.three.blue",
        "a.four",
        "a.four.green",
        "a.four.green.alpha",
        "a.five",
    )

    for module_to_add in all_modules:
        graph.add_module(module_to_add)

    for _importer, _imported in (
        (a_one_green, a_two_green),
        (a_two, a_two_green),
        (a_two_green, a_three_blue),
        (a_five, a_four_green_alpha),
        (a_four_green_alpha, a_two_green),
    ):
        graph.add_import(importer=_importer, imported=_imported)

    if isinstance(expected_result, Exception):
        with pytest.raises(expected_result.__class__):
            graph.direct_import_exists(importer=importer,
                                       imported=imported,
                                       as_packages=as_packages)
    else:
        assert expected_result == graph.direct_import_exists(
            importer=importer, imported=imported, as_packages=as_packages)