curious_guy
curious_guy

Reputation: 39

Is this the right way of testing a pandas dataframe in python?

I have a function which returns a dataframe which has rows and columns. here's the function: app.py

def pandas_factory(colnames, rows):
    return pd.DataFrame(rows, columns=colnames)

And here is my unit testcase for the same: test_app.py

    def test_panda_fact(self):
        from dlp.dlp import pandas_factory
        df1 = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
        with pytest.raises(TypeError) as er:
            mock_open = mock.mock_open(read_data=df1)
            with patch('dlp.dlp.pandas_factory', mock_open):
                obj = pandas_factory
            self.assertTrue(obj)

The testcase runs but am I testing it in the right way?

Upvotes: 0

Views: 277

Answers (1)

NotAName
NotAName

Reputation: 4347

So here's an example of how I would go about testing this function:

import pandas
import numpy as np

from dlp.dlp import pandas_factory

def test_type():
    df1 = pandas_factory(rows=np.array([[1, 2],[3, 4]]), colnames=['a', 'b'])
    assert isinstance(df1, pandas.core.frame.DataFrame)

def test_size():
    cols = np.random.randint(10, 100)
    length = np.random.randint(10, 100)
    data = np.zeros((length, cols))

    df = pandas_factory(rows=data, colnames=[str(i) for i in range(cols)])
    assert df.shape == (length, cols)

Something along those lines. Here I'm assuming that rows is some form of data, like a numpy array.

Upvotes: 1

Related Questions