コード例 #1
0
    def test_option_use_col_name_as_header(self):
        file_obj = bytes_io_from_str(
            textwrap.dedent("""\
      name1,name2,name3
      a1,1,c1
      a2,2,c2
      a3,3,c3
      """))

        parse_options = {"include_col_names_as_headers": False}
        parsed_file = import_csv._parse_open_file(
            file_obj, parse_options=parse_options)[1][0]
        self._check_num_cols(parsed_file, 3)
        self._check_col(parsed_file, 0, "", "Text",
                        ["name1", "a1", "a2", "a3"])
        self._check_col(parsed_file, 1, "", "Text", ["name2", "1", "2", "3"])
        self._check_col(parsed_file, 2, "", "Text",
                        ["name3", "c1", "c2", "c3"])

        parse_options = {"include_col_names_as_headers": True}
        parsed_file = import_csv._parse_open_file(
            file_obj, parse_options=parse_options)[1][0]
        self._check_num_cols(parsed_file, 3)
        self._check_col(parsed_file, 0, "name1", "Text", ["a1", "a2", "a3"])
        self._check_col(parsed_file, 1, "name2", "Int", [1, 2, 3])
        self._check_col(parsed_file, 2, "name3", "Text", ["c1", "c2", "c3"])
コード例 #2
0
    def test_option_use_col_name_as_header_no_headers(self):
        file_obj = bytes_io_from_str(
            textwrap.dedent("""\
      ,,,
      ,,,
      n1,2,n3
      a1,1,c1,d1
      a2,4,c2
      a3,5,c3
      """))

        parse_options = {"include_col_names_as_headers": False}
        parsed_file = import_csv._parse_open_file(
            file_obj, parse_options=parse_options)[1][0]
        self._check_num_cols(parsed_file, 4)
        self._check_col(parsed_file, 0, "", "Text", ["n1", "a1", "a2", "a3"])
        self._check_col(parsed_file, 1, "", "Int", [2, 1, 4, 5])
        self._check_col(parsed_file, 2, "", "Text", ["n3", "c1", "c2", "c3"])
        self._check_col(parsed_file, 3, "", "Text", ["", "d1", "", ""])

        parse_options = {"include_col_names_as_headers": True}
        parsed_file = import_csv._parse_open_file(
            file_obj, parse_options=parse_options)[1][0]
        self._check_num_cols(parsed_file, 4)
        self._check_col(parsed_file, 0, "n1", "Text", ["a1", "a2", "a3"])
        self._check_col(parsed_file, 1, "2", "Int", [1, 4, 5])
        self._check_col(parsed_file, 2, "n3", "Text", ["c1", "c2", "c3"])
        self._check_col(parsed_file, 3, "", "Text", ["d1", "", ""])
コード例 #3
0
    def test_option_num_rows(self):
        file_obj = bytes_io_from_str(
            textwrap.dedent("""\
      name1,name2,name3
      a1,b1,c1
      a2,b2,c2
      a3,b3,c3
      """))

        parse_options = {}
        parsed_file = import_csv._parse_open_file(
            file_obj, parse_options=parse_options)[1][0]
        self._check_num_cols(parsed_file, 3)
        self._check_col(parsed_file, 0, "name1", "Text", ['a1', 'a2', 'a3'])
        self._check_col(parsed_file, 1, "name2", "Text", ['b1', 'b2', 'b3'])
        self._check_col(parsed_file, 2, "name3", "Text", ['c1', 'c2', 'c3'])

        parse_options = {"NUM_ROWS": 2}
        parsed_file = import_csv._parse_open_file(
            file_obj, parse_options=parse_options)[1][0]
        self._check_num_cols(parsed_file, 3)
        self._check_col(parsed_file, 0, "name1", "Text", ["a1", "a2"])
        self._check_col(parsed_file, 1, "name2", "Text", ["b1", "b2"])
        self._check_col(parsed_file, 2, "name3", "Text", ["c1", "c2"])

        parse_options = {"NUM_ROWS": 10}
        parsed_file = import_csv._parse_open_file(
            file_obj, parse_options=parse_options)[1][0]
        self._check_num_cols(parsed_file, 3)
        self._check_col(parsed_file, 0, "name1", "Text", ['a1', 'a2', 'a3'])
        self._check_col(parsed_file, 1, "name2", "Text", ['b1', 'b2', 'b3'])
        self._check_col(parsed_file, 2, "name3", "Text", ['c1', 'c2', 'c3'])
