File: test_csv_table.py

package info (click to toggle)
thuban 1.2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,752 kB
  • sloc: python: 30,427; ansic: 6,181; xml: 4,127; cpp: 1,595; makefile: 166
file content (78 lines) | stat: -rw-r--r-- 2,282 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Copyright (C) 2003 by Intevation GmbH
# Authors:
# Frank Koormann <frank.koormann@intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with the software for details.

"""
Test the CSV table export
"""

__version__ = "$Revision"
# $Source$
# $Id: test_csv_table.py 1398 2003-07-10 14:55:49Z jonathan $

import unittest

import support
support.initthuban()

from Thuban.Model.table import MemoryTable, \
     FIELDTYPE_DOUBLE, FIELDTYPE_INT, FIELDTYPE_STRING, \
     table_to_csv

class TestCSVTable(unittest.TestCase, support.FileTestMixin):

    def setUp(self):
        """Create a simple table and write to file."""
        self.table = MemoryTable([("type", FIELDTYPE_STRING),
                                  ("value", FIELDTYPE_DOUBLE),
                                  ("code", FIELDTYPE_INT)],
                                 [("UNKNOWN", 0.0, 0),
                                  ("Foo", 0.5, -1),
                                  ("Foo", 0.25, 100),
                                  ("bar", 1e10, 17)])

    def test_table_to_cvs(self):
        """Test table_to_csv()"""
        filename = self.temp_file_name("test_export_csv.csv")
        table_to_csv(self.table, filename)
        file = open(filename, "r")

        # Tile line
        line=file.readline()
        self.assertEquals(line,'#type,value,code\n')

        # Data lines
        line=file.readline()
        self.assertEquals(line,'UNKNOWN,0.0,0\n')

        line=file.readline()
        self.assertEquals(line,'Foo,0.5,-1\n')

        line=file.readline()
        self.assertEquals(line,'Foo,0.25,100\n')

        line=file.readline()
        self.assertEquals(line,'bar,10000000000.0,17\n')
        self.assertEquals(file.readline(),'')

        # save selected records
        table_to_csv(self.table, filename, [1, 3])
        file = open(filename, "r")

        # Tile line
        line=file.readline()
        self.assertEquals(line,'#type,value,code\n')

        # Data lines
        line=file.readline()
        self.assertEquals(line,'Foo,0.5,-1\n')

        line=file.readline()
        self.assertEquals(line,'bar,10000000000.0,17\n')
        self.assertEquals(file.readline(),'')
        
if __name__ == "__main__":
    support.run_tests()