Esempio n. 1
0
    def test_add_statement_to_group_priority(self):
        groups = ImportGroups()
        groups.extend([RemainderGroup(), LocalGroup()])

        groups.add_statement_to_group(ImportStatement([], ".a"))

        self.assertListEqual(groups[0].statements, [])
        self.assertListEqual(groups[1].statements, [ImportStatement([], ".a")])
Esempio n. 2
0
 def test_all_line_numbers(self) -> None:
     assert ImportGroups().all_line_numbers() == []
     assert ImportGroups([
         BaseImportGroup(
             statements=[ImportStatement("foo", line_numbers=[2, 7])]),
         BaseImportGroup(
             statements=[ImportStatement("bar", line_numbers=[1, 2])]),
     ]).all_line_numbers() == [1, 2, 7]
Esempio n. 3
0
    def test_all_line_numbers(self):
        groups = ImportGroups()

        self.assertListEqual(groups.all_line_numbers(), [])

        g = BaseImportGroup()
        g.statements = [mock.MagicMock(line_numbers=[2, 7],
                                       spec=ImportStatement)]
        groups.groups.append(g)

        g = BaseImportGroup()
        g.statements = [mock.MagicMock(line_numbers=[1, 2],
                                       spec=ImportStatement)]
        groups.groups.append(g)

        self.assertListEqual(groups.all_line_numbers(), [1, 2, 7])
Esempio n. 4
0
    def test_add_statement_to_group_one(self):
        groups = ImportGroups()
        groups.extend([LocalGroup()])

        with self.assertRaises(ValueError):
            groups.add_statement_to_group(ImportStatement([], 'a'))

        groups.add_statement_to_group(ImportStatement([], '.a'))

        self.assertListEqual(groups[0].statements, [ImportStatement([], '.a')])
Esempio n. 5
0
    def test_add_statement_to_group_priority(self):
        groups = ImportGroups()
        groups.groups = [
            RemainderGroup(),
            LocalGroup(),
        ]

        groups.add_statement_to_group(
            ImportStatement([], '.a')
        )

        self.assertListEqual(
            groups.groups[0].statements,
            []
        )
        self.assertListEqual(
            groups.groups[1].statements,
            [ImportStatement([], '.a')]
        )
Esempio n. 6
0
    def test_add_group(self):
        groups = ImportGroups()

        with self.assertRaises(ValueError):
            groups.add_group({})

        with self.assertRaises(ValueError):
            groups.add_group({'type': 'foo'})

        groups.add_group({'type': 'stdlib'})

        self.assertEqual(len(groups.groups), 1)
        self.assertEqual(groups.groups[0].__class__, StdLibGroup)
Esempio n. 7
0
    def test_add_group(self):
        groups = ImportGroups()

        with self.assertRaises(ValueError):
            groups.add_group({})

        with self.assertRaises(ValueError):
            groups.add_group({"type": "foo"})

        groups.add_group({"type": "stdlib"})

        self.assertEqual(len(groups), 1)
        self.assertEqual(groups[0].__class__, StdLibGroup)
Esempio n. 8
0
    def test_formatted_with_artifacts(self):
        artifacts = {'sep': '\r\n'}

        groups = ImportGroups(file_artifacts=artifacts)
        groups.extend([
            RemainderGroup(file_artifacts=artifacts),
            LocalGroup(file_artifacts=artifacts),
        ])

        groups.add_statement_to_group(
            ImportStatement([], '.a', file_artifacts=artifacts))
        groups.add_statement_to_group(
            ImportStatement([], 'foo', file_artifacts=artifacts))

        self.assertEqual(groups.formatted(), 'import foo\r\n'
                         '\r\n'
                         'import .a')
Esempio n. 9
0
    def test_formatted_with_artifacts(self):
        artifacts = {"sep": "\r\n"}

        groups = ImportGroups(file_artifacts=artifacts)
        groups.extend([
            RemainderGroup(file_artifacts=artifacts),
            LocalGroup(file_artifacts=artifacts),
        ])

        groups.add_statement_to_group(
            ImportStatement([], ".a", file_artifacts=artifacts))
        groups.add_statement_to_group(
            ImportStatement([], "foo", file_artifacts=artifacts))

        self.assertEqual(groups.formatted(), "import foo\r\n"
                         "\r\n"
                         "import .a")
