예제 #1
0
파일: tests.py 프로젝트: pgwthf/TSB
    def test_Pool(self):
        date1 = date(2000,1,1)
        date2 = date(2010,1,5)
        date3 = date(2010,1,20)
        date4 = date(2011,1,5)
        date5 = date(2011,1,18)
        date6 = date(2012,1,4)
        aapl = Stock.objects.get(name='AAPL')
        intc = Stock.objects.get(name='INTC')
        fdx = Stock.objects.get(name='FDX')
        snp = Stock.objects.create(name='^GSPC', description='S&P500', 
                currency=Stock.US_DOLLAR, startdate=date1, enddate=date4)
        pool = Pool.objects.create(name='test', description='test', index=snp, 
                startdate=date2, enddate=date5)
        StockPoolDates.objects.create(stock=aapl, pool=pool)
        StockPoolDates.objects.create(stock=intc, pool=pool)
        StockPoolDates.objects.create(stock=fdx, pool=pool)

        print 'SIZE', pool.size()

    # _get_raw_list
        temp = StockPoolDates.objects.create(stock=fdx, pool=pool)
        raw_list = pool._get_raw_list()
        self.assertEqual(len(raw_list), 4) # does not include index 
        temp.delete()
        raw_list = pool._get_raw_list()
        self.assertEqual(len(raw_list), 3) # does not include index 


#TMP
#        pool2 = Pool.objects.create(name='test2', description='test2', 
#                index=intc, startdate=date2, enddate=date5)
#        StockPoolDates.objects.create(stock=intc, pool=pool2)
#        print 'set prices'
#        print 'WARNINGS', pool2.set_prices(date2, date5)
#        print 'INDEX', pool2.index.price.close[date3]
#        print 'INTC', intc.index.price.close[date3]
#/TMP


    # _get_list
        #

    # _get_offset_list
        dated_stock_list = pool._get_offset_list()
        self.assertEqual(len(dated_stock_list), 4) #must include index 
        for stockdates in dated_stock_list:
            self.assertEqual(stockdates[2], date5) 
            self.assertLessEqual(stockdates[1], last_year(date2))
        # change the date ranges of the stocks in the pool
        for st,sd,ed in ((aapl,date3,date6), (intc,date3,date4),
                                                        (fdx,date1,date4)):
            spd = StockPoolDates.objects.get(stock=st, pool=pool)
            spd.startdate = sd
            spd.enddate = ed
            spd.save()

        dated_stock_list = pool._get_offset_list()
        self.assertEqual(len(dated_stock_list), 4) #must include index
        for row in ((snp, last_year(date2), date5),
                    (aapl, last_year(date3), date5),
                    (intc, last_year(date3), date4),
                    (fdx, last_year(date2), date4)):
            self.assertIn(row, dated_stock_list)

        pool.enddate = None
        today = date.today()
        dated_stock_list = pool._get_offset_list()
        self.assertEqual(len(dated_stock_list), 4) #must include index
        for row in ((snp, last_year(date2), None),
                    (aapl, last_year(date3), date6),
                    (intc, last_year(date3), date4),
                    (fdx, last_year(date2), date4)):
            self.assertIn(row, dated_stock_list)

#    # validate_dates
        pool.enddate = date5
        warnings = pool.check_date_ranges()
        self.assertEqual(len(warnings), 1)
        self.assertIn('enddate', warnings[0])

        snp.set_dates(date3, date6)
        warnings = pool.check_date_ranges()
        self.assertEqual(len(warnings), 1)
        self.assertIn('startdate', warnings[0])

        snp.set_dates(date1, date6)
        warnings = pool.check_date_ranges()
        self.assertFalse(warnings)

    # _set_cache
        self.assertFalse(hasattr(pool, '_cache'))
        pool._set_cache()
        self.assertTrue(hasattr(pool, '_cache'))
        self.assertEqual(len(pool._cache), 3)
        for row in ((aapl, date3, date5),
                    (intc, date3, date4),
                    (fdx,  date2, date4)):
            self.assertIn(row, pool._cache)

    # get_cached_stocklist
        del pool._cache
        self.assertFalse(hasattr(pool, '_cache'))
        self.assertEqual(pool.get_cached_stocklist(last_year(date1)), [])
        self.assertEqual(pool.get_cached_stocklist(date1), [])
        self.assertFalse(hasattr(pool, '_cache'))
        self.assertEqual(pool.get_cached_stocklist(date2), [fdx,])
        self.assertTrue(hasattr(pool, '_cache'))
        self.assertEqual(set(pool.get_cached_stocklist(date3)), set([aapl, intc, fdx]))
        self.assertEqual(set(pool.get_cached_stocklist(date4)), set([aapl, intc, fdx]))
        self.assertEqual(pool.get_cached_stocklist(date5), [aapl,])
        self.assertEqual(pool.get_cached_stocklist(date6), [])

    #  _get_stock_index_list
        self.assertEqual(pool._get_stock_index_list(last_year(date1)), [])
        self.assertEqual(pool._get_stock_index_list(date1), [])
        self.assertEqual(set(pool._get_stock_index_list(date2)), set([snp, fdx,]))
        self.assertEqual(set(pool._get_stock_index_list(date3)), set([snp, aapl, intc, fdx]))
        self.assertEqual(set(pool._get_stock_index_list(date4)), set([snp, aapl, intc, fdx]))
        self.assertEqual(set(pool._get_stock_index_list(date5)), set([snp, aapl,]))
        self.assertEqual(pool._get_stock_index_list(date6), [])
        #

    # activate
        self.assertIsNotNone(pool.enddate)
        pool.activate()
        self.assertIsNone(pool.enddate)

    # deactivate
        self.assertRaises(ValueError, pool.deactivate)
    # download_prices
        self.assertEqual(Price.objects.all().count(), 0)
        snp.set_dates(False, None)
        pool.download_prices()

        self.assertRaises(Price.DoesNotExist, Price.objects.get, stock=snp, 
                                    date=previous_weekday(last_year(date3)))
        self.assertTrue(Price.objects.get(stock=snp, 
                                    date=next_weekday(last_year(date3))))
