Skip to content

Commit 4b24a89

Browse files
author
Michael Erickson
committed
Added tests for CASTable.rename
1 parent 52cc8d4 commit 4b24a89

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

swat/tests/cas/test_table.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5174,6 +5174,38 @@ def test_partition_inputs(self):
51745174
pcolinfo = pcolinfo.drop('ID', errors='ignore').drop('Label', errors='ignore')
51755175
self.assertTablesEqual(colinfo[['Two', 'Model', 'One', 'MSRP']], pcolinfo)
51765176

5177+
def test_rename(self):
5178+
tbl = self.table
5179+
5180+
# Rename by name:
5181+
makeCol = tbl['Make']
5182+
self.assertTrue(any(col in 'Make' for col in list(tbl.columns)))
5183+
tbl.rename({'Make':'Manufacturer'})
5184+
# No column named "Make" and a column named "Manufacturer"
5185+
self.assertFalse(any(col in 'Make' for col in list(tbl.columns)))
5186+
self.assertTrue(any(col in 'Manufacturer' for col in list(tbl.columns)))
5187+
# Shouldn't be a "new" column, just 'Make' renamed
5188+
self.assertEqual(makeCol, tbl['Manufacturer'])
5189+
5190+
# Rename by function:
5191+
# Column Manufacturer -> Manufacturer_0
5192+
tbl.rename(lambda col : col + "_0")
5193+
for col in list(tbl.columns):
5194+
# Last two characters should be _0 for each col
5195+
self.assertEqual(col[-2:], "_0")
5196+
5197+
# Rename by name for col that doesn't exist
5198+
# errors='ignore'
5199+
originalCols = list(copy.deepcopy(tbl.columns))
5200+
tbl.rename({'nope':'nuh uh'})
5201+
self.assertFalse(any(col in 'nope' for col in list(tbl.columns)))
5202+
self.assertListEqual(originalCols, list(tbl.columns))
5203+
5204+
# Rename by name for col that doesn't exist
5205+
# errors='raise'
5206+
with self.assertRaises(KeyError):
5207+
tbl.rename(tbl.rename({'nope':'nuh uh'}, errors='raise'))
5208+
51775209
def test_reset_index(self):
51785210
tbl = self.table
51795211

0 commit comments

Comments
 (0)