def test_single_location_open(self): """test if on elocation is oepn or closed""" closing = ClosingTimePath() #hennings supermarket in PA input_list = ['ChIJkfMeIS2fxokRKgvjrsrWagA'] result = closing.calculate_path(input_list, datetime.now()) time = datetime.now() curr_time = int(time.strftime("%H%M")) #if hennings is closed, add it to closed list if (curr_time < 700 or curr_time > 2300): self.assertTrue(len(result[0]) == 0) self.assertTrue(len(result[1]) == 0) self.assertTrue(len(result[2]) == 1) self.assertTrue(len(result[3]) == 0) #not closed, we ran this within the acceptable closing time else: self.assertTrue(len(result[0]) == 0) self.assertTrue(len(result[1]) == 0) self.assertTrue(len(result[2]) == 0) self.assertTrue(len(result[3]) == 1) self.assertTrue(result[3][0][1] == 2300)
def test_single_and_always(self): """Code designed to test that places are sorted correctly""" closing = ClosingTimePath() #hennings supermarket in PA, hilton in troy input_list = ['ChIJkfMeIS2fxokRKgvjrsrWagA', 'ChIJ7d-xRZcP3okRYq4CVW3e56k'] result = closing.calculate_path(input_list, datetime.now()) time = datetime.now() curr_time = int(time.strftime("%H%M")) #if hennings is closed, add it to closed list if (curr_time < 700 or curr_time > 2300): self.assertTrue(len(result[0]) == 0) self.assertTrue(len(result[1]) == 1) self.assertTrue(len(result[2]) == 1) self.assertTrue(len(result[3]) == 1) self.assertTrue(result[3][0][1] == 2500) #not closed, we ran this within the acceptable closing time else: self.assertTrue(len(result[0]) == 0) self.assertTrue(len(result[1]) == 1) self.assertTrue(len(result[2]) == 0) self.assertTrue(len(result[3]) == 2) #both locations should appear as open, 24/7 place is last self.assertTrue(result[3][0][1] == 2300) self.assertTrue(result[3][1][1] == 2500)
def test_empty_list(self): """Test for when the user inputs no locations""" closing = ClosingTimePath() input_list = [] result = closing.calculate_path(input_list, datetime.now()) #empty path returns 4 empty lists self.assertTrue(len(result[0]) == 0) self.assertTrue(len(result[1]) == 0) self.assertTrue(len(result[2]) == 0) self.assertTrue(len(result[3]) == 0)
def test_single_always_open(self): """Test for when user submits only one 24/7 location""" closing = ClosingTimePath() #hilton in troy, open 24/7 input_list = ['ChIJ7d-xRZcP3okRYq4CVW3e56k'] result = closing.calculate_path(input_list, datetime.now()) #24/7 places are aded to both regular list and 24/7 list self.assertTrue(len(result[0]) == 0) self.assertTrue(len(result[1]) == 1) self.assertTrue(len(result[2]) == 0) self.assertTrue(len(result[3]) == 1)
def test_multi_error(self): """Test for computing 3 separate error locations""" closing = ClosingTimePath() #philadelphia, PA, albany,ny, new york, ny input_list = ['ChIJ60u11Ni3xokRwVg-jNgU9Yk', 'ChIJS_tPzDQK3okRxCjnoBJjoeE', 'ChIJOwg_06VPwokRYv534QaPC8g'] result = closing.calculate_path(input_list, datetime.now()) #same as previous test, only all 3 are culprits, they should notbe added anywhere else self.assertTrue(len(result[0]) == 3) self.assertTrue(len(result[1]) == 0) self.assertTrue(len(result[2]) == 0) self.assertTrue(len(result[3]) == 0)
def test_single_error(self): """Test for single location with no known time""" closing = ClosingTimePath() #philadelphia, PA input_list = ['ChIJ60u11Ni3xokRwVg-jNgU9Yk'] result = closing.calculate_path(input_list, datetime.now()) #philly has no closing time, it only gets added to error self.assertTrue(len(result[0]) == 1) self.assertTrue(len(result[1]) == 0) self.assertTrue(len(result[2]) == 0) self.assertTrue(len(result[3]) == 0)
def test_error_and_always(self): """Test one error location and one always active location""" closing = ClosingTimePath() #philadelphia and samaritan hospital in troy input_list = ['ChIJ60u11Ni3xokRwVg-jNgU9Yk', 'ChIJjZrhSJkP3okR7aNWoQVvGCg'] result = closing.calculate_path(input_list, datetime.now()) self.assertTrue(len(result[0]) == 1) self.assertTrue(len(result[1]) == 1) self.assertTrue(len(result[2]) == 0) self.assertTrue(len(result[3]) == 1) #24 hour place should have 2500 listed for closing time in spite of error self.assertTrue(result[3][0][1] == 2500)
def test_multi_always(self): """Test for computing 3 separate error locations""" closing = ClosingTimePath() #hilton in troy, samaritan hospital in troy input_list = ['ChIJ7d-xRZcP3okRYq4CVW3e56k', 'ChIJjZrhSJkP3okR7aNWoQVvGCg'] result = closing.calculate_path(input_list, datetime.now()) #same as previous test, only all 3 are culprits, they should notbe added anywhere else self.assertTrue(len(result[0]) == 0) self.assertTrue(len(result[1]) == 2) self.assertTrue(len(result[2]) == 0) self.assertTrue(len(result[3]) == 2) #24 hour times are 2500, should be stored in tuple alongside index self.assertTrue(result[3][0][1] == 2500) self.assertTrue(result[3][1][1] == 2500)
def test_two_not_special(self): """Test for 2 locations that have a deifnite closing time""" closing = ClosingTimePath() #hennings supermarket in PA, wal-mart in Troy input_list = ['ChIJkfMeIS2fxokRKgvjrsrWagA', 'ChIJ55INakMF3okRwIYWbgiifaE'] result = closing.calculate_path(input_list, datetime.now()) time = datetime.now() curr_time = int(time.strftime("%H%M")) #both places are closed here if (curr_time < 600 and curr_time >= 0): self.assertTrue(len(result[0]) == 0) self.assertTrue(len(result[1]) == 0) self.assertTrue(len(result[2]) == 2) self.assertTrue(len(result[3]) == 0) #only walmart is open elif (curr_time >= 600 and curr_time < 700): self.assertTrue(len(result[0]) == 0) self.assertTrue(len(result[1]) == 0) self.assertTrue(len(result[2]) == 1) self.assertTrue(len(result[3]) == 1) self.assertTrue(result[3][0][1] == 200) #both places are now open elif (curr_time >= 700 and curr_time < 2300): self.assertTrue(len(result[0]) == 0) self.assertTrue(len(result[1]) == 0) self.assertTrue(len(result[2]) == 0) self.assertTrue(len(result[3]) == 2) self.assertTrue(result[3][0][1] == 2300) self.assertTrue(result[3][1][1] == 0) #henning's closed, walmart open else: self.assertTrue(len(result[0]) == 0) self.assertTrue(len(result[1]) == 0) self.assertTrue(len(result[2]) == 1) self.assertTrue(len(result[3]) == 1) self.assertTrue(result[3][0][1] == 0)
def home_page(): """ Main page of site """ if request.method == 'POST': print("got post") content = request.get_json() print(content) location_list = parse_request(content) list_of_id = id_parsing(content) start_time = parse_start_time(content) print("!!!!!start_time ", start_time) """ Here we begin computing routes to send in the JSON response Format of JSON Response: { closed_stores: bool, path_found: bool, paths: [ { path: [ ... ], travel_time: int }, { path: [ ... ], travel_time: int }, { path: [ ... ], travel_time: int }, { path: [ ... ], travel_time: int } ] } """ solution_dictionary = {'paths': []} # Optimal Path optimal_path = OptimalPath() context = RouteContext(optimal_path) solution = context.compute_route(location_list, start_time) if solution == -1: solution_dictionary['path_found'] = False solution_dictionary['paths'].append({ 'path': [], 'travel_time': -1 }) else: solution_dictionary['path_found'] = True path = construct_optimal_response(content, solution[0]) optimal_travel_time = compute_travel_time(path, 'driving', start_time) solution_dictionary['paths'].append({ 'name': 'Optimal Path', 'path': path, 'travel_time': optimal_travel_time }) # Closing Time Path closing_path = ClosingTimePath() context2 = RouteContext(closing_path) solution2 = context2.compute_route(list_of_id, start_time) path_list = construct_closing_time_response(content, solution2) solution_dictionary['closed_stores'] = True if solution2[2] else False travel_time = -1 if not solution_dictionary[ 'path_found'] else compute_travel_time(path_list, 'driving', start_time) solution_dictionary['paths'].append({ 'name': 'Closing Time Path', 'path': path_list, 'travel_time': travel_time }) # Distance Path distance_path = DistancePath() context3 = RouteContext(distance_path) solution3 = context3.compute_route(location_list, start_time) if solution == -1: solution_dictionary['paths'].append({ 'path': [], 'travel_time': -1 }) else: path = construct_optimal_response(content, solution3[0]) distance_travel_time = -1 if not solution_dictionary[ 'path_found'] else compute_travel_time(path, 'driving', start_time) solution_dictionary['paths'].append({ 'name': 'Distance Path', 'path': path, 'travel_time': distance_travel_time }) # Default Path default_path = DefaultPath() context4 = RouteContext(default_path) solution4 = context4.compute_route(location_list, start_time) if solution == -1: solution_dictionary['paths'].append({ 'path': [], 'travel_time': -1 }) else: path = construct_optimal_response(content, solution4) default_travel_time = -1 if not solution_dictionary[ 'path_found'] else compute_travel_time(path, 'driving', start_time) solution_dictionary['paths'].append({ 'name': 'Default Path', 'path': path, 'travel_time': default_travel_time }) return jsonify(solution_dictionary) if request.method == 'GET': return render_template('index.html')