Beispiel #1
0
def test_add_same_code_element():
	db_url = 'postgresql://*****:*****@localhost:5432/src_add_same'	
	db = SQLAlchemyORM(db_url)
	db.create_all(True)

	filepath = 'some/path/file.src'
	file = File(filepath)
	src_file = SourceFile(file)

	f1 = Operation('get', src_file)
	src_file.add_code_element(f1)

	session = db.create_session()
	session.add(src_file)
	session.commit()	

	src_filedb = session.query(SourceFile).one()
	f2 = Operation('get', src_filedb)

	with pytest.raises(Exception) as e:
		src_filedb.add_code_element(f2)
	assert (('Code element \'get\' of type \'Operation\' is already present')
			in str(e.value))
	session.close()
	db.drop_all()	 	
Beispiel #2
0
def test_add_same_relation(mocker):
    mocker.patch.object(GitPython, 'IsGitRepo', return_value=True)
    mocker.patch.object(GitPython, 'CurrentBranch', return_value='master')
    repo1 = Repository('some/path/terrame')
    sys1 = System('terrame', repo1)
    f1 = File('Cell.lua')
    src1 = SourceFile(f1)
    op1 = Operation('Cell', src1)
    src1.add_code_element(op1)

    f2 = File('CellularSpace.lua')
    src2 = SourceFile(f2)
    op2 = Operation('CellularSpace', src2)
    src2.add_code_element(op2)

    sys1.add_source_file(src1)
    sys1.add_source_file(src2)

    repo2 = Repository('some/path/ca')
    sys2 = System('ca', repo2)
    f3 = File('Anneal.lua')
    src3 = SourceFile(f3)
    c1 = Call('Cell', src3)
    src3.add_code_element(c1)
    c2 = Call('CellularSpace', src3)
    src3.add_code_element(c2)

    sys2.add_source_file(src3)

    ecosystem = Ecosystem()

    tme_author = Author(Person('TerraMe Dev', '*****@*****.**'))
    ca_author = Author(Person('CA Dev', '*****@*****.**'))
    mocker.patch.object(op1, 'author', return_value=tme_author, autospec=True)
    mocker.patch.object(op2, 'author', return_value=tme_author, autospec=True)
    mocker.patch.object(c1, 'author', return_value=ca_author, autospec=True)
    mocker.patch.object(c2, 'author', return_value=ca_author, autospec=True)

    to_info = RelationInfo(sys1, src1, op1)
    from_info = FromRelationInfo(sys2, src3, c1, 10)

    rel1 = Relationship(from_info, to_info)
    rel2 = Relationship(from_info, to_info)

    assert rel1.from_system == rel2.from_system
    assert rel1.to_system == rel2.to_system
    assert rel1.from_code_element == rel2.from_code_element
    assert rel1.to_code_element == rel2.to_code_element

    ecosystem.add_relationship(rel1)
    ecosystem.add_relationship(rel2)

    assert len(ecosystem.relationships) == 2
    ecosystem.relationships[0] == rel1
    ecosystem.relationships[1] == rel2
Beispiel #3
0
def test_source_file_crud():
	db_url = 'postgresql://*****:*****@localhost:5432/src_file_crud'
	db = SQLAlchemyORM(db_url)
	db.create_all(True)

	# create
	filepath = 'some/path/file.src'
	file = File(filepath)
	src_file = SourceFile(file)
	f1 = Operation('get', src_file)
	f2 = Operation('add', src_file)
	c1 = Call('call', src_file)	
	src_file.add_code_element(f1)
	src_file.add_code_element(f2)
	src_file.add_code_element(c1)

	session = db.create_session()
	session.add(file)
	session.commit()

	# read
	src_filedb = session.query(SourceFile).get(1)
	assert src_filedb.file_id == file.id
	assert src_filedb.file.ext == file.ext
	assert src_filedb.code_element_by_key(f1.key)
	assert src_filedb.code_element_by_key(f2.key)
	assert src_filedb.code_element_by_key(c1.key)

	# update
	file.ext = 'crs'
	f3 = Operation('update', src_file)
	session.add(f3)
	session.commit()
	src_filedb = session.query(SourceFile).get(1)
	code_elements = session.query(Operation).filter_by(source_file_id=src_filedb.id).all()	
	assert src_filedb.file.ext == file.ext
	assert len(code_elements) == 3

	# delete
	session.delete(src_file)
	session.commit()
	src_filedb = session.query(SourceFile).get(1)
	filedb = session.query(File).get(1)
	funcsdb = session.query(Operation).all()
	assert src_filedb is None
	assert filedb.name == 'file'
	assert len(funcsdb) == 0

	session.close()
	db.drop_all()
