1
def fetch_holidays_in_date_range(src):
    query = "SELECT * from holiday_tab where id = src"
    db = dbconnect.connect()
    # defining the cursor for reading data
    cursor = db.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
    # query the database
    cursor.execute(query.format(src));
    rows = cursor.fetchall()
    dbconnect.destroy(cursor, db)
    return rows

Could someone help, how to mock this code in pytest or in unittest. I've googled for mock db and I hardly found anything.

  • Could [this answer](https://stackoverflow.com/questions/35143055/how-to-mock-psycopg2-cursor-object) (how to mock postgresql cursor) help you? – MrBean Bremen Feb 24 '20 at 12:20

1 Answers1

0

Pytest doesn't support you to run the test on production/main DB if you are using pytest-django. There is a better approach to solve this issue.

  • Pytest DB resolve method

  • This says that whenever you run the test with a marker @pytest.mark.django_db, tests are run on another newly created db with name test_your production_db_name.

  • So if your db name is hello, pytest will create a new db called test_hello and runs tests on it
Nithin Mohan
  • 372
  • 1
  • 9