#34 Beginning of unit test.
Merged 6 years ago by cverna. Opened 6 years ago by cverna.
cverna/libpagure unit_tests  into  master

file modified
+2
@@ -11,3 +11,5 @@ 

  *.pyc

  *.swp

  *.egg-info

+ .cache

+ .coverage 

\ No newline at end of file

file modified
+35
@@ -2,6 +2,41 @@ 

  

  A Python library for Pagure APIs. Pagure is a light-weight git-centered forge based on pygit2, created by Pierre-Yves Chibon.

  

+ ## Docker Development environment

+ 

+ To build the development environment we provide a Dockerfile. You can build the container as follow:

+ 

+     $ cd devel

+     $ docker build -t libpagure_dev .

+     $ cd ..

+ 

+ Once the container is built you can run the tests using the following command for Python 3.6

+ 

+     $ docker run -it --rm -v `pwd`:/code:z libpagure_dev py.test-3.6 --cov libpagure

+ 

+ and for Python 2.7.

+ 

+     $ docker run -it --rm -v `pwd`:/code:z libpagure_dev py.test-2.7 --cov libpagure

+ 

+ You can also run an interactive shell inside the container using:

+ 

+     $ docker run -it --rm -v `pwd`:/code:z libpagure_dev

+ 

+ In each case `pwd` command needs to return the root path of libpagure repository. (ie where this readme is)

+ 

+ ## Running the unit tests outside the Docker environment

+ 

+ First you need to install the dependencies needed ::

+ 

+     $ sudo dnf install python2-requests python3-requests python2-flake8\

+     python3-flake8 python2-pytest python3-pytest python3-pytest-cov\

+     python2-pytest-cov python2-pytest-mock python3-pytest-mock

+ 

+ Then you can execute the test suite using the following commands for Python 2.7 and 3.6. ::

+ 

+     $ py.test-3.6 --cov libpagure

+     $ py.test-2.7 --cov libpagure

+ 

  ## Installation

  ---

  

file added
+12
@@ -0,0 +1,12 @@ 

+ FROM registry.fedoraproject.org/fedora:27

+ 

+ LABEL maintainer = "Clement Verna <cverna@fedoraproject.org>"\

+       summary = "libpagure development environment"\

+       usage = "docker run -it --rm -v `pwd`:/code:z libpagure_dev"

+ 

+ RUN dnf -y install python2-requests python3-requests python2-flake8\

+     python3-flake8 python2-pytest python3-pytest python3-pytest-cov\

+     python2-pytest-cov python2-pytest-mock python3-pytest-mock

+ 

+ WORKDIR /code

+ ENV PYTHONPATH /code 

\ No newline at end of file

file added
+282
@@ -0,0 +1,282 @@ 

+ import pytest

+ 

+ from libpagure import Pagure

+ 

+ 

+ @pytest.fixture(scope='module')

+ def simple_pg():

+     """ Create a simple Pagure object

+     to be used in test

+     """

+     pg = Pagure(pagure_repository="testrepo")

+     return pg

+ 

+ 

+ def test_pagure_object():

+     """ Test the pagure object creation """

+ 

+     pg = Pagure(pagure_token="a token",

+                 pagure_repository="test_repo")

+     assert pg.token == "a token"

+     assert pg.repo == "test_repo"

+     assert pg.namespace is None

+     assert pg.username is None

+     assert pg.instance == "https://pagure.io"

+     assert pg.insecure is False

+     assert pg.header == {"Authorization": "token a token"}

+ 

+ 

+ basic_url_data = [

+     (None, None, 'testrepo', 'https://pagure.io/api/0/testrepo/'),

+     (None, 'testnamespace', 'testrepo',

+      'https://pagure.io/api/0/testnamespace/testrepo/'),

+     ('testfork', None, 'testrepo',

+      'https://pagure.io/api/0/fork/testfork/testrepo/'),

+     ('testfork', 'testnamespace', 'testrepo',

+      'https://pagure.io/api/0/fork/testfork/testnamespace/testrepo/'),

+ ]

