Пример #1
0
 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
Пример #2
0
 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)
Пример #3
0
    def test_attrlist(self):
        names = ['obj' + str(i) for i in range(10)]
        l = [VisibleObject(n) for n in names]
        self.assertTrue(VisibleAttributeList(l))

        l = [object() for n in names]
        self.assertRaises(TypeError, VisibleAttributeList, l)

        l = [
            MyVO(n).modify_object('DataRangeProp', DataRange()) for n in names
        ]
        self.assertRaises(TypeError, VisibleAttributeList, l)

        # accept Attributes with more than depth 2 -> adding list to value typese
        t = int, float, str, type(None), VisibleObject, list
        a = VisibleAttributeList([o.to_serializable() for o in l],
                                 value_types=t)
        for r in a:
            self.assertTrue(isinstance(r, VisibleObject))
        for aa in a:
            aa.register()
        self.assertEqual(a[0], VisibleObject(a[0].to_serializable(1)))

        for r in a.to_serializable():
            for c in r:
                self.assertTrue(dumps(c))  # JSON to_serializable
                self.assertTrue(
                    isinstance(c, (int, float, str, type(None), list)))

        obj = MyVO('My')
        obj.modify_object('ObjListProp', l)
        obj.modify_object('AttrListProp', a)
        self.assertTrue(obj.to_json())
Пример #4
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)
Пример #5
0
 def __init__(self, *args):
     super(MyVO, self).__init__(*args)
     self._none_prop_ = None
     self._str_prop_ = str('my str')
     self._int_prop_ = int(100)
     self._flt_prop_ = float(99.01)
     self._obj_prop_ = VisibleObject('YourVisibleObject')
     self._obj_list_prop_ = ObjectList()
     self._attr_list_prop_ = AttributeList()
     self._data_range_prop_ = DataRange()
Пример #6
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())
Пример #7
0
    def test_json(self):
        objs = [1, 2, 3], [1e-1, 1e-1, 1e1,
                           1e3], 0.12345, 99, 'abc', None, int(12345), {
                               'A': 3,
                               3: 4,
                               'a': 'B'
                           }
        for i in [None, 0, 1, 2]:
            for o in objs:
                self.assertEqual(
                    UnicumJSONEncoder(indent=i).encode(o),
                    JSONEncoder(indent=i).encode(o))
                self.assertEqual(
                    UnicumJSONEncoder(indent=i).encode(o),
                    JSONEncoder(indent=i).encode(o))

        rng = DataRange([[' ', 'X'], [1, 2], [0, 1e10]])
        obj = MyVO().modify_object('DataRangeProp', rng)
        unicum_json = dumps(obj,
                            cls=UnicumJSONEncoder,
                            indent=2,
                            key_order=(
                                "Module",
                                "Name",
                                "Class",
                            ))
        self.assertTrue(unicum_json.find("Module") < unicum_json.find("Name"))
        self.assertTrue(unicum_json.find("Name") < unicum_json.find("Class"))
        self.assertEqual(len(unicum_json.split("\n")),
                         2 + len(obj.to_serializable()) + len(rng) + 2)

        for i in [None, 0, 1, 2]:
            unicum_json = dumps(obj, cls=UnicumJSONEncoder, indent=i)
            standard_json = dumps(obj.to_serializable(), indent=i)
            unicum_json_2 = dumps(MyVO.from_json(standard_json),
                                  cls=UnicumJSONEncoder,
                                  indent=i)
            standard_json_2 = dumps(
                MyVO.from_json(unicum_json).to_serializable(), indent=i)
            self.assertEqual(unicum_json_2, unicum_json_2)
            self.assertEqual(standard_json, standard_json_2)
Пример #8
0
 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)
Пример #9
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)