def test_table_breakout(self):
        test_csv = """col1,col2
        foo,1
        foo,2
        bar,3
        bar,4
        baz,5
        baz,6
        """

        expected_foo = """col1,col2
        foo,1
        foo,2
        """

        expected_bar = """col1,col2
        bar,3
        bar,4
        """

        expected_baz = """col1,col2
        baz,5
        baz,6
        """

        with TemporaryDirectory() as workdir:
            workdir = Path(workdir)
            input_file = workdir / "in.csv"
            with open(input_file, "w") as fd:
                for line in test_csv.split("\n"):
                    if not line.isspace():
                        fd.write(f"{line.strip()}\n")

            output_folder = workdir / "outputs"
            output_folder.mkdir(exist_ok=True, parents=True)
            table_breakout(input_file, output_folder, "col1")

            expected = expected_foo
            csv_output = output_folder / "foo" / "in.csv"
            for line1, line2 in zip(expected.split("\n"),
                                    read_lines(csv_output)):
                self.assertEqual(line1.strip(), line2.strip())

            expected = expected_bar
            csv_output = output_folder / "bar" / "in.csv"
            for line1, line2 in zip(expected.split("\n"),
                                    read_lines(csv_output)):
                self.assertEqual(line1.strip(), line2.strip())

            expected = expected_baz
            csv_output = output_folder / "baz" / "in.csv"
            for line1, line2 in zip(expected.split("\n"),
                                    read_lines(csv_output)):
                self.assertEqual(line1.strip(), line2.strip())
Esempio n. 2
0
    def test_table_breakout_unsorted(self):
        test_csv = _make_test_csv_file("""
            col1,col2
            foo,1
            foo,2
            bar,3
            bar,4
            baz,5
            foo,6
            """)

        with temporary_directory() as output_folder:
            with self.assertRaises(Exception):
                table_breakout(test_csv, output_folder, "col1")
Esempio n. 3
0
    def test_table_breakout(self):
        test_csv = _make_test_csv_file(
            """
            col1,col2
            foo,1
            foo,2
            bar,3
            bar,4
            baz,5
            baz,6
            """
        )

        expected = {}
        expected["foo"] = _make_test_csv_file(
            """
            col1,col2
            foo,1
            foo,2
            """
        )
        expected["bar"] = _make_test_csv_file(
            """
            col1,col2
            bar,3
            bar,4
            """
        )
        expected["baz"] = _make_test_csv_file(
            """
            col1,col2
            baz,5
            baz,6
            """
        )

        with temporary_directory() as output_folder:
            output_name = "test.csv"
            table_breakout(test_csv, output_folder, "col1", output_name=output_name)

            for breakout_value in ("foo", "bar", "baz"):
                output_path = output_folder / breakout_value / output_name
                _compare_tables_equal(self, output_path, expected[breakout_value])
    def test_table_breakout_unsorted(self):
        test_csv = """col1,col2
        foo,1
        foo,2
        bar,3
        bar,4
        baz,5
        foo,6
        """

        with TemporaryDirectory() as workdir:
            workdir = Path(workdir)
            input_file = workdir / "in.csv"
            with open(input_file, "w") as fd:
                for line in test_csv.split("\n"):
                    if not line.isspace():
                        fd.write(f"{line.strip()}\n")

            output_folder = workdir / "outputs"
            output_folder.mkdir(exist_ok=True, parents=True)
            with self.assertRaises(Exception):
                table_breakout(input_file, output_folder, "col1")