source: orange/Orange/testing/unit/tests/test_table.py @ 10278:f3b1ffae9c29

Revision 10278:f3b1ffae9c29, 2.5 KB checked in by Miha Stajdohar <miha.stajdohar@…>, 2 years ago (diff)

Unittest2 for python 2.6.

Line 
1""" Orange.data.Table related unit-tests
2"""
3try:
4    import unittest2 as unittest
5except:
6    import unittest
7from Orange.misc import testing
8import Orange
9import cPickle
10
11def native(table):
12    table = table.native()
13    for i in range(len(table)):
14        table[i] = [v.native() for v in table[i].native()]
15    return table
16
17def names_iter():
18    for name in testing.ALL_DATASETS:
19        yield name.replace(" ", "_").replace("-", "_"), (name,)
20
21@testing.data_driven(data_iter=names_iter())
22class TestLoading(unittest.TestCase):
23
24    @testing.test_on_data
25    def test_load_on(self, name):
26        """ Test the loading of the data set
27        """
28        table = Orange.data.Table(name)
29        self.assertIsNotNone(getattr(table, "attributeLoadStatus"), "No attributeLoadStatus")
30
31    @testing.test_on_data
32    def test_pickling_on(self, name):
33        """ Test data table pickling.
34        """
35        table = Orange.data.Table(name)
36        s = cPickle.dumps(table)
37        table_clone = cPickle.loads(s)
38#        self.assertEqual(table.domain, table_clone.domain)
39#        self.assertEqual(table.domain.class_var, table_clone.domain.class_var)
40        self.assertEqual(native(table), native(table_clone), "Native representation is not equal!")
41
42
43import tempfile
44
45@testing.datasets_driven
46class TestSaving(unittest.TestCase):
47    @testing.test_on_data
48    def test_R_on(self, name):
49        data = Orange.data.Table(name)
50        with tempfile.NamedTemporaryFile(suffix=".R") as f:
51            data.save(f.name)
52
53#    @testing.test_on_data
54#    def test_toC50(self, name):
55#        data = Orange.data.Table(name)
56
57    @testing.test_on_datasets(datasets=testing.CLASSIFICATION_DATASETS + \
58                              testing.REGRESSION_DATASETS)
59    def test_arff_on(self, data):
60        with tempfile.NamedTemporaryFile(suffix=".arff") as f:
61            data.save(f.name)
62            f.flush()
63            data_arff = Orange.data.Table(f.name)
64    @testing.test_on_datasets(datasets=testing.CLASSIFICATION_DATASETS + \
65                              testing.REGRESSION_DATASETS)
66    def test_svm_on(self, data):
67        with tempfile.NamedTemporaryFile(suffix=".svm") as f:
68            data.save(f.name)
69            f.flush()
70            data_svm = Orange.data.Table(f.name)
71
72    @testing.test_on_datasets
73    def test_csv_on(self, data):
74        with tempfile.NamedTemporaryFile(suffix=".csv") as f:
75            Orange.data.io.save_csv(f, data, dialect="excel-tab")
76            f.flush()
77            f.seek(0)
78            Orange.data.io.load_csv(f)
79
80
81
82
83if __name__ == "__main__":
84    unittest.main()
Note: See TracBrowser for help on using the repository browser.