Esempio n. 10
0
    def test_add_statement_to_group_one(self):
        groups = ImportGroups()
        groups.extend([LocalGroup()])

        with self.assertRaises(ValueError):
            groups.add_statement_to_group(ImportStatement([], "a"))

        groups.add_statement_to_group(ImportStatement([], ".a"))

        self.assertListEqual(groups[0].statements, [ImportStatement([], ".a")])
Esempio n. 11
0
    def test_add_statement(self) -> None:
        groups = ImportGroups([LocalGroup()])

        with pytest.raises(ValueError):
            groups.add_statement(ImportStatement("a"))

        groups.add_statement(ImportStatement(".a"))

        assert groups.groups[0].statements == [ImportStatement(".a")]
Esempio n. 12
0
    def test_add_group(self):
        groups = ImportGroups()

        with self.assertRaises(ValueError):
            groups.add_group({})

        with self.assertRaises(ValueError):
            groups.add_group({"type": "foo"})

        groups.add_group({"type": "stdlib"})

        self.assertEqual(len(groups), 1)
        self.assertEqual(groups[0].__class__, StdLibGroup)
Esempio n. 13
0
    def test_formatted_with_artifacts(self) -> None:
        artifacts = Artifacts(sep="\r\n")

        groups = ImportGroups(
            [
                RemainderGroup(artifacts=artifacts),
                LocalGroup(artifacts=artifacts)
            ],
            artifacts=artifacts,
        )

        groups.add_statement(ImportStatement(".a"))
        groups.add_statement(ImportStatement("foo"))

        assert groups.formatted() == "import foo\r\n\r\nimport .a"
Esempio n. 14
0
    def test_formatted_with_artifacts(self):
        artifacts = {"sep": "\r\n"}

        groups = ImportGroups(file_artifacts=artifacts)
        groups.extend(
            [
                RemainderGroup(file_artifacts=artifacts),
                LocalGroup(file_artifacts=artifacts),
            ]
        )

        groups.add_statement_to_group(
            ImportStatement([], ".a", file_artifacts=artifacts)
        )
        groups.add_statement_to_group(
            ImportStatement([], "foo", file_artifacts=artifacts)
        )

        self.assertEqual(
            groups.formatted(), "import foo\r\n" "\r\n" "import .a"
        )
Esempio n. 15
0
    def test_formatted_with_artifacts(self):
        artifacts = {'sep': '\r\n'}

        groups = ImportGroups(file_artifacts=artifacts)
        groups.groups = [
            RemainderGroup(file_artifacts=artifacts),
            LocalGroup(file_artifacts=artifacts),
        ]

        groups.add_statement_to_group(
            ImportStatement([], '.a', file_artifacts=artifacts)
        )
        groups.add_statement_to_group(
            ImportStatement([], 'foo', file_artifacts=artifacts)
        )

        self.assertEqual(
            groups.formatted(),
            'import foo\r\n'
            '\r\n'
            'import .a'
        )
Esempio n. 16
0
    def test_add_statement_to_group_one(self):
        groups = ImportGroups()
        groups.groups = [
            LocalGroup()
        ]

        with self.assertRaises(ValueError):
            groups.add_statement_to_group(
                ImportStatement([], 'a')
            )

        groups.add_statement_to_group(
            ImportStatement([], '.a')
        )

        self.assertListEqual(
            groups.groups[0].statements,
            [ImportStatement([], '.a')]
        )
Esempio n. 17
0
 def test_str_mock(self, mock_as_string):
     self.assertEqual(
         getattr(ImportGroups(),
                 '__{}__'.format(six.text_type.__name__))(),
         mock_as_string.return_value
     )
Esempio n. 18
0
 def test_formatted_empty(self):
     self.assertEqual(ImportGroups().formatted(), '')
Esempio n. 19
0
 def test_as_string(self):
     self.assertEqual(ImportGroups().as_string(), '')
Esempio n. 20
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)
Esempio n. 21
0
    def test_add_statement_priority(self) -> None:
        groups = ImportGroups([RemainderGroup(), LocalGroup()])
        groups.add_statement(ImportStatement(".a"))

        assert groups.groups[0].statements == []
        assert groups.groups[1].statements == [ImportStatement(".a")]
Esempio n. 22
0
 def test_init(self):
     groups = ImportGroups()
     self.assertListEqual(groups.groups, [])
Esempio n. 23
0
 def test_as_string(self) -> None:
     assert str(ImportGroups()) == ""
Esempio n. 24
0
 def test_formatted_empty(self) -> None:
     assert ImportGroups().formatted() == ""