|
@@ 317-333 (lines=17) @@
|
| 314 |
|
""" |
| 315 |
|
self.assertEqual(s, r) |
| 316 |
|
|
| 317 |
|
def test_to_csv_with_custom_key_valid(self): |
| 318 |
|
d = IODict({ |
| 319 |
|
'results': [ |
| 320 |
|
{ 'id':'1', 'name':'Alice', 'age':'20', 'height':'62', 'weight':'120.6', }, |
| 321 |
|
{ 'id':'2', 'name':'Freddie', 'age':'21', 'height':'74', 'weight':'190.6', }, |
| 322 |
|
{ 'id':'3', 'name':'Bob', 'age':'17', 'height':'68', 'weight':'120.0', }, |
| 323 |
|
{ 'id':'4', 'name':'François', 'age':'32', 'height':'75', 'weight':'110.05', }, |
| 324 |
|
], |
| 325 |
|
}) |
| 326 |
|
s = d.to_csv('results', columns=['id', 'name', 'age', 'height', 'weight']) |
| 327 |
|
r = """id,name,age,height,weight |
| 328 |
|
1,Alice,20,62,120.6 |
| 329 |
|
2,Freddie,21,74,190.6 |
| 330 |
|
3,Bob,17,68,120.0 |
| 331 |
|
4,François,32,75,110.05 |
| 332 |
|
""" |
| 333 |
|
self.assertEqual(s, r) |
| 334 |
|
|
| 335 |
|
def test_to_csv_with_custom_key_invalid(self): |
| 336 |
|
d = IODict({ |
|
@@ 281-297 (lines=17) @@
|
| 278 |
|
""" |
| 279 |
|
self.assertEqual(s, r) |
| 280 |
|
|
| 281 |
|
def test_to_csv_with_custom_columns(self): |
| 282 |
|
d = IODict({ |
| 283 |
|
'values': [ |
| 284 |
|
{ 'id':'1', 'name':'Alice', 'age':'20', 'height':'62', 'weight':'120.6', }, |
| 285 |
|
{ 'id':'2', 'name':'Freddie', 'age':'21', 'height':'74', 'weight':'190.6', }, |
| 286 |
|
{ 'id':'3', 'name':'Bob', 'age':'17', 'height':'68', 'weight':'120.0', }, |
| 287 |
|
{ 'id':'4', 'name':'François', 'age':'32', 'height':'75', 'weight':'110.05', }, |
| 288 |
|
], |
| 289 |
|
}) |
| 290 |
|
s = d.to_csv(key='values', columns=['id', 'name', 'family_name', 'age', 'height', 'gender', 'weight']) |
| 291 |
|
r = """id,name,family_name,age,height,gender,weight |
| 292 |
|
1,Alice,,20,62,,120.6 |
| 293 |
|
2,Freddie,,21,74,,190.6 |
| 294 |
|
3,Bob,,17,68,,120.0 |
| 295 |
|
4,François,,32,75,,110.05 |
| 296 |
|
""" |
| 297 |
|
self.assertEqual(s, r) |
| 298 |
|
|
| 299 |
|
def test_to_csv_with_custom_delimiter_and_quotes(self): |
| 300 |
|
d = IODict({ |
|
@@ 263-279 (lines=17) @@
|
| 260 |
|
with self.assertRaises(ValueError): |
| 261 |
|
IODict(url, format='csv') |
| 262 |
|
|
| 263 |
|
def test_to_csv(self): |
| 264 |
|
d = IODict({ |
| 265 |
|
'values': [ |
| 266 |
|
{ 'id':'1', 'name':'Alice', 'age':'20', 'height':'62', 'weight':'120.6', }, |
| 267 |
|
{ 'id':'2', 'name':'Freddie', 'age':'21', 'height':'74', 'weight':'190.6', }, |
| 268 |
|
{ 'id':'3', 'name':'Bob', 'age':'17', 'height':'68', 'weight':'120.0', }, |
| 269 |
|
{ 'id':'4', 'name':'François', 'age':'32', 'height':'75', 'weight':'110.05', }, |
| 270 |
|
], |
| 271 |
|
}) |
| 272 |
|
s = d.to_csv() |
| 273 |
|
r = """age,height,id,name,weight |
| 274 |
|
20,62,1,Alice,120.6 |
| 275 |
|
21,74,2,Freddie,190.6 |
| 276 |
|
17,68,3,Bob,120.0 |
| 277 |
|
32,75,4,François,110.05 |
| 278 |
|
""" |
| 279 |
|
self.assertEqual(s, r) |
| 280 |
|
|
| 281 |
|
def test_to_csv_with_custom_columns(self): |
| 282 |
|
d = IODict({ |