Ejemplo n.º 1
0
 def test_can_add_function_where_no_params_are_bound(self):
     g = Gremlin()
     
     g.unbound('func', 'val1', 'val2')
     
     expected = 'g.func(val1, val2)'
     
     self.assertTrue(str(g) == expected)
     self.assertTrue(len(g.bound_params) == 0)
Ejemplo n.º 2
0
    def test_can_add_function_where_no_params_are_bound(self):
        g = Gremlin()

        g.unbound('func', 'val1', 'val2')

        expected = 'g.func(val1, val2)'

        self.assertTrue(str(g) == expected)
        self.assertTrue(len(g.bound_params) == 0)
Ejemplo n.º 3
0
    def test_can_add_function_where_no_params_are_bound_and_func_with_bound_params(self):
        g = Gremlin()
        
        g.unbound('func', 'val1', 'val2').isbound('hello')
        
        s        = str(g)
        params   = g.bound_params
        hello    = get_dict_key(params, 'hello')
        expected = 'g.func(val1, val2).isbound(%s)' % hello

        self.assertTrue(str(g) == expected)
        self.assertTrue(len(g.bound_params) == 1)
Ejemplo n.º 4
0
    def test_can_add_function_where_no_params_are_bound_and_func_with_bound_params(
            self):
        g = Gremlin()

        g.unbound('func', 'val1', 'val2').isbound('hello')

        s = str(g)
        params = g.bound_params
        hello = get_dict_key(params, 'hello')
        expected = 'g.func(val1, val2).isbound(%s)' % hello

        self.assertTrue(str(g) == expected)
        self.assertTrue(len(g.bound_params) == 1)
Ejemplo n.º 5
0
    def _add_edge(self, entity, set_variable=None):
        if not entity[GIZMO_LABEL[0]]:
            msg = 'A label is required in order to create an edge'
            logger.exception(msg)
            raise AstronomerQueryException(msg)

        def get_or_create_ends():
            """this function will determine if the edge has both ends. If
            either end is an _Entity object it will get the reference to
            the object or save it and create a reference. Either the entity's
            id or reference will be used when saving the edge.
            """
            out_v = entity.out_v
            out_v_ref = None
            in_v = entity.in_v
            in_v_ref = None

            if out_v is None or in_v is None:
                error = ('Both out and in vertices must be set before'
                         ' saving the edge')
                logger.exception(error)
                raise AstronomerQueryException(error)

            if isinstance(out_v, _Entity):
                if out_v[GIZMO_ID]:
                    out_v = out_v[GIZMO_ID]
                else:
                    out_v_ref = self.mapper.get_entity_variable(out_v)

                    if not out_v_ref:
                        self.mapper.save(out_v)
                        out_v_ref = self.mapper.get_entity_variable(out_v)

                    if out_v_ref:
                        out_v = out_v_ref

            if isinstance(in_v, _Entity):
                if in_v[GIZMO_ID]:
                    in_v = in_v[GIZMO_ID]
                else:
                    in_v_ref = self.mapper.get_entity_variable(in_v)

                    if not in_v_ref:
                        self.mapper.save(in_v)
                        in_v_ref = self.mapper.get_entity_variable(in_v)

                    if in_v_ref:
                        in_v = in_v_ref

            return {
                'out': {
                    'is_ref': out_v_ref,
                    'v': out_v,
                },
                'in': {
                    'is_ref': in_v_ref,
                    'v': in_v,
                },
            }

        ends = get_or_create_ends()
        name = str(entity)
        gremlin = self.gremlin
        g = Gremlin(gremlin.gv)
        label = next_param('{}_label'.format(name), entity[GIZMO_LABEL[0]])
        """
        g.V($OUT_ID).next().addEdge($LABEL, g.V($IN_ID).next()).property(....)
        """
        in_v = ends['in']
        out_v = ends['out']

        if in_v['is_ref']:
            g.unbound('V', in_v['v'])
        else:
            in_id = next_param('{}_in'.format(name), in_v['v'])

            g.V(in_id)

        g.func('next')

        if out_v['is_ref']:
            gremlin.unbound('V', out_v['v'])
        else:
            out_id = next_param('{}_out'.format(name), out_v['v'])

            gremlin.V(out_id)

        ignore = [GIZMO_LABEL[0], GIZMO_LABEL[1], GIZMO_TYPE]
        edge_args = [label, g]

        # edge properites only get one value and no meta-properties
        for field, changes in entity.changes.items():
            if field in ignore:
                continue

            try:
                if changes['immutable']:
                    value = changes['values']['values'][-1]
                else:
                    value = changes['values'][-1]
            except:
                continue

            field_param = next_param('{}_{}'.format(name, field), field)
            field_value = next_param('{}_value'.format(field_param.name),
                                     value)
            edge_args += [field_param, field_value]

        gremlin.func('next').addEdge(*edge_args)

        return self._add_gremlin_query(entity)