首页 > 解决方案 > Rename new column function testing

问题描述

I am testing the below "if mode" that someone has written by input values to see if function is working as expected

def modify_col(self, tbl, new_col, mode, left, right):  
      if mode == 'rename_column':
      self.df[tbl] = self.df[tbl].withColumn(new_col, F.col(left))

I wrote the below unit test code and my issue is the function is wrong or am I inputing wrong values or the logic I put in is wrong: below is the unit testing I ran and its failing

def test_modify_rename_column(self):
    """ 
    Test make_new_col function. 

    """
    test_data= [
      {'A': 12,'B': 11},
      {'A': 12,'B': 13}
    ]

    expected = [
                {'A': 12, 'B': 11,'rename_column_test':1},
                {'A': 12, 'B': 13,'rename_column_test':1}
        ]

    self.lib.df['test_data'] = self.spark.createDataFrame(test_data).alias('test_data')

    self.lib.modify_col(
      tbl = 'test_data',
      new_col = 'rename_column_test',
      mode = 'rename_column',
      left = 'A',
      right = 'B'
    )
  
    actual = (
     self.lib.df['test_data']
    .toPandas()
    .sort_values(['A'])
    .to_dict(orient='records')
    )
    assert expected == actual  

I am getting an error:

> AssertionError: 
AssertionError                            Traceback (most recent call last)
in engine
----> 1 t.test_modify_rename_column()

<ipython-input-1-35d82a35840a> in test_modify_rename_column(self)
    893         .to_dict(orient='records')
    894         )
--> 895         assert expected == actual  
    896       
    897       

AssertionError: 

Any help can provide to fix this issue would be really appreciated. Thanking you in advance

标签: pythonpysparkrenamecolumnsortingcdsw

解决方案


推荐阅读