def newClient(self): """Creates a new client. Variables -------- client_name: expected input 'string' client_email: expected input 'string' with embedded @ symbol Exceptions --------- @raise exception:ClientException error for invalid input """ client_name = input("Please enter the client's name: ") client_email = input("Please enter the client's email address: ") if re.match("[0-9]", client_name) is not None: raise ClientException("Client name can only contain characters") if client_email.find("@") >= 0: self.last_client_id += 1 #creates client my_client = Client(self.last_client_id, client_name, client_email) #puts created clients in the diction clients self.clients[str(my_client.getID())] = my_client return my_client else: raise ClientException( "Client cannot be created; invalid email address")
def __init__(self, clients_file_name): ''' Constructor ''' self.clients_file_name = clients_file_name self.clients = {} self.last_client_id = 0 with open(self.clients_file_name, "r") as clients_file: self.last_client_id = int(clients_file.readline()) for line in clients_file: line = line.rstrip() (clt_id, clt_name, clt_email, clt_pos) = line.split(":") client = Client(int(clt_id), clt_name, clt_email) if len(clt_pos) > 0: positions = clt_pos.split(",") for position in positions: (symbol, qty, acq_date, mod_date) = position.split("|") client.addPosition( Position( symbol, int(qty), datetime.strptime(acq_date, Position.DATE_FORMAT).date(), datetime.strptime( mod_date, Position.DATE_FORMAT).date())) self.clients[clt_id] = client
def setUp(self): self.yahoo = YahooServer('../trades.conf') self.alpha = Alphavantage('../alpha.conf') mock = ServerMock.MockAlphaServer() self.price_server=PriceServer(mock) self.json_regex=re.compile(r"^\{.*\}$", RegexFlag.DOTALL) self.url_regex=re.compile(r"https://.*/.*?", RegexFlag.DOTALL) self.client = Client(1, "Adil", "*****@*****.**")
def newClient(self): client_name = input("Please enter the client's name: ") client_email = input("Please enter the client's email address: ") if client_email.find("@") >= 0: self.last_client_id += 1 my_client = Client(self.last_client_id, client_name, client_email) self.clients[str(my_client.getID())] = my_client return my_client else: raise ClientException( "Client cannot be created; invalid email address")
def setUp(self): """Sets URL to connect to the Yahoo and Alphavanatage servers. """ self.yahoo = YahooServer('../trades.conf') self.alpha = Alphavantage('../alpha.conf') mock = ServerMock.MockAlphaServer() self.price_server = PriceServer(mock) self.json_regex = re.compile( r"^\{.*\}$", re.DOTALL) #match everything from the start to the end self.url_regex = re.compile( r"https://.*/.*?", re.DOTALL) #match everything including the // self.client = Client(1, "Adil", "*****@*****.**") #example client
def __init__(self, clients_file_name): """Constructor takes argument for clients file name to initialise super variable self.clients_file_name, creates a dictionary to store clients and a variable to store the last client id in the dictionary. """ self.clients_file_name = clients_file_name self.clients = {} self.last_client_id = 0 with open(self.clients_file_name, "r") as clients_file: #Read the last number of client stored in the file self.last_client_id = int(clients_file.readline()) for line in clients_file: #remove the extra spaces before and after string line = line.rstrip() #split properties using ':' (clt_id, clt_name, clt_email, clt_pos) = line.split(":") #store client in trades.Client class client = Client(int(clt_id), clt_name, clt_email) if len(clt_pos) > 0: #if positions greater than 1 split using a ',' positions = clt_pos.split(",") for position in positions: #do another split of each position property using a '|' (symbol, qty, acq_date, mod_date) = position.split("|") #add client position by calling the addPosition() function and pass arguments client.addPosition( Position( symbol, int(qty), datetime.strptime(acq_date, Position.DATE_FORMAT).date(), datetime.strptime( mod_date, Position.DATE_FORMAT).date())) #store client in our dictionary named client self.clients[clt_id] = client
class MarketDataTest(unittest.TestCase): def setUp(self): self.yahoo = YahooServer('../trades.conf') self.alpha = Alphavantage('../alpha.conf') mock = ServerMock.MockAlphaServer() self.price_server=PriceServer(mock) self.json_regex=re.compile(r"^\{.*\}$", RegexFlag.DOTALL) self.url_regex=re.compile(r"https://.*/.*?", RegexFlag.DOTALL) self.client = Client(1, "Adil", "*****@*****.**") def test_connect_to_Yahoo_server(self): url=self.yahoo.getURL() self.assertRegex(url, self.url_regex, 'URL of server should match a REGEX') @unittest.skip("skipping yahoo json test") def test_get_Yahoo_data_as_json(self): self.assertTrue('chart' in self.yahoo.getDataAsJSON(), 'Data from server looks like a json object') @unittest.skip("skipping yahoo text test") def test_get_Yahoo_data_as_text(self): self.assertRegex(self.yahoo.getDataAsText(), self.json_regex, 'Text from server looks like a json object') def test_connect_to_Alphavantage_server(self): url = self.alpha.getURL() self.assertRegex(url, 'https://.*/.*?', 'URL of server should match a REGEX') @unittest.skip("skipping alpha json test") def test_get_Alphavantage_data_as_json(self): self.assertTrue('Meta Data' in self.alpha.getDataAsJSON(), 'Data from server looks like a json object') @unittest.skip("skipping alpha text test") def test_get_Alphavantage_data_as_text(self): self.assertRegex(self.alpha.getDataAsText(), self.json_regex, 'Text from server looks like a json object') #@unittest.skip("skipping today's price test") @unittest.expectedFailure(PriceUnavailableEx) def test_tody_price_by_symbol(self): self.assertEqual(self.price_server.getTodaySecurityPriceBySymbol("MSFT"), '83.8700', 'todays price') #@unittest.skip("skipping last recorded price test") #@unittest.expectedFailure def test_last_recorded_price_by_symbol(self): self.assertEqual(self.price_server.getLastRecordedPriceBySymbol("MSFT"), '83.8700', 'todays price') #@unittest.skip("skipping creating a client test") #@unittest.expectedFailure def test_create_client(self): client = Client(1, "Adil", "*****@*****.**") self.assertEqual("Adil", client.getName(), "Clent created successfully") #@unittest.skip("skipping adding a position") #@unittest.expectedFailure def test_add_position(self): self.client.addPosition(Position("GOOG", 100, datetime.datetime.now())) self.assertEqual(100, self.client.getPosition("GOOG").getQuantity()) self.client.addPosition(Position("GOOG", 100, datetime.datetime.now())) self.assertEqual(200, self.client.getPosition("GOOG").getQuantity())
def test_create_client(self): client = Client(1, "Adil", "*****@*****.**") self.assertEqual("Adil", client.getName(), "Clent created successfully")
class MarketDataTest(unittest.TestCase): """Made up of testcase function for market data test.""" def setUp(self): """Sets URL to connect to the Yahoo and Alphavanatage servers. """ self.yahoo = YahooServer('../trades.conf') self.alpha = Alphavantage('../alpha.conf') mock = ServerMock.MockAlphaServer() self.price_server = PriceServer(mock) self.json_regex = re.compile( r"^\{.*\}$", re.DOTALL) #match everything from the start to the end self.url_regex = re.compile( r"https://.*/.*?", re.DOTALL) #match everything including the // self.client = Client(1, "Adil", "*****@*****.**") #example client def test_connect_to_Yahoo_server(self): """Get Yahoo server Url and check against REGEX for a match. if there is no match this test is failed """ url = self.yahoo.getURL() self.assertRegex(url, self.url_regex, 'URL of server should match a REGEX') def test_connect_to_Alphavantage_server(self): """Get Yahoo server Url and check against REGEX for a match. if there is no match this test is failed """ url = self.alpha.getURL() self.assertRegex(url, 'https://.*/.*?', 'URL of server should match a REGEX') #@unittest.skip("skipping yahoo json test") def test_get_Yahoo_data_as_json(self): self.assertTrue('chart' in self.yahoo.getDataAsJSON(), 'Data from server looks like a json object') #@unittest.skip("skipping yahoo text test") def test_get_Yahoo_data_as_text(self): self.assertRegex(self.yahoo.getDataAsText(), self.json_regex, 'Text from server looks like a json object') #@unittest.skip("skipping alpha json test") def test_get_Alphavantage_data_as_json(self): self.assertTrue('Meta Data' in self.alpha.getDataAsJSON(), 'Data from server looks like a json object') #@unittest.skip("skipping alpha text test") def test_get_Alphavantage_data_as_text(self): self.assertRegex(self.alpha.getDataAsText(), self.json_regex, 'Text from server looks like a json object') #@unittest.skip("skipping today's price test") @unittest.expectedFailure(PriceUnavailableEx) def test_tody_price_by_symbol(self): """Checks todays price against '83.8700' for equality EXception: @raise PriceUnvailable exception """ self.assertEqual( self.price_server.getTodaySecurityPriceBySymbol("MSFT"), '83.8700', 'todays price') #@unittest.skip("skipping last recorded price test") #@unittest.expectedFailure def test_last_recorded_price_by_symbol(self): """Checks todays price against '83.8700' for equality. Note ---- AN expected failure is possible if no price is recorder for symbol """ self.assertEqual( self.price_server.getLastRecordedPriceBySymbol("MSFT"), '83.8700', 'todays price') #@unittest.skip("skipping creating a client test") #@unittest.expectedFailure def test_create_client(self): """Test create client and print success message""" client = Client(1, "Adil", "*****@*****.**") self.assertEqual("Adil", client.get_name(), "Clent created successfully") #@unittest.skip("skipping adding a position") #@unittest.expectedFailure def test_add_position(self): """Add position to symbol for client and check if quantity matches.""" self.client.addPosition(Position("GOOG", 100, datetime.datetime.now())) self.assertEqual(100, self.client.getPosition("GOOG").getQuantity()) self.client.addPosition(Position("GOOG", 100, datetime.datetime.now())) self.assertEqual(200, self.client.getPosition("GOOG").getQuantity())
def test_create_client(self): """Test create client and print success message""" client = Client(1, "Adil", "*****@*****.**") self.assertEqual("Adil", client.get_name(), "Clent created successfully")