예제 #1
0
 def makePositions(self, test, use_transaction=True):
     session = yield self.create(test, use_transaction)
     instruments = yield session.query(Instrument).all()
     funds = yield session.query(Fund).all()
     if use_transaction:
         with session.begin() as t:
             for f in funds:
                 insts = populate('choice', self.pos_len,
                                  choice_from=instruments)
                 for dt in self.dates:
                     for inst in insts:
                         t.add(Position(instrument=inst, dt=dt, fund=f,
                                        size=randint(-100000, 100000)))
         yield t.on_result
     else:
         for f in funds:
             insts = populate('choice', self.pos_len,
                              choice_from=instruments)
             for dt in self.dates:
                 for inst in insts:
                     yield Position(instrument=inst, dt=dt, fund=f,
                                    size=randint(-100000, 100000)).save()
     #
     self.num_pos = yield session.query(Position).count()
     yield session
예제 #2
0
 def makePositions(self, test, use_transaction=True):
     session = yield self.create(test, use_transaction)
     instruments = yield session.query(Instrument).all()
     funds = yield session.query(Fund).all()
     if use_transaction:
         with session.begin() as t:
             for f in funds:
                 insts = populate('choice',
                                  self.pos_len,
                                  choice_from=instruments)
                 for dt in self.dates:
                     for inst in insts:
                         t.add(
                             Position(instrument=inst,
                                      dt=dt,
                                      fund=f,
                                      size=randint(-100000, 100000)))
         yield t.on_result
     else:
         for f in funds:
             insts = populate('choice',
                              self.pos_len,
                              choice_from=instruments)
             for dt in self.dates:
                 for inst in insts:
                     yield Position(instrument=inst,
                                    dt=dt,
                                    fund=f,
                                    size=randint(-100000, 100000)).save()
     #
     self.num_pos = yield session.query(Position).count()
     yield session
예제 #3
0
 def setUp(self):
     size = self.sizes.get(getattr(self,'test_size','normal'))
     inst_names = populate('string',size, min_len = 5, max_len = 20)
     inst_types = populate('choice',size, choice_from = insts_types)
     inst_ccys  = populate('choice',size, choice_from = ccys_types)
     with transaction(Instrument) as t:
         for name,typ,ccy in zip(inst_names,inst_types,inst_ccys):
             Instrument(name = name, type = typ, ccy = ccy).save(t)
예제 #4
0
 def generate(self, fieldtype='string', **kwargs):
     fsize, dsize = self.size
     if fieldtype == 'date':
         self.fields = populate('date', fsize,
                                start=date(1971, 12, 30),
                                end=date.today())
     else:
         self.fields = populate('string', fsize, min_len=5, max_len=30)
     self.data = populate('string', fsize, min_len=dsize, max_len=dsize)
예제 #5
0
 def generate(self, min_len=10, max_len=20, **kwargs):
     self.keys = populate('string',
                          self.size,
                          min_len=min_len,
                          max_len=max_len)
     self.values = populate('string',
                            self.size,
                            min_len=min_len + 10,
                            max_len=max_len + 20)
예제 #6
0
 def setUpClass(cls):
     d1 = populate('float', size = 20)
     d2 = populate('float', size = 20)
     cls.fields = {'a':d1,'b':d2}
     dates = [date(2010,1,i+1) for i in range(20)]
     d1[3] = d1[0] = d1[18] = nan
     d2[3] =  d2[9] = nan
     cls.data = [(dt,{'a':a,'b':b}) for dt,a,b in zip(dates,d1,d2)]
     cls.ColumnTS = ColumnTS
     super(TestMissingValues, cls).setUpClass()
예제 #7
0
 def generate(self, fieldtype='string', **kwargs):
     fsize, dsize = self.size
     if fieldtype == 'date':
         self.fields = populate('date',
                                fsize,
                                start=date(1971, 12, 30),
                                end=date.today())
     else:
         self.fields = populate('string', fsize, min_len=5, max_len=30)
     self.data = populate('string', fsize, min_len=dsize, max_len=dsize)
예제 #8
0
 def fill(self):
     for name, curncy, typ in izip(instnames, instccys, insttypes):
         FinIns(name=name, ccy=curncy, type=typ).save(False)
     FinIns.commit()
     n = 0
     t = timer()
     for name, ccy, group in izip(fundnames, fundccys, fundgroups):
         holder = PortfolioHolder(name=name, ccy=ccy, group=group).save()
         for dt in dates:
             n += 1
             Portfolio(holder=holder, dt=dt).save()
     logger.info('Built %s master portfolios in %s seconds' %
                 (n, timer() - t))
     funds = Portfolio.objects.all()
     finins = list(FinIns.objects.all())
     n = 0
     t = timer()
     for fund in funds:
         npos = randint(MIN_POSITIONS_PER_FUND, MAX_POSITIONS_PER_FUND)
         for inst in populate('choice', npos, choice_from=finins):
             pos = Position.objects.filter(portfolio=fund, instrument=inst)
             if not pos:
                 n += 1
                 fund.addnewposition(inst,
                                     size=randint(10, 10000),
                                     value=uniform(20000, 100000))
     logger.info('Built %s positions in %s seconds' % (n, timer() - t))
