lundi 11 janvier 2016

Factory pattern and Mock/Stub

I have a factory that is responsible to build some task of my ETL process. See code below:

class ConcreteFactory(AbstractFactory):

    def __init__(self, config):
        super(AbstractFactory, self).__init__(config)


    def build(self, command):
        config = self.config 

        if command == "load_customers_cache":
            api =  ConcreteApi()
            cache =  ConcreteCache()
            return LoadCustomersCache(cache, api)

        if command == "load_customers_bseller":
            db = ConcreteDB()
            cache =  ConcreteCache()
            return LoadCustomersBSeller(db, cache)

        if (command == "download_from_ftp"):
            ftp = ConcreteFtp()
            d = DownloadFromFTP(ftp)
            return d 

For testing purpose, i would like to test, for e.g., the download_from_ftp. So i need to mock the ConcreteFTP.

What is the best approach?

  1. Can I create another factory that is responsible to build the external services like ConcreteAPI, ConcreteCache and ConcreteFTP, and inject it into this ConcreteFactory.

  2. Pass **kwargs to the ConcreteFactory, and try to get kwargs["ftp"] and if it returns not NONE I use it as my FTP class. So I can pass a mock to the ConcreteFactory.

  3. Create another factory that extends from AbstractFactory, and build the mock that I need.

  4. Other suggestion, pattern...

Aucun commentaire:

Enregistrer un commentaire