Skip to content

Commit

Permalink
Merge pull request #426 from c-martinez/gitlab-tests
Browse files Browse the repository at this point in the history
Adding mock gilab repo
  • Loading branch information
albertmeronyo authored Sep 6, 2023
2 parents 9f1828f + 1307d6e commit a7fc744
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
22 changes: 13 additions & 9 deletions src/fileLoaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def getTextForName(self, query_name):
return '', None

def _getText(self, queryFullName):
"""To be implemented by sub-classes"""
"""To be implemented by sub-classes.
Returns None if the file does not exist."""
raise NotImplementedError("Subclasses must override _getText()!")

def fetchFiles(self):
Expand Down Expand Up @@ -122,7 +123,8 @@ def getTextFor(self, fileItem):
return str(fileItem['decoded_content'], 'utf-8')

def _getText(self, query_name):
"""Return the content of the specified file contained in the github repo."""
"""Return the content of the specified file contained in the github repo.
Returns None if the file does not exist."""
try:
c = self.gh_repo.get_contents(self.subdir + query_name)
return str(c.decoded_content, 'utf-8')
Expand Down Expand Up @@ -214,12 +216,11 @@ def fetchFiles(self):
return files

def getRawRepoUri(self):
"""Returns the root url of the github repo."""
# TODO: replace by gh_repo.html_url ?
"""Returns the root url of the gitlab repo."""
return path.join(static.GITLAB_URL, self.user, self.repo, '-', 'raw', self.branch)

def getTextFor(self, fileItem):
"""Returns the contents of the given file item on the github repo."""
"""Returns the contents of the given file item on the gitlab repo."""
raw_query_uri = fileItem['download_url']

# Add query URI as used entity by the logged activity
Expand All @@ -228,7 +229,8 @@ def getTextFor(self, fileItem):
return str(fileItem['decoded_content'], 'utf-8')

def _getText(self, query_name):
"""Return the content of the specified file contained in the github repo."""
"""Return the content of the specified file contained in the gitlab repo.
Returns None if the file does not exist."""
try:
file_path = path.join(self.subdir, query_name)
f = self.gl_repo.files.get(file_path=file_path, ref=self.branch)
Expand All @@ -238,7 +240,7 @@ def _getText(self, query_name):
return None

def getRepoTitle(self):
"""Return the title of the github repo."""
"""Return the title of the gitlab repo."""
return self.gl_repo.name

def getContactName(self):
Expand Down Expand Up @@ -329,7 +331,8 @@ def getTextFor(self, fileItem):
return self._getText(fileItem['download_url'])

def _getText(self, filename):
"""Return the content of the specified file contained in the local repo."""
"""Return the content of the specified file contained in the local repo.
Returns None if the file does not exist."""
targetFile = path.join(self.baseDir, filename)
if path.exists(targetFile):
with open(targetFile, 'r') as f:
Expand Down Expand Up @@ -446,7 +449,8 @@ def getTextForName(self, query_name):
return '', None

def _getText(self, itemName):
"""Return the content of the specified item in the specification."""
"""Return the content of the specified item in the specification.
Returns None if the file does not exist."""
if itemName in self.spec['files']:
headers = {'Accept' : 'text/plain'}
itemUrl = self.spec['files'][itemName]['download_url']
Expand Down
39 changes: 33 additions & 6 deletions tests/mock_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

from collections import namedtuple
from grlc.fileLoaders import LocalLoader
import base64

base_url = path.join('tests', 'repo')
def buildEntry(entryName):

def buildGHEntry(entryName):
entryName = entryName.replace(base_url, '')

# Named tuple containing properties of mocked github ContentFile
Expand All @@ -22,22 +24,47 @@ def buildEntry(entryName):
type = u'file',
decoded_content = 'FAKE FILE CONTENT'.encode() # Because Github ContentFile object contains bytes.
)
mock_files = [ buildEntry(f) for f in glob(path.join(base_url, '*')) ]