예제 #9
0
 def testInSearch(self):
     words = self.make_items(num = 30, content = True)
     sw = ' '.join(populate('choice', 5, choice_from = words))
     res1 = Item.objects.search(sw)
     res2 = Item.objects.search(sw, lookup = 'in')
     self.assertTrue(res2)
     self.assertTrue(res1.count() < res2.count())
예제 #10
0
 def testInSearch(self):
     models = self.mapper
     query = models.item.query()
     sw = ' '.join(populate('choice', 5, choice_from=self.words))
     res1 = yield query.search(sw).all()
     res2 = yield query.search(sw, lookup='in').all()
     self.assertTrue(res2)
     self.assertTrue(len(res1) < len(res2))
예제 #11
0
 def testInSearch(self):
     models = self.mapper
     query = models.item.query()
     sw = ' '.join(populate('choice', 5, choice_from=self.words))
     res1 = yield query.search(sw).all()
     res2 = yield query.search(sw, lookup='in').all()
     self.assertTrue(res2)
     self.assertTrue(len(res1) < len(res2))
예제 #12
0
 def __test_big_search(self):
     #TODO:
     #this test sometimes fails. Need to be fixed
     models = self.mapper
     sw = ' '.join(populate('choice', 1, choice_from=self.words))
     qs = yield models.item.search(sw).all()
     self.assertTrue(qs)
     for item in qs:
         self.assertTrue(sw in item.name or sw in item.content)
예제 #13
0
 def __test_big_search(self):
     #TODO:
     #this test sometimes fails. Need to be fixed
     models = self.mapper
     sw = ' '.join(populate('choice', 1, choice_from=self.words))
     qs = yield models.item.search(sw).all()
     self.assertTrue(qs)
     for item in qs:
         self.assertTrue(sw in item.name or sw in item.content)
예제 #14
0
 def create(self, test):
     session = test.session()
     with session.begin() as t:
         for name, typ, ccy in zip(self.inames, self.itypes, self.iccys):
             t.add(Instrument(name=name, type=typ, ccy=ccy))
         for name in self.gnames:
             t.add(Group(name=name))
         for name, ccy in zip(self.inames, self.iccys):
             t.add(Fund(name=name, ccy=ccy))
     with session.begin() as t:
         for i in test.session().query(Instrument).load_only('id'):
             t.add(ObjectAnalytics(model_type=Instrument, object_id=i.id))
         for i in test.session().query(Fund).load_only('id'):
             t.add(ObjectAnalytics(model_type=Fund, object_id=i.id))
     obj_len = self.size[1]
     groups = populate('choice', obj_len, choice_from=session.query(Group))
     objs = populate('choice', obj_len, choice_from=session.query(ObjectAnalytics))
     with test.session().begin() as t:
         for g, o in zip(groups, objs):
             t.add(AnalyticData(group=g, object=o))
예제 #15
0
 def makePositions(self):
     '''Create Positions objects which hold foreign key to instruments and funds'''
     instruments = Instrument.objects.all()
     n = 0
     for f in Fund.objects.all():
         insts = populate('choice',POS_LEN,choice_from = instruments)
         for dt in dates:
             for inst in insts:
                 n += 1
                 Position(instrument = inst, dt = dt, fund = f).save(False)
     Position.commit()
     return n
예제 #16
0
파일: data.py 프로젝트: kuno/python-stdnet
 def makePositions(self, test, use_transaction = True):
     self.create(test, use_transaction)
     session = test.session()
     instruments = session.query(Instrument).all()
     if use_transaction:
         with session.begin():
             for f in session.query(Fund):
                 insts = populate('choice', self.pos_len,
                                  choice_from = instruments)
                 for dt in self.dates:
                     for inst in insts:
                         session.add(Position(instrument = inst, dt = dt,
                                              fund = f))
     else:
         for f in Fund.objects.query(Fund):
             insts = populate('choice', self.pos_len,
                             choice_from = instruments)
             for dt in self.dates:
                 for inst in insts:
                     Position(instrument = inst, dt = dt, fund = f).save()
                     
     self.num_pos = session.query(Position).count()
