python unittest

  • function
def modify_time(time_str):
    '''
    set time of kwargs

    :param time_str: like '2017-02-16'
    :return: dict
    '''
    kwargs = {'earliest_time':'', \
              'latest_time':'', \
              'search_mode':'normal', \
              'exec_mode':'blocking'}
    append_hms = 'T00:00:00.000'
    tmp_year = time_str.split('-')[0]
    tmp_month = time_str.split('-')[1]
    tmp_day = time_str.split('-')[2]

    if '16' in tmp_day:
        str_replace = time_str.replace('16', '01')
        kwargs['earliest_time'] = str_replace + append_hms
        kwargs['latest_time'] = time_str + append_hms
        return kwargs

    if '01' in tmp_day:
        tmp_str_replace = tmp_month + '-' + tmp_day

        if tmp_month == '01':
            time_new_year = str(int(tmp_year)-1)
            time_new_str = time_new_year+ '-' +'12' + '-' + '16'
            str_replace = time_str.replace(time_str,time_new_str)
        else:
            time_new_month = str(int(tmp_month) - 1)
            time_new_str = '0' + time_new_month + '-' + '16'
            str_replace = time_str.replace(tmp_str_replace, time_new_str)

        kwargs['earliest_time'] = str_replace + append_hms
        kwargs['latest_time'] = time_str + append_hms
        return kwargs

  • test_function
import unittest

from generatePeakUtil import modify_time


class Test(unittest.TestCase):
    def setUp(self):
        self.time_str = '2017-02-01'

    def tearDown(self):
        self.time_str = None

    def test_split(self):
        d = modify_time(self.time_str)['earliest_time']
        self.assertEqual(d, '2017-01-16T00:00:00.000')

if __name__ == '__main__':
    unittest.main()
posted @ 2017-02-28 11:20  idlewith  阅读(129)  评论(0编辑  收藏  举报