Beispiel #4
0
def test_operation_exists():
	filepath = 'some/path/file.src'
	file = File(filepath)
	src_file = SourceFile(file)

	f1 = Operation('get', src_file)
	src_file.add_code_element(f1)	

	assert src_file.code_element_exists(f1)
Beispiel #5
0
def test_operation_crud():
    db_url = 'postgresql://*****:*****@localhost:5432/operation_crud'
    db = SQLAlchemyORM(db_url)
    db.create_all(True)

    # create
    filepath = 'some/path/file.src'
    file = File(filepath)
    src_file = SourceFile(file)
    f1 = Operation('get', src_file)
    f2 = Operation('add', src_file)

    session = db.create_session()
    session.add(src_file)
    session.commit()

    # read
    funcsdb = session.query(Operation).all()
    assert len(funcsdb) == 2
    assert funcsdb[0].name == 'get'
    assert funcsdb[1].name == 'add'

    # update
    f1.name = 'getX'
    session.commit()
    f1db = session.query(Operation).get(1)
    assert f1db.name == 'getX'

    # delete
    session.delete(f1)
    session.commit()
    funcsdb = session.query(Operation).all()
    assert len(funcsdb) == 1
    session.delete(f2)
    session.commit()
    funcsdb = session.query(Operation).all()
    assert len(funcsdb) == 0
    filedb = session.query(File).get(1)
    src_filedb = session.query(SourceFile).get(1)
    assert filedb.name == 'file'
    assert src_filedb.ext == 'src'

    session.close()
    db.drop_all()
Beispiel #6
0
def test_add_operation_same_name():
	filepath = 'some/path/file.src'
	file = File(filepath)
	src_file = SourceFile(file)

	f1 = Operation('get', src_file)
	src_file.add_code_element(f1)

	with pytest.raises(Exception) as e:
		src_file.add_code_element(f1)
	assert (('Code element \'get\' of type \'Operation\' is already present')
			in str(e.value))		
Beispiel #7
0
    def _java_reverse_engineering(self, src_file, src):
        code_elements = []
        parser = JavaParser()
        try:
            parser.parser(src)
            classes = parser.extract_operations()
            self._remove_classes_by_modifiers(classes)
            operations = []
            for i in range(len(classes)):
                methods = classes[i]['operations']
                operations = [method['name'] for method in methods]
                self._remove_methods_duplicated(methods)
                self._remove_private_modifiers(methods)
                for op in methods:
                    code_elements.append(Operation(op, src_file))

            calls = parser.extract_calls()
            self._remove_inner_calls_java(calls, operations)
            self._remove_duplicated_java(calls)
            for call in calls:
                cal = Call(call['ref'], src_file)
                cal.caller = call['caller']
                code_elements.append(cal)

            associations = parser.extract_associations()
            self._remove_duplicated(associations)
            for ass in associations:
                code_elements.append(Association(ass, src_file))
        except SyntaxException:
            pass
        except LexerException:
            pass
        except Exception as e:
            print('\n')
            print(src_file.fullpath)
            print(e)
            traceback.print_exc()
            raise e

        return code_elements
Beispiel #8
0
def test_add_operation():
	db_url = 'postgresql://*****:*****@localhost:5432/src_file_one_to_one'	
	db = SQLAlchemyORM(db_url)
	db.create_all(True)

	filepath = 'some/path/file.src'
	file = File(filepath)
	src_file = SourceFile(file)

	f1 = Operation('get', src_file)
	src_file.add_code_element(f1)

	session = db.create_session()
	session.add(src_file)
	session.commit()	

	f1db = session.query(Operation).get(1)
	assert f1db.name == 'get'
	assert f1db.source_file_id == 1

	session.close()
	db.drop_all()	 
