示例#1
0
    def should_consume_tokens(self):
        flow = base.Flow()
        flow.origin = base.Node(key='node_0')
        flow.destination = base.Node(key='node_1')

        self.item.qty = 3
        self.item.produce(flow)

        assert self.item.avalaible_tokens[0].qty == 3
        # 3 units on node_1
        self.session.add(self.item)
        self.session.commit()

        next_flow = base.Flow()
        next_flow.origin = flow.destination
        self.item.qty = 1
        self.item.consume(next_flow)
        self.session.commit()

        assert len(self.item.avalaible_tokens) == 1
        assert self.item.avalaible_tokens[0].qty == 2
        self.item.qty = None
        self.item.consume(next_flow)
        self.session.commit()
        assert len(self.item.avalaible_tokens) == 0
示例#2
0
    def should_throw_inputs_and_outputs(self):
        flow = base.Flow()
        operations = [base.Flow() for _ in range(3)]
        for op in operations:
            op.throw = Mock()
            flow.operations.append(op)

        destination = base.Node()
        origin = base.Node()

        _input = base.Item()
        output = base.Item()
        resource = base.Resource()
        output.resource = _input.resource = resource

        _input.consume = Mock()
        output.produce = Mock()
        flow.origin = flow.destination = None
        flow.inputs = [_input]
        flow.outputs = [output]

        flow.throw()
        _input.consume.assert_not_called()
        output.produce.assert_not_called()
        for op in operations:
            op.throw.assert_called_with()

        flow.origin = origin
        flow.destination = destination

        flow.throw()
        _input.consume.asseert_called_with(flow)
        output.produce.assert_called_with(flow)
示例#3
0
    def should_persist_basic_relationships(self):
        path = self.path

        path.from_node = base.Node(key='from')
        path.to_node = base.Node(key='to')
        path.role = base.Node(key='role')

        self.session.add(path)
        self.session.commit()

        assert path.role.key == 'role'
        assert path.from_node.key == 'from'
        assert path.to_node.key == 'to'
示例#4
0
    def should_persist_basic_relationships(self):
        flow = base.Flow()
        role = base.Node()
        role.key = 'role'
        flow.path = base.Path(role=role)

        responsible = base.Node()
        responsible.key = 'responsible'
        flow.responsible = responsible
        op = base.Flow()
        flow.operations.append(op)
        self.session.add(flow)
        self.session.commit()

        assert op.parent == flow
        assert flow.path.role.key == 'role'
        assert flow.responsible.key == 'responsible'
示例#5
0
    def should_produce_tokens(self):
        flow = base.Flow()
        flow.destination = base.Node(key='destination')
        flow.origin = base.Node(key='origin')

        self.item.qty = 2
        self.item.produce(flow)

        self.session.add(self.item)
        self.session.commit()

        other = self.session.query(base.Item).one()
        assert other.avalaible_tokens
        token = other.avalaible_tokens[0]
        assert token.qty == 2
        assert token.node == flow.destination
        assert token.consumer is None
        assert token.producer == flow
示例#6
0
 def setup_method(self, method):
     super().setup_method(method)
     resource = base.Resource()
     resource.key = 'resource'
     item = base.Item()
     item.resource = resource
     item.tracking = '123456789'
     node = base.Node()
     flow = base.Flow()
     self.token = base.Token(item=item, node=node, qty=10, producer=flow)
示例#7
0
    def should_modify_pars_one_by_one(self):
        node = base.Node()
        node.key = 'key'
        node.name = 'name'
        node.description = 'description'
        node.other = 1
        assert node.pars['other'] == 1

        node.pars['other'] = 2

        assert node.pars['other'] == 2
        assert json.loads(node.pars._pars)['other'] == 2
示例#8
0
    def should_get_stocks(self):
        flow_1 = base.Flow()
        flow_1.destination = node_0 = base.Node(key='node_0')

        flow_2 = base.Flow()
        flow_2.destination = node_1 = base.Node(key='node_1')

        self.item.qty = 1
        self.item.produce(flow_1)

        self.item.qty = 2
        self.item.produce(flow_2)
        self.session.commit()

        assert len(self.item.avalaible_tokens) == 2

        stocks = self.item.get_stocks()

        assert len(stocks) == 2
        assert node_0 in stocks
        assert node_1 in stocks
        assert stocks[node_1][0].qty + stocks[node_0][0].qty == 3
示例#9
0
    def should_create_operations(self):
        path = base.Path()
        steps = [base.Path() for _ in range(3)]
        for step in steps:
            path.steps.append(step)
            step.create_flow = Mock()
            step.create_flow.return_value = base.Flow()
        flow = base.Flow(path=path)
        flow.responsible = base.Node(key='resp')

        calls = []
        for step, operation in zip(steps, flow.op_creator()):
            assert operation == step.create_flow.return_value
            step.create_flow.assert_called_with()
            assert operation.responsible == flow.responsible
            assert operation.parent == flow
示例#10
0
    def should_load_other_attributes_in_pars(self):
        node = base.Node()
        node.key = 'key'
        node.name = 'name'
        node.description = 'description'
        node.other = 1
        assert node.pars['other'] == 1

        node.pars['another'] = 'value'

        assert node.pars['another'] == 'value'
        self.session.add(node)
        self.session.commit()

        assert node.pars._dict_pars == {'another': 'value', 'other': 1}
        other = self.session.query(base.Node).filter_by(key='key').one()

        assert json.loads(node.pars._pars)['other'] == 1
示例#11
0
 def setup_validate_responsible(self):
     role = base.Node(key='role')
     self.path.role = role
示例#12
0
    def should_allocate_origin_and_destination(self):
        flow = base.Flow()
        operations = [base.Flow() for _ in range(3)]
        for op in operations:
            op.allocate = Mock()
            flow.operations.append(op)

        with pytest.raises(base.FlowAllocateException):
            flow.allocate()

        flow.finished_on = Mock()
        flow.state = 'finished'

        destination = base.Node()
        origin = base.Node()
        from_node = base.Node()
        to_node = base.Node()

        path = base.Path()
        path.from_node = from_node
        path.to_node = to_node

        flow.path = path

        flow.origin = origin
        flow.destination = destination
        flow.allocate()

        for op in operations:
            op.allocate.assert_called_with()

        assert flow.origin == origin
        assert flow.destination == destination

        flow.origin = flow.destination = None
        flow.allocate()

        assert flow.origin == from_node
        assert flow.destination == to_node

        parent_path = base.Path()
        parent_path.from_node = from_node
        parent_path.to_node = to_node
        path.parent = parent_path

        path.from_node = path.to_node = None
        flow.origin = flow.destination = None

        flow.allocate()
        assert flow.origin == from_node
        assert flow.destination == to_node

        flow.state = 'cancelled'
        flow.destination = destination
        flow.origin = origin

        flow.allocate()

        assert flow.destination == origin
        for op in operations:
            assert op.state == 'cancelled'