예제 #17
0
    def makePositions(self):
        '''Create Positions objects which hold foreign key to instruments
 and funds.'''
        session = self.session()
        instruments = session.query(Instrument)
        n = 0
        with session.begin():
            for f in session.query(Fund):
                insts = populate('choice',POS_LEN,choice_from = instruments)
                for dt in dates:
                    for inst in insts:
                        n += 1
                        session.add(Position(instrument = inst, dt = dt,
                                             fund = f))
        return n
예제 #18
0
def finance_data(inst_len = INST_LEN, fund_len = FUND_LEN,
                 num_dates = NUM_DATES):
    return (
            populate('string',inst_len, min_len = 5, max_len = 20),
            populate('choice',inst_len, choice_from = insts_types),
            populate('choice',inst_len, choice_from = ccys_types),
            populate('string',fund_len, min_len = 5, max_len = 20),
            populate('choice',fund_len, choice_from = ccys_types),
            populate('date',num_dates,start=datetime.date(2009,6,1),
                     end=datetime.date(2010,6,6))
            )
예제 #19
0
    def testFollowersTransaction(self):
        """Add followers to a user"""
        # unwind queryset here since we are going to use it in a double loop
        users = list(User.objects.all())
        N = len(users)

        # Follow users
        with transaction(User) as t:
            for user in users:
                n = randint(MIN_FOLLOWERS, MAX_FOLLOWERS)
                following = user.following
                for tofollow in populate("choice", n, choice_from=users):
                    following.add(tofollow, transaction=t)

        for user in users:
            for following in user.following.all():
                self.assertTrue(user in following.followers.all())
예제 #20
0
 def testFollowers(self):
     '''Add followers to a user'''
     users = User.objects.all()
     N = users.count()
     
     # Follow users
     for user in users:
         n = randint(MIN_FOLLOWERS,MAX_FOLLOWERS)
         for tofollow in populate('choice',n, choice_from = users):
             if tofollow.id != user.id:
                 user.following.add(tofollow)
         user.save()
         self.assertTrue(user.following.size()>0)
     
     for user in users:
         for following in user.following:
             self.assertTrue(user in following.followers)
예제 #21
0
    def testFollowers(self):
        '''Add followers to a user'''
        users = User.objects.all()
        N = users.count()

        # Follow users
        for user in users:
            n = randint(MIN_FOLLOWERS, MAX_FOLLOWERS)
            for tofollow in populate('choice', n, choice_from=users):
                if tofollow.id != user.id:
                    user.following.add(tofollow)
            user.save()
            self.assertTrue(user.following.size() > 0)

        for user in users:
            for following in user.following:
                self.assertTrue(user in following.followers)
예제 #22
0
 def generate(self, insts_types=None, ccys_types=None, **kwargs):
     inst_len, fund_len, pos_len, num_dates = self.size
     insts_types = insts_types or INSTS_TYPES
     ccys_types = ccys_types or CCYS_TYPES
     self.pos_len = pos_len
     self.inst_names = populate('string', inst_len, min_len=5, max_len=20)
     self.inst_types = populate('choice', inst_len, choice_from=insts_types)
     self.inst_ccys = populate('choice', inst_len, choice_from=ccys_types)
     self.fund_names = populate('string', fund_len, min_len=5, max_len=20)
     self.fund_ccys = populate('choice', fund_len, choice_from=ccys_types)
     self.dates = populate('date', num_dates, start=date(2009, 6, 1),
                           end=date(2010, 6, 6))
예제 #23
0
 def generate(self, insts_types=None, ccys_types=None, **kwargs):
     inst_len, fund_len, pos_len, num_dates = self.size
     insts_types = insts_types or INSTS_TYPES
     ccys_types = ccys_types or CCYS_TYPES
     self.pos_len = pos_len
     self.inst_names = populate('string', inst_len, min_len=5, max_len=20)
     self.inst_types = populate('choice', inst_len, choice_from=insts_types)
     self.inst_ccys = populate('choice', inst_len, choice_from=ccys_types)
     self.fund_names = populate('string', fund_len, min_len=5, max_len=20)
     self.fund_ccys = populate('choice', fund_len, choice_from=ccys_types)
     self.dates = populate('date',
                           num_dates,
                           start=date(2009, 6, 1),
                           end=date(2010, 6, 6))