コード例 #4
0
    def test_option_num_rows_no_header(self):
        file_obj = bytes_io_from_str(
            textwrap.dedent("""\
      ,,
      ,,
      a1,1,c1
      a2,2,c2
      a3,3,c3
      """))

        parse_options = {}
        parsed_file = import_csv._parse_open_file(
            file_obj, parse_options=parse_options)[1][0]
        self._check_num_cols(parsed_file, 3)
        self._check_col(parsed_file, 0, "", "Text", ['a1', 'a2', 'a3'])
        self._check_col(parsed_file, 1, "", "Int", [1, 2, 3])
        self._check_col(parsed_file, 2, "", "Text", ['c1', 'c2', 'c3'])

        parse_options = {"NUM_ROWS": 2}
        parsed_file = import_csv._parse_open_file(
            file_obj, parse_options=parse_options)[1][0]
        self._check_num_cols(parsed_file, 3)
        self._check_col(parsed_file, 0, "", "Text", ['a1', 'a2'])
        self._check_col(parsed_file, 1, "", "Int", [1, 2])
        self._check_col(parsed_file, 2, "", "Text", ['c1', 'c2'])
コード例 #5
0
    def test_guess_missing_user_option(self):
        file_obj = bytes_io_from_str(
            textwrap.dedent("""\
      name1,;name2,;name3
      a1,;b1,;c1
      a2,;b2,;c2
      a3,;b3,;c3
      """))
        parse_options = {
            "delimiter": ';',
            "escapechar": None,
            "lineterminator": '\r\n',
            "quotechar": '"',
            "quoting": csv.QUOTE_MINIMAL
        }

        parsed_file = import_csv._parse_open_file(
            file_obj, parse_options=parse_options)[1][0]
        self._check_num_cols(parsed_file, 3)
        self._check_col(parsed_file, 0, "name1,", "Text",
                        ["a1,", "a2,", "a3,"])
        self._check_col(parsed_file, 1, "name2,", "Text",
                        ["b1,", "b2,", "b3,"])
        self._check_col(parsed_file, 2, "name3", "Text", ["c1", "c2", "c3"])

        # Sniffer detects delimiters in order [',', '\t', ';', ' ', ':'],
        # so for this file_obj it will be ','
        parsed_file = import_csv._parse_open_file(file_obj,
                                                  parse_options={})[1][0]
        self._check_num_cols(parsed_file, 3)
        self._check_col(parsed_file, 0, "name1", "Text", ["a1", "a2", "a3"])
        self._check_col(parsed_file, 1, ";name2", "Text",
                        [";b1", ";b2", ";b3"])
        self._check_col(parsed_file, 2, ";name3", "Text",
                        [";c1", ";c2", ";c3"])