+ 

+ 

+ @pytest.mark.parametrize("user, namespace, repo, expected",

+                          basic_url_data)

+ def test_create_basic_url(user, namespace, repo, expected):

+     """ Test creation of url in function of argument

+     passed to the Pagure class.

+     """

+     pg = Pagure(pagure_repository=repo,

+                 fork_username=user,

+                 namespace=namespace)

+     url = pg.create_basic_url()

+     assert url == expected

+ 

+ 

+ def test_api_version(mocker, simple_pg):

+     """ Test the call to the version API """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.api_version()

+     Pagure._call_api.assert_called_once_with('https://pagure.io/api/0/version')

+ 

+ 

+ def test_list_users(mocker, simple_pg):

+     """ Test the call to the users API """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.list_users(pattern='c')

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/users', params={'pattern': 'c'})

+ 

+ 

+ def test_list_tags(mocker, simple_pg):

+     """ Test the call to the tags API """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.list_tags(pattern='easy')

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/testrepo/tags', params={'pattern': 'easy'})

+ 

+ 

+ def test_list_groups(mocker, simple_pg):

+     """ Test the call to the groups API """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.list_groups()

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/groups', params=None)

+ 

+ 

+ def test_error_codes(mocker, simple_pg):

+     """ Test the call to the error codes API """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.error_codes()

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/error_codes')

+ 

+ 

+ pr_data = [

+     ('teststatus', 'testassignee', 'testauthor',

+         {'status': 'teststatus', 'assignee': 'testassignee', 'author': 'testauthor'}),

+     (None, None, None, {})

+ ]

+ 

+ 

+ @pytest.mark.parametrize("status, assignee, author, expected", pr_data)

+ def test_list_requests(mocker, simple_pg, status, assignee, author, expected):

+     """ Test the API call to the pull-requests endpoint """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.list_requests(status, assignee, author)

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/testrepo/pull-requests', params=expected)

+ 

+ 

+ def test_request_info(mocker, simple_pg):

+     """ Test the API call to get pull-request info """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.request_info('123')

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/testrepo/pull-request/123')

+ 

+ 

+ def test_merge_request(mocker, simple_pg):

+     """ Test the API call to merge a pull-request """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.merge_request('123')

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/testrepo/pull-request/123/merge', method='POST')

+ 

+ 

+ def test_close_request(mocker, simple_pg):

+     """ Test the API call to close a pull-request """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.close_request('123')

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/testrepo/pull-request/123/close', method='POST')

+ 

+ 

+ comment_data = [

+     ("test body", None, None, None, {'comment': 'test body'}),

+     ("test body", "testcommit", "testfilename", "testrow",

+      {'comment': 'test body', 'commit': 'testcommit', 'filename': 'testfilename',

+       'row': 'testrow'})

+ ]

+ 

+ 

+ @pytest.mark.parametrize("body, commit, filename, row, expected", comment_data)

+ def test_comment_request(mocker, simple_pg, body, commit, filename, row, expected):

+     """ Test the API call to comment on a pull-request """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.comment_request('123', body, commit, filename, row)

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/testrepo/pull-request/123/comment', method='POST',

+         data=expected)

+ 

+ 

+ flag_data = [

+     ('testuser', 'testpercent', 'testcomment', 'testurl', None, None,

+      {'username': 'testuser', 'percent': 'testpercent', 'comment': 'testcomment',

+       'url': 'testurl'}),

+     ('testuser', 'testpercent', 'testcomment', 'testurl', 'testuid', 'testcommit',

+      {'username': 'testuser', 'percent': 'testpercent', 'comment': 'testcomment',

+       'url': 'testurl', 'uid': 'testuid', 'commit': 'testcommit'})

+ ]

+ 

+ 

+ @pytest.mark.parametrize("username, percent, comment, url, uid, commit, expected",

+                          flag_data)

+ def test_flag_request(mocker, simple_pg, username, percent, comment, url, uid,

+                       commit, expected):

