()
| 2021 | |
| 2022 | |
| 2023 | def test_write_quoting_style(): |
| 2024 | t = pa.Table.from_arrays([[1, 2, None], ["a", None, "c"]], ["c1", "c2"]) |
| 2025 | buf = io.BytesIO() |
| 2026 | for write_options, res in [ |
| 2027 | (WriteOptions(quoting_style='none'), b'"c1","c2"\n1,a\n2,\n,c\n'), |
| 2028 | (WriteOptions(), b'"c1","c2"\n1,"a"\n2,\n,"c"\n'), |
| 2029 | (WriteOptions(quoting_style='all_valid'), |
| 2030 | b'"c1","c2"\n"1","a"\n"2",\n,"c"\n'), |
| 2031 | ]: |
| 2032 | with CSVWriter(buf, t.schema, write_options=write_options) as writer: |
| 2033 | writer.write_table(t) |
| 2034 | assert buf.getvalue() == res |
| 2035 | buf.seek(0) |
| 2036 | |
| 2037 | # Test writing special characters with different quoting styles |
| 2038 | t = pa.Table.from_arrays([[",", "\""]], ["c1"]) |
| 2039 | buf = io.BytesIO() |
| 2040 | for write_options, res in [ |
| 2041 | (WriteOptions(quoting_style='needed'), b'"c1"\n","\n""""\n'), |
| 2042 | (WriteOptions(quoting_style='none'), pa.lib.ArrowInvalid), |
| 2043 | ]: |
| 2044 | with CSVWriter(buf, t.schema, write_options=write_options) as writer: |
| 2045 | try: |
| 2046 | writer.write_table(t) |
| 2047 | except Exception as e: |
| 2048 | # This will trigger when we try to write a comma (,) |
| 2049 | # without quotes, which is invalid |
| 2050 | assert isinstance(e, res) |
| 2051 | break |
| 2052 | assert buf.getvalue() == res |
| 2053 | buf.seek(0) |
| 2054 | |
| 2055 | |
| 2056 | def test_write_quoting_header(): |
nothing calls this directly
no test coverage detected