Pytest how to conduct testing for function returning dataframe that needs snowflake connection?

Viewed 177

Say if we have an object as below and I would like to do a unit test (pytest) on the extract_data_from_db method particularly without connecting to the actual database, is that possible?

import pandas as pd
import snowflake.connector

class Something:
   def __init__(self):
      self.database = snowflake.connector.connect(
        user='test_user',
        account='test_account',
        password='test_pwd',
    )
   
   @property
   def extract_data_from_db(self):
      query = 'select * from table'
      data = pd.read_sql_query(query, self.database)
      return data
   

I've tried to do the following the mock the whole methods, but running coverage test shows that the code is not covered (due to the output is mocked)

import mock
import pytest

some_df = pd.DataFrame({'a':[1]}

@pytest.fixture
def some_fixture(mocker):
   mocker.patch('Something.extract_data_from_db', return_value=some_df)

# Function to test
def test_extract_data_from_db(some_fixture):
   assert some_fixture.extract_data_from_db
   
0 Answers
Related