Skip to content

Commit 964750c

Browse files
committed
TST: added string generator for to_csv testing
* related issue: pandas-dev#20353
1 parent c5c5c11 commit 964750c

File tree

3 files changed

+154
-112
lines changed

3 files changed

+154
-112
lines changed

pandas/tests/frame/test_to_csv.py

+60-49
Original file line numberDiff line numberDiff line change
@@ -842,11 +842,11 @@ def test_to_csv_unicodewriter_quoting(self):
842842
encoding='utf-8')
843843

844844
result = buf.getvalue()
845-
expected = ('"A","B"' + os.linesep +
846-
'1,"foo"' + os.linesep +
847-
'2,"bar"' + os.linesep +
848-
'3,"baz"' + os.linesep)
849-
845+
expected_rows = ['"A","B"',
846+
'1,"foo"',
847+
'2,"bar"',
848+
'3,"baz"']
849+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
850850
assert result == expected
851851

852852
def test_to_csv_quote_none(self):
@@ -857,9 +857,10 @@ def test_to_csv_quote_none(self):
857857
df.to_csv(buf, quoting=csv.QUOTE_NONE,
858858
encoding=encoding, index=False)
859859
result = buf.getvalue()
860-
expected = ('A' + os.linesep +
861-
'hello' + os.linesep +
862-
'{"hello"}' + os.linesep)
860+
expected_rows = ['A',
861+
'hello',
862+
'{"hello"}']
863+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
863864
assert result == expected
864865

865866
def test_to_csv_index_no_leading_comma(self):
@@ -868,10 +869,11 @@ def test_to_csv_index_no_leading_comma(self):
868869

869870
buf = StringIO()
870871
df.to_csv(buf, index_label=False)
871-
expected = ('A,B' + os.linesep +
872-
'one,1,4' + os.linesep +
873-
'two,2,5' + os.linesep +
874-
'three,3,6' + os.linesep)
872+
expected_rows = ['A,B',
873+
'one,1,4',
874+
'two,2,5',
875+
'three,3,6']
876+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
875877
assert buf.getvalue() == expected
876878

877879
def test_to_csv_line_terminators(self):
@@ -1074,9 +1076,10 @@ def test_to_csv_quoting(self):
10741076
'c_string': ['a', 'b,c'],
10751077
})
10761078

1077-
expected = (',c_bool,c_float,c_int,c_string' + os.linesep +
1078-
'0,True,1.0,42.0,a' + os.linesep +
1079-
'1,False,3.2,,"b,c"' + os.linesep)
1079+
expected_rows = [',c_bool,c_float,c_int,c_string',
1080+
'0,True,1.0,42.0,a',
1081+
'1,False,3.2,,"b,c"']
1082+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
10801083
result = df.to_csv()
10811084
assert result == expected
10821085

@@ -1086,19 +1089,19 @@ def test_to_csv_quoting(self):
10861089
result = df.to_csv(quoting=csv.QUOTE_MINIMAL)
10871090
assert result == expected
10881091

1089-
expected = ('"","c_bool","c_float","c_int","c_string"' + os.linesep +
1090-
'"0","True","1.0","42.0","a"' + os.linesep +
1091-
'"1","False","3.2","","b,c"' + os.linesep)
1092-
1092+
expected_rows = ['"","c_bool","c_float","c_int","c_string"',
1093+
'"0","True","1.0","42.0","a"',
1094+
'"1","False","3.2","","b,c"']
1095+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
10931096
result = df.to_csv(quoting=csv.QUOTE_ALL)
10941097
assert result == expected
10951098

10961099
# see gh-12922, gh-13259: make sure changes to
10971100
# the formatters do not break this behaviour
1098-
expected = ('"","c_bool","c_float","c_int","c_string"' + os.linesep +
1099-
'0,True,1.0,42.0,"a"' + os.linesep +
1100-
'1,False,3.2,"","b,c"' + os.linesep)
1101-
1101+
expected_rows = ['"","c_bool","c_float","c_int","c_string"',
1102+
'0,True,1.0,42.0,"a"',
1103+
'1,False,3.2,"","b,c"']
1104+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
11021105
result = df.to_csv(quoting=csv.QUOTE_NONNUMERIC)
11031106
assert result == expected
11041107

@@ -1109,24 +1112,27 @@ def test_to_csv_quoting(self):
11091112
quoting=csv.QUOTE_NONE,
11101113
escapechar=None)
11111114

