def testFlushRelatedModel(self): self.data.makePositions() self.assertTrue(Instrument.flush()>self.data.num_insts) self.assertEqual(Position.objects.all().count(),0) self.assertEqual(Instrument.objects.all().count(),0) # Now we check the database if it is empty as it should Fund.flush() keys = list(Instrument._meta.cursor.keys()) self.assertEqual(len(keys),0)
def testFlushSimpleModel(self): '''Use the class method flush to remove all instances of a Model including filters.''' self.data.create() self.assertTrue(Instrument.flush()>self.data.num_insts) self.assertEqual(Instrument.objects.all().count(),0) Fund.flush() # Now we check the database if it is empty as it should keys = list(Instrument._meta.cursor.keys()) self.assertEqual(len(keys),0)
def setUp(self): '''Create Instruments and Funds commiting at the end for speed''' orm = self.orm orm.register(Instrument) orm.register(Fund) orm.register(Position) orm.register(PortfolioView) orm.register(UserDefaultView) for name,typ,ccy in izip(inst_names,inst_types,inst_ccys): Instrument(name = name, type = typ, ccy = ccy).save(False) Instrument.commit() for name,ccy in izip(fund_names,fund_ccys): Fund(name = name, ccy = ccy).save(False) Fund.commit()
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)) yield t.on_result iall = yield test.session().query(Instrument).load_only('id').all() fall = yield test.session().query(Fund).load_only('id').all() with session.begin() as t: for i in iall: t.add(ObjectAnalytics(model_type=Instrument, object_id=i.id)) for i in fall: t.add(ObjectAnalytics(model_type=Fund, object_id=i.id)) yield t.on_result obj_len = self.size[1] groups = yield session.query(Group).all() objs = yield session.query(ObjectAnalytics).all() groups = self.populate('choice', obj_len, choice_from=groups) objs = self.populate('choice', obj_len, choice_from=objs) with test.session().begin() as t: for g, o in zip(groups, objs): t.add(AnalyticData(group=g, object=o)) yield t.on_result