def checkDirectionFromTo(): '''A function that tests to ensure the directionFromTo function is working as expected in all cases, returning False while printing an error message if this is not the case. ''' # load in fresh data dataSource = "http://research.cs.queensu.ca/home/cords2/bikes.txt" bikeData = bike.castCleanData(bike.readData(dataSource)) idList = bike.getIdList(bikeData) # for every possible permutation of two stations: for firstStation in idList: for secondStation in idList: try: # get the direction direc = bike.directionFromTo(firstStation, secondStation, bikeData) # make sure it is in the list of possible outputs if direc not in [ 'NORTHEAST', 'NORTHWEST', 'SOUTHEAST', 'NORTH', 'SOUTH', 'WEST', 'EAST', 'Same location.', 'SOUTHWEST' ]: print('invalid direction') return False except: print('could not get a direction. ') return False return True
def checkGetIdList(): '''A function that returns a boolean value indicating if the getIdList function is working as expected. It will print a short description of the error if it catches one. ''' # load in fresh data dataSource = "http://research.cs.queensu.ca/home/cords2/bikes.txt" bikeData = bike.castCleanData(bike.readData(dataSource)) # generate the id list to check idList = bike.getIdList(bikeData) # check every id for id in idList: # check for correct data types if type(id) != str: print('Id is the wrong type') return False # check that each id is indeed a number but in string form try: int(id) except: print('There is an id that cannot be converted to int') return False # if some ids were missed or extras were introduced. if len(idList) != len(bikeData) - 1: print('There is an incorrect number of ids') return False return True
def checkLookupStation(): '''A function that checks whether the lookup station function is working in all cases, returning a boolean value indicating True or False. It also prints a short message hinting at the possible error. ''' # load fresh data dataSource = "http://research.cs.queensu.ca/home/cords2/bikes.txt" bikeData = bike.castCleanData(bike.readData(dataSource)) idList = bike.getIdList(bikeData) # lookup every station outputList = [bike.lookupStation(id, bikeData) for id in idList] for output in outputList: # check if a value in idList cannot be looked up if output == 'ErrorNotFound': print('bad id in idlist') return False # check if an output does not meet the description of a station elif len(output) != 7: print('An output does not have the right number of items.') return False return True
def checkReturnBike(): '''A function that tests to ensure the returnBike function is working as expected in all cases, returning False while printing an error message if this is not the case. ''' # load fresh data dataSource = "http://research.cs.queensu.ca/home/cords2/bikes.txt" bikeData = bike.castCleanData(bike.readData(dataSource)) idList = bike.getIdList(bikeData) # check the function on legitimate inputs output = [bike.returnBike(id, bikeData) for id in idList] for x in output: # if there is no success/fail boolean returned if x[0] != True and x[0] != False: print('Error in renting bikes.') return False # check the function on bad inputs output2 = [ bike.returnBike(id, bikeData) for id in ['fakeId', '', '1234', 'alsonotid'] ] for x in output2: # if the proper error code is not returned if x[0] != 'ErrorNotFound': print('Error in handling bad ids.') return False return True
def checkRentBike(): '''A function that ensures the rentBike function is working in all cases, and returns False if it is not along with printing a short error message. ''' # load in fresh data dataSource = "http://research.cs.queensu.ca/home/cords2/bikes.txt" bikeData = bike.castCleanData(bike.readData(dataSource)) idList = bike.getIdList(bikeData) # call the rentbike function on every station output = [bike.rentBike(id, bikeData) for id in idList] for x in output: # if the function did not return True for Success or False for fail if x[0] != True and x[0] != False: print('Error in renting bikes.') return False # call the rentbike function on fictitious data it should be able to handle output2 = [ bike.rentBike(id, bikeData) for id in ['fakeId', '', '1234', 'alsonotid'] ] for x in output2: # if the function does not return the appropriate error code if x[0] != 'ErrorNotFound': print('Error in handling bad ids.') return False return True
def checkCheckBikeAvailability(): '''A function that tests to ensure the checkBikeAvailability function is working as expected in all cases, returning False while printing an error message if this is not the case. ''' # load in fresh data dataSource = "http://research.cs.queensu.ca/home/cords2/bikes.txt" bikeData = bike.castCleanData(bike.readData(dataSource)) idList = bike.getIdList(bikeData) # check the availability of every station outputs = [bike.checkBikeAvailability(id, bikeData) for id in idList] for output in outputs: # if the output is not an integer if type(output) != int: print('bike availability is not an int.') return False return True
def simulateUse(turns): """A function that will import the data, and then randomly do an operation for turns number of times where turns is large enough that there is a high probability that every sequence if use is explored. This will find any cases where the logic will cause a runtime error. """ # load and cast the data dataSource = "http://research.cs.queensu.ca/home/cords2/bikes.txt" bData = bike.castCleanData(bike.readData(dataSource)) # get the id lisr idList = bike.getIdList(bData) bike.intro() # the following code will run turns number of times for x in range(turns): # select a random operation choice = random.choice(['1', '2', '3', '4', '5', '6', '7']) # try renting a bike if choice == '1': id = random.choice(idList) success, bData = bike.rentBike(id, bData) # try returning a bike elif choice == '2': id = random.choice(idList) success, bData = bike.returnBike(id, bData) # try checking bike availability elif choice == '3': id = random.choice(idList) numAvailable = bike.checkBikeAvailability(id, bData) # try listing bike availability elif choice == '4': sortedList = bike.listAvailable(bData) # try getting a direc elif choice == '5': idOne = random.choice(idList) idTwo = random.choice(idList) direction = bike.directionFromTo(idOne, idTwo, bData) elif choice == '6': id = random.choice(idList) output = bike.niceLine(bike.lookupStation(id, bData)) elif choice == '7': fullStations = bike.listFull(bData) # lets the user quit the program. elif choice == 'q' or choice == 'Q': print('Thank-you for using the Toronto Bike Share Program!') running = False return else: print('Sorry, your selection is not an option. ') # print a status. if x % 10000 == 0: print("Testing: {}%".format(round(x * 100 / turns, 1))) print('Done testing, no runtime errors. ') return True