def buildGLEntry(entryName):
entryName = entryName.replace(base_url, '')

return { 'type': 'blob',
'name': entryName
}

mock_gh_files = [ buildGHEntry(f) for f in glob(path.join(base_url, '*')) ]
mock_gl_files = [ buildGLEntry(f) for f in glob(path.join(base_url, '*')) ]

class MockGithubRepo:
def get_contents(self, filename, ref=None):
if filename == "":
return mock_files
return mock_gh_files
else:
for f in mock_files:
for f in mock_gh_files:
if filename in f.name: # filenames contain extra /
return f
return None


class MockGitlabRepo:
pass
class MockGitlabModule:
def __init__(self) -> None:
gl_repo = Mock()

gl_repo.repository_tree = Mock(return_value=mock_gl_files)
gl_repo.files.get.side_effect = self.gl_files_content

self.projects = Mock()
self.projects.get.return_value = gl_repo

def gl_files_content(self, file_path, ref):
'''Returns none if the file is not in the known repo'''
for glf in mock_gl_files:
if file_path in glf['name']: # filenames contain extra /
f = Mock()
f_content = "The text of a file"
f.content = base64.b64encode(f_content.encode("utf-8"))
return f
return None


def mock_requestsUrl(url, headers={}, params={}):
Expand Down
16 changes: 7 additions & 9 deletions tests/test_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from grlc.fileLoaders import LocalLoader, GithubLoader, GitlabLoader, URLLoader
from grlc.queryTypes import qType

from tests.mock_data import MockGithubRepo, MockGitlabRepo, mock_requestsUrl
from tests.mock_data import MockGithubRepo, MockGitlabModule, mock_requestsUrl


class TestGithubLoader(unittest.TestCase):
Expand Down Expand Up @@ -80,15 +80,13 @@ def test_getEndpointText(self):

class TestGitlabLoader(unittest.TestCase):
@classmethod
# TODO: patch gitlab object?
# TODO: Enable tests (remove x from 'xtest' names)
# @patch('???', return_value=MockGitlabRepo())
@patch('grlc.fileLoaders.gitlab.Gitlab', return_value=MockGitlabModule())
def setUpClass(self, mocked_repo):
self.user = 'fakeuser'
self.repo = 'fakerepo'
self.loader = GitlabLoader(self.user, self.repo, subdir=None, sha=None, prov=None)

def xtest_fetchFiles(self):
def test_fetchFiles(self):
files = self.loader.fetchFiles()

# Should return a list of file items
Expand All @@ -101,7 +99,7 @@ def xtest_fetchFiles(self):
for fItem in files:
self.assertIn('download_url', fItem, "File items should have a download_url")

def xtest_getRawRepoUri(self):
def test_getRawRepoUri(self):
repoUri = self.loader.getRawRepoUri()

# Should be a string
Expand All @@ -111,7 +109,7 @@ def xtest_getRawRepoUri(self):
self.assertIn(self.user, repoUri, "Should contain user")
self.assertIn(self.repo, repoUri, "Should contain repo")

def xtest_getTextFor(self):
def test_getTextFor(self):
files = self.loader.fetchFiles()

# the contents of each file
Expand All @@ -128,7 +126,7 @@ def xtest_getTextFor(self):
with self.assertRaises(Exception, msg="Should raise exception for invalid file items"):
text = self.loader.getTextFor({})

def xtest_getTextForName(self):
def test_getTextForName(self):
testableNames = [
('test-rq', qType['SPARQL']),
('test-sparql', qType['SPARQL']),
Expand All @@ -138,7 +136,7 @@ def xtest_getTextForName(self):
text, actualType = self.loader.getTextForName(name)
self.assertEqual(expectedType, actualType, "Query type should match %s != %s" % (expectedType, actualType))

def xtest_getEndpointText(self):
def test_getEndpointText(self):
endpoint = self.loader.getEndpointText()

# Should be some text
Expand Down

0 comments on commit a7fc744

Please sign in to comment.