예제 #24
0
파일: data.py 프로젝트: kuno/python-stdnet
 def __init__(self,
              size = 'normal',
              insts_types = None,
              ccys_types = None):
     inst_len, fund_len, pos_len, num_dates = self.sizes.get(size)
     insts_types = insts_types or INSTS_TYPES
     ccys_types = ccys_types or CCYS_TYPES
     self.pos_len = pos_len
     self.inst_names = populate('string',inst_len, min_len = 5, max_len = 20)
     self.inst_types = populate('choice',inst_len, choice_from = insts_types)
     self.inst_ccys = populate('choice',inst_len, choice_from = ccys_types)
     self.fund_names = populate('string',fund_len, min_len = 5, max_len = 20)
     self.fund_ccys = populate('choice',fund_len, choice_from = ccys_types)
     self.dates =  populate('date',num_dates,start=datetime.date(2009,6,1),
                      end=datetime.date(2010,6,6))
예제 #25
0
 def testFollowersTransaction(self):
     '''Add followers to a user'''
     # unwind queryset here since we are going to use it in a double loop
     models = self.mapper
     session = models.session()
     users = yield models.user.query(session).all()
     N = len(users)
     # Follow users
     with session.begin() as t:
         for user in users:
             self.assertEqual(user.session, session)
             N = self.data.followers()
             following = user.following
             for tofollow in populate('choice', N, choice_from=users):
                 following.add(tofollow)
     yield t.on_result
     for user in users:
         following = yield user.following.query().all()
         for user2 in following:
             group = yield user2.followers.query()
             self.assertTrue(user in group)
예제 #26
0
 def testFollowersTransaction(self):
     '''Add followers to a user'''
     # unwind queryset here since we are going to use it in a double loop
     models = self.mapper
     session = models.session()
     users = yield models.user.query(session).all()
     N = len(users)
     # Follow users
     with session.begin() as t:
         for user in users:
             self.assertEqual(user.session, session)
             N = self.data.followers()
             following = user.following
             for tofollow in populate('choice', N, choice_from=users):
                 following.add(tofollow)
     yield t.on_result
     for user in users:
         following = yield user.following.query().all()
         for user2 in following:
             group = yield user2.followers.query()
             self.assertTrue(user in group)
예제 #27
0
 def testFollowers(self):
     '''Add followers to a user'''
     # unwind queryset here since we are going to use it in a double loop
     models = self.mapper
     users = yield models.user.query().all()
     N = len(users)
     count = []
     # Follow users
     for user in users:
         N = self.data.followers()
         uset = set()
         for tofollow in populate('choice', N, choice_from=users):
             uset.add(tofollow)
             user.following.add(tofollow)
         count.append(len(uset))
         self.assertTrue(user.following.query().count() > 0)
     #
     for user, N in zip(users, count):
         all_following = user.following.query()
         self.assertEqual(all_following.count(), N)
         for following in all_following:
             self.assertTrue(user in following.followers.query())
예제 #28
0
    def testFollowers(self):
        """Add followers to a user"""
        # unwind queryset here since we are going to use it in a double loop
        users = list(User.objects.all())
        N = len(users)

        count = []
        # Follow users
        for user in users:
            n = randint(MIN_FOLLOWERS, MAX_FOLLOWERS)
            uset = set()
            for tofollow in populate("choice", n, choice_from=users):
                uset.add(tofollow)
                user.following.add(tofollow)
            count.append(len(uset))
            self.assertTrue(user.following.all().count() > 0)

        for user, N in zip(users, count):
            all_following = user.following.all()
            self.assertEqual(all_following.count(), N)
            for following in all_following:
                self.assertTrue(user in following.followers.all())
예제 #29
0
 def testFollowers(self):
     '''Add followers to a user'''
     # unwind queryset here since we are going to use it in a double loop
     models = self.mapper
     users = yield models.user.query().all()
     N = len(users)
     count = []
     # Follow users
     for user in users:
         N = self.data.followers()
         uset = set()
         for tofollow in populate('choice', N, choice_from=users):
             uset.add(tofollow)
             user.following.add(tofollow)
         count.append(len(uset))
         self.assertTrue(user.following.query().count()>0)
     #
     for user, N in zip(users, count):
         all_following = user.following.query()
         self.assertEqual(all_following.count(), N)
         for following in all_following:
             self.assertTrue(user in following.followers.query())
예제 #30
0
    def make_items(self, num=30, content=False, related=None):
        '''Bulk creation of Item for testing search engine. Return a set
of words which have been included in the Items.'''
        names = populate('choice', num, choice_from=basic_english_words)
        session = self.session()
        words = set()
        if content:
            contents = WORDS_GROUPS(num)
        else:
            contents = ['']*num
        with session.begin():
            for name, content in zip(names,contents):
                if len(name) > 3:
                    words.add(name)
                    if content:
                        words.update(content.split())
                    session.add(Item(name=name,
                                     counter=randint(0,10),
                                     content=content,
                                     related=related))
        wis = WordItem.objects.for_model(Item)
        self.assertTrue(wis.count())
        return words