Beispiel #9
0
    def _lua_reverse_engineering(self, src_file, src):
        code_elements = []
        parser = LuaParser()
        try:
            parser.parser(src)
            functions = (parser.extract_functions() +
                         parser.extract_table_functions())
            self._remove_duplicated(functions)
            for func in functions:
                code_elements.append(Operation(func, src_file))

            calls = parser.extract_calls() + parser.extract_global_calls()
            local_functions = parser.extract_local_functions()
            all_functions = functions + local_functions
            self._remove_inner_calls(calls, all_functions)
            self._remove_duplicated(calls)
            for call in calls:
                code_elements.append(Call(call, src_file))
        except SyntaxException:
            pass
        except ChunkException:
            pass

        return code_elements
Beispiel #10
0
	def make_relations(self, sys_from, sys_to, session=NullSession()):
		from_source_files = sys_from.source_files
		to_source_files = sys_to.source_files
		for from_fullpath, from_src_file in from_source_files.items():
			from_code_elements = from_src_file.code_elements()
			from_associations = self._get_associations(from_code_elements)			
			for to_fullpath, to_src_file in to_source_files.items():
				if (self._has_association(to_src_file, from_associations)
						or to_src_file.ext == 'lua'):
					for from_key, from_code_element in from_code_elements.items():
						if isinstance(from_code_element, Call):
							to_operation = Operation(from_code_element.name, to_src_file)
							if to_src_file.code_element_exists(to_operation):	
								to_code_element = to_src_file.code_element_by_key(to_operation.key) 
								from_code_element_count = self._total_of_calls(from_src_file,
															 from_code_element)
								from_info = FromRelationInfo(sys_from, from_src_file, 
												from_code_element, from_code_element_count)
								to_info = RelationInfo(sys_to, to_src_file, to_code_element)
								rel = Relationship(from_info, to_info)
								self.ecosystem.add_relationship(rel)
								session.add(rel)
							session.expunge(to_operation)
					session.commit()
Beispiel #11
0
def test_add_relationship(mocker):
    db_url = 'postgresql://*****:*****@localhost:5432/eco_add_relation'
    db = SQLAlchemyORM(db_url)
    db.create_all(True)

    repo1 = Repository('repo/terrame')
    sys1 = System('terrame', repo1)
    f1 = File('some/path/file1.src')
    src1 = SourceFile(f1)
    f11 = Operation('get', src1)
    f12 = Operation('add', src1)
    c11 = Call('call', src1)
    src1.add_code_element(f11)
    src1.add_code_element(f12)
    src1.add_code_element(c11)
    sys1.add_source_file(src1)

    repo2 = Repository('repo/ca')
    sys2 = System('ca', repo2)
    f2 = File('some/path/file2.src')
    src2 = SourceFile(f2)
    f21 = Operation('call', src2)
    c21 = Call('get', src2)
    src2.add_code_element(f21)
    sys2.add_source_file(src2)

    session = db.create_session()
    session.add(src1)
    session.add(src2)
    session.commit()

    tme_author = Author(Person('TerraMe Dev', '*****@*****.**'))
    ca_author = Author(Person('CA Dev', '*****@*****.**'))
    mocker.patch.object(f11, 'author', return_value=tme_author, autospec=True)
    mocker.patch.object(f12, 'author', return_value=tme_author, autospec=True)
    mocker.patch.object(c11, 'author', return_value=tme_author, autospec=True)
    mocker.patch.object(f21, 'author', return_value=ca_author, autospec=True)
    mocker.patch.object(c21, 'author', return_value=ca_author, autospec=True)

    to_info = RelationInfo(sys1, src1, c11)
    from_info = FromRelationInfo(sys2, src2, f21, 10)
    rel1 = Relationship(from_info, to_info)

    to_info = RelationInfo(sys1, src1, f11)
    from_info = FromRelationInfo(sys2, src2, c21, 20)
    rel2 = Relationship(from_info, to_info)

    eco = Ecosystem()
    eco.add_relationship(rel1)
    eco.add_relationship(rel2)

    session.add(eco)
    session.commit()
    ecodb = session.query(Ecosystem).one()
    relsdb = ecodb.relationships

    assert len(relsdb) == 2

    rel1db = relsdb[0]
    assert rel1db.from_system.name == 'ca'
    assert rel1db.to_system.name == 'terrame'
    assert rel1db.from_source_file.name == 'file2'
    assert rel1db.to_source_file.name == 'file1'
    assert rel1db.from_code_element.name == 'call'
    assert rel1db.to_code_element.name == 'call'
    assert rel1db.from_author.name == 'CA Dev'
    assert rel1db.to_author.name == 'TerraMe Dev'
    assert rel1db.from_code_element_count == 10

    rel2db = relsdb[1]
    assert rel2db.from_system.name == 'ca'
    assert rel2db.to_system.name == 'terrame'
    assert rel2db.from_source_file.name == 'file2'
    assert rel2db.to_source_file.name == 'file1'
    assert rel2db.from_code_element.name == 'get'
    assert rel2db.to_code_element.name == 'get'
    assert rel2db.from_author.name == 'CA Dev'
    assert rel2db.to_author.name == 'TerraMe Dev'
    assert rel2db.from_code_element_count == 20

    session.close()
    db.drop_all()