1112-
expected = (',c_bool,c_float,c_int,c_string' + os.linesep +
1113-
'0,True,1.0,42.0,a' + os.linesep +
1114-
'1,False,3.2,,b!,c' + os.linesep)
1115+
expected_rows = [',c_bool,c_float,c_int,c_string',
1116+
'0,True,1.0,42.0,a',
1117+
'1,False,3.2,,b!,c']
1118+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
11151119
result = df.to_csv(quoting=csv.QUOTE_NONE,
11161120
escapechar='!')
11171121
assert result == expected
11181122

1119-
expected = (',c_bool,c_ffloat,c_int,c_string' + os.linesep +
1120-
'0,True,1.0,42.0,a' + os.linesep +
1121-
'1,False,3.2,,bf,c' + os.linesep)
1123+
expected_rows = [',c_bool,c_ffloat,c_int,c_string',
1124+
'0,True,1.0,42.0,a',
1125+
'1,False,3.2,,bf,c']
1126+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
11221127
result = df.to_csv(quoting=csv.QUOTE_NONE,
11231128
escapechar='f')
11241129
assert result == expected
11251130

11261131
# see gh-3503: quoting Windows line terminators
11271132
# presents with encoding?
1128-
text = ('a,b,c' + os.linesep +
1129-
'1,"test \r\n",3' + os.linesep)
1133+
text_rows = ['a,b,c',
1134+
'1,"test \r\n",3']
1135+
text = tm.convert_rows_list_to_csv_str(text_rows)
11301136
df = pd.read_csv(StringIO(text))
11311137
buf = StringIO()
11321138
df.to_csv(buf, encoding='utf-8', index=False)
@@ -1136,9 +1142,10 @@ def test_to_csv_quoting(self):
11361142
# with multi-indexes
11371143
df = pd.DataFrame({'a': [1, 2], 'b': [3, 4], 'c': [5, 6]})
11381144
df = df.set_index(['a', 'b'])
1139-
expected = ('"a","b","c"' + os.linesep +
1140-
'"1","3","5"' + os.linesep +
1141-
'"2","4","6"' + os.linesep)
1145+
expected_rows = ['"a","b","c"',
1146+
'"1","3","5"',
1147+
'"2","4","6"']
1148+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
11421149
assert df.to_csv(quoting=csv.QUOTE_ALL) == expected
11431150

11441151
def test_period_index_date_overflow(self):
@@ -1150,19 +1157,21 @@ def test_period_index_date_overflow(self):
11501157
df = pd.DataFrame([4, 5, 6], index=index)
11511158
result = df.to_csv()
11521159

1153-
expected = (',0' + os.linesep +
1154-
'1990-01-01,4' + os.linesep +
1155-
'2000-01-01,5' + os.linesep +
1156-
'3005-01-01,6' + os.linesep)
1160+
expected_rows = [',0',
1161+
'1990-01-01,4',
1162+
'2000-01-01,5',
1163+
'3005-01-01,6']
1164+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
11571165
assert result == expected
11581166

11591167
date_format = "%m-%d-%Y"
11601168
result = df.to_csv(date_format=date_format)
11611169

1162-
expected = (',0' + os.linesep +
1163-
'01-01-1990,4' + os.linesep +
1164-
'01-01-2000,5' + os.linesep +
1165-
'01-01-3005,6' + os.linesep)
1170+
expected_rows = [',0',
1171+
'01-01-1990,4',
1172+
'01-01-2000,5',
1173+
'01-01-3005,6']
1174+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
11661175
assert result == expected
11671176

11681177
# Overflow with pd.NaT
@@ -1172,10 +1181,11 @@ def test_period_index_date_overflow(self):
11721181
df = pd.DataFrame([4, 5, 6], index=index)
11731182
result = df.to_csv()
11741183

1175-
expected = (',0' + os.linesep +
1176-
'1990-01-01,4' + os.linesep +
1177-
',5' + os.linesep +
1178-
'3005-01-01,6' + os.linesep)
1184+
expected_rows = [',0',
1185+
'1990-01-01,4',
1186+
',5',
1187+
'3005-01-01,6']
1188+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
11791189
assert result == expected
11801190

11811191
def test_multi_index_header(self):
@@ -1188,7 +1198,8 @@ def test_multi_index_header(self):
11881198
header = ["a", "b", "c", "d"]
11891199
result = df.to_csv(header=header)
11901200

1191-
expected = (',a,b,c,d' + os.linesep +
1192-
'0,1,2,3,4' + os.linesep +
1193-
'1,5,6,7,8' + os.linesep)
1201+
expected_rows = [',a,b,c,d',
1202+
'0,1,2,3,4',
1203+
'1,5,6,7,8']
1204+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
11941205
assert result == expected

0 commit comments

Comments
 (0)