예제 #31
0
파일: portfolio.py 프로젝트: OspreyX/flow
 def fill(self):
     for name,curncy,typ in izip(instnames,instccys,insttypes):
         FinIns(name = name, ccy = curncy, type = typ).save(False)
     FinIns.commit()
     n = 0
     t = timer()
     for name,ccy,group in izip(fundnames,fundccys,fundgroups):
         holder = PortfolioHolder(name = name, ccy = ccy, group = group).save()
         for dt in dates:
             n+=1
             Portfolio(holder = holder, dt = dt).save()
     logger.info('Built %s master portfolios in %s seconds' % (n,timer()-t))
     funds  = Portfolio.objects.all()
     finins = list(FinIns.objects.all())
     n      = 0
     t      = timer()
     for fund in funds:
         npos = randint(MIN_POSITIONS_PER_FUND,MAX_POSITIONS_PER_FUND)
         for inst in populate('choice',npos,choice_from = finins):
             pos = Position.objects.filter(portfolio = fund, instrument = inst)
             if not pos:
                 n += 1
                 fund.addnewposition(inst, size = randint(10,10000), value = uniform(20000,100000))
     logger.info('Built %s positions in %s seconds' % (n,timer()-t))
예제 #32
0
import ccy
from jflow import api, test
from jflow.db.instdata.populate import stocks_from_index
from jflow.db.portfolio.models import *

# Number of instruments
NUMINSTS = 100
GROUPS = 3
NUMFUNDS = 5
NUMDATES = 2
MIN_POSITIONS_PER_FUND = 10
MAX_POSITIONS_PER_FUND = 30

#Create Random Data
ccys = ccy.all()
instnames = populate('string', NUMINSTS, min_len=5, max_len=12)
insttypes = populate('choice',
                     NUMINSTS,
                     choice_from=['equity', 'bond', 'future'])
instccys = populate('choice', NUMINSTS, choice_from=ccys)

groupnames = populate('string', GROUPS, min_len=2, max_len=5)

fundnames = populate('string', NUMFUNDS, min_len=5, max_len=10)
fundccys = populate('choice', NUMFUNDS, choice_from=['EUR', 'GBP', 'USD'])
fundgroups = populate('choice', NUMFUNDS, choice_from=groupnames)
dates = populate('date', NUMDATES, start=date.today() - timedelta(3 * 360))


class FinInsTest(test.TestCase):
    def setUp(self):
예제 #33
0
 def setUp(self):
     keys = populate(datatype='date', size=100)
     vals = populate(datatype='string', size=100)
     self.data = izip(keys, vals)
예제 #34
0
from datetime import datetime
from itertools import izip
from random import randint

from stdnet.test import TestCase
from stdnet.utils import populate

from examples.models import User, Post

NUM_USERS = 100
MIN_FOLLOWERS = 10
MAX_FOLLOWERS = 30

usernames = populate('string', NUM_USERS, min_len=5, max_len=20)
passwords = populate('string', NUM_USERS, min_len=8, max_len=20)


class TestTwitter(TestCase):
    def setUp(self):
        self.orm.register(User)
        self.orm.register(Post)
        for username, password in izip(usernames, passwords):
            User(username=username, password=password).save(False)
        User.commit()

    def testFollowers(self):
        '''Add followers to a user'''
        users = User.objects.all()
        N = users.count()

        # Follow users
예제 #35
0
파일: bench.py 프로젝트: kuno/python-stdnet
__benchmark__ = True
from datetime import datetime, date

from stdnet import test
from stdnet.apps.timeseries.tests.models import TimeSeries, HashTimeSeries
from stdnet.utils import populate, todate, zip


NUM_DATES = 1000

names     = populate('string',NUM_DATES, min_len=6, max_len=15)
dates     = populate('date',NUM_DATES)
dates2    = populate('date',NUM_DATES,start=date(2009,1,1),end=date(2010,1,1))
values    = populate('float',NUM_DATES, start = 10, end = 400)
alldata   = zip(dates,values)
alldata2  = zip(dates2,values)
testdata  = dict(alldata)
testdata2 = dict(alldata2)

class UpdateTimeSerie(test.TestCase):
    model = TimeSeries
    number = 100
    tag    = 'ts'
    def register(self):
        self.names = iter(names)
        self.orm.register(self.model)
    
    def __str__(self):
        return '%s(%s)' % (self.__class__.__name__,NUM_DATES*self.number)
    
    def run(self):
예제 #36
0
from random import randint

from stdnet import test, orm
from stdnet.utils import populate, zip, range

from examples.models import SimpleModel


