gnome-applets r11040 - in trunk/invest-applet: . invest



Author: callum
Date: Tue Oct  7 00:39:45 2008
New Revision: 11040
URL: http://svn.gnome.org/viewvc/gnome-applets?rev=11040&view=rev

Log:
Basic test suite for the invest applet and a generic fix for parsing errors (including bug 554425).

Added:
   trunk/invest-applet/invest/test.py
Modified:
   trunk/invest-applet/ChangeLog
   trunk/invest-applet/invest/Makefile.am
   trunk/invest-applet/invest/quotes.py

Modified: trunk/invest-applet/invest/Makefile.am
==============================================================================
--- trunk/invest-applet/invest/Makefile.am	(original)
+++ trunk/invest-applet/invest/Makefile.am	Tue Oct  7 00:39:45 2008
@@ -29,3 +29,5 @@
 	defs.py.in \
 	invest-applet.py \
 	invest-chart
+
+TESTS = test.py

Modified: trunk/invest-applet/invest/quotes.py
==============================================================================
--- trunk/invest-applet/invest/quotes.py	(original)
+++ trunk/invest-applet/invest/quotes.py	Tue Oct  7 00:39:45 2008
@@ -139,65 +139,69 @@
 		else:
 			self.quotes_valid = True
 
-		quote_items = quotes.items ()
-		quote_items.sort ()
-
-		simple_quotes_change = 0
-		self.simple_quotes_count = 0
-		self.positions_balance = 0
-		self.positions_count = 0
-
-		for ticker, val in quote_items:
-			pb = None
+		try:
+			quote_items = quotes.items ()
+			quote_items.sort ()
 			
-			# Check whether the symbol is a simple quote, or a portfolio value
-			is_simple_quote = True
-			for purchase in invest.STOCKS[ticker]:
-				if purchase["amount"] != 0:
-					is_simple_quote = False
-					break
-			
-			if is_simple_quote:
-				self.simple_quotes_count += 1
-				row = self.insert(0, [ticker, True, 0, 0, val["trade"], val["variation_pct"], pb])
-				simple_quotes_change += val['variation_pct']
-			else:
-				self.positions_count += 1
-				current = sum([purchase["amount"]*val["trade"] for purchase in invest.STOCKS[ticker] if purchase["amount"] != 0])
-				paid = sum([purchase["amount"]*purchase["bought"] + purchase["comission"] for purchase in invest.STOCKS[ticker] if purchase["amount"] != 0])
-				balance = current - paid
-				if paid != 0:
-					change = 100*balance/paid
-				else:
-					change = 100 # Not technically correct, but it will look more intuitive than the real result of infinity.
-				row = self.insert(0, [ticker, False, balance, change, val["trade"], val["variation_pct"], pb])
-				self.positions_balance += balance
-
-			if len(ticker.split('.')) == 2:
-				url = 'http://ichart.europe.yahoo.com/h?s=%s' % ticker
-			else:
-				url = 'http://ichart.yahoo.com/h?s=%s' % ticker
+			simple_quotes_change = 0
+			self.simple_quotes_count = 0
+			self.positions_balance = 0
+			self.positions_count = 0
 			
