def test_lock(self): st1 = Struct({_k: [_i] for _i, _k in enumerate(['a', 'b', 'c'])}) st1._lock() with self.assertRaises(AttributeError): st1.a = 1 with self.assertRaises(AttributeError): st1.d = 1 with self.assertRaises(AttributeError): st1['a'] = 1 with self.assertRaises(AttributeError): st1['d'] = 1 with self.assertRaises(AttributeError): del st1.b with self.assertRaises(AttributeError): st1.col_remove('c') with self.assertRaises(AttributeError): _ = st1.col_pop('c') with self.assertRaises(AttributeError): _ = st1.col_rename('c', 'C') with self.assertRaises(AttributeError): _ = st1.col_map({}) with self.assertRaises(AttributeError): _ = st1.col_move_to_back('c') with self.assertRaises(AttributeError): _ = st1.col_move_to_front('c') st1._unlock() st1.a = 1 st1.d = 1 del st1.b self.assertEqual(list(st1.keys()), ['a', 'c', 'd']) self.assertEqual(st1.a, 1) self.assertEqual(st1.d, 1)
def test_col_swap(self): orig_cols = ['a', 'b', 'c', 'd'] dict1 = {_k: [_i] for _i, _k in enumerate(orig_cols)} st = Struct(dict1) for _i, _k in enumerate(orig_cols): self.assertEqual(st[_k][0], _i) st.a, st.b = st.b, st.a self.assertEqual(st['a'][0], 1) self.assertEqual(st['b'][0], 0) st.col_swap(['a', 'b'], ['b', 'a']) for _i, _k in enumerate(orig_cols): self.assertEqual(st[_k][0], _i) st.col_swap(['a', 'b', 'c'], ['c', 'b', 'a']) self.assertEqual(st['a'][0], 2) self.assertEqual(st['b'][0], 1) self.assertEqual(st['c'][0], 0) self.assertEqual(list(st.keys()), orig_cols) st = Struct(dict1) st.col_swap(['a', 'b', 'c', 'd'], ['c', 'b', 'a', 'd']) self.assertEqual(st['a'][0], 2) self.assertEqual(st['b'][0], 1) self.assertEqual(st['c'][0], 0) self.assertEqual(st['d'][0], 3) self.assertEqual(list(st.keys()), orig_cols)