mardi 30 juin 2015

Unit testing an SQL query Python using Mock

I have a class called Loader, which loads datasets from an SQL server via queries. Such that by invoking loadFromSQL(query) in Loader, one gets the table as specified by query. The specific structure of Loader is:

import petl as etl
import pandas as pd

class Loader:
    """
    This is a class from which one can load data from an SQL server.
    """

    def __init__(self, connection_string):
        """
        This is the initialization file, and it requires the connection_string.

        :param connection_string:
        :type connection_string: str
        :return:
        """

        self.connection = pyodbc.connect(connection_string)

    def loadFromSQL(self, query):
        """
        This function loads the data according to the query passed in query.

        :param query:
        :type query: str
        """

        self.originalTableETL = etl.fromdb(self.connection, query)

        self.originalTablePD = etl.todataframe(self.originalTableETL)

Now, the thing is that I wish to test this class, i.e. to verify that the SQL querying is correct. From scouring the web I found that Mock could do the trick - the problem is that I'm not really sure how to do this when I have a database. In fact, I do not even know where to start as I haven't found any examples relating to my problem. Do you guys have any tips and/or a good documentation relating to my problem? Thanks!

Aucun commentaire:

Enregistrer un commentaire