1

Hi I have a method on my main.py which basically receives a dataframe and connection string, I am sending this dataframe to sql database on PostgreSQL.

def connect_with_postgres(df_table,sql_conn):
    db = create_engine(sql_conn)
    conn=db.connect()
    #build a table and put the dataframe information into
    df_table.to_sql('table_name', con=conn, if_exists='replace', schema='schemaname', index=False)
    conn.close()
    print("The data has been stored into the database.")

Then on my unit test I am taking a csv with a sample of data and put it into variable on my mocks.py

with open('mocks/table.csv', 'r') as file:
    mock_table_csv = file.read()
    file.close()

And last one my unit test function:

def test_connect_with_postgres(self):
    conn_result = Mock()
    mock_conn.connect.return_value = conn_result
    mock_table = pd.read_csv(StringIO(mock_table_csv))
    output = connect_with_postgres(mock_table,conn)

But is trying to get a real connection string, shows me this

TypeError: cannot unpack non-iterable Mock object

If I send a real conn string it works but the idea is to mock the connection string (fake one) and test that the function works, any idea how to make this happend?

Regards

2
  • stackoverflow.com/a/57249876/16475089 Commented Mar 21, 2023 at 4:42
  • Hi I already tried with that but shows me this error ValueError: not enough values to unpack (expected 3, got 0), and as I mentioned sending a real conn works. Also I am using SQL Alchemy Commented Mar 21, 2023 at 4:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.