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")])
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]
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])
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')])
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')] )
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)
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)
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')
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")
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")])
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")]
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"
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" )
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' )
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')] )
def test_str_mock(self, mock_as_string): self.assertEqual( getattr(ImportGroups(), '__{}__'.format(six.text_type.__name__))(), mock_as_string.return_value )
def test_formatted_empty(self): self.assertEqual(ImportGroups().formatted(), '')
def test_as_string(self): self.assertEqual(ImportGroups().as_string(), '')
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)
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")]
def test_init(self): groups = ImportGroups() self.assertListEqual(groups.groups, [])
def test_as_string(self) -> None: assert str(ImportGroups()) == ""
def test_formatted_empty(self) -> None: assert ImportGroups().formatted() == ""