def need_update_price_tests(): """ unit test cases for need_update_price() """ print('need_update_price_tests:') # test for index file that does not exist filename = 'data/index/price-2010-10.csv' if os.path.exists(filename): os.remove(filename) need_update_index_test(filename, True) # test for modtime_dt != filename_dt but now > expire(modtime + 1 hour) filename = 'data/index/price-2010-10.csv' fetch.update_price('index', 2010, 10, filename) os.utime(filename, (0, 0)) need_update_index_test(filename, False) # test for modtime_dt == filename_dt but now < expire(modtime + 1 hour) now = datetime.now() filename = 'data/index/price-' + str(now.year) + \ '-' + ('%02d') % (now.month) + '.csv' fetch.update_price('index', now.year, now.month, filename) need_update_index_test(filename, False)
def update_price_test(input_param, expect_url, expect_filename): """ test module for update_price() """ stock, year, month, filename = input_param # output if os.path.exists(filename): os.remove(filename) res = fetch.update_price(stock, year, month, filename) if res == False: print('pass') # correctly handle decode error exception return # expect if os.path.exists(expect_filename): os.remove(expect_filename) with request.urlopen(expect_url) as response, \ open(expect_filename, 'w') as expect_file: expect_file.write(response.read().decode('big5hkscs')) if filecmp.cmp(filename, expect_filename): # same print('pass') else: print('fail')