+     """ Test the API call to flag a pull-request """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.flag_request('123', username, percent, comment, url, uid, commit)

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/testrepo/pull-request/123/flag', method='POST',

+         data=expected)

+ 

+ 

+ def test_create_issue(mocker, simple_pg):

+     """ Test the API call to create an issue """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.create_issue('A test issue', 'Some issue content', True)

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/testrepo/new_issue', method='POST',

+         data={'title': 'A test issue', 'issue_content': 'Some issue content',

+               'priority': True})

+ 

+ 

+ def test_list_issues(mocker, simple_pg):

+     """ Test the API call to list all issues of a project """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.list_issues('status', 'tags', 'assignee', 'author',

+                           'milestones', 'priority', 'no_stones', 'since')

+     expected = {'status': 'status', 'tags': 'tags', 'assignee': 'assignee',

+                 'author': 'author', 'milestones': 'milestones', 'priority': 'priority',

+                 'no_stones': 'no_stones', 'since': 'since'}

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/testrepo/issues', params=expected)

+ 

+ 

+ def test_issue_info(mocker, simple_pg):

+     """ Test the API call to info about a project issue """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.issue_info('123')

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/testrepo/issue/123')

+ 

+ 

+ def test_list_comment(mocker, simple_pg):

+     """ Test the API call to info about a project issue """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.get_list_comment('123', '001')

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/testrepo/issue/123/comment/001')

+ 

+ 

+ def test_change_issue_status(mocker, simple_pg):

+     """ Test the API call to change the status of a project issue """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.change_issue_status('123', 'Closed', 'wontfix')

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/testrepo/issue/123/status', method='POST',

+         data={'status': 'Closed', 'close_status': 'wontfix'})

+ 

+ 

+ def test_change_issue_milestone(mocker, simple_pg):

+     """ Test the API call to change the milestone of a project issue """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.change_issue_milestone('123', 'Tomorrow')

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/testrepo/issue/123/milestone', method='POST',

+         data={'milestone': 'Tomorrow'})

+ 

+ 

+ def test_comment_issue(mocker, simple_pg):

+     """ Test the API call to change the milestone of a project issue """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.comment_issue('123', 'A comment')

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/testrepo/issue/123/comment', method='POST',

+         data={'comment': 'A comment'})

+ 

+ 

+ def test_project_tags(mocker, simple_pg):

+     """ Test the API call to get a project tags """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.project_tags()

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/testrepo/git/tags')

+ 

+ 

+ def test_list_projects(mocker, simple_pg):

+     """ Test the API call to list all projects on a pagure instance """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.list_projects('tags', 'pattern', 'username', 'owner',

+                             'namespace', 'fork', 'short', 1, 100)

+     expected = {'tags': 'tags', 'pattern': 'pattern', 'username': 'username',

+                 'owner': 'owner', 'namespace': 'namespace', 'fork': 'fork',

+                 'short': 'short', 'page': '1', 'per_page': '100'}

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/projects', params=expected)

+ 

+ 

+ def test_user_info(mocker, simple_pg):

+     """ Test the API call to get info about a user """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.user_info('auser')

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/user/auser')

+ 

+ 

+ def test_new_project(mocker, simple_pg):

+     """ Test the API call to list all projects on a pagure instance """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.new_project('name', 'description', 'namespace', 'url',

+                             'avatar_email', True, True)

+     expected = {'name': 'name', 'description': 'description', 'namespace': 'namespace',

+                 'url': 'url', 'avatar_email': 'avatar_email',

+                 'create_readme': True, 'private': True}

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/new', data=expected, method='POST')

+ 

+ 

+ def test_project_branches(mocker, simple_pg):

+     """ Test the API call to get info about a user """

+     mocker.patch('libpagure.Pagure._call_api')

+     simple_pg.project_branches()

+     Pagure._call_api.assert_called_once_with(

+         'https://pagure.io/api/0/testrepo/git/branches')

This adding a container environment to run unit test for python3.6 and python2.7 + basic unit test for all api call. Coverage for libpagure.py is now 94%

rebased onto 58aee65

