Beispiel #1
0
 def test_should_add_statement(self) -> None:
     assert self.group(group_config=GroupConfig(
         type="packages", packages=["foo"])).should_add_statement(
             ImportStatement("foo.bar"))
     assert not self.group(group_config=GroupConfig(
         type="packages", packages=["foo"])).should_add_statement(
             ImportStatement("os.path"))
Beispiel #2
0
 def test_ini(self) -> None:
     Config.from_ini(StdPath("config.ini"),
                     "\n".join(["[importanize]"
                                ])) == Config(StdPath("config.ini"))
     Config.from_ini(
         StdPath("config.ini"),
         "\n".join([
             "[importanize]",
             "after_imports_new_lines=5",
             "length=100",
             "formatter=lines",
             "groups=",
             "   stdlib",
             "   packages:mypackage",
             "exclude=exclude",
             "add_imports=",
             "   import foo",
             "allow_plugins=false",
         ]),
     ) == Config(
         path=StdPath("config.json"),
         after_imports_new_lines=5,
         length=100,
         formatter=LinesFormatter,
         groups=[
             GroupConfig(type="stdlib"),
             GroupConfig(type="packages", packages=["foo"]),
         ],
         exclude=["exclude"],
         add_imports=[ImportStatement("foo")],
         are_plugins_allowed=False,
     )
Beispiel #3
0
    def test_importanize_incompatible_groups(self) -> None:
        self.config.groups = [GroupConfig(type="stdlib")]
        result = next(
            run_importanize_on_source(self.input_text,
                                      RuntimeConfig(_config=self.config)))

        assert not result.is_success
Beispiel #4
0
 def test_json(self) -> None:
     assert Config.from_json(StdPath("config.json"),
                             "{}") == Config(StdPath("config.json"))
     assert Config.from_json(
         StdPath("config.json"),
         json.dumps({
             "after_imports_new_lines": "5",
             "length": "100",
             "formatter": "lines",
             "groups": [{
                 "type": "remainder"
             }],
             "exclude": ["exclude"],
             "add_imports": ["import foo"],
         }),
     ) == Config(
         path=StdPath("config.json"),
         after_imports_new_lines=5,
         length=100,
         formatter=LinesFormatter,
         groups=[GroupConfig(type="remainder")],
         exclude=["exclude"],
         add_imports=(ImportStatement("foo"), ),
     )
Beispiel #5
0
 def test_validate_group_config(self) -> None:
     g = GroupConfig(type="packages", packages=["foo"])
     assert BaseImportGroup.validate_group_config(g) is g
Beispiel #6
0
    def test_from_config(self) -> None:
        groups = ImportGroups.from_config(config=Config(
            groups=[GroupConfig(type="stdlib")]))

        assert len(groups.groups) == 1
        assert isinstance(groups.groups[0], StdLibGroup)
Beispiel #7
0
 def test_should_add_statement(self) -> None:
     assert self.group().should_add_statement(ImportStatement(".foo.bar"))
     assert not self.group(group_config=GroupConfig(
         type="local")).should_add_statement(ImportStatement("os.path"))
Beispiel #8
0
 def test_validate_group_config(self) -> None:
     g = GroupConfig(type="packages", packages=["foo"])
     assert self.group.validate_group_config(g) is g
     with pytest.raises(ValueError):
         self.group.validate_group_config(GroupConfig(type="packages"))
Beispiel #9
0
 def test_as_dict(self) -> None:
     assert GroupConfig.default().as_dict() == {"type": "default"}
     assert GroupConfig(type="packages", packages=["foo"]).as_dict() == {
         "type": "packages",
         "packages": ["foo"],
     }
Beispiel #10
0
 def test_default(self) -> None:
     assert GroupConfig.default().type == "default"