jeudi 17 décembre 2020

Mock oracle database for testing with pytest

I have a function get_latest_scrape_date which looks like this:

def get_latest_scrape_date(database):

    try:
        last_scraped_date = database.session.query(func.max(BookReviews.date)).all()[0][0]
    
    except (KeyError):
        LOG.warning('The database is empty')
        last_scraped_date = date(1970,1,1)  # dummy date

    return  last_scraped_date

The function retuns the latest date which can be found in my Oracle database. The database has four columns and one of it is date. Now I want to test get_latest_scrape_date with pytest. Of course, I want to avoid real requests to the database each time I run the test. That means, I have to mock my database inside my test environment. I am not very experienced with testing code.

  1. How do I mock an Oracle database which has 4 columns?
  2. How to I access the mocked database in order to use it for my test?

Aucun commentaire:

Enregistrer un commentaire