Ejemplo n.º 1
0
    def test_init(self):
        new_datarange = DataRange(self.datarange.to_serializable())
        self.assertEqual(self.datarange.to_serializable(),
                         new_datarange.to_serializable())

        new_datarange = DataRange(self.datarange)
        self.assertEqual(self.datarange.to_serializable(),
                         new_datarange.to_serializable())
Ejemplo n.º 2
0
    def test_pickle(self):
        try:
            import dill as pickle
        except ImportError:
            pass
        else:
            dr = DataRange()
            p = pickle.dumps(dr)
            d = pickle.loads(p)
            self.assertEqual(type(d), DataRange)
            self.assertEqual(dr.to_serializable(), d.to_serializable())

            self.assertEqual(type(self.datarange), DataRange)
            p = pickle.dumps(self.datarange)
            d = pickle.loads(p)
            self.assertEqual(self.datarange, d)
Ejemplo n.º 3
0
class DataRangeTest(TestCase):
    def setUp(self):
        h = '', 'A', 'B', 'C', 'D'
        x = 'X', 1, 2, 3, 4
        y = 'Y', 4, 5, 6, 7
        z = 'Z', 7, 8, 9, 10
        self.datarange = DataRange([h, x, y, z])
        pass

    def test_init(self):
        new_datarange = DataRange(self.datarange.to_serializable())
        self.assertEqual(self.datarange.to_serializable(),
                         new_datarange.to_serializable())

        new_datarange = DataRange(self.datarange)
        self.assertEqual(self.datarange.to_serializable(),
                         new_datarange.to_serializable())

    def test_keys(self):
        self.assertEqual(self.datarange.col_keys(), ['A', 'B', 'C', 'D'])
        self.assertEqual(self.datarange.row_keys(), ['X', 'Y', 'Z'])

    def test_get(self):
        self.assertEqual(self.datarange.get(('X', 'A')), 1)
        self.assertEqual(self.datarange.row('X'), [1, 2, 3, 4])
        self.assertEqual(self.datarange.col('D'), [4, 7, 10])
        # self.assertEqual(self.datarange[0][-1], 4)
        # self.assertEqual(self.datarange[0, -1], 4)
        # self.assertEqual(self.datarange['X']['C'], 3)
        self.assertEqual(self.datarange['X', 'C'], 3)

    def test_slice(self):
        self.assertEqual(self.datarange[0:2], self.datarange['X':'Y'])
        self.assertEqual(self.datarange[0:1], [[1, 2, 3, 4]])
        self.assertEqual(self.datarange[(0, 1):(2, 3)], [[2, 3], [5, 6]])
        # self.assertEqual([l[1:3] for l in self.datarange[0:2]], [[2, 3], [5, 6]])
        # self.assertEqual([l[1:3] for l in self.datarange['X':'Y']], [[2, 3], [5, 6]])
        # self.assertEqual([l['B':'C'] for l in self.datarange['X':'Y']], [[2, 3], [5, 6]])
        self.assertEqual(self.datarange[('X', 'B'):('Y', 'C')],
                         [[2, 3], [5, 6]])
        self.assertEqual(self.datarange[(None, 'B'):('Y', 'C')],
                         [[2, 3], [5, 6]])
        self.assertEqual(self.datarange[(None, 'B'):('Y', None)],
                         [[2, 3, 4], [5, 6, 7]])

    def test_flatten(self):
        self.assertEqual(DataRange(self.datarange.to_serializable()),
                         self.datarange)
        s = "DataRange([[None, 'A', 'B', 'C', 'D'], ['X', 1, 2, 3, 4], ['Y', 4, 5, 6, 7], ['Z', 7, 8, 9, 10]])"
        self.assertEqual(str(self.datarange), s)

    def test_set(self):
        # self.datarange['X']['C'] = 4
        # self.assertEqual(self.datarange['X']['C'], 4)
        self.datarange['X', 'C'] = 3
        self.assertTrue('U' not in self.datarange.row_keys())
        self.datarange['U', 'C'] = 4
        self.assertTrue('U' in self.datarange.row_keys())

    def test_append(self):
        self.datarange.append('W', list(range(4)))
        l = lambda: self.datarange.row_append('W', list(range(4)))
        self.assertRaises(KeyError, l)
        self.assertEqual(len(self.datarange), 4)
        self.assertEqual(self.datarange.row_keys()[-1], 'W')
        self.assertEqual(self.datarange.row('W'), list(range(4)))

        self.datarange.row_append('U', list(range(2, 6)))
        self.assertEqual(self.datarange.row('U'), list(range(2, 6)))

        self.datarange.col_append('T', list(range(5)))
        self.assertEqual(self.datarange.col('T'), list(range(5)))

    def test_copy(self):
        self.assertEqual(self.datarange, copy(self.datarange))
        self.assertEqual(type(self.datarange), type(copy(self.datarange)))
        self.assertEqual(self.datarange, deepcopy(self.datarange))
        self.assertEqual(type(self.datarange), type(deepcopy(self.datarange)))

    def test_transpose(self):
        # self.assertEqual(type(list(self.datarange)), list)
        l = [self.datarange.row(r) for r in self.datarange.row_keys()]
        self.assertEqual(self.datarange.item_list, l)
        self.assertEqual(type(self.datarange),
                         type(self.datarange.transpose()))

    def test_pickle(self):
        try:
            import dill as pickle
        except ImportError:
            pass
        else:
            dr = DataRange()
            p = pickle.dumps(dr)
            d = pickle.loads(p)
            self.assertEqual(type(d), DataRange)
            self.assertEqual(dr.to_serializable(), d.to_serializable())

            self.assertEqual(type(self.datarange), DataRange)
            p = pickle.dumps(self.datarange)
            d = pickle.loads(p)
            self.assertEqual(self.datarange, d)

    def test_json(self):
        for i in [None, 0, 1, 2]:
            unicum_json = dumps(self.datarange,
                                cls=UnicumJSONEncoder,
                                indent=i)
            standard_json = dumps(self.datarange.to_serializable(), indent=i)
            unicum_json_2 = dumps(DataRange(loads(standard_json)),
                                  cls=UnicumJSONEncoder,
                                  indent=i)
            standard_json_2 = dumps(DataRange(
                loads(unicum_json)).to_serializable(),
                                    indent=i)
            self.assertEqual(unicum_json_2, unicum_json_2)
            self.assertEqual(standard_json, standard_json_2)