SIZE = 200
sports = ['football','rugby','swimming','running','cycling']

codes = set(populate('string',SIZE, min_len = 5, max_len = 20))
SIZE = len(codes)
groups = populate('choice',SIZE,choice_from=sports)
codes = list(codes)

def randomcode(num = 1):
    a = set()
    while len(a) < num:
        a.add(codes[randint(0,len(codes)-1)])
    if num == 1:
        return tuple(a)[0]
    else:
        return a


class TestUniqueFilter(test.TestCase):
    model = SimpleModel
    
    def setUp(self):
        session = self.session()
예제 #37
0
import random

from stdnet import odm, InvalidTransaction
from examples.models import SimpleModel, Dictionary
from stdnet.utils import test, populate

LEN = 100
names = populate('string', LEN, min_len=5, max_len=20)


class TransactionReceiver(object):
    def __init__(self):
        self.transactions = []

    def __call__(self, signal, sender, instances=None, session=None, **kwargs):
        self.transactions.append((sender, instances))


class TestTransactions(test.TestWrite):
    model = SimpleModel

    def setUp(self):
        models = self.mapper
        self.receiver = TransactionReceiver()
        models.post_commit.bind(self.receiver, self.model)

    def testCreate(self):
        session = self.session()
        query = session.query(self.model)
        with session.begin() as t:
            self.assertEqual(t.session, session)
예제 #38
0
from itertools import izip
from datetime import date
from random import uniform

from stdnet.test import TestCase
from stdnet.contrib.timeserie.utils import dategenerator, default_parse_interval
from stdnet.utils import populate

from models import TimeSerie


NUM_DATES = 300

dates    = populate('date',NUM_DATES)
values   = populate('float',NUM_DATES, start = 10, end = 400)
alldata  = list(izip(dates,values))
testdata = dict(alldata)


class TestTimeSerie(TestCase):
    
    def setUp(self):
        self.orm.register(TimeSerie)
        TimeSerie(ticker = 'GOOG').save()
        
    def get(self, ticker = 'GOOG'):
        return TimeSerie.objects.get(ticker = ticker)
        
    def filldata(self):
        d = self.get()
        d.data.update(testdata)
예제 #39
0
 def testSearch(self):
     engine = self.mapper.search_engine
     text = ' '.join(populate('choice', 1, choice_from=self.words))
     result = yield engine.search(text)
     self.assertTrue(result)
예제 #40
0
from copy import copy
from itertools import izip

from stdnet import FieldError
from stdnet.test import TestCase
from stdnet.utils import populate

from examples.models import SimpleList

elems = populate('string', 200)


class TestLListField(TestCase):
    
    def setUp(self):
        self.orm.register(SimpleList)
        
    def testPushBackPopBack(self):
        li = SimpleList()
        self.assertEqual(li.id,None)
        li.save()
        names = li.names
        for elem in elems:
            names.push_back(elem)
        li.save()
        self.assertEqual(li.names.size(),len(elems))
        for elem in reversed(elems):
            self.assertEqual(li.names.pop_back(),elem)
        self.assertEqual(li.names.size(),0)
    
    def testPushFrontPopFront(self):
예제 #41
0
 def setUp(self):
     self.data = populate(datatype='date', size=100)
예제 #42
0
from datetime import datetime
from itertools import izip

from stdnet.test import TestCase
from stdnet.utils import populate

from examples.models import Calendar, DateValue

NUM_DATES = 100

dates = populate('date', NUM_DATES)
values = populate('string', NUM_DATES, min_len=10, max_len=120)


class TestOrderedSet(TestCase):
    def setUp(self):
        self.orm.register(Calendar)
        self.orm.register(DateValue)
        ts = Calendar(name='MyCalendar').save()
        for dt, value in izip(dates, values):
            ts.add(dt, value)
        ts.save()

    def testOrder(self):
        ts = Calendar.objects.get(name='MyCalendar')
        self.assertEqual(ts.data.size(), NUM_DATES)
        dprec = None
        for event in ts.data:
            if dprec:
                self.assertTrue(event.dt >= dprec)
            dprec = event.dt
예제 #43
0
from copy import copy
from itertools import izip

from stdnet import FieldError
from stdnet.test import TestCase
from stdnet.utils import populate

from examples.models import SimpleList

elems = populate('string', 200)


class TestLListField(TestCase):
    def setUp(self):
        self.orm.register(SimpleList)

    def testPushBackPopBack(self):
        li = SimpleList()
        self.assertEqual(li.id, None)
        li.save()
        names = li.names
        for elem in elems:
            names.push_back(elem)
        li.save()
        self.assertEqual(li.names.size(), len(elems))
        for elem in reversed(elems):
            self.assertEqual(li.names.pop_back(), elem)
        self.assertEqual(li.names.size(), 0)

    def testPushFrontPopFront(self):
        li = SimpleList().save()
