def gen_flows_from_choice(choice, flow_event_model, choice_stocks): """ Generate a series of flows from a choices tuple array. """ rv = [] i = 0 try: while(True): up = choice[i+1] down = choice[i] up_stock_slug = up[0] down_stock_slug = down[0] up_slug = "rising_%s" % up_stock_slug up_name = "Rising to %s" % up[1].lower() down_slug = "dropping_%s" % down_stock_slug down_name = "Dropping to %s" % down[1].lower() rv.append(Flow(slug=up_slug, name=up_name, flow_event_model=flow_event_model, sources=[choice_stocks[down_stock_slug]], sinks=[choice_stocks[up_stock_slug]])) rv.append(Flow(slug=down_slug, name=down_name, flow_event_model=flow_event_model, sources=[choice_stocks[up_stock_slug]], sinks=[choice_stocks[down_stock_slug]])) i += 1 except IndexError: pass return rv
def testCreateFlowEventShouldCreateDBRecord(self): # Create an object in the db - use stockrecord just because it is here and simple sr = StockRecord.objects.create(stock='no_stock', count=0) self.args["flow_event_model"] = Mock() f = Flow(**self.args) fe = f.add_event(sr, self.stocks[0], self.stocks[1]) pos, kw = f.flow_event_model.call_args expect = {"flow": f.slug, "subject": sr, "source": self.stocks[0].slug, "sink": self.stocks[1].slug } self.assertEqual(expect, kw) self.assertTrue(f.flow_event_model.return_value.save.called)
def testFlowAllWithSourceAndOrSinkShouldReturnAQSFilteredByTheSink(self): f = Flow(**self.args) qs_mock = f.all() sink_mock = Mock() source_mock = Mock() rv_mock = f.all(sink=sink_mock) self.assertEqual(((), {"sink": sink_mock.slug}), qs_mock.filter.call_args) qs_mock.reset_mock() rv_mock = f.all(source=source_mock) self.assertEqual(((), {"source": source_mock.slug}), qs_mock.filter.call_args) qs_mock.reset_mock() rv_mock = f.all(sink=sink_mock, source=source_mock) self.assertEqual(((), {"source": source_mock.slug}), qs_mock.filter.call_args) qs2_mock = qs_mock.filter.return_value self.assertEqual(((), {"sink": sink_mock.slug}), qs2_mock.filter.call_args)
def profile_states_to_stocks(prev_field_vals, cur_field_vals): """ Compare the field values to determine the state. """ prev_consist_slug, = prev_field_vals if prev_field_vals else (None, ) cur_consist_slug, = cur_field_vals if cur_field_vals else (None, ) prev_consist_stock = consist_slug_to_stock.get(prev_consist_slug, None) cur_consist_stock = consist_slug_to_stock.get(cur_consist_slug, None) return ((prev_consist_stock,), (cur_consist_stock,)) ## The flows flows = [] flows.append(Flow(slug="starting_user", name="Starting user", flow_event_model=ProfileFlowEvent, sources=[None], sinks=consist_slug_to_stock.values())) #An example of how to generate flows for a choice set def gen_flows_from_choice(choice, flow_event_model, choice_stocks): """ Generate a series of flows from a choices tuple array. """ rv = [] i = 0 try: while(True): up = choice[i+1] down = choice[i] up_stock_slug = up[0] down_stock_slug = down[0]
def testFlowAllShouldReturnTheFlowQS(self): f = Flow(**self.args) self.assertEqual(f.queryset, f.all())
def testFlowAddEventShouldReturnNoneIfSourceIsNotInSources(self): f = Flow(**self.args) self.assertTrue(f.add_event(Mock(), Mock(), self.stocks[1]) is None)
from stockandflow.tracker import ModelTracker from profiles.models import Profile from processes.models import UserFlowEvent # The stocks - user-specific stocks are in profile stocks = [] # The flows flows = [] # Track a record of logins, and as part of that flow update the profile consistency # The states do not need to be recorded, so instead of stocks there are just # integers for the source and sink. login_flow = Flow(slug="logging_in", name="Logging in", flow_event_model=UserFlowEvent, sources=[0], sinks=[1], event_callables=(Profile.logged_in, )) flows.append(login_flow) # The tracker def user_states_to_stocks(prev_field_vals, cur_field_vals): """ Check if the last login day has changed. """ no_change = ((0, ), (0, )) yes_change = ((0, ), (1, )) if prev_field_vals is None: if cur_field_vals is None: return no_change