# why use today???
#        self.assertTrue(Price.objects.get(stock=snp, 
#                                    date=previous_weekday(today)))

        self.assertRaises(Price.DoesNotExist, Price.objects.get, stock=aapl, 
                                    date=previous_weekday(last_year(date3)))
        self.assertTrue(Price.objects.get(stock=aapl, 
                                    date=next_weekday(last_year(date3))))
        self.assertTrue(Price.objects.get(stock=aapl, 
                                    date=previous_weekday(date6)))
        self.assertRaises(Price.DoesNotExist, Price.objects.get, stock=aapl, 
                                    date=next_weekday(date6))

        self.assertRaises(Price.DoesNotExist, Price.objects.get, stock=intc, 
                                    date=previous_weekday(last_year(date3)))
        self.assertTrue(Price.objects.get(stock=intc, 
                                    date=next_weekday(last_year(date3))))
        self.assertTrue(Price.objects.get(stock=intc, 
                                    date=previous_weekday(date4)))
        self.assertRaises(Price.DoesNotExist, Price.objects.get, stock=intc, 
                                    date=next_weekday(date4))

        self.assertRaises(Price.DoesNotExist, Price.objects.get, stock=fdx, 
                                    date=previous_weekday(last_year(date2)))
        self.assertTrue(Price.objects.get(stock=fdx, 
                                    date=next_weekday(last_year(date2))))
        self.assertTrue(Price.objects.get(stock=fdx, 
                                    date=previous_weekday(date4)))
        self.assertRaises(Price.DoesNotExist, Price.objects.get, stock=fdx, 
                                    date=next_weekday(date4))

    # deactivate
        pool.deactivate()
        self.assertEqual(pool.enddate, previous_weekday(today))

    # check_prices
        self.assertEqual(pool.check_prices(), [])
        # intc,date3,date4),

        spd = StockPoolDates.objects.get(stock=intc, pool=pool)
        spd.startdate = date3
        spd.enddate = date5
        spd.save()
        self.assertEqual(len(pool.check_prices()), 1)

        spd = StockPoolDates.objects.get(stock=intc, pool=pool)
        spd.startdate = date2
        spd.enddate = date4
        spd.save()
        self.assertEqual(len(pool.check_prices()), 1)

    # get_todays_stocks_for_download
        spd = StockPoolDates.objects.get(stock=aapl, pool=pool)
        spd.enddate = None
        spd.save()
        del pool._cache
        self.assertEqual(Pool.get_todays_stocks_for_download(
                                                        Stock.US_DOLLAR), [])
        pool.activate()
        self.assertEqual(Pool.get_todays_stocks_for_download(Stock.EURO), [])
        self.assertEqual(set(Pool.get_todays_stocks_for_download(
                                    Stock.US_DOLLAR)), set([aapl, snp]))
        self.assertEqual(Pool.get_todays_stocks_for_download(
                                    Stock.BRITISH_POUND), [])

    # download_prices_today

        #
 
#        import cPickle as pickle
#        stock_list = Pool.get_todays_stocks_for_download(Stock.US_DOLLAR)
#        print stock_list
#        pickle.dump(stock_list, open('save.p', 'wb'))
        # what if stock does not exist!??

#
    # export_all
#        self.assertEqual(Pool.objects.all().count(), 1)
#        self.assertEqual(Stock.objects.all().count(), 4)
#        self.assertEqual(StockPoolDates.objects.all().count(), 3)
#        out = open("test.json", "w")
#        Pool.export_all(out)
#        out.close()
#        Pool.objects.all().delete()
#        StockPoolDates.objects.all().delete()
#        self.assertEqual(Pool.objects.all().count(), 0)
#        self.assertEqual(Stock.objects.all().count(), 4)
#        self.assertEqual(StockPoolDates.objects.all().count(), 0)
#        Pool.import_all("test.json")
#        print Pool.objects.all()
#        print Stock.objects.all()
#        print StockPoolDates.objects.all()
#        self.assertEqual(Pool.objects.all().count(), 1)
#        self.assertEqual(Stock.objects.all().count(), 4)
        self.assertEqual(StockPoolDates.objects.all().count(), 3)


    # copy
        pool = Pool.objects.get(name='test')
        new = pool.copy()
        copy = Pool.objects.get(name=new.name)
        self.assertEqual(pool.index, copy.index)
        self.assertEqual(pool.startdate, copy.startdate)
        self.assertEqual(pool.enddate, copy.enddate)
        for s_pool,s_copy in zip(pool.members.all(), copy.members.all()):
            self.assertEqual(s_pool, s_copy)
            poolmember = s_pool.stockpooldates_set.get(pool=pool)
            copymember = s_copy.stockpooldates_set.get(pool=copy)
            self.assertEqual(poolmember.startdate, copymember.startdate)
            self.assertEqual(poolmember.enddate, copymember.enddate)
            self.assertEqual(poolmember.stock, copymember.stock)
예제 #2
0
파일: tests.py 프로젝트: EdwinvO/pyutillib
 def test_previous_weekday(self):
     for day1, day2 in ((8,7), (9,7), (10,7), (11,10), (12,11), (13,12), 
                        (14,13), (15,14), (16,14), (17,14)):
         self.assertEqual(du.previous_weekday(dt.date(2000,1,day1)), 
                 dt.date(2000,1,day2))