-			image_retriever = invest.chart.ImageRetriever(url)
-			image_retriever.connect("completed", self.set_pb_callback, row)
-			image_retriever.start()
-			
-		if self.simple_quotes_count > 0:
-			self.avg_simple_quotes_change = simple_quotes_change/float(self.simple_quotes_count)
-		else:
-			self.avg_simple_quotes_change = 0
-
-		if self.avg_simple_quotes_change != 0:
-			simple_quotes_change_sign = self.avg_simple_quotes_change / abs(self.avg_simple_quotes_change)
-		else:
-			simple_quotes_change_sign = 0
+			for ticker, val in quote_items:
+				pb = None
+				
+			# Check whether the symbol is a simple quote, or a portfolio value
+				is_simple_quote = True
+				for purchase in invest.STOCKS[ticker]:
+					if purchase["amount"] != 0:
+						is_simple_quote = False
+						break
+					
+					if is_simple_quote:
+						self.simple_quotes_count += 1
+						row = self.insert(0, [ticker, True, 0, 0, val["trade"], val["variation_pct"], pb])
+						simple_quotes_change += val['variation_pct']
+					else:
+						self.positions_count += 1
+						current = sum([purchase["amount"]*val["trade"] for purchase in invest.STOCKS[ticker] if purchase["amount"] != 0])
+						paid = sum([purchase["amount"]*purchase["bought"] + purchase["comission"] for purchase in invest.STOCKS[ticker] if purchase["amount"] != 0])
+						balance = current - paid
+						if paid != 0:
+							change = 100*balance/paid
+						else:
+							change = 100 # Not technically correct, but it will look more intuitive than the real result of infinity.
+							row = self.insert(0, [ticker, False, balance, change, val["trade"], val["variation_pct"], pb])
+							self.positions_balance += balance
+							
+							if len(ticker.split('.')) == 2:
+								url = 'http://ichart.europe.yahoo.com/h?s=%s' % ticker
+							else:
+								url = 'http://ichart.yahoo.com/h?s=%s' % ticker
+								
+								image_retriever = invest.chart.ImageRetriever(url)
+								image_retriever.connect("completed", self.set_pb_callback, row)
+								image_retriever.start()
+								
+								if self.simple_quotes_count > 0:
+									self.avg_simple_quotes_change = simple_quotes_change/float(self.simple_quotes_count)
+								else:
+									self.avg_simple_quotes_change = 0
+									
+									if self.avg_simple_quotes_change != 0:
+										simple_quotes_change_sign = self.avg_simple_quotes_change / abs(self.avg_simple_quotes_change)
+									else:
+										simple_quotes_change_sign = 0
+										
+									# change icon
+										if self.simple_quotes_count > 0:
+											self.change_icon_callback(simple_quotes_change_sign)
+										else:
+											positions_balance_sign = self.positions_balance/abs(self.positions_balance)
+											self.change_icon_callback(positions_balance_sign)
+		except:
+			self.quotes_valid = False
 
-		# change icon
-		if self.simple_quotes_count > 0:
-			self.change_icon_callback(simple_quotes_change_sign)
-		else:
-			positions_balance_sign = self.positions_balance/abs(self.positions_balance)
-			self.change_icon_callback(positions_balance_sign)
 
 	def set_pb_callback(self, retriever, row):
 		self.set_value(row, 6, retriever.image.get_pixbuf())

Added: trunk/invest-applet/invest/test.py
==============================================================================
--- (empty file)
+++ trunk/invest-applet/invest/test.py	Tue Oct  7 00:39:45 2008
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+
+import unittest
+
+import quotes
+import invest
+
+print """=====================================================================
+Please note that these tests will fail if you do not have any stocks
+already set up in the applet. This is an unfortunate state of affairs
+that we hope to have fixed in the near future. For now, please run
+the applet and enter at least one stock to be monitored. The default
+is just fine.
+====================================================================="""
+
+def null_function (*args):
+    pass
+
+class TestQuotes (unittest.TestCase):
+    def testQuoteUpdater_populate (self):
+        qu = quotes.QuoteUpdater (null_function, null_function)
+        # We require a pre-created invest.STOCKS for the moment (since
+        # the quotes module will load the real database). Pick the first
+        # stock as the one we test.
+        goodname = invest.STOCKS.keys()[0]
+        quote = { goodname: { "ticker": goodname, "trade": 386.91, "time": "10/3/2008", "date": "4.00pm", "variation": -3.58, "open": 397.14, "variation_pct": 10 }}
+        qu.populate (quote)
+        self.assertEqual (qu.quotes_valid, True)
+        # In response to bug 554425, try a stock that isn't in our database
+        quote = { "clearlyFake": { "ticker": "clearlyFake", "trade": 386.91, "time": "10/3/2008", "date": "4.00pm", "variation": -3.58, "open": 397.14, "variation_pct": 10 }}
+        qu.populate (quote)
+        self.assertEqual (qu.quotes_valid, False)
+
+if __name__ == '__main__':
+    unittest.main ()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]