Beispiel #1
0
    def test_returns_empty_list_when_import_but_no_available_details(self):
        graph = ImportGraph()

        importer, imported = "foo", "bar"
        graph.add_import(importer=importer, imported=imported),

        assert [] == graph.get_import_details(importer=importer,
                                              imported=imported)
Beispiel #2
0
    def test_import_details_to_descendant_are_lost(self):
        graph = ImportGraph()
        for module in [
                "foo",
                "foo.green",
                "bar.blue",
        ]:
            graph.add_module(module)

        graph.add_import(
            importer="bar.blue",
            imported="foo.green",
            line_number=1,
            line_contents="from foo import green",
        )

        graph.squash_module("foo")

        assert [] == graph.get_import_details(importer="bar.blue",
                                              imported="foo")
Beispiel #3
0
    def test_import_details_from_squashed_root_are_preserved(self):
        graph = ImportGraph()
        for module in [
                "foo",
                "foo.green",
                "bar.blue",
        ]:
            graph.add_module(module)
        import_details = dict(
            importer="foo",
            imported="bar.blue",
            line_number=1,
            line_contents="from . import bar",
        )
        graph.add_import(**import_details)

        graph.squash_module("foo")

        assert [import_details
                ] == graph.get_import_details(importer="foo",
                                              imported="bar.blue")
Beispiel #4
0
    def test_happy_path(self):
        graph = ImportGraph()

        imports_info = [
            dict(
                importer="mypackage.foo",
                imported="mypackage.bar",
                line_number=1,
                line_contents="from . import bar",
            ),
            dict(
                importer="mypackage.foo",
                imported="mypackage.bar",
                line_number=10,
                line_contents="from .bar import a_function",
            ),
        ]
        for import_info in imports_info:
            graph.add_import(**import_info)

        assert imports_info == graph.get_import_details(
            importer="mypackage.foo", imported="mypackage.bar")
Beispiel #5
0
    def test_returns_only_relevant_imports(self):
        graph = ImportGraph()

        imports_info = [
            dict(
                importer="mypackage.foo",
                imported="mypackage.bar",
                line_number=1,
                line_contents="from . import bar",
            )
        ]
        graph.add_import(**imports_info[0])

        # Also add a different import in the same module.
        graph.add_import(
            importer="mypackage.foo",
            imported="mypackage.baz",
            line_number=2,
            line_contents="from . import baz",
        )

        assert imports_info == graph.get_import_details(
            importer="mypackage.foo", imported="mypackage.bar")
Beispiel #6
0
    def test_returns_empty_list_when_no_import(self):
        graph = ImportGraph()

        assert [] == graph.get_import_details(importer="foo", imported="bar")