예제 #44
0
파일: list.py 프로젝트: kuno/python-stdnet
from copy import copy
from time import sleep

from stdnet import test, orm, MultiFieldError
from stdnet.utils import populate, zip

from examples.models import SimpleList

elems = populate('string', 100)


class BaseTestListField(test.TestCase,test.TestMultiFieldMixin):
    model = SimpleList
    
    def get_object_and_field(self):
        d = SimpleList().save()
        return d,d.names
    
    def adddata(self,li):
        '''Add elements to a list without using transactions.'''
        names = li.names
        for elem in elems:
            names.push_back(elem)
        li.save()
        self.assertEqual(li.names.size(),len(elems))


class TestListField(BaseTestListField):
    '''Test ListField'''        
    def testPushBackPopBack(self):
        li = SimpleList()
예제 #45
0
 def testSearch(self):
     engine = self.mapper.search_engine
     text = ' '.join(populate('choice', 1, choice_from=self.words))
     result = yield engine.search(text)
     self.assertTrue(result)
예제 #46
0
import datetime
import unittest
import logging
from itertools import izip

import stdnet
from stdnet.test import TestCase
from stdnet.utils import populate

from examples.models import TestDateModel

NUM_DATES = 100
names = populate('string', NUM_DATES, min_len=5, max_len=20)
dates = populate('date',
                 NUM_DATES,
                 start=datetime.date(2010, 5, 1),
                 end=datetime.date(2010, 6, 1))


class TestAtomFields(TestCase):
    def setUp(self):
        self.orm.register(TestDateModel)
        self.meta = TestDateModel._meta

    def unregister(self):
        self.orm.unregister(TestDateModel)

    def create(self):
        for na, dt in izip(names, dates):
            m = TestDateModel(name=na, dt=dt)
            m.save(False)
예제 #47
0
import random

import stdnet
from stdnet import orm, test
from stdnet.utils import populate

from examples.models import SimpleModel

LEN = 100
names = populate('string',LEN, min_len = 5, max_len = 20)


class TestManager(test.TestCase):
    model = SimpleModel
    
    def setUp(self):
        self.register()
        
    def fill(self):
        with SimpleModel.objects.transaction() as t:
            for name in names:
                SimpleModel(code = name).save(t)
                
    def testGetOrCreate(self):
        v, created = SimpleModel.objects.get_or_create(code = 'test')
        self.assertTrue(created)
        self.assertEqual(v.code,'test')
        v2,created = SimpleModel.objects.get_or_create(code = 'test')
        self.assertFalse(created)
        self.assertEqual(v,v2)
        
예제 #48
0
import base
from datetime import date
from timeit import default_timer as timer
from stdnet.utils import populate, date2timestamp

print("Getting data")
v = populate('date', 100000, start=date(1970, 1, 1), converter=date2timestamp)


def timeit(f):
    t1 = timer()
    f()
    dt = timer() - t1
    print("%s executed in %s seconds" % (f.__name__, dt))


def sort1():
    sorted(v)


def sortinline():
    v.sort()


print("Start")
timeit(sort1)
timeit(sortinline)
예제 #49
0
from stdnet.utils import test, populate, zip, iteritems, to_string

from examples.models import Dictionary

from .struct import MultiFieldMixin
        
keys = populate('string', 200)
values = populate('string', 200, min_len=20, max_len=300)


class TestMultiField(test.CleanTestCase):
    multipledb = 'redis'
    model = Dictionary
    
    def setUp(self):
        self.register()
        m = self.model(name = 'bla').save()
        m.data['ciao'] = 'bla'
        m.data['hello'] = 'foo'
        m.data['hi'] = 'pippo'
        m.data['salut'] = 'luna'
        m.save()
        m = self.model(name = 'luca').save()
        m.data['hi'] = 'pippo'
        m.data['salut'] = 'luna'
        m.save()
        
    def testloadNotSelected(self):
        '''Get the model and check that no data-structure data
 has been loaded.'''
        cache = self.model._meta.dfields['data'].get_cache_name()
예제 #50
0
def finance_data(inst_len = INST_LEN, fund_len = FUND_LEN,
                 num_dates = NUM_DATES):
    return (
            populate('string',inst_len, min_len = 5, max_len = 20),
            populate('choice',inst_len, choice_from = insts_types),
            populate('choice',inst_len, choice_from = ccys_types),
            populate('string',fund_len, min_len = 5, max_len = 20),
            populate('choice',fund_len, choice_from = ccys_types),
            populate('date',num_dates,start=datetime.date(2009,6,1),
                     end=datetime.date(2010,6,6))
            )
    

