def test_delete_cascade(): db_url = 'postgresql://*****:*****@localhost:5432/author_del_cascade' db = SQLAlchemyORM(db_url) db.create_all(True) repo1 = Repository('repo/terrame') person = Person('dev1', '*****@*****.**') author1 = Author(person, repo1) repo2 = Repository('repo/ca') author2 = Author(person, repo2) assert author2.id is None session = db.create_session() session.add(author1) session.commit() assert author2.id is not None authorsdb = session.query(Author).all() assert len(authorsdb) == 2 session.delete(person) session.commit() authorsdb = session.query(Author).all() assert len(authorsdb) == 0 session.close() db.drop_all()
def test_same_person_in_two_repos(): db_url = 'postgresql://*****:*****@localhost:5432/author_two_repos' db = SQLAlchemyORM(db_url) db.create_all(True) repo1 = Repository('repo/terrame') person = Person('dev1', '*****@*****.**') author1 = Author(person, repo1) repo2 = Repository('repo/ca') author2 = Author(person, repo2) assert author2.id is None session = db.create_session() session.add(author1) session.commit() assert author2.id is not None authorsdb = session.query(Author).all() assert len(authorsdb) == 2 assert authorsdb[0].person == authorsdb[1].person assert authorsdb[0].repository != authorsdb[1].repository session.close() db.drop_all()
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
def test_crud(): # create repo = Repository('repo/terrame') # sys = System('terrame', repo) commit_info = CommitInfo('hashhashhash') commit_info.date = datetime.datetime(2019, 2, 6, 14, 14, 55) commit_info.msg = 'commit message' commit_info.author_name = 'author' commit_info.author_email = '*****@*****.**' author = Author(Person(commit_info.author_name, commit_info.author_email), repo) commit = Commit(commit_info, author, repo) modinfo = ModificationInfo('some/path/file.ext') modinfo.old_path = '' modinfo.new_path = 'some/path/file.ext' modinfo.added = 10 modinfo.removed = 0 modinfo.type = 'ADD' file = File(modinfo.filename) mod = Modification(modinfo, file, commit) assert mod.id is None session = db.create_session() session.add(commit) session.commit() assert mod.id is not None # read commitdb = session.query(Commit).get(1) assert commitdb.msg == commit_info.msg assert commitdb.date.strftime('%Y-%m-%d %H:%M:%S') == '2019-02-06 14:14:55' assert commitdb.hash == commit_info.hash assert commitdb.repository.path == repo.path assert commitdb.author.name == commit_info.author_name assert commitdb.author.email == commit_info.author_email # update commit.msg = 'updating message' session.commit() commitdb = session.query(Commit).get(1) assert commitdb.msg == commit.msg # delete session.delete(commit) session.commit() commitdb = session.query(Commit).get(1) moddb = session.query(Modification).get(1) repodb = session.query(Repository).get(1) authordb = session.query(Author).get(1) filedb = session.query(File).get(1) assert commitdb is None assert moddb is None assert repodb.path == repo.path assert authordb.email == author.email assert filedb.id == 1 == file.id session.close() db.drop_all()
def test_crud(): # create repo = Repository('repo/terrame') # sys = System('terrame', repo) commit_info = CommitInfo('hashhashhash') commit_info.date = datetime.datetime(2019, 2, 6, 14, 14, 55) commit_info.msg = 'commit message' commit_info.author_name = 'author' commit_info.author_email = '*****@*****.**' author = Author(Person(commit_info.author_name, commit_info.author_email)) commit = Commit(commit_info, author, repo) modinfo = ModificationInfo('some/path/file.ext') modinfo.old_path = '' modinfo.new_path = 'some/path/file.ext' modinfo.added = 10 modinfo.removed = 0 modinfo.status = 'ADD' file = File(modinfo.filename) mod = Modification(modinfo, file, commit) session = db.create_session() session.add(mod) session.commit() # read moddb = session.query(Modification).get(1) assert moddb.new_path == 'some/path/file.ext' assert moddb.old_path == '' assert moddb.added == 10 assert moddb.removed == 0 assert moddb.status == 'ADD' assert moddb.commit_id == 1 assert moddb.file_id == 1 # update mod.status = 'DELETED' session.commit() moddb = session.query(Modification).get(1) assert moddb.status == 'DELETED' # delete session.delete(mod) session.commit() moddb = session.query(Modification).get(1) commitdb = session.query(Commit).get(1) repodb = session.query(Repository).get(1) authordb = session.query(Author).get(1) filedb = session.query(File).get(1) assert moddb is None assert commitdb.id == 1 assert repodb.id == 1 assert authordb.id == 1 assert filedb.id == 1 session.close() db.drop_all()
def test_crud(): db_url = 'postgresql://*****:*****@localhost:5432/author_crud' db = SQLAlchemyORM(db_url) db.create_all(True) # create repo = Repository('repo/terrame') person = Person('dev1', '*****@*****.**') author = Author(person, repo) session = db.create_session() session.add(author) session.commit() authordb = session.query(Author).one() assert authordb.person == person assert authordb.repository == repo session.close() db.drop_all()
def test_get_commit_source_file(): db_url = 'postgresql://*****:*****@localhost:5432/miner_sources' db = SQLAlchemyORM(db_url) db.create_all(True) repo = Repository('repo/terrame') sys = System('terrame', repo) miner = RepositoryMiner(repo, sys) commit_info = miner.get_commit_info( '082dff5e822ea1b4491911b7bf434a7f47a4be26') author = Author(Person(commit_info.author_name, commit_info.author_email)) commit = Commit(commit_info, author, repo) session = db.create_session() for mod_info in commit_info.modifications: file = File(mod_info.new_path) sys.add_file(file) mod = Modification(mod_info, file, commit) if miner.is_source_file(file): srcfile = SourceFile(file) code_elements = miner.extract_code_elements(srcfile, mod) for element in code_elements: element.modification = mod session.add(element) session.add(mod) session.commit() afile = sys.get_file('base/lua/CellularSpace.lua') srcfiledb = session.query(SourceFile).filter_by(file_id=afile.id).first() assert srcfiledb.ext == 'lua' assert srcfiledb.name == 'CellularSpace' assert srcfiledb.code_elements_len() == 78 functions = session.query(Operation).filter_by( source_file_id=srcfiledb.id).all() assert srcfiledb.code_element_exists(functions[0]) assert functions[0].name == 'CellularSpace' session.close() db.drop_all()
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()
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'
def test_get_commit(): db_url = 'postgresql://*****:*****@localhost:5432/miner_get_commit' db = SQLAlchemyORM(db_url) db.create_all(True) repo = Repository('repo/terrame') sys = System('terrame', repo) miner = RepositoryMiner(repo, sys) commit_info = miner.get_commit_info( '80a562be869dbb984229f608ae9a04d05c5e1689') assert commit_info.msg == 'Initial commit' assert commit_info.date.strftime( '%Y-%m-%d %H:%M:%S') == '2014-09-15 08:12:11' assert commit_info.hash == '80a562be869dbb984229f608ae9a04d05c5e1689' assert commit_info.author_name == 'pedro-andrade-inpe' assert commit_info.author_email == '*****@*****.**' assert len(commit_info.modifications) == 1 assert commit_info.modifications[0].filename == 'LICENSE' assert commit_info.modifications[0].old_path is None assert commit_info.modifications[0].new_path == 'LICENSE' assert commit_info.modifications[0].added == 674 assert commit_info.modifications[0].removed == 0 assert commit_info.modifications[0].status == 'ADD' author = Author(Person(commit_info.author_name, commit_info.author_email)) commit = Commit(commit_info, author, repo) fmodinfo = commit_info.modifications[0] file = File(fmodinfo.filename) filemod = Modification(fmodinfo, file, commit) session = db.create_session() session.add(repo) session.add(sys) session.add(commit) session.add(file) session.add(filemod) session.commit() filemoddb = session.query(Modification).get(1) commitdb = filemoddb.commit filedb = filemoddb.file assert commitdb.msg == 'Initial commit' assert commitdb.date.strftime('%Y-%m-%d %H:%M:%S') == '2014-09-15 08:12:11' assert commitdb.hash == '80a562be869dbb984229f608ae9a04d05c5e1689' assert commitdb.repository.path == repo.path assert commitdb.author.name == 'pedro-andrade-inpe' assert commitdb.author.email == '*****@*****.**' assert filedb.fullpath == 'LICENSE' assert filemoddb.old_path is None assert filemoddb.new_path == 'LICENSE' assert filemoddb.added == 674 assert filemoddb.removed == 0 assert filemoddb.status == 'ADD' commit_info = miner.get_commit_info( 'ffb8347b2de44eb05f6c5eba3b3cb8b7716acebb') assert commit_info.msg == 'Delete LICENSE' assert commit_info.date.strftime( '%Y-%m-%d %H:%M:%S') == '2014-09-17 11:49:45' assert commit_info.hash == 'ffb8347b2de44eb05f6c5eba3b3cb8b7716acebb' assert commit_info.author_name == 'pedro-andrade-inpe' assert commit_info.author_email == '*****@*****.**' assert len(commit_info.modifications) == 1 assert commit_info.modifications[0].filename == 'LICENSE' assert commit_info.modifications[0].old_path == 'LICENSE' assert commit_info.modifications[0].new_path is None assert commit_info.modifications[0].added == 0 assert commit_info.modifications[0].removed == 674 assert commit_info.modifications[0].status == 'DELETE' commit = Commit(commit_info, author, repo) fmodinfo = commit_info.modifications[0] filemod = Modification(fmodinfo, file, commit) session.add(commit) session.add(filemod) session.commit() filemoddb2 = session.query(Modification).get(2) commitdb2 = filemoddb2.commit filedb2 = filemoddb2.file assert commitdb2.msg == 'Delete LICENSE' assert commitdb2.date.strftime( '%Y-%m-%d %H:%M:%S') == '2014-09-17 11:49:45' assert commitdb2.hash == 'ffb8347b2de44eb05f6c5eba3b3cb8b7716acebb' assert commitdb2.repository.id == repo.id assert commitdb2.author.name == 'pedro-andrade-inpe' assert commitdb2.author.email == '*****@*****.**' assert filedb2.fullpath == 'LICENSE' assert filemoddb2.old_path == 'LICENSE' assert filemoddb2.new_path is None assert filemoddb2.added == 0 assert filemoddb2.removed == 674 assert filemoddb2.status == 'DELETE' filemoddb1 = session.query(Modification).get(1) commitdb1 = filemoddb1.commit filedb1 = filemoddb1.file assert commitdb1.msg == 'Initial commit' assert commitdb1.date.strftime( '%Y-%m-%d %H:%M:%S') == '2014-09-15 08:12:11' assert commitdb1.hash == '80a562be869dbb984229f608ae9a04d05c5e1689' assert commitdb1.repository.path == repo.path assert commitdb2.author.name == 'pedro-andrade-inpe' assert commitdb2.author.email == '*****@*****.**' assert filedb1.fullpath == 'LICENSE' assert filemoddb1.old_path is None assert filemoddb1.new_path == 'LICENSE' assert filemoddb1.added == 674 assert filemoddb1.removed == 0 assert filemoddb1.status == 'ADD' session.close() db.drop_all()