def testExistingRepoWithErroneousGraphFiles(self): content1 = '<urn:x> <urn:y> <urn:z> .' content2 = '<urn:1> <urn:2> <urn:3> .\n' content2 += '<urn:a> <urn:b> <urn:c> .\n' content3 = '<urn:a> <urn:b> <urn:c> .' repoContent = {'no uri': content1, 'http://aksw.org/': content2, '': content3} with TemporaryRepositoryFactory().withGraphs(repoContent) as repo: conf = QuitGraphConfiguration(repository=repo) conf.initgraphconfig('master') self.assertEqual(conf.mode, 'graphfiles') graphs = conf.getgraphs() self.assertEqual( sorted([str(x) for x in graphs]), ['http://aksw.org/']) files = conf.getfiles() self.assertEqual(sorted(files), ['graph_1.nt']) serialization = conf.getserializationoffile('graph_1.nt') self.assertEqual(serialization, 'nt') gfMap = conf.getgraphurifilemap() self.assertEqual(gfMap, { rdflib.term.URIRef('http://aksw.org/'): 'graph_1.nt', }) self.assertEqual(conf.getgraphuriforfile('graph_1.nt').n3(), '<http://aksw.org/>') self.assertEqual(conf.getfileforgraphuri('http://aksw.org/'), 'graph_1.nt')
def testDefaultGraph(self): content1 = '<urn:x> <urn:y> <urn:z> <http://example.org/> .' repoContent = {'http://example.org/': content1} with TemporaryRepositoryFactory().withGraphs(repoContent, 'configfile') as repo: conf = quit.conf.QuitStoreConfiguration(configfile=os.path.join(repo.workdir, 'config.ttl'), namespace='http://quit.instance/') quitInstance = quit.core.Quit(conf, quit.git.Repository(repo.path), None) self.assertEqual(quitInstance.getDefaultBranch(), "master")
def testGraphConfigurationMethods(self): content1 = '<urn:x> <urn:y> <urn:z> .' content2 = '<urn:1> <urn:2> <urn:3> .\n' content2 += '<urn:a> <urn:b> <urn:c> .' repoContent = {'http://example.org/': content1, 'http://aksw.org/': content2} with TemporaryRepositoryFactory().withGraphs(repoContent, 'configfile') as repo: conf = QuitGraphConfiguration(repository=repo) conf.initgraphconfig('master') conf.removegraph('http://aksw.org/') self.assertEqual(conf.getgraphurifilemap(), { rdflib.term.URIRef('http://example.org/'): 'graph_1.nt'}) self.assertEqual(conf.getfileforgraphuri('http://aksw.org/'), None) self.assertEqual(conf.getgraphuriforfile('graph_0.nt'), None) self.assertEqual(conf.getserializationoffile('graph_0.nt'), None) conf.addgraph('http://aksw.org/', 'new_file.nt', 'nt') self.assertEqual(conf.getgraphurifilemap(), { rdflib.term.URIRef('http://aksw.org/'): 'new_file.nt', rdflib.term.URIRef('http://example.org/'): 'graph_1.nt'}) self.assertEqual(conf.getfileforgraphuri('http://aksw.org/'), 'new_file.nt') self.assertEqual(conf.getgraphuriforfile('new_file.nt').n3(), '<http://aksw.org/>') self.assertEqual(conf.getserializationoffile('new_file.nt'), 'nt')
def testPullRepoWithUnbornHead(self): """Test if it is possible to pull from a remote repository.""" graphContent = """ <http://ex.org/x> <http://ex.org/y> <http://ex.org/z> <http://example.org/> .""" with TemporaryRepositoryFactory().withGraph("http://example.org/", graphContent) as remote: with TemporaryRepository(False) as local: local.remotes.create("origin", remote.path) quitRepo = quit.git.Repository(local.workdir) self.assertFalse(remote.is_empty) self.assertTrue(local.is_empty) self.assertTrue(quitRepo.is_empty) remoteHead = remote.revparse_single('HEAD').hex with self.assertRaises(RevisionNotFound): quitRepo.revision('HEAD') quitRepo.pull() self.assertEqual( quitRepo.revision('origin/master').id, remoteHead) self.assertFalse(remote.is_empty) self.assertFalse(local.is_empty) self.assertFalse(quitRepo.is_empty)
def testStoreConfigurationWithConfigfile(self): content1 = '<urn:x> <urn:y> <urn:z> .' repoContent = {'http://example.org/': content1} with TemporaryRepositoryFactory().withGraphs(repoContent, 'configfile') as repo: conf = QuitStoreConfiguration(configfile=join(repo.workdir, 'config.ttl'), namespace=self.ns) self.assertEqual(conf.getRepoPath(), repo.workdir) self.assertEqual(conf.getDefaultBranch(), None)
def testFetchUpstreamRepo(self): """Test if it is possible to from from a remote, which set as upstream.""" graphContent = """ <http://ex.org/x> <http://ex.org/x> <http://ex.org/x> <http://example.org/> .""" with TemporaryRepositoryFactory().withGraph("http://example.org/", graphContent) as remote: with TemporaryRepository(clone_from_repo=remote) as local: quitRepo = quit.git.Repository(local.workdir) self.assertFalse(remote.is_empty) self.assertFalse(local.is_empty) self.assertFalse(quitRepo.is_empty) with open(path.join(remote.workdir, "graph.nq"), "a") as graphFile: graphContent = """ <http://ex.org/x> <http://ex.org/y> <http://ex.org/z> <http://example.org/> .""" graphFile.write(graphContent) createCommit(repository=remote) remoteHead = remote.revparse_single('HEAD').hex localHead = local.revparse_single('HEAD').hex self.assertNotEqual(localHead, remoteHead) quitRepo.fetch() self.assertEqual( quitRepo.revision('origin/master').id, remoteHead) self.assertFalse(remote.is_empty) self.assertFalse(local.is_empty) self.assertFalse(quitRepo.is_empty)
def testPushRepoWithDivergedRemote(self): """Test for an exception, if the local and remote repositories are diverged.""" with TemporaryRepositoryFactory().withEmptyGraph( "http://example.org/") as remote: graphContent = """ <http://ex.org/x> <http://ex.org/y> <http://ex.org/z> <http://example.org/> .""" with TemporaryRepositoryFactory().withGraph( "http://example.org/", graphContent) as local: local.remotes.create("origin", remote.path) quitRepo = quit.git.Repository(local.workdir) self.assertFalse(remote.is_empty) self.assertFalse(local.is_empty) with self.assertRaises(pygit2.GitError): quitRepo.push("origin", "master")
def testPullRepoFromNamedRemote(self): """Test if it is possible to pull from a remote repository, which is not called origin.""" graphContent = """ <http://ex.org/x> <http://ex.org/y> <http://ex.org/z> <http://example.org/> .""" with TemporaryRepositoryFactory().withGraph("http://example.org/", graphContent) as remote: with TemporaryRepository(False) as local: local.remotes.create("upstream", remote.path) quitRepo = quit.git.Repository(local.workdir) self.assertFalse(remote.is_empty) self.assertTrue(local.is_empty) self.assertTrue(quitRepo.is_empty) remoteHead = remote.revparse_single('HEAD').hex with self.assertRaises(RevisionNotFound): quitRepo.revision('HEAD') quitRepo.pull(remote_name='upstream', refspec="master") self.assertEqual(quitRepo.revision('HEAD').id, remoteHead) self.assertFalse(remote.is_empty) self.assertFalse(local.is_empty) self.assertFalse(quitRepo.is_empty)
def testPullRepoClonedAndPullWithConflict(self): """Test clone, commit on remote and pull with conflict.""" graphContent = "<http://ex.org/a> <http://ex.org/b> <http://ex.org/c> <http://example.org/> .\n" with TemporaryRepositoryFactory().withGraph("http://example.org/", graphContent) as remote: with TemporaryDirectory() as localDirectory: quitRepo = quit.git.Repository(localDirectory, create=True, origin=remote.path) self.assertFalse(remote.is_empty) self.assertFalse(quitRepo.is_empty) remoteHead = remote.revparse_single('HEAD').hex self.assertEqual(quitRepo.revision('HEAD').id, remoteHead) index = quitRepo._repository.index with open(path.join(localDirectory, "graph.nq"), "a") as graphFile: graphFile.write( "<http://ex.org/x> <http://ex.org/y> <http://ex.org/y> <http://example.org/> .\n" ) index.add("graph.nq") index.write() tree = index.write_tree() author = Signature('QuitStoreTest', '*****@*****.**') localCommitid = quitRepo._repository.create_commit( 'HEAD', author, author, "from local", tree, [remoteHead]) self.assertFalse(remote.is_empty) self.assertFalse(quitRepo.is_empty) index = remote.index with open(path.join(remote.workdir, "graph.nq"), "a") as graphFile: graphFile.write( "<http://ex.org/x> <http://ex.org/z> <http://ex.org/z> <http://example.org/> .\n" ) index.add("graph.nq") index.write() tree = index.write_tree() remoteCommitid = remote.create_commit('HEAD', author, author, "from remote", tree, [remoteHead]) remoteQuitRepo = quit.git.Repository(remote.workdir) with self.assertRaises(QuitMergeConflict): quitRepo.pull() self.assertEqual( quitRepo.revision('HEAD').id, str(localCommitid)) self.assertEqual( remoteQuitRepo.revision('HEAD').id, str(remoteCommitid))
def testStoreConfigurationUpstream(self): content1 = '<urn:x> <urn:y> <urn:z> .' repoContent = {'http://example.org/': content1} with TemporaryRepositoryFactory().withGraphs(repoContent, 'configfile') as repo: conf = QuitStoreConfiguration( configfile=join(repo.workdir, 'config.ttl'), upstream='http://cool.repo.git', namespace=self.ns) self.assertEqual(conf.getRepoPath(), repo.workdir) self.assertEqual(conf.getUpstream(), 'http://cool.repo.git')
def testPushRepoWithRemoteName(self): """Test if it is possible to push to a remote repository, which is not called orign.""" with TemporaryRepository(True) as remote: graphContent = "<http://ex.org/x> <http://ex.org/y> <http://ex.org/z> <http://example.org/> ." with TemporaryRepositoryFactory().withGraph( "http://example.org/", graphContent) as local: local.remotes.create("upstream", remote.path) quitRepo = quit.git.Repository(local.workdir) self.assertTrue(remote.is_empty) self.assertFalse(local.is_empty) quitRepo.push("upstream", "master") self.assertFalse(remote.is_empty) self.assertFalse(local.is_empty)
def testNamespace(self): content1 = '<urn:x> <urn:y> <urn:z> .' repoContent = {'http://example.org/': content1} with TemporaryRepositoryFactory().withGraphs(repoContent) as repo: good = ['http://example.org/thing#', 'https://example.org/', 'http://example.org/things/'] bad = [None, 'file:///home/quit/', 'urn:graph/', 'urn:graph', '../test'] # good base namespaces for uri in good: conf = QuitStoreConfiguration(targetdir=repo.workdir, namespace=uri) self.assertEqual(conf.namespace, uri) # bad base namespaces for uri in bad: with self.assertRaises(InvalidConfigurationError): QuitStoreConfiguration(targetdir=repo.workdir, namespace=uri)
def testPushRepoWithRemoteReject(self): """Test for an exception, if the remote repositories rejects a push. CAUTION: This test is disabled, because it requires a remote with pre-receive hook. Unfortunately the libgit2 does not execute pre-receive hooks on local repositories. """ graphContent = """ <http://ex.org/x> <http://ex.org/y> <http://ex.org/z> <http://example.org/> .""" with TemporaryRepositoryFactory().withGraph("http://example.org/", graphContent) as local: local.remotes.create("origin", "ssh://[email protected]/testing.git") quitRepo = quit.git.Repository(local.workdir) self.assertFalse(local.is_empty) with self.assertRaises(QuitGitPushError): quitRepo.push()
def testPushRepo(self): """Test if it is possible to push to an empty remote repository.""" with TemporaryRepository(True) as remote: graphContent = """ <http://ex.org/x> <http://ex.org/y> <http://ex.org/z> .""" with TemporaryRepositoryFactory().withGraph( "http://example.org/", graphContent) as local: local.remotes.create("origin", remote.path) quitRepo = quit.git.Repository(local.workdir) self.assertTrue(remote.is_empty) self.assertFalse(local.is_empty) quitRepo.push("origin", "master") self.assertFalse(remote.is_empty) self.assertFalse(local.is_empty)
def testPushRepoNotConfiguredNamedRemote(self): """Test if the push failes if the specified remote was not defined.""" with TemporaryRepository(is_bare=True) as remote: graphContent = """ <http://ex.org/x> <http://ex.org/y> <http://ex.org/z> <http://example.org/> .""" with TemporaryRepositoryFactory().withGraph( "http://example.org/", graphContent) as local: local.remotes.create("origin", remote.path) quitRepo = quit.git.Repository(local.workdir) self.assertTrue(remote.is_empty) self.assertFalse(local.is_empty) with self.assertRaises(RemoteNotFound): quitRepo.push("upstream", "master") self.assertTrue(remote.is_empty) self.assertFalse(local.is_empty)
def testPullRepoFromNotConfiguredRemote(self): """Test if it is possible to pull from a remote repository, which is not called origin.""" graphContent = """ <http://ex.org/x> <http://ex.org/y> <http://ex.org/z> <http://example.org/> .""" with TemporaryRepositoryFactory().withGraph("http://example.org/", graphContent) as remote: with TemporaryRepository(False) as local: local.remotes.create("origin", remote.path) quitRepo = quit.git.Repository(local.workdir) self.assertFalse(remote.is_empty) self.assertTrue(local.is_empty) self.assertTrue(quitRepo.is_empty) with self.assertRaises(RemoteNotFound): quitRepo.pull(remote_name='upstream') self.assertFalse(remote.is_empty) self.assertTrue(local.is_empty) self.assertTrue(quitRepo.is_empty)
def testPullRepoClonedAndPullWithChanges(self): """Test clone, commit on remote and pull.""" graphContent = """ <http://ex.org/x> <http://ex.org/y> <http://ex.org/z> <http://example.org/> .""" with TemporaryRepositoryFactory().withGraph("http://example.org/", graphContent) as remote: with TemporaryDirectory() as localDirectory: quitRepo = quit.git.Repository(localDirectory, create=True, origin=remote.path) self.assertFalse(remote.is_empty) self.assertFalse(quitRepo.is_empty) remoteHead = remote.revparse_single('HEAD').hex self.assertEqual(quitRepo.revision('HEAD').id, remoteHead) quitRepo.pull() self.assertEqual(quitRepo.revision('HEAD').id, remoteHead) self.assertFalse(remote.is_empty) self.assertFalse(quitRepo.is_empty) remoteQuitRepo = quit.git.Repository(remote.workdir) index = remoteQuitRepo.index(remoteHead) graphContent += """ <http://ex.org/x> <http://ex.org/z> <http://ex.org/z> <http://example.org/> .""" index.add("graph.nq", graphContent) author = Signature('QuitStoreTest', '*****@*****.**') commitid = index.commit("from test", author.name, author.email) quitRepo.pull() self.assertEqual(quitRepo.revision('HEAD').id, str(commitid))
def testPullRepoClonedNoChanges(self): """Test pull if both repos are at the same state.""" graphContent = """ <http://ex.org/x> <http://ex.org/y> <http://ex.org/z> <http://example.org/> .""" with TemporaryRepositoryFactory().withGraph("http://example.org/", graphContent) as remote: with TemporaryDirectory() as localDirectory: quitRepo = quit.git.Repository(localDirectory, create=True, origin=remote.path) self.assertFalse(remote.is_empty) self.assertFalse(quitRepo.is_empty) remoteHead = remote.revparse_single('HEAD').hex self.assertEqual(quitRepo.revision('HEAD').id, remoteHead) quitRepo.pull("origin", "master") self.assertEqual(quitRepo.revision('HEAD').id, remoteHead) self.assertFalse(remote.is_empty) self.assertFalse(quitRepo.is_empty)
def testPullRepoClonedAndPullWithMerge(self): """Test clone, commit on remote and pull with merge, which resolves without conflicts.""" graphContent = "<http://ex.org/a> <http://ex.org/b> <http://ex.org/c> <http://example.org/> .\n" with TemporaryRepositoryFactory().withGraph("http://example.org/", graphContent) as remote: with TemporaryDirectory() as localDirectory: quitRepo = quit.git.Repository(localDirectory, create=True, origin=remote.path) self.assertFalse(remote.is_empty) self.assertFalse(quitRepo.is_empty) remoteHead = remote.revparse_single('HEAD').hex self.assertEqual(quitRepo.revision('HEAD').id, remoteHead) index = quitRepo.index(remoteHead) graph2Content = "<http://ex.org/x> <http://ex.org/y> <http://ex.org/y> <http://example.org/> .\n" index.add("graph2.nq", graph2Content) index.add("graph2.nq.graph", "http://example2.org/") author = Signature('QuitStoreTest', '*****@*****.**') localCommitid = index.commit("from local", author.name, author.email) quitRepo._repository.checkout_tree( quitRepo._repository.get(localCommitid)) self.assertEqual( quitRepo.revision('HEAD').id, str(localCommitid)) self.assertFalse(remote.is_empty) self.assertFalse(quitRepo.is_empty) remoteQuitRepo = quit.git.Repository(remote.workdir) index = remoteQuitRepo.index(remoteHead) graphContent += "<http://ex.org/x> <http://ex.org/z> <http://ex.org/z> <http://example.org/> .\n" index.add("graph.nq", graphContent) remoteCommitid = index.commit("from remote", author.name, author.email) remoteQuitRepo._repository.checkout_tree( remoteQuitRepo._repository.get(remoteCommitid)) quitRepo.pull() self.assertNotEqual( quitRepo.revision('HEAD').id, str(localCommitid)) self.assertNotEqual( quitRepo.revision('HEAD').id, str(remoteCommitid)) # check if head has local and remote commit id as ancestor self.assertListEqual([ parent.id for parent in quitRepo.revision('HEAD').parents ], [str(localCommitid), str(remoteCommitid)]) # check if the merged commit contains all file contents with open(path.join(localDirectory, "graph.nq")) as f: self.assertEqual("".join(f.readlines()), graphContent) with open(path.join(localDirectory, "graph2.nq")) as f: self.assertEqual("".join(f.readlines()), graph2Content)
def testNoConfigInformation(self): with TemporaryRepositoryFactory().withNoConfigInformation() as repo: conf = QuitGraphConfiguration(repository=repo) conf.initgraphconfig('master') self.assertEqual(conf.mode, 'graphfiles')
def testWrongConfigurationFile(self): with TemporaryRepositoryFactory().withBothConfigurations() as repo: conf = QuitGraphConfiguration(repository=repo) self.assertRaises(InvalidConfigurationError, conf.initgraphconfig, 'master')