def setUp(self): # This function runs before each test in the MelonsTest class. # This is useful if there are variables that you will need access to # in all or most of your tests. self.melon = Melon( 2, 'Hybrid', 'Crenshaw', 2.0, 'http://www.rareseeds.com/assets/1/14/DimRegular/crenshaw.jpg', 'green', True)
def prep_and_painting(self): count = 0 melons = [] # Pick melons until we reach the requested quantity while len(melons) < quantity: # Make sure we haven't reached our limit for the total # number of melons we're allowed to pick if count > MELON_LIMIT: print "\n------------------------------" print "ALL MELONS HAVE BEEN PICKED" print "ORDERS FAILED TO BE FULFILLED!" print "------------------------------\n" sys.exit() # Have the robot pick a 'melon' -- check to # see if it is a Winter Squash or not. if melon_type != "Winter Squash": m = Melon(melon_type) else: m = Squash(melon_type) robots.pickerbot.pick(m) count += 1 # Prepare the melon m.prep() # Evaluate the melon presentable = robots.inspectorbot.evaluate(m) if presentable: melons.append(m) else: robots.trashbot.trash(m) continue print "------" print "Robots Picked {} {} for order of {}".format(count, melon_type, quantity) # Pack the melons for shipping boxes = robots.packerbot.pack(melons) # Ship the boxes robots.shipperbot.ship(boxes) print "------\n"
class MelonsTest(unittest.TestCase): '''Testing the functions in the melons.py file and Melon class methods''' def setUp(self): # This function runs before each test in the MelonsTest class. # This is useful if there are variables that you will need access to # in all or most of your tests. self.melon = Melon( 2, 'Hybrid', 'Crenshaw', 2.0, 'http://www.rareseeds.com/assets/1/14/DimRegular/crenshaw.jpg', 'green', True) def test_get_by_id(self): '''When we call the get_by_id function we should get back the correct melon object.''' # Even though self.melon and the melon returned from get_by_id(2) are both # Crenshaw melons with all the same attributes they are not strictly equal # because they are different instences of the Melon class. This is why we # are checking that the common_name attributes are equal, rather than # checking that the objects themsleves are equal. self.assertEqual(get_by_id(2).common_name, self.melon.common_name) def test_melon_price(self): # testing that price_string method on the melon class returns the correct value self.assertEqual(self.melon.price_str(), '$2.00') def test_read_melon_types_from_file(self): # testing that the output from read_melon_types_from_file contains data we expect. melon_dict = read_melon_types_from_file("melons.txt") self.assertEqual(melon_dict[35].common_name, 'Irish Grey Watermelon')
def assess_and_pack_orders(): """Assesses and packs order objects. Distinguishes between melons/squashes.""" # Check to make sure we've been passed an argument on the # command line. If not, display instructions. if len(sys.argv) < 2: show_help() sys.exit() # Get the name of the log file to open from the command line logfilename = sys.argv[1] # Open the log file f = open(logfilename) # Read each line from the log file and process it for line in f: # Each line should be in the format: # <melon name>: <quantity> melon_type, quantity = line.rstrip().split(':') quantity = int(quantity) print "\n-----" print "Fullfilling order of {} {}".format(quantity, melon_type) print "-----\n" count = 0 melons = [] # Pick melons until we reach the requested quantity while len(melons) < quantity: # Make sure we haven't reached our limit for the total # number of melons we're allowed to pick if count > MELON_LIMIT: print "\n------------------------------" print "ALL MELONS HAVE BEEN PICKED" print "ORDERS FAILED TO BE FULFILLED!" print "------------------------------\n" sys.exit() # Have the robot pick a 'melon' -- check to # see if it is a Winter Squash or not. if melon_type != "Winter Squash": m = Melon(melon_type) else: m = Squash(melon_type) robots.pickerbot.pick(m) count += 1 # Prepare the melon m.prep() # Evaluate the melon presentable = robots.inspectorbot.evaluate(m) if presentable: melons.append(m) else: robots.trashbot.trash(m) continue print "------" print "Robots Picked {} {} for order of {}".format(count, melon_type, quantity) # Pack the melons for shipping boxes = robots.packerbot.pack(melons) # Ship the boxes robots.shipperbot.ship(boxes) print "------\n"