Back to index

python3.2  3.2.2
Public Member Functions
test.test_csv.TestDictFields Class Reference

List of all members.

Public Member Functions

def test_write_simple_dict
 "long" means the row is longer than the number of fieldnames "short" means there are fewer elements in the row than fieldnames
def test_write_no_fields
def test_read_dict_fields
def test_read_dict_no_fieldnames
def test_read_dict_fieldnames_from_file
def test_read_dict_fieldnames_chain
def test_read_long
def test_read_long_with_rest
def test_read_long_with_rest_no_fieldnames
def test_read_short
def test_read_multi
def test_read_with_blanks
def test_read_semi_sep

Detailed Description

Definition at line 535 of file test_csv.py.


Member Function Documentation

Definition at line 580 of file test_csv.py.

00580 
00581     def test_read_dict_fieldnames_chain(self):
00582         import itertools
00583         with TemporaryFile("w+") as fileobj:
00584             fileobj.write("f1,f2,f3\r\n1,2,abc\r\n")
00585             fileobj.seek(0)
00586             reader = csv.DictReader(fileobj)
00587             first = next(reader)
00588             for row in itertools.chain([first], reader):
00589                 self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"])
00590                 self.assertEqual(row, {"f1": '1', "f2": '2', "f3": 'abc'})

Here is the call graph for this function:

Definition at line 571 of file test_csv.py.

00571 
00572     def test_read_dict_fieldnames_from_file(self):
00573         with TemporaryFile("w+") as fileobj:
00574             fileobj.write("f1,f2,f3\r\n1,2,abc\r\n")
00575             fileobj.seek(0)
00576             reader = csv.DictReader(fileobj,
00577                                     fieldnames=next(csv.reader(fileobj)))
00578             self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"])
00579             self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'})

Here is the call graph for this function:

Definition at line 553 of file test_csv.py.

00553 
00554     def test_read_dict_fields(self):
00555         with TemporaryFile("w+") as fileobj:
00556             fileobj.write("1,2,abc\r\n")
00557             fileobj.seek(0)
00558             reader = csv.DictReader(fileobj,
00559                                     fieldnames=["f1", "f2", "f3"])
00560             self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'})

Here is the call graph for this function:

Definition at line 561 of file test_csv.py.

00561 
00562     def test_read_dict_no_fieldnames(self):
00563         with TemporaryFile("w+") as fileobj:
00564             fileobj.write("f1,f2,f3\r\n1,2,abc\r\n")
00565             fileobj.seek(0)
00566             reader = csv.DictReader(fileobj)
00567             self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'})
00568             self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"])

Here is the call graph for this function:

Definition at line 591 of file test_csv.py.

00591 
00592     def test_read_long(self):
00593         with TemporaryFile("w+") as fileobj:
00594             fileobj.write("1,2,abc,4,5,6\r\n")
00595             fileobj.seek(0)
00596             reader = csv.DictReader(fileobj,
00597                                     fieldnames=["f1", "f2"])
00598             self.assertEqual(next(reader), {"f1": '1', "f2": '2',
00599                                              None: ["abc", "4", "5", "6"]})

Here is the call graph for this function:

Definition at line 600 of file test_csv.py.

00600 
00601     def test_read_long_with_rest(self):
00602         with TemporaryFile("w+") as fileobj:
00603             fileobj.write("1,2,abc,4,5,6\r\n")
00604             fileobj.seek(0)
00605             reader = csv.DictReader(fileobj,
00606                                     fieldnames=["f1", "f2"], restkey="_rest")
00607             self.assertEqual(next(reader), {"f1": '1', "f2": '2',
00608                                              "_rest": ["abc", "4", "5", "6"]})

Here is the call graph for this function:

Definition at line 609 of file test_csv.py.

00609 
00610     def test_read_long_with_rest_no_fieldnames(self):
00611         with TemporaryFile("w+") as fileobj:
00612             fileobj.write("f1,f2\r\n1,2,abc,4,5,6\r\n")
00613             fileobj.seek(0)
00614             reader = csv.DictReader(fileobj, restkey="_rest")
00615             self.assertEqual(reader.fieldnames, ["f1", "f2"])
00616             self.assertEqual(next(reader), {"f1": '1', "f2": '2',
00617                                              "_rest": ["abc", "4", "5", "6"]})

Here is the call graph for this function:

Definition at line 631 of file test_csv.py.

00631 
00632     def test_read_multi(self):
00633         sample = [
00634             '2147483648,43.0e12,17,abc,def\r\n',
00635             '147483648,43.0e2,17,abc,def\r\n',
00636             '47483648,43.0,170,abc,def\r\n'
00637             ]
00638 
00639         reader = csv.DictReader(sample,
00640                                 fieldnames="i1 float i2 s1 s2".split())
00641         self.assertEqual(next(reader), {"i1": '2147483648',
00642                                          "float": '43.0e12',
00643                                          "i2": '17',
00644                                          "s1": 'abc',
00645                                          "s2": 'def'})

Here is the call graph for this function:

Definition at line 655 of file test_csv.py.

00655 
00656     def test_read_semi_sep(self):
00657         reader = csv.DictReader(["1;2;abc;4;5;6\r\n"],
00658                                 fieldnames="1 2 3 4 5 6".split(),
00659                                 delimiter=';')
00660         self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc',
00661                                          "4": '4', "5": '5', "6": '6'})

Here is the call graph for this function:

Definition at line 618 of file test_csv.py.

00618 
00619     def test_read_short(self):
00620         with TemporaryFile("w+") as fileobj:
00621             fileobj.write("1,2,abc,4,5,6\r\n1,2,abc\r\n")
00622             fileobj.seek(0)
00623             reader = csv.DictReader(fileobj,
00624                                     fieldnames="1 2 3 4 5 6".split(),
00625                                     restval="DEFAULT")
00626             self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc',
00627                                              "4": '4', "5": '5', "6": '6'})
00628             self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc',
00629                                              "4": 'DEFAULT', "5": 'DEFAULT',
00630                                              "6": 'DEFAULT'})

Here is the call graph for this function:

Definition at line 646 of file test_csv.py.

00646 
00647     def test_read_with_blanks(self):
00648         reader = csv.DictReader(["1,2,abc,4,5,6\r\n","\r\n",
00649                                  "1,2,abc,4,5,6\r\n"],
00650                                 fieldnames="1 2 3 4 5 6".split())
00651         self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc',
00652                                          "4": '4', "5": '5', "6": '6'})
00653         self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc',
00654                                          "4": '4', "5": '5', "6": '6'})

Here is the call graph for this function:

Definition at line 549 of file test_csv.py.

00549 
00550     def test_write_no_fields(self):
00551         fileobj = StringIO()
00552         self.assertRaises(TypeError, csv.DictWriter, fileobj)

Here is the call graph for this function:

"long" means the row is longer than the number of fieldnames "short" means there are fewer elements in the row than fieldnames

Definition at line 538 of file test_csv.py.

00538 
00539     def test_write_simple_dict(self):
00540         with TemporaryFile("w+", newline='') as fileobj:
00541             writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"])
00542             writer.writeheader()
00543             fileobj.seek(0)
00544             self.assertEqual(fileobj.readline(), "f1,f2,f3\r\n")
00545             writer.writerow({"f1": 10, "f3": "abc"})
00546             fileobj.seek(0)
00547             fileobj.readline() # header
00548             self.assertEqual(fileobj.read(), "10,,abc\r\n")

Here is the call graph for this function:


The documentation for this class was generated from the following file: