'Environment inherited Base class condition

I'm writing up some testing modules for a project I'm working on. I'm relatively new to testing, and I'm struggling to test the inherited base class properly.

app/base.py

#app/base.py
import os
ENV = os.getenv('ENV', 'development')

class Base:
    """BASE CLASS"""
    def __init__(self,config_data,...):...
    self._api = config_data[ENV]

    @property
    def stuff(self):...self.get_stuff()

class Production(Base):
    """PRODUCTION CLASS"""
    def get_stuff(self):...with request(...)

class Development(Base):
    """DEVELOPMENT CLASS"""
    def get_stuff(self):... with open(...)
        
class BaseConfig(Production if ENV == 'production' else
                 (Development if ENV == 'development' else Base)):
    """ENV BASECLASS"""

app/api.py

# app/api.py
from app.base import BaseConfig
class API(BaseConfig):
    def __init__(self, **kwargs):

        with open('app/config.json', mode='r', encoding='UTF-8') as config_file:
            api_config: ConfigFile = json.load(config_file)

        super().__init__(api_config, kwargs)
   @property
   def response(self):
       return super().stuff

tests/base_test.py

# tests/base_test.py
from unittest import mock
import pytest
from app import base

@mock.patch('app.base.ENV', 'production')
def test_inherited_base():
    assert base.BaseConfig.__base__ == base.Production

FAILED tests/base_test.py::test_inherited_base - AssertionError: assert <class 'app.base.Development'> == <class 'app.base.Production'>

I was not getting an assertion error when I tested the self._api values. So I'm assuming that once I import base in my testing module I can not retro actively set the inherited base statement.



Solution 1:[1]

This ended up being the solution for the time being

# app/base.py
def get_base() -> Type[Base]:
    """env baseclass"""
    return (Production if ENV == 'production' else
            (Development if ENV == 'development' else Base))

# tests/base_test.py

@pytest.fixture()
@mock.patch('app.base.ENV', 'production')
def prod_instance():
    """app.base.Production fixture"""
    with open('app/config.json', mode='r', encoding='UTF-8') as config_file:
        api_config = json.load(config_file)
    Base = base.get_base()
    assert Base == base.Production
    return Base(api_config, SETUP)


@pytest.fixture()
def dev_instance():
    """app.base.Development fixture"""
    with open('app/config.json', mode='r', encoding='UTF-8') as config_file:
        api_config = json.load(config_file)
    Base = base.get_base()
    assert Base == base.Development
    return Base(api_config, SETUP)


def test_inhertied_base(prod_instance, dev_instance):
    """inherited base"""
    assert isinstance(prod_instance, base.Production)
    assert isinstance(dev_instance, base.Development)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jason Leaver