Beispiel #12
0
def test_make_relations(mocker):
	mocker.patch.object(GitPython, 'IsGitRepo', return_value=True)
	mocker.patch.object(GitPython, 'CurrentBranch', return_value='master')
	repo1 = Repository('some/path/terrame')
	sys1 = System('terrame', repo1)
	f1 = File('Cell.lua')
	src1 = SourceFile(f1)
	op1 = Operation('Cell', src1)
	src1.add_code_element(op1)

	f2 = File('CellularSpace.lua')	
	src2 = SourceFile(f2)
	op2 = Operation('CellularSpace', src2)
	src2.add_code_element(op2)	

	sys1.add_source_file(src1)
	sys1.add_source_file(src2)

	repo2 = Repository('some/path/ca')
	sys2 = System('ca', repo2)
	f3 = File('Anneal.lua')
	src3 = SourceFile(f3)
	c1 = Call('Cell', src3)
	src3.add_code_element(c1)
	c2 = Call('CellularSpace', src3)
	src3.add_code_element(c2)

	sys2.add_source_file(src3)	

	ecosystem = Ecosystem()

	tme_author = Author(Person('TerraMe Dev', '*****@*****.**'))
	ca_author = Author(Person('CA Dev', '*****@*****.**'))
	mocker.patch.object(op1, 'author', return_value=tme_author, autospec=True)
	mocker.patch.object(op2, 'author', return_value=tme_author, autospec=True)
	mocker.patch.object(c1, 'author', return_value=ca_author, autospec=True)
	mocker.patch.object(c2, 'author', return_value=ca_author, autospec=True)	

	mocker.patch.object(EcosystemAnalyzer, '_total_of_calls', return_value=10)

	ecolyzer = EcosystemAnalyzer(ecosystem)
	ecolyzer.make_relations(sys2, sys1)

	relationships = ecosystem.relationships

	assert len(relationships) == 2

	rel1 = relationships[0]
	rel2 = relationships[1]

	assert rel1.from_system.name == 'ca'
	assert rel1.from_author.name == 'CA Dev'
	assert rel1.from_author.email == '*****@*****.**'
	assert rel1.to_system.name == 'terrame'
	assert rel1.to_author.name == 'TerraMe Dev'	
	assert rel1.to_author.email == '*****@*****.**'	
	assert rel1.from_code_element.name == rel1.to_code_element.name == 'Cell'
	
	assert rel2.from_system.name == 'ca'
	assert rel2.from_author.name == 'CA Dev'
	assert rel2.from_author.email == '*****@*****.**'	
	assert rel2.to_system.name == 'terrame'
	assert rel2.to_author.name == 'TerraMe Dev'	
	assert rel2.to_author.email == '*****@*****.**'	
	assert rel2.from_code_element.name == rel2.to_code_element.name == 'CellularSpace'