def create_rf(self): errors = {} if not self.record.get(self.TRADE_ID): errors[self.TRADE_ID] = 'must be specified' model_klass = RecordProcessor.get_model(self.record.get(self.TRADE_TYPE,"")) if not model_klass: errors[self.TRADE_TYPE] = 'Valid values for {} are [{}]'.format(self.TRADE_TYPE, ",".join([m.model_name() for m in self.model_list()])) if not any([self.is_cancel(), self.is_edit(), self.is_new()]): errors[self.MSG_TYPE] = 'Valid Msg Types are {}'.format(",".join(self.MSG_TYPE_VALUES)) rf = None if not errors: rf = ReactiveFramework(model_klass()) loaded = rf.load(self.record[self.TRADE_ID]) if loaded: if self.is_new(): errors[self.MSG_TYPE] = "{} already exists".format(self.TRADE_ID) else: if self.is_cancel() or self.is_edit(): errors[self.MSG_TYPE] = "{} does not exist".format(self.TRADE_ID) self.errors = self._format_errors(errors) if self.errors: return self._rf = rf
def create_rf(self): errors = {} if not self.record.get(self.TRADE_ID): errors[self.TRADE_ID] = 'must be specified' model_klass = RecordProcessor.get_model( self.record.get(self.TRADE_TYPE, "")) if not model_klass: errors[self.TRADE_TYPE] = 'Valid values for {} are [{}]'.format( self.TRADE_TYPE, ",".join([m.model_name() for m in self.model_list()])) if not any([self.is_cancel(), self.is_edit(), self.is_new()]): errors[self.MSG_TYPE] = 'Valid Msg Types are {}'.format(",".join( self.MSG_TYPE_VALUES)) rf = None if not errors: rf = ReactiveFramework(model_klass()) loaded = rf.load(self.record[self.TRADE_ID]) if loaded: if self.is_new(): errors[self.MSG_TYPE] = "{} already exists".format( self.TRADE_ID) else: if self.is_cancel() or self.is_edit(): errors[self.MSG_TYPE] = "{} does not exist".format( self.TRADE_ID) self.errors = self._format_errors(errors) if self.errors: return self._rf = rf
def test_validate(self): m = StubModel() rf = ReactiveFramework(m) self.assertDictEqual( { 'commission': 'Not set', 'currency': 'Not set', 'quantity': 'Not set', 'settle_date': 'Not set', 'trade_date': 'Not set' }, rf.validate())
def test_are_dependent_set_two_depends(self): m = StubModel() rf = ReactiveFramework(m) self.assertFalse(rf._are_dependents_set('settle_date')) rf.set_value('trade_date', date(2015, 1, 1)) self.assertFalse(rf._are_dependents_set('settle_date')) rf.set_value('calendar', 'USD') self.assertTrue(rf._are_dependents_set('settle_date'))
def test_save(self): m = StubModel() rf = ReactiveFramework(m) rf.set_value('quantity', 100) rf.validate() rf.save() trade = m.get_domain_object('Trade') self.assertEquals(trade.quantity, rf.get_value('quantity')) self.assertTrue(m.save_called)
def test_are_dependent_set_two_depends(self): m = StubModel() rf = ReactiveFramework(m) self.assertFalse(rf._are_dependents_set( 'settle_date' ) ) rf.set_value('trade_date', date(2015,1,1)) self.assertFalse(rf._are_dependents_set( 'settle_date' ) ) rf.set_value('calendar', 'USD') self.assertTrue(rf._are_dependents_set( 'settle_date' ) )
def test_can_create_rf(self): m = StubModel() m.FIELD_DEPENDS = {'quantity': []} rf = ReactiveFramework(m) self.assertEqual(m, rf.model) self.assertEquals({}, rf.depends_notifty) self.assertEquals(None, rf.model.quantity.value)
def test_load(self): m = StubModel() rf = ReactiveFramework(m) trade = m.get_domain_object('Trade') trade.quantity = Decimal("100") trade.trade_date = date(2015,2,1) trade.settle_date = date(2015,2,3) trade.currency = 'EUR' rf.load(123) self.assertEqual(123, m.load_called) self.assertEqual(100, rf.get_value('quantity')) self.assertEqual(date(2015,2,1), rf.get_value('trade_date')) self.assertEqual(date(2015,2,3), rf.get_value('settle_date')) self.assertEqual('EUR', rf.get_value('currency')) self.assertEqual('US', rf.get_value('calendar')) self.assertEqual(100, rf.get_value('commission')) for field in rf.get_fields(): self.assertEquals(False, field.has_user_entered_value)
def test_when_recalculation_method_error_is_recorded_but_not_raised(self): class FailedStub(StubModel): def calc_settle_date(self): raise Exception("I failed") m = FailedStub() rf = ReactiveFramework(m) rf.set_value('trade_date', date(2015, 1, 1)) rf.set_value('calendar', 'USD') self.assertEquals(None, rf.get_value('settle_date')) f = rf.get_field('settle_date') self.assertEquals("I failed", f.error)
def test_depends_notify(self): m = StubModel() rf = ReactiveFramework(m) self.assertEqual( sorted(['calendar', 'currency', 'quantity', 'trade_date']), sorted(rf.depends_notifty.keys())) self.assertEqual(['calendar'], rf.depends_notifty['currency']) self.assertEqual(['settle_date'], rf.depends_notifty['calendar']) self.assertEqual(sorted(['settle_date', 'commission']), sorted(rf.depends_notifty['trade_date']))
def test_when_recalculation_method_error_is_recorded_but_not_raised(self): class FailedStub(StubModel): def calc_settle_date(self): raise Exception("I failed") m = FailedStub() rf = ReactiveFramework(m) rf.set_value('trade_date', date(2015,1,1)) rf.set_value('calendar', 'USD') self.assertEquals(None, rf.get_value('settle_date' ) ) f = rf.get_field('settle_date' ) self.assertEquals("I failed", f.error)
def test_when_mapper_fails_multiple_errors_are_returned(self): class FailedStub(StubModel): FIELD_DEPENDS = {'currency': ['instrument'], 'instrument': []} def map_instrument(self, field, direction): raise Exception("I failed") m = FailedStub() rf = ReactiveFramework(m) rf.set_value('instrument', "ABC") rf.set_value('currency', "USD") status = rf.validate() expect = {'instrument': 'I failed', 'currency': 'I failed'} self.assertEquals(expect, status)
def test_when_mapper_fails_multiple_errors_are_returned(self): class FailedStub(StubModel): FIELD_DEPENDS = { 'currency':['instrument'], 'instrument':[] } def map_instrument(self, field, direction): raise Exception("I failed") m = FailedStub() rf = ReactiveFramework(m) rf.set_value('instrument', "ABC") rf.set_value('currency', "USD") status = rf.validate() expect = {'instrument': 'I failed', 'currency': 'I failed'} self.assertEquals(expect, status)
def test_delete(self): rf = ReactiveFramework(StubModel()) rf.delete() self.assertEqual(True, rf.model.delete_called)
def test_load(self): m = StubModel() rf = ReactiveFramework(m) trade = m.get_domain_object('Trade') trade.quantity = Decimal("100") trade.trade_date = date(2015, 2, 1) trade.settle_date = date(2015, 2, 3) trade.currency = 'EUR' rf.load(123) self.assertEqual(123, m.load_called) self.assertEqual(100, rf.get_value('quantity')) self.assertEqual(date(2015, 2, 1), rf.get_value('trade_date')) self.assertEqual(date(2015, 2, 3), rf.get_value('settle_date')) self.assertEqual('EUR', rf.get_value('currency')) self.assertEqual('US', rf.get_value('calendar')) self.assertEqual(100, rf.get_value('commission')) for field in rf.get_fields(): self.assertEquals(False, field.has_user_entered_value)
def book_a_trade(self): rf = ReactiveFramework(VanillaModel()) rf.set_value('quantity', 100) rf.set_value('price', 600) rf.set_value('action', "Buy") rf.set_value('instrument', "GOOGL.O") rf.set_value('trade_date', date(2015, 5, 5)) rf.set_value('fund', "Fund1") rf.set_value('trader', "Trader1") rf.set_value('analyst', "Analyst1") rf.set_value('broker', "Broker1") rf.set_value('clearer', "Clearer1") rf.set_value('sector', "sec1") rf.set_value('strategy', "strat1") rf.set_value('trade_id', "ZZ{}".format(get_next_id())) self.assertEquals({}, rf.validate()) trade_id = rf.save() return trade_id
def test_calc_settle_date_basic(self): rf = ReactiveFramework(VanillaModel()) rf.set_value('trade_date', date(2015, 5, 6)) self.assertEquals(date(2015, 5, 8), rf.get_value('settle_date'))
def test_set_field_recalcs(self): m = StubModel() rf = ReactiveFramework(m) self.assertEquals([], rf.set_value('quantity', 100)) self.assertEquals(['commission'], rf.set_value('trade_date', date(2015, 2, 1))) self.assertEquals(['commission'], rf.set_value('quantity', 150)) self.assertListEqual(sorted(['calendar', 'settle_date']), sorted(rf.set_value('currency', 'USD'))) self.assertListEqual( sorted(['commission', 'settle_date']), sorted(rf.set_value('trade_date', date(2015, 2, 1)))) self.assertEquals([], rf.set_value('commission', 4)) self.assertEquals(['settle_date'], rf.set_value('trade_date', date(2015, 2, 1))) self.assertEquals('USD', rf.get_value('currency')) self.assertEquals(date(2015, 2, 1), rf.get_value('trade_date')) self.assertEquals(date(2015, 2, 2), rf.get_value('settle_date')) self.assertEquals(4, rf.get_value('commission')) self.assertEquals("US", rf.get_value('calendar'))
def test_are_dependent_set_no_depends(self): m = StubModel() rf = ReactiveFramework(m) self.assertTrue( rf._are_dependents_set( 'quantity' ) )
def test_are_dependent_set_one_depends(self): m = StubModel() rf = ReactiveFramework(m) self.assertFalse(rf._are_dependents_set( 'calendar' ) ) rf.set_value('currency', 'USD') self.assertTrue(rf._are_dependents_set( 'calendar' ) )
def test_are_dependent_set_no_depends(self): m = StubModel() rf = ReactiveFramework(m) self.assertTrue(rf._are_dependents_set('quantity'))
def book_a_trade(self): rf = ReactiveFramework(VanillaModel()) rf.set_value('quantity', 100) rf.set_value('price', 600) rf.set_value('action', "Buy") rf.set_value('instrument', "GOOGL.O") rf.set_value('trade_date', date(2015, 5, 5)) rf.set_value('fund', "Fund1") rf.set_value('trader', "Trader1") rf.set_value('analyst', "Analyst1") rf.set_value('broker', "Broker1") rf.set_value('clearer', "Clearer1") rf.set_value('sector', "sec1") rf.set_value('strategy', "strat1") rf.set_value('trade_id', "ZZ{}".format(get_next_id())) self.assertEquals({}, rf.validate() ) trade_id = rf.save() return trade_id
def test_load_existing_trade(self): trade_id = self.book_a_trade() rf = ReactiveFramework(VanillaModel()) self.assertEquals(trade_id, rf.load(trade_id))
def test_integration(self): s = Session() s.query(Trade).delete() s.query(Portfolio).delete() trade_id = self.book_a_trade() trades = s.query(Trade).all() self.assertEquals(1, len(trades)) self.assertEquals(trade_id, trades[0].trade_id) self.assertEquals(100, trades[0].quantity) self.assertEquals(600, trades[0].price) self.assertEquals("Buy", trades[0].action.value) self.assertEquals("GOOGL.O", trades[0].instrument.name) self.assertEquals("USD", trades[0].currency.name) self.assertEquals(date(2015, 5, 5), trades[0].trade_date) self.assertEquals(date(2015, 5, 7), trades[0].settle_date) self.assertEquals("Fund1", trades[0].portfolio.fund.name) s = Session() ports = s.query(Portfolio).all() self.assertEquals(1, len(ports)) self.assertEquals('Fund1', ports[0].fund.name) self.assertEquals('Trader1', ports[0].trader.name) self.assertEquals('Analyst1', ports[0].analyst.name) self.assertEquals('Broker1', ports[0].broker.name) self.assertEquals('Clearer1', ports[0].clearer.name) self.assertEquals('sec1', ports[0].sector) self.assertEquals('strat1', ports[0].strategy) rf = ReactiveFramework(VanillaModel()) rf.load(trade_id) self.assertEquals(100, rf.get_value('quantity')) self.assertEquals(600, rf.get_value('price')) self.assertEquals("Buy", rf.get_value('action')) self.assertEquals("GOOGL.O", rf.get_value('instrument')) self.assertEquals("USD", rf.get_value('currency')) self.assertEquals(date(2015, 5, 5), rf.get_value('trade_date')) self.assertEquals(date(2015, 5, 7), rf.get_value('settle_date')) self.assertEquals("Fund1", rf.get_value('fund')) self.assertEquals("Trader1", rf.get_value('trader')) self.assertEquals("Analyst1", rf.get_value('analyst')) self.assertEquals("Broker1", rf.get_value('broker')) self.assertEquals("Clearer1", rf.get_value('clearer')) self.assertEquals("sec1", rf.get_value('sector')) self.assertEquals("strat1", rf.get_value('strategy')) rf.set_value('quantity', -140) rf.set_value('price', 600) rf.set_value('action', "Sell") rf.set_value('instrument', "GOOGL.O") rf.set_value('trade_date', date(2015, 5, 5)) rf.set_value('fund', "Fund2") self.assertEquals({}, rf.validate()) rf.save() s = Session() trades = s.query(Trade).all() self.assertEquals(1, len(trades)) self.assertEquals(-140, trades[0].quantity) self.assertEquals(600, trades[0].price) self.assertEquals("Sell", trades[0].action.value) self.assertEquals("GOOGL.O", trades[0].instrument.name) self.assertEquals("USD", trades[0].currency.name) self.assertEquals(date(2015, 5, 5), trades[0].trade_date) self.assertEquals(date(2015, 5, 7), trades[0].settle_date) self.assertEquals("Fund2", trades[0].portfolio.fund.name) rf.delete() trades = s.query(Trade).all() self.assertEquals(0, len(trades))
def test_integration(self): s = Session() s.query(Trade).delete() s.query(Portfolio).delete() trade_id = self.book_a_trade() trades = s.query(Trade).all() self.assertEquals( 1, len(trades) ) self.assertEquals( trade_id, trades[0].trade_id) self.assertEquals( 100, trades[0].quantity ) self.assertEquals( 600, trades[0].price ) self.assertEquals( "Buy", trades[0].action.value ) self.assertEquals( "GOOGL.O", trades[0].instrument.name ) self.assertEquals( "USD", trades[0].currency.name ) self.assertEquals( date(2015,5,5), trades[0].trade_date ) self.assertEquals( date(2015,5,7), trades[0].settle_date ) self.assertEquals( "Fund1", trades[0].portfolio.fund.name ) s = Session() ports = s.query(Portfolio).all() self.assertEquals( 1, len(ports) ) self.assertEquals( 'Fund1', ports[0].fund.name ) self.assertEquals( 'Trader1', ports[0].trader.name ) self.assertEquals( 'Analyst1', ports[0].analyst.name ) self.assertEquals( 'Broker1', ports[0].broker.name ) self.assertEquals( 'Clearer1', ports[0].clearer.name ) self.assertEquals( 'sec1', ports[0].sector ) self.assertEquals( 'strat1', ports[0].strategy ) rf = ReactiveFramework(VanillaModel()) rf.load(trade_id) self.assertEquals( 100, rf.get_value('quantity' )) self.assertEquals( 600, rf.get_value('price' )) self.assertEquals( "Buy", rf.get_value('action' )) self.assertEquals( "GOOGL.O", rf.get_value('instrument' )) self.assertEquals( "USD", rf.get_value('currency')) self.assertEquals( date(2015,5,5), rf.get_value('trade_date' )) self.assertEquals( date(2015,5,7), rf.get_value('settle_date' )) self.assertEquals( "Fund1", rf.get_value('fund' )) self.assertEquals( "Trader1", rf.get_value('trader' )) self.assertEquals( "Analyst1", rf.get_value('analyst' )) self.assertEquals( "Broker1", rf.get_value('broker' )) self.assertEquals( "Clearer1", rf.get_value('clearer' )) self.assertEquals( "sec1", rf.get_value('sector' )) self.assertEquals( "strat1", rf.get_value('strategy' )) rf.set_value( 'quantity', -140) rf.set_value( 'price', 600) rf.set_value( 'action', "Sell") rf.set_value( 'instrument', "GOOGL.O") rf.set_value( 'trade_date', date(2015,5,5)) rf.set_value( 'fund', "Fund2") self.assertEquals({}, rf.validate()) rf.save() s = Session() trades = s.query(Trade).all() self.assertEquals( 1, len(trades) ) self.assertEquals( -140, trades[0].quantity ) self.assertEquals( 600, trades[0].price ) self.assertEquals( "Sell", trades[0].action.value ) self.assertEquals( "GOOGL.O", trades[0].instrument.name ) self.assertEquals( "USD", trades[0].currency.name ) self.assertEquals( date(2015,5,5), trades[0].trade_date ) self.assertEquals( date(2015,5,7), trades[0].settle_date ) self.assertEquals( "Fund2", trades[0].portfolio.fund.name ) rf.delete() trades = s.query(Trade).all() self.assertEquals( 0, len(trades) )
def test_load_missing_trade(self): rf = ReactiveFramework(VanillaModel()) self.assertEquals(None, rf.load('ABC123'))
def test_calc_settle_date_basic(self): rf = ReactiveFramework(VanillaModel()) rf.set_value('trade_date', date(2015,5,6)) self.assertEquals(date(2015,5,8), rf.get_value('settle_date'))
def test_validate(self): m = StubModel() rf = ReactiveFramework(m) self.assertDictEqual({'commission': 'Not set', 'currency': 'Not set', 'quantity': 'Not set', 'settle_date': 'Not set', 'trade_date': 'Not set'}, rf.validate())
def test_are_dependent_set_one_depends(self): m = StubModel() rf = ReactiveFramework(m) self.assertFalse(rf._are_dependents_set('calendar')) rf.set_value('currency', 'USD') self.assertTrue(rf._are_dependents_set('calendar'))
def test_set_field_recalcs(self): m = StubModel() rf = ReactiveFramework(m) self.assertEquals([], rf.set_value('quantity', 100)) self.assertEquals(['commission'], rf.set_value('trade_date', date(2015,2,1))) self.assertEquals(['commission'], rf.set_value('quantity', 150)) self.assertListEqual(sorted(['calendar', 'settle_date']), sorted(rf.set_value('currency', 'USD'))) self.assertListEqual(sorted(['commission', 'settle_date']), sorted(rf.set_value('trade_date', date(2015,2,1)))) self.assertEquals([], rf.set_value('commission', 4)) self.assertEquals(['settle_date'], rf.set_value('trade_date', date(2015,2,1))) self.assertEquals('USD', rf.get_value('currency')) self.assertEquals( date(2015,2,1), rf.get_value('trade_date')) self.assertEquals( date(2015,2,2), rf.get_value('settle_date')) self.assertEquals( 4, rf.get_value('commission')) self.assertEquals( "US", rf.get_value('calendar'))