def test_consolidate_delivery(self): from processes.consolidate import consolidate_delivery from os import mkdir, remove, rmdir, path from utility.utility import To_Datetime delivery = self.shift.deliveries[0] file_list = delivery.file_list() # make directory mkdir(file_list['directory']) # create indavidual files with open(file_list['start_time'], 'w') as start_time_file: start_time_file.write(str(delivery.start_time)) with open(file_list['miles_traveled'], 'w') as miles_traveled_file: miles_traveled_file.write(str(delivery.miles_traveled)) with open(file_list['average_speed'], 'w') as average_speed_file: average_speed_file.write(str(delivery.average_speed)) with open(file_list['end_time'], 'w') as end_time_file: end_time_file.write(str(delivery.end_time)) # check that files were created and others dont exist self.assertTrue(path.exists(file_list['start_time'])) self.assertTrue(path.exists(file_list['miles_traveled'])) self.assertTrue(path.exists(file_list['average_speed'])) self.assertTrue(path.exists(file_list['end_time'])) self.assertFalse(path.exists(file_list['info'])) self.assertFalse(path.exists(file_list['completed_ids'])) # run the function consolidate_delivery(delivery) # check that the files were deleted and created self.assertFalse(path.exists(file_list['start_time'])) self.assertFalse(path.exists(file_list['miles_traveled'])) self.assertFalse(path.exists(file_list['average_speed'])) self.assertFalse(path.exists(file_list['end_time'])) self.assertTrue(path.exists(file_list['info'])) self.assertTrue(path.exists(file_list['completed_ids'])) # check that correct data was written to files with open(file_list['info'], 'r') as info_file: data = info_file.readline().split(',') self.assertEqual(float(data[0]), delivery.miles_traveled) self.assertEqual(int(data[1]), delivery.average_speed) self.assertEqual( To_Datetime(data[2]).from_datetime(), delivery.start_time) self.assertEqual( To_Datetime(data[3]).from_datetime(), delivery.end_time) with open(file_list['completed_ids'], 'r') as completed_ids_file: self.assertEqual(int(completed_ids_file.readline()), delivery.id) # delete files and directories remove(file_list['completed_ids']) remove(file_list['info']) rmdir(file_list['directory'])
def test_track_extra_stop_shift(self): import objects from os import mkdir, path, remove, rmdir from processes.track import track_extra_stop from utility.utility import To_Datetime, now extra_stop = self.shift.extra_stops[0] file_list = extra_stop.file_list() # make directories needed for split mkdir(self.shift.file_list()['directory']) # check that files dont exist self.assertFalse(path.exists(file_list['directory'])) self.assertFalse(path.exists(file_list['start_time'])) self.assertFalse(path.exists(file_list['location'])) self.assertFalse(path.exists(file_list['reason'])) self.assertFalse(path.exists(file_list['miles_traveled'])) self.assertFalse(path.exists(file_list['end_time'])) # run function track_extra_stop(extra_stop, test=True) # check that files were created self.assertTrue(path.exists(file_list['directory'])) self.assertTrue(path.exists(file_list['start_time'])) self.assertTrue(path.exists(file_list['location'])) self.assertTrue(path.exists(file_list['reason'])) self.assertTrue(path.exists(file_list['miles_traveled'])) self.assertTrue(path.exists(file_list['end_time'])) # check that correct data was written to files with open(file_list['start_time'], 'r') as start_time_file: self.assertEqual( To_Datetime(start_time_file.readline()).from_datetime(), extra_stop.start_time) with open(file_list['location'], 'r') as location_file: self.assertEqual(location_file.readline(), extra_stop.location) with open(file_list['reason'], 'r') as reason_file: self.assertEqual(reason_file.readline(), extra_stop.reason) with open(file_list['miles_traveled'], 'r') as miles_traveled_file: self.assertEqual(float(miles_traveled_file.readline()), extra_stop.miles_traveled) with open(file_list['end_time'], 'r') as end_time_file: self.assertEqual( To_Datetime(end_time_file.readline()).from_datetime(), extra_stop.end_time) # delete files and directories remove(file_list['start_time']) remove(file_list['location']) remove(file_list['reason']) remove(file_list['miles_traveled']) remove(file_list['end_time']) rmdir(file_list['directory']) rmdir(self.shift.file_list()['directory'])
def test_to_datetime_from_date(self): from datetime import datetime from utility.utility import To_Datetime string = '2020-09-07' result = To_Datetime(string).from_date() self.assertIsInstance(result, datetime) string = '2020-01-30' result = To_Datetime(string).from_date() self.assertIsInstance(result, datetime) string = '2020-11-13' result = To_Datetime(string).from_date() self.assertIsInstance(result, datetime)
def test_to_datetime_from_time(self): from utility.utility import To_Datetime from datetime import datetime string = '09:13:27.73218' result = To_Datetime(string).from_time() self.assertIsInstance(result, datetime) string = '09:13:27.000' result = To_Datetime(string).from_time() self.assertIsInstance(result, datetime) string = '09:13:27' result = To_Datetime(string).from_time() self.assertIsInstance(result, datetime)
def test_consolidate_order(self): from objects import Tip from os import mkdir, remove, rmdir, path from processes.consolidate import consolidate_order from utility.utility import To_Datetime delivery = self.shift.deliveries[0] parent_directory = delivery.file_list()['directory'] order = delivery.orders[0] file_list = order.file_list() # create directories mkdir(parent_directory) mkdir(file_list['directory']) # create indavidual files with open(file_list['id'], 'w') as id_file: id_file.write(str(order.id)) with open(file_list['tip'], 'w') as tip_file: tip_file.write(str(order.tip.csv())) with open(file_list['miles_traveled'], 'w') as miles_traveled_file: miles_traveled_file.write(str(order.miles_traveled)) with open(file_list['end_time'], 'w') as end_time_file: end_time_file.write(str(order.end_time)) # check that files were created and others dont exist self.assertTrue(path.exists(file_list['id'])) self.assertTrue(path.exists(file_list['tip'])) self.assertTrue(path.exists(file_list['miles_traveled'])) self.assertTrue(path.exists(file_list['end_time'])) self.assertFalse(path.exists(file_list['info'])) self.assertFalse(path.exists(file_list['completed_ids'])) # run the function consolidate_order(order) # check that the files were deleted and created self.assertFalse(path.exists(file_list['id'])) self.assertFalse(path.exists(file_list['tip'])) self.assertFalse(path.exists(file_list['miles_traveled'])) self.assertFalse(path.exists(file_list['end_time'])) self.assertTrue(path.exists(file_list['info'])) self.assertTrue(path.exists(file_list['completed_ids'])) # check that correct data was written to files with open(file_list['info'], 'r') as info_file: data = info_file.readline().split(',') self.assertEqual( Tip(float(data[0]), float(data[1]), float(data[2])).csv(), order.tip.csv()) self.assertEqual(float(data[3]), order.miles_traveled) self.assertEqual( To_Datetime(data[4]).from_datetime(), order.end_time) with open(file_list['completed_ids'], 'r') as completed_ids_file: self.assertEqual(int(completed_ids_file.readline()), order.id) # delete files and directories remove(file_list['info']) remove(file_list['completed_ids']) rmdir(parent_directory)
def test_start_delivery(self): from os import mkdir, path, remove, rmdir from processes.track import start_delivery from utility.utility import To_Datetime delivery = self.shift.deliveries[0] file_list = delivery.file_list() # make directories mkdir(self.shift.file_list()['directory']) # check that file and directory dont exist self.assertFalse(path.exists(file_list['directory'])) self.assertFalse(path.exists(file_list['start_time'])) # run function start_delivery(delivery, test=True) # check that file and directory were created self.assertTrue(path.exists(file_list['directory'])) self.assertTrue(path.exists(file_list['start_time'])) # check that the correct data was written to file with open(file_list['start_time'], 'r') as start_time_file: self.assertEqual( To_Datetime(start_time_file.readline()).from_datetime(), delivery.start_time) # delete file and directories remove(file_list['start_time']) rmdir(file_list['directory']) rmdir(self.shift.file_list()['directory'])
def test_start_split(self): import objects from os import mkdir, path, remove, rmdir from processes.track import start_split from utility.utility import To_Datetime, now split = self.shift.split file_list = split.file_list() # make directories needed for split mkdir(self.shift.file_list()['directory']) # check that files dont exist self.assertFalse(path.exists(file_list['directory'])) self.assertFalse(path.exists(file_list['start_time'])) # run function start_split(split, test=True) # check that files were created self.assertTrue(path.exists(file_list['directory'])) self.assertTrue(path.exists(file_list['start_time'])) # check that the correct data was written to the file with open(file_list['start_time'], 'r') as start_time_file: self.assertEqual( To_Datetime(start_time_file.readline()).from_datetime(), split.start_time) # delete files and directories remove(file_list['start_time']) rmdir(file_list['directory']) rmdir(self.shift.file_list()['directory'])
def test_start_shift(self): from os import mkdir, path, remove, rmdir from processes.track import start_shift from utility.utility import To_Datetime file_list = self.shift.file_list() # check that file and directory dont exist self.assertFalse(path.exists(file_list['directory'])) self.assertFalse(path.exists(file_list['start_time'])) # run function start_shift(self.shift, test=True) # check that the file and directory were created self.assertTrue(path.exists(file_list['directory'])) self.assertTrue(path.exists(file_list['start_time'])) # check that the correct data was saved with open(file_list['start_time'], 'r') as start_time_file: self.assertEqual( To_Datetime(start_time_file.readline()).from_datetime(), self.shift.start_time) # delete file and directory remove(file_list['start_time']) rmdir(file_list['directory'])
def test_consolidate_extra_stop_delivery(self): from objects import Extra_Stop from os import mkdir, remove, rmdir, path from processes.consolidate import consolidate_extra_stop from utility.utility import To_Datetime delivery = self.shift.deliveries[1] extra_stop = delivery.extra_stops[0] file_list = extra_stop.file_list() # create directory mkdir(delivery.file_list()['directory']) mkdir(file_list['directory']) # create indavidual files with open(file_list['location'], 'w') as location_file: location_file.write(extra_stop.location) with open(file_list['reason'], 'w') as reason_file: reason_file.write(extra_stop.reason) with open(file_list['miles_traveled'], 'w') as miles_traveled_file: miles_traveled_file.write(str(extra_stop.miles_traveled)) with open(file_list['end_time'], 'w') as end_time_file: end_time_file.write(str(extra_stop.end_time)) # check that files were created and others dont exist self.assertTrue(path.exists(file_list['location'])) self.assertTrue(path.exists(file_list['reason'])) self.assertTrue(path.exists(file_list['miles_traveled'])) self.assertTrue(path.exists(file_list['end_time'])) self.assertFalse(path.exists(file_list['info'])) self.assertFalse(path.exists(file_list['completed_ids'])) # run the function consolidate_extra_stop(extra_stop) # check that the files were deleted and created self.assertFalse(path.exists(file_list['location'])) self.assertFalse(path.exists(file_list['reason'])) self.assertFalse(path.exists(file_list['miles_traveled'])) self.assertFalse(path.exists(file_list['end_time'])) self.assertTrue(path.exists(file_list['info'])) self.assertTrue(path.exists(file_list['completed_ids'])) # check that correct data was written to files with open(file_list['info'], 'r') as info_file: data = info_file.read().split('\n') self.assertEqual(data[0], extra_stop.location) self.assertEqual(data[1], extra_stop.reason) self.assertEqual(float(data[2]), extra_stop.miles_traveled) self.assertEqual( To_Datetime(data[3]).from_datetime(), extra_stop.end_time) with open(file_list['completed_ids'], 'r') as completed_ids_file: self.assertEqual(int(completed_ids_file.readline()), extra_stop.id) # delete files and directories remove(file_list['info']) remove(file_list['completed_ids']) remove(file_list['running_id']) rmdir(delivery.file_list()['directory'])
def test_consolidate_split(self): from objects import Split from os import mkdir, remove, rmdir, path from processes.consolidate import consolidate_split from utility.utility import To_Datetime split = self.shift.split file_list = split.file_list() # create directory mkdir(file_list['directory']) # create indavidual files with open(file_list['start_time'], 'w') as start_time_file: start_time_file.write(str(split.start_time)) with open(file_list['miles_traveled'], 'w') as miles_traveled_file: miles_traveled_file.write(str(split.miles_traveled)) with open(file_list['end_time'], 'w') as end_time_file: end_time_file.write(str(split.end_time)) # check that files were created and others dont exist self.assertTrue(path.exists(file_list['start_time'])) self.assertTrue(path.exists(file_list['miles_traveled'])) self.assertTrue(path.exists(file_list['end_time'])) self.assertFalse(path.exists(file_list['info'])) # run the function consolidate_split(split) # check that the files were deleted and created self.assertFalse(path.exists(file_list['start_time'])) self.assertFalse(path.exists(file_list['miles_traveled'])) self.assertFalse(path.exists(file_list['end_time'])) self.assertTrue(path.exists(file_list['info'])) # check that correct data was written to files with open(file_list['info'], 'r') as info_file: data = info_file.readline().split(',') self.assertEqual(float(data[0]), split.miles_traveled) self.assertEqual( To_Datetime(data[1]).from_datetime(), split.start_time) self.assertEqual( To_Datetime(data[2]).from_datetime(), split.end_time) # delete files and directories remove(file_list['info'])
def load_delivery(delivery): from objects import Delivery if not isinstance(delivery, Delivery): # todo: need to write error message raise TypeError from objects import Extra_Stop, Order from os import path from utility.file import Read from utility.utility import To_Datetime # get list of files and directory file_list = delivery.file_list() # in progress if delivery.in_progress: # start time if path.exists(file_list['start_time']): delivery.start_time = Read(file_list['start_time']).datetime() # distance if path.exists(file_list['distance']): delivery.distance = Read(file_list['distance']).decimal() # average speed if path.exists(file_list['average_speed']): delivery.average_speed = Read(file_list['average_speed']).integer() # end time if path.exists(file_list['end_time']): delivery.end_time = Read(file_list['end_time']).datetime() # completed else: # delivery info delivery_data = Read(file_list['info']).comma() # distance if delivery_data[0] != '': delivery.distance = float(delivery_data[0]) # average speed if delivery_data[1] != '': delivery.average_speed = int(delivery_data[1]) # start time if delivery_data[2] != '': delivery.start_time = To_Datetime(delivery_data[2]).from_datetime() # end time if delivery_data[3] != '': delivery.end_time = To_Datetime(delivery_data[3]).from_datetime() return delivery
def test_csv(self): from utility.utility import To_Datetime expected = 'None,None,None,None' self.assertEqual(self.delivery.csv(), expected) start_time = To_Datetime('2020-08-23 12:03:00.000').from_datetime() end_time = To_Datetime('2020-08-23 12:37:00.000').from_datetime() distance = 7.2 average_speed = 21 self.delivery.start_time = start_time self.delivery.end_time = end_time self.delivery.miles_traveled = distance self.delivery.average_speed = average_speed expected = '{},{},{},{}'.format(distance, average_speed, start_time, end_time) self.assertEqual(self.delivery.csv(), expected)
def test_track_order(self): import objects from os import mkdir, path, remove, rmdir from processes.track import track_order from utility.utility import To_Datetime delivery = self.shift.deliveries[0] order = delivery.orders[0] file_list = order.file_list() # make directory mkdir(self.shift.file_list()['directory']) mkdir(delivery.file_list()['directory']) # check that directory and files dont exist self.assertFalse(path.exists(file_list['directory'])) self.assertFalse(path.exists(file_list['id'])) self.assertFalse(path.exists(file_list['tip'])) self.assertFalse(path.exists(file_list['miles_traveled'])) self.assertFalse(path.exists(file_list['end_time'])) # run function track_order(order, test=True) # check that directory and files were created self.assertTrue(path.exists(file_list['directory'])) self.assertTrue(path.exists(file_list['id'])) self.assertTrue(path.exists(file_list['tip'])) self.assertTrue(path.exists(file_list['miles_traveled'])) self.assertTrue(path.exists(file_list['end_time'])) # check that the correct data was written to the files with open(file_list['id'], 'r') as id_file: self.assertEqual(int(id_file.readline()), order.id) with open(file_list['tip'], 'r') as tip_file: tip_data = tip_file.readline().split(',') self.assertEqual( objects.Tip(tip_data[0], tip_data[1], tip_data[2]).csv(), order.tip.csv()) with open(file_list['miles_traveled'], 'r') as miles_traveled_file: self.assertEqual(float(miles_traveled_file.readline()), order.miles_traveled) with open(file_list['end_time'], 'r') as end_time_file: self.assertEqual( To_Datetime(end_time_file.readline()).from_datetime(), order.end_time) # delete files and directories remove(file_list['id']) remove(file_list['tip']) remove(file_list['miles_traveled']) remove(file_list['end_time']) rmdir(file_list['directory']) rmdir(delivery.file_list()['directory']) rmdir(self.shift.file_list()['directory'])
def load_split(split): from objects import Split if not isinstance(split, Split): raise TypeError from os import path from utility.file import Read from utility.utility import To_Datetime file_list = split.file_list() # in progress if split.in_progress: # distance if path.exists(file_list['distance']): split.distance = Read(file_list['distance']).decimal() # start time if path.exists(file_list['start_time']): split.start_time = Read(file_list['start_time']).datetime() # end time if path.exists(file_list['end_time']): split.end_time = Read(file_list['end_time']).datetime() # completed else: # split info split_info = Read(file_list['info']).comma() # distance if split_info[0] != '': split.distance = float(split_info[0]) # start time if split_info[1] != '': split.start_time = To_Datetime(split_info[1]).from_datetime() # end time if split_info[2] != '': split.end_time = To_Datetime(split_info[2]).from_datetime() return split
def setUp(self): import objects from utility.utility import To_Datetime from utility.utility import now self.shift = objects.Shift(now().date()) self.miles_traveled = 12.3 self.fuel_economy = 21.2 self.vehicle_compensation = 13.07 self.device_compensation = .27 self.extra_tips_claimed = 3.0 self.total_hours = 9.7 self.start_time = To_Datetime('2020-07-13 09:00:00.000').from_datetime() self.end_time = To_Datetime('2020-07-13 18:30:00.000').from_datetime() self.delivery_1 = objects.Delivery(self.shift, 0) self.delivery_2 = objects.Delivery(self.shift, 1) self.delivery_3 = objects.Delivery(self.shift, 2) self.order_1 = objects.Order(self.delivery_1, 5) self.order_2 = objects.Order(self.delivery_2, 10) self.order_3 = objects.Order(self.delivery_3, 16) self.order_4 = objects.Order(self.delivery_3, 22)
def remove_id_from_file(self): from utility.file import Read, write from utility.utility import To_Datetime ids_file = self.file_list()['completed_ids'] id_list = [ To_Datetime(id).from_date() for id in Read(ids_file).comma() ] new_ids_list = [str(id.date()) for id in id_list if id != self.id] if len(new_ids_list) == 0: from os import remove remove(ids_file) elif len(new_ids_list) > 0: from utility.file import write write(','.join(new_ids_list), ids_file)
def test_end_delivery(self): from os import mkdir, path, remove, rmdir from processes.track import end_delivery from utility.utility import To_Datetime delivery = self.shift.deliveries[0] file_list = delivery.file_list() # make directories mkdir(self.shift.file_list()['directory']) mkdir(file_list['directory']) # check that files dont exist self.assertFalse(path.exists(file_list['miles_traveled'])) self.assertFalse(path.exists(file_list['average_speed'])) self.assertFalse(path.exists(file_list['end_time'])) # run function end_delivery(delivery, test=True) # check that files were created self.assertTrue(path.exists(file_list['miles_traveled'])) self.assertTrue(path.exists(file_list['average_speed'])) self.assertTrue(path.exists(file_list['end_time'])) # check that the correct data was written to the files with open(file_list['miles_traveled'], 'r') as miles_traveled_file: self.assertEqual(float(miles_traveled_file.readline()), delivery.miles_traveled) with open(file_list['average_speed'], 'r') as average_speed_file: self.assertEqual(int(average_speed_file.readline()), delivery.average_speed) with open(file_list['end_time'], 'r') as end_time_file: self.assertEqual( To_Datetime(end_time_file.readline()).from_datetime(), delivery.end_time) # delete files and directories remove(file_list['miles_traveled']) remove(file_list['average_speed']) remove(file_list['end_time']) rmdir(file_list['directory']) rmdir(self.shift.file_list()['directory'])
def load_order(order): from objects import Order if not isinstance(order, Order): # todo: need to fix error message for taking delivery from resources.error_messages import load__order__wrong_parameter raise TypeError(load__order__wrong_parameter) from os import path from utility.file import Read from utility.utility import To_Datetime file_list = order.file_list() # in progress if order.in_progress: # id if path.exists(file_list['id']): order.id = Read(file_list['id']).integer() # tip if path.exists(file_list['tip']): order.tip = load_tip(file_list['tip']) # distance if path.exists(file_list['distance']): order.distance = Read(file_list['distance']).decimal() # end time if path.exists(file_list['end_time']): order.end_time = Read(file_list['end_time']).datetime() # completed else: from objects import Tip # order data order_data = Read(order.file_list()['info']).comma() # tip order.tip = Tip(order_data[0], order_data[1], order_data[2]) # distance if order_data[3] != '': order.distance = float(order_data[3]) # end time if order_data[4] != '': order.end_time = To_Datetime(order_data[4]).from_datetime() return order
def test_consolidate_shift(self): from processes.consolidate import consolidate_shift from os import mkdir, remove, rmdir, path from utility.utility import To_Datetime file_list = self.shift.file_list() # write indavidual files # start time with open(file_list['start_time'], 'w') as start_time_file: start_time_file.write(str(self.shift.start_time)) # end time with open(file_list['end_time'], 'w') as end_time_file: end_time_file.write(str(self.shift.end_time)) # distance with open(file_list['miles_traveled'], 'w') as miles_traveled_file: miles_traveled_file.write(str(self.shift.miles_traveled)) # fuel economy with open(file_list['fuel_economy'], 'w') as fuel_economy_file: fuel_economy_file.write(str(self.shift.fuel_economy)) # vehicle compensation with open(file_list['vehicle_compensation'], 'w') as vehicle_compensation_file: vehicle_compensation_file.write( str(self.shift.vehicle_compensation)) # device compensation with open(file_list['device_compensation'], 'w') as device_compensation_file: device_compensation_file.write(str(self.shift.device_compensation)) # total hours with open(file_list['total_hours'], 'w') as total_hours_file: total_hours_file.write(str(self.shift.total_hours)) # extra tips claimed with open(file_list['extra_tips_claimed'], 'w') as extra_tips_claimed_file: extra_tips_claimed_file.write(str(self.shift.extra_tips_claimed)) # check that files were created and the others dont self.assertTrue(path.exists(file_list['start_time'])) self.assertTrue(path.exists(file_list['end_time'])) self.assertTrue(path.exists(file_list['miles_traveled'])) self.assertTrue(path.exists(file_list['fuel_economy'])) self.assertTrue(path.exists(file_list['vehicle_compensation'])) self.assertTrue(path.exists(file_list['device_compensation'])) self.assertTrue(path.exists(file_list['total_hours'])) self.assertTrue(path.exists(file_list['extra_tips_claimed'])) self.assertFalse(path.exists(file_list['info'])) self.assertFalse(path.exists(file_list['completed_ids'])) # run function consolidate_shift(self.shift) # check indavidual files were deleted and two was created self.assertFalse(path.exists(file_list['start_time'])) self.assertFalse(path.exists(file_list['end_time'])) self.assertFalse(path.exists(file_list['miles_traveled'])) self.assertFalse(path.exists(file_list['fuel_economy'])) self.assertFalse(path.exists(file_list['vehicle_compensation'])) self.assertFalse(path.exists(file_list['device_compensation'])) self.assertFalse(path.exists(file_list['total_hours'])) self.assertFalse(path.exists(file_list['extra_tips_claimed'])) self.assertTrue(path.exists(file_list['info'])) self.assertTrue(path.exists(file_list['completed_ids'])) # check that the correct data was written to the files # info file with open(file_list['info'], 'r') as info_file: data = info_file.readline().split(',') self.assertEqual(float(data[0]), self.shift.miles_traveled) self.assertEqual(float(data[1]), self.shift.fuel_economy) self.assertEqual(float(data[2]), self.shift.vehicle_compensation) self.assertEqual(float(data[3]), self.shift.device_compensation) self.assertEqual(float(data[4]), self.shift.extra_tips_claimed) self.assertEqual(float(data[5]), self.shift.total_hours) self.assertEqual( To_Datetime(data[6]).from_datetime(), self.shift.start_time) self.assertEqual( To_Datetime(data[7]).from_datetime(), self.shift.end_time) # id file with open(file_list['completed_ids'], 'r') as completed_ids_file: self.assertEqual( To_Datetime(completed_ids_file.readline()).from_date().date(), self.shift.id) # delete files and directory remove(file_list['completed_ids']) remove(file_list['info'])
def load_shift(shift): from objects import Shift if not isinstance(shift, Shift): raise TypeError from objects import Delivery, Extra_Stop, Split from os import path from utility.file import Read from utility.utility import To_Datetime file_list = shift.file_list() # in progress if shift.in_progress: # start time if path.exists(file_list['start_time']): shift.start_time = Read(file_list['start_time']).datetime() # end time if path.exists(file_list['end_time']): shift.end_time = Read(file_list['end_time']).datetime() # distance if path.exists(file_list['distance']): shift.distance = Read(file_list['distance']).decimal() # fuel economy if path.exists(file_list['fuel_economy']): shift.fuel_economy = Read(file_list['fuel_economy']).decimal() # vehical compensation if path.exists(file_list['vehicle_compensation']): shift.vehicle_compensation =\ Read(file_list['vehicle_compensation']).decimal() # device compensation if path.exists(file_list['device_compensation']): shift.device_compensation =\ Read(file_list['device_compensation']).decimal() # total hours if path.exists(file_list['total_hours']): shift.total_hours = Read(file_list['total_hours']).decimal() # extra tips claimed if path.exists(file_list['extra_tips_claimed']): shift.extra_tips_claimed =\ Read(file_list['extra_tips_claimed']).decimal() # completed else: # shift info shift_data = Read(file_list['info']).comma() # distance if shift_data[0] != '': shift.distance = float(shift_data[0]) # fuel economy if shift_data[1] != '': shift.fuel_economy = float(shift_data[1]) # vehicle compensation if shift_data[2] != '': shift.vehicle_compensation = float(shift_data[2]) # device compensation if shift_data[3] != '': shift.device_compensation = float(shift_data[3]) # extra tips claimed if shift_data[4] != '': shift.extra_tips_claimed = float(shift_data[4]) # total hours if shift_data[5] != '': shift.total_hours = float(shift_data[5]) # start time if shift_data[6] != '': shift.start_time = To_Datetime(shift_data[6]).from_datetime() # end time if shift_data[7] != '': shift.end_time = To_Datetime(shift_data[7]).from_datetime() return shift
def test_end_shift(self): from os import mkdir, path, remove, rmdir from processes.track import end_shift from utility.utility import To_Datetime file_list = self.shift.file_list() # make directory mkdir(file_list['directory']) # check that files dont exist self.assertFalse(path.exists(file_list['end_time'])) self.assertFalse(path.exists(file_list['miles_traveled'])) self.assertFalse(path.exists(file_list['fuel_economy'])) self.assertFalse(path.exists(file_list['vehicle_compensation'])) self.assertFalse(path.exists(file_list['device_compensation'])) self.assertFalse(path.exists(file_list['total_hours'])) self.assertFalse(path.exists(file_list['extra_tips_claimed'])) # run function end_shift(self.shift, test=True) # check that files do exist self.assertTrue(path.exists(file_list['end_time'])) self.assertTrue(path.exists(file_list['miles_traveled'])) self.assertTrue(path.exists(file_list['fuel_economy'])) self.assertTrue(path.exists(file_list['vehicle_compensation'])) self.assertTrue(path.exists(file_list['device_compensation'])) self.assertTrue(path.exists(file_list['total_hours'])) self.assertTrue(path.exists(file_list['extra_tips_claimed'])) # check the correct data was written to files # end time with open(file_list['end_time'], 'r') as end_time_file: self.assertEqual( To_Datetime(end_time_file.readline()).from_datetime(), self.shift.end_time) # distance with open(file_list['miles_traveled'], 'r') as miles_traveled_file: self.assertEqual(float(miles_traveled_file.readline()), self.shift.miles_traveled) # fuel economy with open(file_list['fuel_economy'], 'r') as fuel_economy_file: self.assertEqual(float(fuel_economy_file.readline()), self.shift.fuel_economy) # vehicle compensation with open(file_list['vehicle_compensation'], 'r') as vehicle_compensation_file: self.assertEqual(float(vehicle_compensation_file.readline()), self.shift.vehicle_compensation) # device compensation with open(file_list['device_compensation'], 'r') as device_compensation_file: self.assertEqual(float(device_compensation_file.readline()), self.shift.device_compensation) # total hours with open(file_list['total_hours'], 'r') as total_hours_file: self.assertEqual(float(total_hours_file.readline()), self.shift.total_hours) # extra tips claimed with open(file_list['extra_tips_claimed'], 'r') as extra_tips_claimed_file: self.assertEqual(float(extra_tips_claimed_file.readline()), self.shift.extra_tips_claimed) # delete files and directory remove(file_list['end_time']) remove(file_list['miles_traveled']) remove(file_list['fuel_economy']) remove(file_list['vehicle_compensation']) remove(file_list['device_compensation']) remove(file_list['total_hours']) remove(file_list['extra_tips_claimed']) rmdir(file_list['directory'])
def load_extra_stop(extra_stop): from objects import Extra_Stop if not isinstance(extra_stop, Extra_Stop): raise TypeError from objects import Delivery, Shift from os import path from utility.file import Read from utility.utility import To_Datetime # get files and directory file_list = extra_stop.file_list() # in progress if extra_stop.in_progress: # location if path.exists(file_list['location']): extra_stop.location = Read(file_list['location']).data # reason if path.exists(file_list['reason']): extra_stop.reason = Read(file_list['reason']).data # distance if path.exists(file_list['distance']): extra_stop.distance =\ Read(file_list['distance']).decimal() # end time if path.exists(file_list['end_time']): extra_stop.end_time = Read(file_list['end_time']).datetime() if isinstance(extra_stop.parent, Shift): # start time if path.exists(file_list['start_time']): extra_stop.start_time = Read( file_list['start_time']).datetime() # completed else: # extra stop info extra_stop_data = Read(file_list['info']).newline() # location if extra_stop_data[0] != '': extra_stop.location = extra_stop_data[0] # reason if extra_stop_data[1] != '': extra_stop.reason = extra_stop_data[1] # distance if extra_stop_data[2] != '': extra_stop.distance = float(extra_stop_data[2]) if isinstance(extra_stop.parent, Delivery): # end time if extra_stop_data[3] != '': extra_stop.end_time = To_Datetime( extra_stop_data[3]).from_datetime() elif isinstance(extra_stop.parent, Shift): # start time if extra_stop_data[3] != '': extra_stop.start_time =\ To_Datetime(extra_stop_data[3]).from_datetime() # end time if extra_stop_data[4] != '': extra_stop.end_time =\ To_Datetime(extra_stop_data[4]).from_datetime() return extra_stop
def completed_shift(): import objects from utility.utility import To_Datetime from utility.utility import now # shift shift = objects.Shift(now().date()) shift.miles_traveled = 12.3 shift.fuel_economy = 21.2 shift.vehicle_compensation = 13.07 shift.device_compensation = .27 shift.extra_tips_claimed = 3.0 shift.total_hours = 9.7 shift.start_time =\ To_Datetime('2020-07-13 09:00:00.100').from_datetime() shift.end_time =\ To_Datetime('2020-07-13 18:30:00.100').from_datetime() shift.carry_out_tips = [objects.Tip(card=3.11), objects.Tip(cash=2.71)] # delivery 1 delivery_1 = objects.Delivery(shift, 0) delivery_1.start_time =\ To_Datetime('2020-07-13 10:30:00.100').from_datetime() delivery_1.end_time =\ To_Datetime('2020-07-13 10:55:00.100').from_datetime() delivery_1.miles_traveled = 3.7 delivery_1.average_speed = 21 # order order_1 = objects.Order(delivery_1, 7) order_1.end_time =\ To_Datetime('2020-07-13 10:43:00.100').from_datetime() order_1.miles_traveled = 1.8 order_1.tip = objects.Tip(cash=5) # add order to delivery delivery_1.order_ids.append(order_1.id) delivery_1.orders.append(order_1) # add delivery to shift shift.delivery_ids.append(delivery_1.id) shift.deliveries.append(delivery_1) # delivery 2 delivery_2 = objects.Delivery(shift, 1) delivery_2.start_time =\ To_Datetime('2020-07-13 11:20:00.100').from_datetime() delivery_2.end_time =\ To_Datetime('2020-07-13 11:47:00.100').from_datetime() delivery_2.miles_traveled = .7 delivery_2.average_speed = 14 # order order_2 = objects.Order(delivery_2, 36) order_2.end_time =\ To_Datetime('2020-07-13 11:31:00.100').from_datetime() order_2.miles_traveled = 3.1 order_2.tip = objects.Tip() # add order to delivery delivery_2.order_ids.append(order_2.id) delivery_2.orders.append(order_2) # extra stop extra_stop_2 = objects.Extra_Stop(delivery_2, 1) extra_stop_2.location = 'mongolian grill' extra_stop_2.reason = 'trade food' extra_stop_2.miles_traveled = 4.1 extra_stop_2.end_time =\ To_Datetime('2020-08-25 13:27:57.100').from_datetime() delivery_2.extra_stop_ids.append(extra_stop_2.id) delivery_2.extra_stops.append(extra_stop_2) # add delivery to shift shift.delivery_ids.append(delivery_2.id) shift.deliveries.append(delivery_2) # delivery 3 delivery_3 = objects.Delivery(shift, 2) delivery_3.start_time =\ To_Datetime('2020-07-13 12:12:00.100').from_datetime() delivery_3.end_time =\ To_Datetime('2020-07-13 12:41:00.100').from_datetime() delivery_3.miles_traveled = 6.7 delivery_3.average_speed = 23 # order 1 order_3 = objects.Order(delivery_3, 47) order_3.end_time =\ To_Datetime('2020-07-13 12:28:00.100').from_datetime() order_3.miles_traveled = 3.4 order_3.tip = objects.Tip(card=2.78) # add order to delivery delivery_3.order_ids.append(order_3.id) delivery_3.orders.append(order_3) # order 2 order_4 = objects.Order(delivery_3, 58) order_4.end_time =\ To_Datetime('2020-07-13 12:28:00.100').from_datetime() order_4.miles_traveled = 3.4 order_4.tip = objects.Tip(card=3.41, cash=3) # add order to delivery delivery_3.order_ids.append(order_4.id) delivery_3.orders.append(order_4) # add delivery to shift shift.delivery_ids.append(delivery_3.id) shift.deliveries.append(delivery_3) # extra stop 1 extra_stop_1 = objects.Extra_Stop(shift, 0) extra_stop_1 = objects.Extra_Stop(shift) extra_stop_1.start_time =\ To_Datetime('2020-08-25 10:05:33.100').from_datetime() extra_stop_1.location = 'bank' extra_stop_1.reason = 'change' extra_stop_1.miles_traveled = 3.6 extra_stop_1.end_time =\ To_Datetime('2020-08-25 10:15:33.100').from_datetime() # add extra stop to shift shift.extra_stop_ids.append(extra_stop_1.id) shift.extra_stops.append(extra_stop_1) # extra stop 2 extra_stop_3 = objects.Extra_Stop(shift, 2) extra_stop_3 = objects.Extra_Stop(shift) extra_stop_3.start_time =\ To_Datetime('2020-08-25 13:17:38.100').from_datetime() extra_stop_3.location = 'mongolian grill' extra_stop_3.reason = 'trade food' extra_stop_3.miles_traveled = 4.1 extra_stop_3.end_time =\ To_Datetime('2020-08-25 13:27:57.100').from_datetime() # add extra stop to shift shift.extra_stop_ids.append(extra_stop_3.id) shift.extra_stops.append(extra_stop_3) # split split = objects.Split(shift) split.start_time = To_Datetime('2020-08-25 14:03:57.100').from_datetime() split.miles_traveled = 3.1 split.end_time = To_Datetime('2020-08-25 16:03:57.100').from_datetime() shift.split = split return shift
def datetime_list(self): from utility.utility import To_Datetime return [To_Datetime(data).from_datetime() for data in self.comma()]