6 years ago

Can we move this line above the command?

Same for this. Also s/python/Python!

rebased onto 4460a55

6 years ago

rebased onto aaa52ba

6 years ago

@sayanchowdhury I have made the requested changes. Do you see anything else that need to be changed ?

+1 ?

rebased onto e6cdcbc

6 years ago

I am not sure how to run these tests.
python3 setup.py test
or
pytest-3 tests
not working for me

I added below line
from pytest_mock import mocker

and now tests are executing with one error for

================================== FAILURES ===================================
____ test_create_issue ____

mocker = <pytest_mock.MockFixture object at 0x7fb881487a58>
simple_pg = <libpagure.libpagure.Pagure object at 0x7fb88145a588>

def test_create_issue(mocker, simple_pg):
    """ Test the API call to create an issue """
    mocker.patch('libpagure.Pagure._call_api')
    simple_pg.create_issue('A test issue', 'Some issue content', True)
    Pagure._call_api.assert_called_once_with(
        'https://pagure.io/api/0/testrepo/new_issue', method='POST',
        data={'title': 'A test issue', 'issue_content': 'Some issue content',
            'private': True})

E AssertionError: Expected call: _call_api('https://pagure.io/api/0/testrepo/new_issue', data={'title': 'A test issue', 'issue_content': 'Some issue content', 'private': True}, method='POST')
E Actual call: _call_api('https://pagure.io/api/0/testrepo/new_issue', data={'title': 'A test issue', 'issue_content': 'Some issue content', 'priority': True}, method='POST')
E
E pytest introspection follows:
E
E Kwargs:
E assert {'data': {'is...thod': 'POST'} == {'data': {'iss...thod': 'POST'}
E Common items:
E {'method': 'POST'}
E Differing items:
E {'data': {'issue_content': 'Some issue content', 'priority': True, 'title': 'A test issue'}} != {'data': {'issue_content': 'Some issue content', 'private': True, 'title': 'A test issue'}}
E Full diff:
E {'data': {'issue_content': 'Some issue content',
E - 'priority': True,
E ? ^^^ ^
E + 'private': True,
E ? ^^ ^
E 'title': 'A test issue'},
E 'method': 'POST'}

tests/test_api.py:180: AssertionError
===================== 1 failed, 30 passed in 0.26 seconds =====================

I added below line
from pytest_mock import mocker

That is not needed, the pytest plugin takes care of importing the fixture. Did you run the test using the docker environment ?

and now tests are executing with one error for
================================== FAILURES ===================================
_ testcreate_issue __

Thanks for catching that, it is fixed now.

rebased onto 7878c7d

6 years ago

I added below line
from pytest_mock import mocker

That is not needed, the pytest plugin takes care of importing the fixture. Did you run the test using the docker environment ?

No but as a end user I don't know how this docker environment will help. Say I don't want to use docker and when we run test in spec file, how will they be run?

I am not sure how to run these tests.
python3 setup.py test
or
pytest-3 tests
not working for me

The readme is updated in this PR to describe how to run the test. If you followed this steps and it is still not working please let me know so that I can improve the readme.

rebased onto 2016e82

6 years ago

No but as a end user I don't know how this docker environment will help. Say I don't want to use docker and when we run test in spec file, how will they be run?

I have added a section to the readme on how to run the tests outside the container environment. Hope this helps

Thanks. I followed README steps and they worked fine for me. See results

platform linux -- Python 3.6.2, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
rootdir: /code, inifile:
plugins: mock-1.6.3, cov-2.5.1
collected 31 items                                                                                       

tests/test_api.py ...............................

----------- coverage: platform linux, python 3.6.2-final-0 -----------
Name                      Stmts   Miss  Cover
---------------------------------------------
libpagure/__init__.py         1      0   100%
libpagure/exceptions.py       2      0   100%
libpagure/libpagure.py      226     19    92%
---------------------------------------------
TOTAL                       229     19    92%

Nice, then I am going to merge this PR.

Thanks for the review

Pull-Request has been merged by cverna

6 years ago