Home > Enterprise >  Where is the error in creating the DailyData class?
Where is the error in creating the DailyData class?

Time:09-14

I am just starting to learn Python, and part of one of my assignments includes creating classes needed for a later project. It is a simple project, so there are only two classes being used:

Stock Class (info for an individual stock)
symbol Variable (stock ticker symbol)
name Variable (name of company)
shares Variable (number of shares owned)
-------- ------------------------------------
add_data(stock_data) Method (used to add a day of price and volume history to stock)
DailyData Class (info for day of stock price and volume history)
date Variable (date)
close Variable (stock closing price for the day)
volume Variable (number of shares traded for the day)

The assignment is to create the classes and then use a Unit Test to make sure the classes have the proper behavior. I successfully created the stock class, but I keep getting errors for DailyData. I'm pretty sure that it's something simple that I'm missing, but I need a nudge in the right direction.

Here is the code I have written:

class Stock:
    def __init__(self, symbol, name, shares):
        self.symbol=0
        self.name=0
        self.shares=0
        self.DataList=[]
    def add_data(self,stock_data):
        self.DataList.append(stock_data)

class DailyData:
    def __init__(self, date, close, volume):
        self.date=0
        self.close=(0)
        self.volume=(0)

Since the UnitTest was successful for the Stock part, I tried to mimic it for the DailyData, which is coming back with errors on all parts.

UnitTest Code:

# Unit Test - Do Not Change Code Below This Line *** *** *** *** *** *** *** *** ***
# main() is used for unit testing only. It will run when stock_class.py is run.
# Run this to test your class code. Once you have eliminated all errors, you are
# ready to continue with the next part of the project.

def main():
    error_count = 0
    error_list = []
    print("Unit Testing Starting---")
    # Test Add Stock
    print("Testing Add Stock...",end="")
    try:
        testStock = Stock("TEST","Test Company",100)
        print("Successful!")
    except:
        print("***Adding Stock Failed!")
        error_count = error_count 1
        error_list.append("Stock Constructor Error")
    # Test Change Symbol
    print("Test Change Symbol...",end="")
    try:
        testStock.symbol = "NEWTEST"
        if testStock.symbol == "NEWTEST":
            print("Successful!")
        else:
            print("***ERROR! Symbol change unsuccessful.")
            error_count = error_count 1
            error_list.append("Symbol Change Error")
    except:
        print("***ERROR! Symbol change failed.")
        error_count = error_count 1
        error_list.append("Symbol Change Failure")
    print("Test Change Name...",end="")
    try:
        testStock.name = "New Test Company"
        if testStock.name == "New Test Company":
            print("Successful!")
        else:
            print("***ERROR! Name change unsuccessful.")
            error_count = error_count 1
            error_list.append("Name Change Error")
    except:
        print("***ERROR! Name change failed.")
        error_count = error_count 1
        error_list.append("Name Change Failure")
        print("Test Change Name...",end="")
    try:
        testStock.shares = 2000
        if testStock.shares == 2000:
            print("Successful!")
        else:
            print("***ERROR! Shares change unsuccessful.")
            error_count = error_count 1
            error_list.append("Shares Change Error")
    except:
        print("***ERROR! Shares change failed.")
        error_count = error_count 1
        error_list.append("Shares Change Failure")
        

    # Test add daily data
    print("Creating daily stock data...",end="")
    daily_data_error = False
    try:
        dayData = DailyData("1/1/20",float(14.50),float(100000))
        testStock.add_data(dayData)
        if testStock.DataList[0].date != "1/1/20":
            error_count = error_count   1
            daily_data_error = True
            error_list.append("Add Daily Data - Problem with Date")
        if testStock.DataList[0].close != 14.50:
            error_count = error_count   1
            daily_data_error = True
            error_list.append("Add Daily Data - Problem with Closing Price")
        if testStock.DataList[0].volume != 100000:
            error_count = error_count   1
            daily_data_error = True
            error_list.append("Add Daily Data - Problem with Volume")  
    except:
        print("***ERROR! Add daily data failed.")
        error_count = error_count   1
        error_list.append("Add daily data Failure!")
        daily_data_error = True
    if daily_data_error == True:
        print("***ERROR! Creating daily data failed.")
    else:
        print("Successful!")
    
    if (error_count) == 0:
        print("Congratulations - All Tests Passed")
    else:
        print("-=== Problem List - Please Fix ===-")
        for em in error_list:
            print(em)
    print("Goodbye")

# Program Starts Here
if __name__ == "__main__":
    # run unit testing only if run as a stand-alone script
    main()

Any insight you can give would be much appreciated! Thank you!

CodePudding user response:

In your __init__ of DailyData you set all your variables to 0 regardless off what is being passed in.

So when the test does dayData = DailyData("1/1/20",float(14.50),float(100000)) those arguments are completely ignored and the variables are all set to 0. The test fails because it takes that implementation, adds it into testStock with testStock.add_data(dayData) and immediately checks to see if the date supplied makes it through with if testStock.DataList[0].date != "1/1/20":, and all it finds is a 0.

I think you may have been meaning to supply default values for the arguments, which should be done in the function definition. Consider instead:

class DailyData:
    def __init__(self, date=0, close=0, volume=0):
        self.date=date
        self.close=close
        self.volume=volume
  • Related