()
| 2091 | |
| 2092 | |
| 2093 | def test_write_quoting_style(): |
| 2094 | t = pa.Table.from_arrays([[1, 2, None], ["a", None, "c"]], ["c1", "c2"]) |
| 2095 | buf = io.BytesIO() |
| 2096 | for write_options, res in [ |
| 2097 | (WriteOptions(quoting_style='none'), b'"c1","c2"\n1,a\n2,\n,c\n'), |
| 2098 | (WriteOptions(), b'"c1","c2"\n1,"a"\n2,\n,"c"\n'), |
| 2099 | (WriteOptions(quoting_style='all_valid'), |
| 2100 | b'"c1","c2"\n"1","a"\n"2",\n,"c"\n'), |
| 2101 | ]: |
| 2102 | with CSVWriter(buf, t.schema, write_options=write_options) as writer: |
| 2103 | writer.write_table(t) |
| 2104 | assert buf.getvalue() == res |
| 2105 | buf.seek(0) |
| 2106 | |
| 2107 | # Test writing special characters with different quoting styles |
| 2108 | t = pa.Table.from_arrays([[",", "\""]], ["c1"]) |
| 2109 | buf = io.BytesIO() |
| 2110 | for write_options, res in [ |
| 2111 | (WriteOptions(quoting_style='needed'), b'"c1"\n","\n""""\n'), |
| 2112 | (WriteOptions(quoting_style='none'), pa.lib.ArrowInvalid), |
| 2113 | ]: |
| 2114 | with CSVWriter(buf, t.schema, write_options=write_options) as writer: |
| 2115 | try: |
| 2116 | writer.write_table(t) |
| 2117 | except Exception as e: |
| 2118 | # This will trigger when we try to write a comma (,) |
| 2119 | # without quotes, which is invalid |
| 2120 | assert isinstance(e, res) |
| 2121 | break |
| 2122 | assert buf.getvalue() == res |
| 2123 | buf.seek(0) |
| 2124 | |
| 2125 | |
| 2126 | def test_write_quoting_header(): |
nothing calls this directly
no test coverage detected