class StockItemTest(unittest.TestCase): def setUp(self): print '*Entering setUp method*' '''This will create a StockItem instance that can be used throughout the StockItemTest class.''' self.Stock1 = StockItem() def test__add__(self): print '*Entering test__add__ method*' '''This will create a new StockItem instance that the user should input the purchase price as 1. This function will then check that __add__ method makes 1 + 1 = 2''' Stock2 = StockItem() addition = self.Stock1.__add__(Stock2) self.assertEqual(addition, 2) def test_storageCost (self): print '*Entering test_storageCost method*' '''Please input purchase price as 1. This method will then check that storage cost is 5% of 1.''' storage = self.Stock1.calculateStorageCost() self.assertEqual(storage,0.05) def test_mul (self): print '*Entering test_mul method*' '''Make sure that user inputs 1 as unit price for this method to properly be able to test __mul__ method''' multi = self.Stock1.__mul__(2.2) self.assertAlmostEquals(multi,2.2) def tearDown(self): print '*Entering tearDown method*' pass
def __mul__(self, x): print '**Entering __mul__ CD method**' '''Method to return the price of multiple copies of a CD with a 10% discount for bulk buying.''' price = StockItem.__mul__(self, x) discount = price * 0.1 newPrice = price - discount return newPrice
def __init__(self): print '\n' print '**Entering __init__ CD method**' StockItem.__init__(self) '''This loop will continuously re-prompt the user to enter the title if it is larger than 100 characters or has no characters at all. A message will also be displayed on screen describing the errors.''' self.title = raw_input('Please enter the title of the CD: ') while True: if ExceptionCatcher.nameCheck(self.title) == True: break else: self.title = raw_input('Please enter the title of the CD: ') '''The user will only be allowed to enter a date in the correct integer format, otherwise they are caught in a loop forever being re-prompted to enter the date in the correct format''' self.dateReleased = raw_input('Please enter the date of release in the following format DDMMYYYY: ') while True: if ExceptionCatcher.dateCheck(self.dateReleased) == True: break else: self.dateReleased = raw_input('Please enter the date of release in the following format DDMMYYYY: ') '''The user is asked to enter one of three genres. They must enter the genre option properly before they are allowed to proceed.''' while True: self.genre = raw_input('Please enter the genre. "rock", "metal", "blues" are the only allowed options: ') if self.genre == 'rock' or self.genre == 'metal' or self.genre == 'blues': break else: continue self.artist = raw_input('Please enter the name of the artist: ') while True: if ExceptionCatcher.nameCheck(self.artist) == True: break else: self.artist = raw_input('Please enter the name of the artist: ')
def calculateStorageCost(self): print '**Entering calculateStorageCost Book method**' '''Method to override Storage cost method in StockItem superclass. I use the calculate Storage Cost method from the superclass and add 1 to the cost, to represent the extra storage cost of books.''' cost = StockItem.calculateStorageCost(self) bookCost = cost + 1 return bookCost
def __init__(self, titleStr, dateStr, genreStr, authorStr, clientNameStr, warehouseNumberInt, pricePerUnitFloat): # print 'Creating a book' # Initialise the StockItem class. StockItem.__init__(self, clientNameStr, warehouseNumberInt, pricePerUnitFloat) # Sets the title, test to make sure its a string and is less than 100 characters. if isinstance(titleStr, str) == True: if len(titleStr) <= 100: self.title = titleStr else: self.title = titleStr[:100] raise StockException('Book', '__init__', 'Title must be less than 100 characters.') else: raise StockException('Book', '__init__', 'Title must be a string.') # Sets the dateReleased, tests to make sure date is in a valid format. try: self.dateReleased = datetime.strptime(dateStr, "%d/%m/%Y") except ValueError: raise StockException('Book', '__init__', 'Date must be in the format DD/MM/YYYY.') # Sets the genre, tests to make sure its a string and a valid genre. if isinstance(genreStr, str) == True: if genreStr in ['Rock', 'Metal', 'Blues']: self.genre = genreStr else: self.genreStr = 'Rock' raise StockException('Book', '__init__', 'Genre must be either Rock, Metal or Blues.') else: self.genreStr = 'Rock' raise StockException('Book', '__init__', 'Genre must be a string.') # Sets the artist, tests to make sure its a string. if isinstance(authorStr, str) == True: self.author = authorStr else: raise StockException('Book', '__init__', 'Author must be a string.')
def __str__(self): print '**Entering __str__ Book method**' '''This method prints out all the information about the book, including the __str__ method from the StockItem superclass. The date is reformatted to make it more readable.''' day = str(self.dateReleased[0:2]) month = str(self.dateReleased[2:4]) year = str(self.dateReleased[4:8]) '''I call the __str__ method from superclass here. As I have overwritten the Calculate Storage Cost method of the super-class, with the special calcualteStorageCost method for books, it is the special storage costs of books that is printed out.''' print StockItem.__str__(self) print 'The book title is "%s"' %(self.title) print 'The author is %s' %(self.author) print 'The genre is %s' %(self.genre) if month == '01': month = 'January' elif month == '02': month = 'February' elif month == '03': month = 'March' elif month == '04': month = 'April' elif month == '05': month = 'May' elif month == '06': month = 'June' elif month == '07': month = 'July' elif month == '08': month = 'August' elif month == '09': month = 'September' elif month == '10': month = 'October' elif month == '11': month = 'November' elif month == '12': month = 'December' return 'The release date is %s/%s/%s' %(day, month, year)
def __str__(self): print '**Entering __str__ CD method**' '''This method prints out all the information about the CD, including the __str__ method from the StockItem superclass. The date is reformatted to make it more readable.''' day = str(self.dateReleased[0:2]) month = str(self.dateReleased[2:4]) year = str(self.dateReleased[4:8]) print StockItem.__str__(self) print 'The CD title is "%s"' %(self.title) print 'The artist is %s' %(self.artist) print 'The genre is %s' %(self.genre) if month == '01': month = 'January' elif month == '02': month = 'February' elif month == '03': month = 'March' elif month == '04': month = 'April' elif month == '05': month = 'May' elif month == '06': month = 'June' elif month == '07': month = 'July' elif month == '08': month = 'August' elif month == '09': month = 'September' elif month == '10': month = 'October' elif month == '11': month = 'November' elif month == '12': month = 'December' return 'The release date is %s/%s/%s' %(day, month, year)
def setUp(self): print '*Entering setUp method*' '''This will create a StockItem instance that can be used throughout the StockItemTest class.''' self.Stock1 = StockItem()