コード例 #6
0
    def test_empty_headers(self):
        file_obj = bytes_io_from_str(
            textwrap.dedent("""\
      ,,-,-
      b,a,a,a,a
      b,a,a,a,a
      b,a,a,a,a
      """))

        parsed_file = import_csv._parse_open_file(file_obj,
                                                  parse_options={})[1][0]
        self._check_num_cols(parsed_file, 5)
        self._check_col(parsed_file, 0, "", "Text", ["b", "b", "b"])
        self._check_col(parsed_file, 1, "", "Text", ["a", "a", "a"])
        self._check_col(parsed_file, 2, "-", "Text", ["a", "a", "a"])
        self._check_col(parsed_file, 3, "-", "Text", ["a", "a", "a"])
        self._check_col(parsed_file, 4, "", "Text", ["a", "a", "a"])

        file_obj = bytes_io_from_str(
            textwrap.dedent("""\
      -,-,-,-,-,-
      b,a,a,a,a
      b,a,a,a,a
      b,a,a,a,a
      """))

        parsed_file = import_csv._parse_open_file(file_obj,
                                                  parse_options={})[1][0]
        self._check_num_cols(parsed_file, 6)
        self._check_col(parsed_file, 0, "-", "Text", ["b", "b", "b"])
        self._check_col(parsed_file, 1, "-", "Text", ["a", "a", "a"])
        self._check_col(parsed_file, 2, "-", "Text", ["a", "a", "a"])
        self._check_col(parsed_file, 3, "-", "Text", ["a", "a", "a"])
        self._check_col(parsed_file, 4, "-", "Text", ["a", "a", "a"])
        self._check_col(parsed_file, 5, "-", "Text", ["", "", ""])
コード例 #7
0
    def test_one_line_file_with_header(self):
        file_obj = bytes_io_from_str(
            textwrap.dedent("""\
      name1,name2,name3
      """))

        parsed_file = import_csv._parse_open_file(file_obj,
                                                  parse_options={})[1][0]
        self._check_num_cols(parsed_file, 3)
        self._check_col(parsed_file, 0, "name1", "Text", [])
        self._check_col(parsed_file, 1, "name2", "Text", [])
        self._check_col(parsed_file, 2, "name3", "Text", [])
コード例 #8
0
    def test_offset_no_header(self):
        file_obj = bytes_io_from_str(
            textwrap.dedent("""\
      4,b1,c1
      4,b2,c2
      4,b3,c3
      """))

        parsed_file = import_csv._parse_open_file(file_obj,
                                                  parse_options={})[1][0]
        self._check_num_cols(parsed_file, 3)
        self._check_col(parsed_file, 0, "", "Int", [4, 4, 4])
        self._check_col(parsed_file, 1, "", "Text", ["b1", "b2", "b3"])
        self._check_col(parsed_file, 2, "", "Text", ["c1", "c2", "c3"])
コード例 #9
0
    def test_wrong_cols2(self):
        file_obj = bytes_io_from_str(
            textwrap.dedent("""\
      name1
      a1,b1
      a2,b2,c2
      """))

        parsed_file = import_csv._parse_open_file(file_obj,
                                                  parse_options={})[1][0]
        self._check_num_cols(parsed_file, 3)
        self._check_col(parsed_file, 0, "name1", "Text", ["a1", "a2"])
        self._check_col(parsed_file, 1, "", "Text", ["b1", "b2"])
        self._check_col(parsed_file, 2, "", "Text", ["", "c2"])
コード例 #10
0
    def test_offset(self):
        file_obj = bytes_io_from_str(
            textwrap.dedent("""\
      ,,,,,,,
      name1,name2,name3
      a1,b1,c1
      a2,b2,c2
      a3,b3,c3,d4
      """))

        parsed_file = import_csv._parse_open_file(file_obj,
                                                  parse_options={})[1][0]
        self._check_num_cols(parsed_file, 4)
        self._check_col(parsed_file, 0, "name1", "Text", ["a1", "a2", "a3"])
        self._check_col(parsed_file, 1, "name2", "Text", ["b1", "b2", "b3"])
        self._check_col(parsed_file, 2, "name3", "Text", ["c1", "c2", "c3"])
        self._check_col(parsed_file, 3, "", "Text", ["", "", "d4"])
コード例 #11
0
    def test_empty_file(self):
        file_obj = bytes_io_from_str(textwrap.dedent("""\
      """))

        parsed_file = import_csv._parse_open_file(file_obj, parse_options={})
        self.assertEqual(parsed_file, ({}, []))