示例#1
0
    def test_create_class(self):

        r = self.client.command("select expand(classes) from metadata:schema")
        self.assertFalse(any(x.name == get_orient_valid_class_name(Foo) for x in r))

        create_class(Foo, client=self.client)

        r = self.client.command("select expand(classes) from metadata:schema")

        # we are making sure that the class is defined in Orient
        # by looping through all the defined classes and looking
        # for Foo, note that Foo doesn't have any attributes
        # or properties to check, we just need to know its there
        self.assertTrue(any(x.name == get_orient_valid_class_name(Foo) for x in r))
示例#2
0
def create_class(klass, client=None):
    from models.orient_sql_utils import is_model
    from models.orient_sql_utils import get_model_extensions
    if not is_model(klass):
        raise ValueError("Only OrientDB models can be used to create Orient classes")
    
    if client is None:
        client = get_connection()
    
    class_name = get_orient_valid_class_name(klass)
    
    create_str = 'CREATE CLASS %s' % class_name
    
    parents = get_model_extensions(klass)
    
    if len(parents) > 0:
        if len(parents) > 1:
            extenders = ",".join(parents)
        else:
            extenders = parents[0]
        create_str  =  '%s EXTENDS %s' % (create_str, extenders)
    
    instance = klass()
        
    LOGGER.debug("creating class with command %s" % create_str)

    client.command(create_str)
    
    for k in instance._fields.keys():
        if not instance._fields[k].inherited:
            field_str = 'CREATE PROPERTY %s.%s %s' % (class_name, 
                                  instance._py_to_orient_field_mapping[k], 
                                  instance._fields[k].orientdb_type)
            LOGGER.debug("applying property with command %s" % (field_str))
            client.command(field_str)
示例#3
0
def get_model_extensions(klass):
    extensions = []
    if not hasattr(klass,'__call__'):
        klass = klass.__class__
    if not isinstance(klass(), Model):
        raise ValueError("This method only examines Model and extending classes or instances")
    if not klass.__base__ is Model:
        for extender in klass.__bases__:
            extensions.append(get_orient_valid_class_name(extender))
    return extensions
示例#4
0
def update(obj, client=None):
    
    if client is None:
        client = get_connection()
        
    class_name = get_orient_valid_class_name(obj)
    
    update_str = "UPDATE %s " % class_name
    
    values = {}
    
    for k in obj._fields.keys():
        values[obj._py_to_orient_field_mapping[k]] = obj._fields[k].orient_value()

    update_str = "%s CONTENT %s" % (update_str, json.dumps(values))
    resp = client.command(update_str)
    rec = resp[0]
    return rec
示例#5
0
def insert(obj, client=None):
    
    if client is None:
        client = get_connection()
        
    class_name = get_orient_valid_class_name(obj)
    
    insert_str = "INSERT INTO %s" % class_name
    
    values = {}
    
    for k in obj._fields.keys():
        values[obj._py_to_orient_field_mapping[k]] = obj._fields[k].orient_value()

    insert_str = "%s CONTENT %s" % (insert_str, json.dumps(values))
    LOGGER.debug("executing insert command: %s" % insert_str)
    resp = client.command(insert_str)
    rec = resp[0]
    obj.rid = rec._rid
    return rec
示例#6
0
    def test_create_class_with_attribues(self):

        create_class(ClassWithAttributes, client=self.client)

        # we will get the properties and make sure they are
        # correct types and names
        r = self.client.command("select expand(properties) from ( select expand(classes) from metadata:schema) where name = '%s'" % get_orient_valid_class_name(ClassWithAttributes))
        cwa = ClassWithAttributes()
        # loop through the fields and make sure they are defined
        # the way we expect in OrientDB
        for f in cwa._fields.keys():
            self.assertTrue(any(x.type == cwa._fields[f].orientdb_type_id for x in r))
            self.assertTrue(any(x.name == to_java_case(f) for x in r))
示例#7
0
    def test_create_inheriting_class(self):
        
        create_class(ClassWithAttributes, client=self.client)
        create_class(InheritingClass, client=self.client)

        # we will get the properties and make sure they are
        # correct types and names
        r = self.client.command("select expand(properties) from ( select expand(classes) from metadata:schema) where name = '%s'" % get_orient_valid_class_name(InheritingClass))
        ic = InheritingClass()
        # loop through the fields and make sure they are defined
        # the way we expect in OrientDB
        # orient will NOT show inherited fields
        for f in ic._fields.keys():
            if not ic._fields[f].inherited:
                self.assertTrue(any(x.type == ic._fields[f].orientdb_type_id for x in r))
                self.assertTrue(any(x.name == to_java_case(f) for x in r))
示例#8
0
 def test_get_class_from_orient_class_name(self):
     # test the whole above shebang wrapper
     klass = get_class_from_orient_class_name(get_orient_valid_class_name(ClassToInsert))
     self.assertEqual(klass, ClassToInsert)
     klass = get_class_from_orient_class_name(get_orient_valid_class_name(ClassToInsert()))
     self.assertEqual(klass, ClassToInsert)
示例#9
0
 def test_get_orient_valid_class_name(self):
     self.assertEqual(get_orient_valid_class_name(ClassToInsert), "tests___dot___orient_sql_tests___dot___ClassToInsert")
     self.assertEqual(get_orient_valid_class_name(ClassToInsert()), "tests___dot___orient_sql_tests___dot___ClassToInsert")