def parse_file(filename, db): # if you cannot open the file then exit w/ an error message try: f = open(filename, 'r') # information about the line number to help give more information about error messages linenum = 0 for line in f: linenum += 1 time, price, units = line.split(' ') # make sure the date in the file is in the appropriate format try: dt = dateutil.parser.parse(time) # make sure timezone is converted into UTC dt_utc = dt.replace(tzinfo=timezone('UTC')) except ValueError: print( "Error in parsing. Make sure in line %d the time is in ISO-8061 format" % linenum) return 2 # make sure the price in the file is in the appropriate format try: p = float(price) except ValueError: print( "Error in parsing. Make sure in line %d the price is a valid value" % linenum) return 3 # make sure the units in the file is in the appropriate format try: q = int(units) except ValueError: print( "Error in parsing. Make sure in line %d the number of units is a valid value" % linenum) return 4 # create a new day_info object and add it to the database new_di = Day_Info(dt_utc, float(price), int(units)) db.add_entry(new_di) f.close() return 0 # give user error message except FileNotFoundError: print("Could not open file path \"", filename, "\". Make sure file is in correct directory") return 1
def test_is_empty_false(self): db = Database() day_info1 = Day_Info(datetime.datetime(2005, 3, 1), 5, 1) db.add_entry(day_info1) self.assertFalse(db.is_empty())
def test_lookup_range_not_included_to_end(self): db = Database() day_info1 = Day_Info(datetime.datetime(2005,3,1),5,1) day_info2 = Day_Info(datetime.datetime(2005,3,2),20,2) day_info3 = Day_Info(datetime.datetime(2005,3,3),50,3) day_info4 = Day_Info(datetime.datetime(2005,3,4),10,4) day_info5 = Day_Info(datetime.datetime(2005,3,5),6,5) day_info6 = Day_Info(datetime.datetime(2005,3,6),9,6) db.add_entry(day_info1) db.add_entry(day_info2) db.add_entry(day_info3) db.add_entry(day_info4) db.add_entry(day_info5) db.add_entry(day_info6) arr = db.lookup_range(datetime.datetime(2005,3,2,5),datetime.datetime(2005,3,7,5)) self.assertEqual(len(arr),4) self.assertTrue(arr[0].compare(day_info3)) self.assertTrue(arr[1].compare(day_info4)) self.assertTrue(arr[2].compare(day_info5)) self.assertTrue(arr[3].compare(day_info6))
def test_add_out_of_order(self): db = Database() day_info1 = Day_Info(datetime.datetime(2005,3,1),5,1) day_info2 = Day_Info(datetime.datetime(2005,3,2),20,2) day_info3 = Day_Info(datetime.datetime(2005,3,3),50,3) day_info4 = Day_Info(datetime.datetime(2005,3,4),10,4) day_info5 = Day_Info(datetime.datetime(2005,3,5),6,5) day_info6 = Day_Info(datetime.datetime(2005,3,6),9,6) db.add_entry(day_info2) db.add_entry(day_info5) db.add_entry(day_info3) db.add_entry(day_info4) db.add_entry(day_info1) db.add_entry(day_info6) inside = db.db self.assertTrue(inside[0].compare(day_info1)) self.assertTrue(inside[1].compare(day_info2)) self.assertTrue(inside[2].compare(day_info3)) self.assertTrue(inside[3].compare(day_info4)) self.assertTrue(inside[4].compare(day_info5)) self.assertTrue(inside[5].compare(day_info6))
def test_get_max(self): db = Database() day_info1 = Day_Info(datetime.datetime(2005, 3, 1), 5, 1) day_info2 = Day_Info(datetime.datetime(2005, 3, 2), 20, 2) day_info3 = Day_Info(datetime.datetime(2005, 3, 3), 50, 3) day_info4 = Day_Info(datetime.datetime(2005, 3, 4), 10, 4) day_info5 = Day_Info(datetime.datetime(2005, 3, 5), 6, 5) day_info6 = Day_Info(datetime.datetime(2005, 3, 6), 9, 6) db.add_entry(day_info1) db.add_entry(day_info2) db.add_entry(day_info3) db.add_entry(day_info4) db.add_entry(day_info5) db.add_entry(day_info6) self.assertTrue(db.get_max().compare(day_info6))
def test_lookup_not_included(self): db = Database() day_info1 = Day_Info(datetime.datetime(2005,3,1),5,1) day_info2 = Day_Info(datetime.datetime(2005,3,2),20,2) day_info3 = Day_Info(datetime.datetime(2005,3,3),50,3) day_info4 = Day_Info(datetime.datetime(2005,3,4),10,4) day_info5 = Day_Info(datetime.datetime(2005,3,5),6,5) day_info6 = Day_Info(datetime.datetime(2005,3,6),9,6) db.add_entry(day_info1) db.add_entry(day_info2) db.add_entry(day_info3) db.add_entry(day_info4) db.add_entry(day_info5) db.add_entry(day_info6) tup = db.lookup(datetime.datetime(2005,3,1,5)) self.assertTrue(tup[0].compare(day_info1)) self.assertTrue(tup[1].compare(day_info2))
def test_compare_different_units(self): di_1 = Day_Info(datetime.datetime(2005, 1, 2, 3), 1, 2) di_2 = Day_Info(datetime.datetime(2005, 1, 2, 3), 1, 100) self.assertEqual(di_1.compare(di_2), False)
def test_compare_same(self): di_1 = Day_Info(datetime.datetime(2005,1,2,3),1,2) di_2 = Day_Info(datetime.datetime(2005,1,2,3),1,2) self.assertEqual(di_1.compare(di_2),True)
def test_get_units(self): di = Day_Info(datetime.datetime(2005, 1, 2, 3, 4), 100, 2) self.assertEqual(2, di.get_units())
def test_get_price(self): di = Day_Info(datetime.datetime(2005, 1, 2, 3, 4), 100, 2) self.assertEqual(100, di.get_price())