def test_expand_nested(self): s = Store() key_a = s.new('a', components=['x', 'y']) dct = {'dependent': {'another_level': {key_a: [0, 1]}}} out = variable.expand(dct, s) expected = {'dependent': {'another_level': {'a - x': 0, 'a - y': 1}}} self.assertDictEqual(out, expected)
def test_expand_list_of_dicts(self): s = Store() key_a = s.new('a', components=['x', 'y']) dct = [{'index': 0, 'dependent': {key_a: [0, 1]}}] out = variable.expand(dct, s) expected = [{'index': 0, 'dependent': {'a - x': 0, 'a - y': 1}}] self.assertListEqual(out, expected)
def test_suffixed(self): s = Store() v1 = s.add('test') v2 = s.suffixed(v1, ' suffix') self.assertEqual(len(s), 2) test2 = s['test suffix'] self.assertTrue(v2 is test2)
def test_expand_basic(self): s = Store() key_a = s.new('a', components=['x', 'y']) dct = {key_a: [0, 1]} out = variable.expand(dct, s) expected = {'a - x': 0, 'a - y': 1} self.assertDictEqual(out, expected)
def test_deserialise_vars(self): lst = ['name=a;doc=docstring_1;unit=mm;components=None;sep= - ;category=None;tags=None;identifier=None', 'name=b;doc=docstring_2;unit=m/s;components=None;sep= - ;category=None;tags=None;identifier=None'] res = persist.deserialise_vars(lst) store = Store() v1 = store.add('a', 'docstring_1', 'mm') v2 = store.add('b', 'docstring_2', 'm/s') self.assertDictEqual(res, store)
def test_new(self): s = Store() name = 'a' doc = 'b' unit = 'c' v = s.new(name, doc, unit) v_check = Variable(name, doc, unit) self.assertEqual(str(v_check), str(v)) self.assertTrue(v in s) self.assertEqual(str(s[v]), str(v))
def test_nearest(self): s = Store() s.new('dummy') s.new('the one we want') s.new('irrelevant') v = s.nearest('one we wnt') self.assertEqual(v.name, 'the one we want')
def test_serialise_vars(self): store = Store() v1 = store.add('a', 'docstring_1', 'mm') v2 = store.add('b', 'docstring_2', 'm/s') lst = [{'index': 0, 'independent': {v1: 1, v2: 2}, 'dependent': {v1: 5, v2: 7}}] b = Box(lst) res = persist.serialise_vars(b) expected = ['name=a;doc=docstring_1;unit=mm;components=None;sep= - ;category=None;tags=None;identifier=None', 'name=b;doc=docstring_2;unit=m/s;components=None;sep= - ;category=None;tags=None;identifier=None'] self.assertListEqual(res, expected)
def test_make_pack(self): store = Store() v1 = store.add('a', 'docstring_1', 'mm') v2 = store.add('b', 'docstring_2', 'm/s') lst = [{'index': 0, 'independent': {v1: 1, v2: 2}, 'dependent': {v1: 5, v2: 7}}] box = Box(lst) dct = persist.make_pack(box) expected = {'data': [{'index': 0, 'independent': {'a [mm]': 1, 'b [m/s]': 2}, 'dependent': {'a [mm]': 5, 'b [m/s]': 7}}], 'vars': ['name=a;doc=docstring_1;unit=mm;components=None;sep= - ;category=None;tags=None;identifier=None', 'name=b;doc=docstring_2;unit=m/s;components=None;sep= - ;category=None;tags=None;identifier=None']} self.assertDictEqual(dct, expected)
def test_tabulate_store2(self): # TODO: FIX t = Tabulator() box = Box(get_lst2_b()) store = Store() d = store.new('d', components=['x', 'y']) index = d.label columns = ['a', 'b'] values = d pt = t.tabulate(box=box, values=values, columns=columns, index=index, store=store) expected = '''a 1 2 b 1 2 1 2 d: x 12 13 16 19 y 30 31 34 37''' self.assertEqual(str(pt), expected)
def test_tabulate_store(self): t = Tabulator() box = Box(get_lst2_b()) store = Store() d = store.new('d', components=['x', 'y']) index = ['a', 'b'] columns = d.label values = d pt = t.tabulate(box=box, values=values, columns=columns, index=index, store=store) expected = '''d: x y a b 1 1 12 30 2 13 31 2 1 16 34 2 19 37''' self.assertEqual(str(pt), expected)
def test_tabulate_store_nested_cols(self): t = Tabulator() box = Box(get_lst2_c()) store = Store() d = store.new('d', components=['x', 'y']) index = ['a', 'b'] columns = ['c', d.label] values = d pt = t.tabulate(box=box, values=values, columns=columns, index=index, store=store) expected = '''c 1 2 d: x y x y a b 1 1 11 21 14 24 2 13 23 12 22 2 1 15 25 16 26 2 17 27 18 28''' self.assertEqual(str(pt), expected)
def test_tabulate_store_units(self): t = Tabulator() store = Store() d = store.new('d', unit='mm', components=['x', 'y']) aliases = Aliases({'d': d}) translated = aliases.translate(get_lst2_b()) box = Box(translated) index = ['a', 'b'] columns = d.label values = d pt = t.tabulate(box=box, values=values, columns=columns, index=index, store=store) expected = '''d: x [mm] y [mm] a b 1 1 12 30 2 13 31 2 1 16 34 2 19 37''' self.assertEqual(str(pt), expected)
def test_unpack(self): pack = {'data': [{'index': 0, 'independent': {'a [mm]': 1, 'b [m/s]': 2}, 'dependent': {'a [mm]': 5, 'b [m/s]': 7}}], 'vars': ['name=a;doc=docstring_1;unit=mm;components=None;sep= - ;category=None;tags=None;identifier=None', 'name=b;doc=docstring_2;unit=m/s;components=None;sep= - ;category=None;tags=None;identifier=None']} store = Store() v1 = store.add('a', 'docstring_1', 'mm') v2 = store.add('b', 'docstring_2', 'm/s') lst = [{'index': 0, 'independent': {v1: 1, v2: 2}, 'dependent': {v1: 5, v2: 7}}] box = Box(lst) box_unpacked = persist.unpack(pack) self.assertListEqual(box, box_unpacked) keys = list(box_unpacked[0]['dependent'].keys()) for k in keys: self.assertTrue(isinstance(k, Variable)) self.assertTrue(k in store) self.assertEqual(store[k].to_str(), k.to_str())
def test_add_csv(self): s = Store() folder = os.path.dirname(os.path.abspath(__file__)) fname = os.path.join(folder, 'test_variable.csv') v = s.add_csv(fname, encoding='utf-8') d1 = { 'name': 'variable a', 'doc': None, 'unit': 'mm', 'components': None, 'sep': ' - ', 'category': 'test', 'tags': None, 'identifier': 'VAR_A' } self.assertDictEqual(d1, s['VAR_A'].to_dict()) d2 = { 'name': 'variable b', 'doc': None, 'unit': 's', 'components': None, 'sep': ' - ', 'category': 'test', 'tags': ['new', 'tag'], 'identifier': 'VAR_B' } self.assertDictEqual(d2, s['VAR_B'].to_dict()) d3 = { 'name': 'variable c', 'doc': 'some doc', 'unit': 'm', 'components': ['x', 'y', 'z'], 'sep': ' - ', 'category': 'test', 'tags': None, 'identifier': 'VAR_C' } self.assertDictEqual(d3, s['VAR_C'].to_dict())
def test_identifier_getitem(self): s = Store() v = s.add('test', identifier='TEST') self.assertEqual(s['TEST'], v)
def test_add(self): s = Store() v = s.add('test') self.assertEqual(len(s), 1)
def test_identifier_getattr(self): s = Store() v = s.add('test', identifier='TEST') self.assertEqual(s.TEST, v)