Skip to content

Commit

Permalink
Fixing the narps_open.utils.status module (Inria-Empenn#109)
Browse files Browse the repository at this point in the history
* [BUG] inside unit_tests workflow

* Browsing all issues pages from Github API

* Get all pages of GitHub issues

* [TEST] Updating test for status module

* [TEST] fetch several issues
  • Loading branch information
bclenet authored Sep 27, 2023
1 parent 2fa3a0d commit a3e0a5f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 51 deletions.
2 changes: 1 addition & 1 deletion docs/status.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ print(pipeline_info['status'])
report.markdown() # Returns a string containing the markdown
```

You can also use the command-line tool as so. Option `-t` is for the team id, option `-d` allows to print only one of the sub parts of the description among : `general`, `exclusions`, `preprocessing`, `analysis`, and `categorized_for_analysis`.
You can also use the command-line tool as so.

```bash
python narps_open/utils/status -h
Expand Down
18 changes: 14 additions & 4 deletions narps_open/utils/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@
def get_opened_issues():
""" Return a list of opened issues and pull requests for the NARPS Open Pipelines project """
request_url = 'https://api.github.com/repos/Inria-Empenn/narps_open_pipelines/issues'
response = get(request_url, timeout = 2)
response.raise_for_status()

return response.json()
request_url += '?page={page_number}?per_page=100'

issues = []
page = True # Will later be replaced by a table
page_number = 1 # According to the doc, first page is not page 0
# https://docs.github.com/en/rest/issues/issues#list-repository-issues
while bool(page) is True : # Test if the page is empty
response = get(request_url.format(page_number = str(page_number)), timeout = 2)
response.raise_for_status()
page = response.json()
issues += page
page_number += 1

return issues

def get_teams_with_pipeline_files():
""" Return a set of teams having a file for their pipeline in the repository """
Expand Down
123 changes: 77 additions & 46 deletions tests/utils/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,62 @@ def mock_api_issue(mocker):
which is actually imported as `get` inside the `narps_open.utils.status` module.
Hence, we patch the `narps_open.utils.status.get` method.
"""
response = Response()
response.status_code = 200
def json_func():
return [
{
"html_url": "url_issue_2",
"number": 2,
"title" : "Issue for pipeline UK24",
"body" : "Nothing to add here."
},
{
"html_url": "url_pull_3",
"number": 3,
"title" : "Pull request for pipeline 2T6S",
"pull_request" : {},
"body" : "Work has been done."
},
{
"html_url": "url_issue_4",
"number": 4,
"title" : None,
"body" : "This is a malformed issue about C88N."
},
{
"html_url": "url_issue_5",
"number": 5,
"title" : "Issue about 2T6S",
"body" : "Something about 2T6S."
}
]
response.json = json_func
mocker.patch('narps_open.utils.status.get', return_value = response)

# Create a method to mock requests.get
def mocked_requests_get(url, params=None, **kwargs):

response = Response()
response.status_code = 200
def json_func_page_0():
return [
{
"html_url": "url_issue_2",
"number": 2,
"title" : "Issue for pipeline UK24",
"body" : "Nothing to add here."
},
{
"html_url": "url_pull_3",
"number": 3,
"title" : "Pull request for pipeline 2T6S",
"pull_request" : {},
"body" : "Work has been done."
}
]

json_func_page_1 = json_func_page_0

def json_func_page_2():
return [
{
"html_url": "url_issue_4",
"number": 4,
"title" : None,
"body" : "This is a malformed issue about C88N."
},
{
"html_url": "url_issue_5",
"number": 5,
"title" : "Issue about 2T6S",
"body" : "Something about 2T6S."
}
]

def json_func_page_3():
return []

if '?page=1' in url:
response.json = json_func_page_1
elif '?page=2' in url:
response.json = json_func_page_2
elif '?page=3' in url:
response.json = json_func_page_3
else:
response.json = json_func_page_0

return response

mocker.patch('narps_open.utils.status.get', side_effect = mocked_requests_get)
mocker.patch(
'narps_open.utils.status.get_teams_with_pipeline_files',
return_value = ['2T6S', 'UK24', 'Q6O0']
Expand Down Expand Up @@ -94,8 +118,6 @@ def test_get_issues(mocker):
which is actually imported as `get` inside the `narps_open.utils.status` module.
Hence, we patch the `narps_open.utils.status.get` method.
"""
get_opened_issues()

# Create a mock API response for 404 error
response = Response()
response.status_code = 404
Expand All @@ -114,18 +136,27 @@ def json_func():
assert len(get_opened_issues()) == 0

# Create a mock API response for the general usecase
response = Response()
response.status_code = 200
def json_func():
return [
{
"html_url": "urls",
"number": 2,
}
]
response.json = json_func

mocker.patch('narps_open.utils.status.get', return_value = response)
def mocked_requests_get(url, params=None, **kwargs):
response = Response()
response.status_code = 200
def json_func_page_1():
return [
{
"html_url": "urls",
"number": 2,
}
]
def json_func_page_2():
return []

if '?page=2' in url:
response.json = json_func_page_2
else:
response.json = json_func_page_1

return response

mocker.patch('narps_open.utils.status.get', side_effect = mocked_requests_get)
issues = get_opened_issues()
assert len(issues) == 1
assert issues[0]['html_url'] == 'urls'
Expand Down

0 comments on commit a3e0a5f

Please sign in to comment.