# -*- coding: utf-8 -*-

"""
Tests that duplicate columns are handled appropriately when parsed by the
CSV engine. In general, the expected result is that they are either thoroughly
de-duplicated (if mangling requested) or ignored otherwise.
"""

from pandas.compat import StringIO


class DupeColumnTests(object):
    def test_basic(self):
        # TODO: add test for condition "mangle_dupe_cols=False"
        # once it is actually supported (gh-12935)
        data = "a,a,b,b,b\n1,2,3,4,5"

        for method in ("read_csv", "read_table"):
            # Check default behavior.
            expected = ["a", "a.1", "b", "b.1", "b.2"]
            df = getattr(self, method)(StringIO(data), sep=",")
            assert list(df.columns) == expected

            df = getattr(self, method)(StringIO(data), sep=",",
                                       mangle_dupe_cols=True)
            assert list(df.columns) == expected

    def test_thorough_mangle(self):
        # see gh-17060
        data = "a,a,a.1\n1,2,3"
        df = self.read_csv(StringIO(data), sep=",", mangle_dupe_cols=True)
        assert list(df.columns) == ["a", "a.1", "a.1.1"]

        data = "a,a,a.1,a.1.1,a.1.1.1,a.1.1.1.1\n1,2,3,4,5,6"
        df = self.read_csv(StringIO(data), sep=",", mangle_dupe_cols=True)
        assert list(df.columns) == ["a", "a.1", "a.1.1", "a.1.1.1",
                                    "a.1.1.1.1", "a.1.1.1.1.1"]

        data = "a,a,a.3,a.1,a.2,a,a\n1,2,3,4,5,6,7"
        df = self.read_csv(StringIO(data), sep=",", mangle_dupe_cols=True)
        assert list(df.columns) == ["a", "a.1", "a.3", "a.1.1",
                                    "a.2", "a.2.1", "a.3.1"]