inst_names,inst_types,inst_ccys,fund_names,fund_ccys,dates = finance_data() 

users      = populate('string', NUM_USERS, min_len = 8, max_len = 14)
view_names = populate('string', 4*FUND_LEN, min_len = 10, max_len = 20)


class BaseFinance(test.TestCase):
    models = (Instrument,Fund,Position,PortfolioView,UserDefaultView)
    model = Instrument
    
    def setUp(self):
        '''Create Instruments and Funds'''
        session = self.session()
        with session.begin():
            for name,typ,ccy in zip(inst_names,inst_types,inst_ccys):
                session.add(Instrument(name = name, type = typ, ccy = ccy))
            for name,ccy in zip(fund_names,fund_ccys):
                session.add(Fund(name = name, ccy = ccy))
예제 #51
0
from stdnet.utils import populate
from stdnet.exceptions import QuerySetError

from examples.models import Instrument, Fund, Position, PortfolioView, UserDefaultView


INST_LEN    = 100
FUND_LEN    = 10
POS_LEN     = 30
NUM_USERS   = 10
NUM_DATES   = 2

ccys_types  = ['EUR','GBP','AUD','USD','CHF','JPY']
insts_types = ['equity','bond','future','cash','option']

inst_names = populate('string',INST_LEN, min_len = 5, max_len = 20)
inst_types = populate('choice',INST_LEN, choice_from = insts_types)
inst_ccys  = populate('choice',INST_LEN, choice_from = ccys_types)

fund_names = populate('string',FUND_LEN, min_len = 5, max_len = 20)
fund_ccys  = populate('choice',FUND_LEN, choice_from = ccys_types)

users      = populate('string', NUM_USERS, min_len = 8, max_len = 14)
view_names = populate('string', 4*FUND_LEN, min_len = 10, max_len = 20)

dates = populate('date',NUM_DATES,start=datetime.date(2009,6,1),end=datetime.date(2010,6,6))



class TestFinanceApplication(TestCase):
    
예제 #52
0
from itertools import izip

from stdnet.test import TestCase
from stdnet.utils import populate

from examples.models import Dictionary

keys = populate('string', 200)
values = populate('string', 200, min_len=20, max_len=300)


class TestLHashField(TestCase):
    def setUp(self):
        self.orm.register(Dictionary)
        d = Dictionary(name='test').save()
        self.data = dict(izip(keys, values))

    def fill(self):
        d = Dictionary.objects.get(name='test')
        d.data.update(self.data)
        self.assertEqual(d.data.size(), 0)
        d.save()
        data = d.data
        self.assertEqual(data.size(), len(self.data))
        return Dictionary.objects.get(name='test')

    def testUpdate(self):
        self.fill()

    def testAdd(self):
        d = Dictionary.objects.get(name='test')
예제 #53
0
 def testPopulateIntegers(self):
     data = populate('integer', size=33)
     self.assertEqual(len(data), 33)
     for d in data:
         self.assertTrue(isinstance(d, int))
예제 #54
0
import unittest
from itertools import izip
from timeit import default_timer as timer

from stdnet.main import getdb
from stdnet.utils import populate, date2timestamp, OrderedDict
from stdnet import settings_test


SIZEMAP = 100

test_keys   = populate('date',SIZEMAP,converter=date2timestamp)
test_values = populate('float',SIZEMAP)


def available(cache):
    if not cache:
        return False
    cache.set("stdnet-test",1)
    avail = cache.get("stdnet-test") or False
    cache.delete("stdnet-test")
    return avail

class testLocMem(unittest.TestCase):
    keyset1   = 'stdnet-test-set'
    keyset2   = 'stdnet-test-set2'
    keymap    = 'stdnet-maptest2'
    map_types = [1]
    
    def setUp(self):
        pass
예제 #55
0
import unittest
from itertools import izip
from timeit import default_timer as timer

from stdnet.main import getdb
from stdnet.utils import populate, date2timestamp, OrderedDict
from stdnet import settings_test

SIZEMAP = 100

test_keys = populate('date', SIZEMAP, converter=date2timestamp)
test_values = populate('float', SIZEMAP)


def available(cache):
    if not cache:
        return False
    cache.set("stdnet-test", 1)
    avail = cache.get("stdnet-test") or False
    cache.delete("stdnet-test")
    return avail


class testLocMem(unittest.TestCase):
    keyset1 = 'stdnet-test-set'
    keyset2 = 'stdnet-test-set2'
    keymap = 'stdnet-maptest2'
    map_types = [1]

    def setUp(self):
        pass