user7496931
user7496931

Reputation:

Writing a unit test for a method in a class that calls a static method

I have the following static method in my Class:

@staticmethod
def get_previous_month(date):
    previous_month = date - relativedelta.relativedelta(months=1)
    return previous_month

Below it, i'm using it in a class method.

def transform_last_price(self, data):

    full_data = data. \
        addfield('TimeSeriesValue'). \
        convert('EffectiveDate', lambda x: date_converter(x)) 

    # Break table to dict_data... Does other stuff...

    # Calls static method:
    base_month = get_previous_month(dict_data[0]['EffectiveDate'])

    # Do more stuff...

    # Transform mapped dict into table
    lastPriceData = etl. \
        fromdicts(dict_data). \
        addfield('FundID', self.FUND_ID). \
        addfield('TimeSeriesTypeID', 12). \
        cutout('MTDReturn')

    return lastPriceData

I'm trying to write a unit test that checks that if some dates were converted into date objects.

def test_convert_date_strings_to_date_objects(self):
    data = etl.fromdicts([{'EffectiveDate': '1990-08-31'}])
    transformed_dates = self.parser.transform_last_price(data)
    date = transformed_dates.values('EffectiveDate')[0]
    self.assertIsInstance(date, datetime.date)

However, i'm getting the following error:

NameError: name 'get_previous_month' is not defined

I'm assuming that the test does not have access to the static method? I'm not sure how to re-write this. I already wrote a test for get_previous_month separately and it's working but i'm writing more of an integration test for transform_last_price

Upvotes: 1

Views: 4768

Answers (1)

Bela Tamas Jozsa
Bela Tamas Jozsa

Reputation: 714

static methods should be called with syntax: self.methodName() or Class.methodName(). you simply use methodName which won't work. (Python would try to resolve it against the global namespace)

Upvotes: 2

Related Questions