#75 Add tests to mdapi
Merged 6 years ago by pingou. Opened 6 years ago by pingou.

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

  *~

  .coverage

  alembic.ini

+ .tox/

+ .pytest_cache/

file modified
+11 -6
@@ -427,12 +427,7 @@ 

          charset='utf-8')

  

  

- @asyncio.coroutine

- def init(loop):

-     logging.basicConfig()

-     logging.config.dictConfig(CONFIG.get('LOGGING') or {'version': 1})

- 

-     app = web.Application(loop=loop)

+ def _set_routes(app):

      routes = []

      prefix = CONFIG.get('PREFIX', '')

      if prefix:
@@ -459,6 +454,16 @@ 

      ])

      for route in routes:

          app.router.add_route('GET', prefix + route[0], route[1])

+     return app

+ 

+ 

+ @asyncio.coroutine

+ def init(loop):

+     logging.basicConfig()

+     logging.config.dictConfig(CONFIG.get('LOGGING') or {'version': 1})

+ 

+     app = web.Application(loop=loop)

+     app = _set_routes(app)

  

      srv = yield from loop.create_server(

          app.make_handler(),

file modified
+6 -6
@@ -155,14 +155,14 @@ 

      packages conflicting with mariadb in rawhide:

      <a href="$PREFIX/rawhide/conflicts/mariadb">rawhide/conflicts/mariadb</a>

  

-     packages enhancing vagrant in rawhide:

-     <a href="$PREFIX/rawhide/enhances/vagrant">rawhide/enhances/vagrant</a>

+     packages enhancing httpd in rawhide:

+     <a href="$PREFIX/rawhide/enhances/httpd">rawhide/enhances/httpd</a>

  

-     packages recommending python3-dateutils in rawhide:

-     <a href="$PREFIX/rawhide/recommends/python3-dateutils">rawhide/recommends/python3-dateutils</a>

+     packages recommending flac in rawhide:

+     <a href="$PREFIX/rawhide/recommends/flac">rawhide/recommends/flac</a>

  

-     packages suggesting tag in rawhide:

-     <a href="$PREFIX/rawhide/suggests/tar">rawhide/suggests/tar</a>

+     packages suggesting R-tools in rawhide:

+     <a href="$PREFIX/rawhide/suggests/R-tools">rawhide/suggests/R-tools</a>

  

      packages supplementing `(hunspell and langpacks-fr)` in rawhide:

      <a href="$PREFIX/rawhide/supplements/(hunspell and langpacks-fr)">rawhide/supplements/(hunspell and langpacks-fr)</a>

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

+ #!/usr/bin/env python3

+ # -*- coding: utf-8 -*-

@@ -0,0 +1,159 @@ 

+ #!/usr/bin/env python3

+ # -*- coding: utf-8 -*-

+ 

+ #

+ # Copyright © 2019  Red Hat, Inc.

+ #

+ # This copyrighted material is made available to anyone wishing to use,

+ # modify, copy, or redistribute it subject to the terms and conditions

+ # of the GNU General Public License v.2, or (at your option) any later

+ # version.  This program is distributed in the hope that it will be

+ # useful, but WITHOUT ANY WARRANTY expressed or implied, including the

+ # implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR

+ # PURPOSE.  See the GNU General Public License for more details.  You

+ # should have received a copy of the GNU General Public License along

+ # with this program; if not, write to the Free Software Foundation,

+ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

+ #

+ # Any Red Hat trademarks that are incorporated in the source

+ # code or documentation are not subject to the GNU General Public

+ # License and may only be used or replicated with the express permission

+ # of Red Hat, Inc.

+ #

+ 

+ '''

+ Tests for mdapi.

+ 

+ '''

+ import json

+ import os

+ import shutil

+ import subprocess

+ import sys

+ import tempfile

+ 

+ import mock

+ import pytest

+ from aiohttp import web

+ from sqlalchemy.exc import SQLAlchemyError

+ 

+ sys.path.insert(0, os.path.join(os.path.dirname(

+     os.path.abspath(__file__)), '..'))

+ 

+ import mdapi

+ 

+ HERE = os.path.join(os.path.dirname(os.path.abspath(__file__)))

+ 

+ TMPDIR = None

+ 

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

+ def set_env(request):

+     """

+     Collects the sqlite database from the mirror to have some data to test

+     against.

+     """

+     global TMPDIR

+     TMPDIR = tempfile.mkdtemp(prefix="mdapi-test-")

+     print("Creating %s" % TMPDIR)

+     configfile = os.path.join(TMPDIR, "config")

+ 

+     with open(configfile, "w") as stream:

+         stream.write("DB_FOLDER = '%s'\n" % TMPDIR)

+ 

+     print("Downloading the databases...")

+     subprocess.check_output(

+         ["../mdapi-get_repo_md", configfile],

+         cwd=HERE,

+     )

+     assert len(os.listdir(TMPDIR)) > 2

+ 

+     def clean_up():

+         print("\nRemoving %s" % TMPDIR)

+         shutil.rmtree(TMPDIR)

+     request.addfinalizer(clean_up)

+ 

+ 

+ @pytest.fixture

+ def tmpdir():

+     return TMPDIR

+ 

+ 

+ @pytest.fixture

+ def cli(set_env, tmpdir, loop, aiohttp_client):

+     mdapi.CONFIG['DB_FOLDER'] = tmpdir

+     app = web.Application()

+     app = mdapi._set_routes(app)

+     return loop.run_until_complete(aiohttp_client(app))

+ 

+ 

+ async def test_view_index_page(cli):

+     resp = await cli.get('/')

+     assert resp.status == 200

+     header = r"""

+                _             _

+               | |           (_)

+  _ __ ___   __| | __ _ _ __  _

+ | '_ ` _ \ / _` |/ _` | '_ \| |

+ | | | | | | (_| | (_| | |_) | |

+ |_| |_| |_|\__,_|\__,_| .__/|_|

+                       | |

+                       |_|

+ """

+     output = await resp.text()

+     assert header in output

+ 

+ 

+ async def test_view_branches(cli):

+     resp = await cli.get('/branches')

+     assert resp.status == 200

+     output = await resp.text()

+     assert 'src_rawhide' in output

+     assert 'rawhide' in output

+ 

+ 

+ async def test_view_pkg_rawhide(cli):

+     resp = await cli.get('/rawhide/pkg/kernel')

+     assert resp.status == 200

+     json.loads(await resp.text())

+ 

+ 

+ async def test_view_pkg_rawhide_invalid(cli):

+     resp = await cli.get('/rawhide/pkg/invalidpackagename')

+     assert resp.status == 404

+     assert '404: Not Found' == await resp.text()

+ 

+ 

+ async def test_view_srcpkg_rawhide(cli):

+     resp = await cli.get('/rawhide/srcpkg/python-natsort')

+     assert resp.status == 200

+     json.loads(await resp.text())

+ 

+ 

+ async def test_view_file_list_rawhide(cli):

+     resp = await cli.get('/rawhide/files/kernel-core')

+     assert resp.status == 200

+     json.loads(await resp.text())

+ 

+ 

+ async def test_view_changelog_rawhide(cli):

+     resp = await cli.get('/rawhide/changelog/kernel')

+     assert resp.status == 200

+     json.loads(await resp.text())

+ 

+ 

+ @pytest.mark.parametrize("action, package, status_code", [

+     ("requires", "R", 200),

+     ("provides", "perl(SetupLog)", 200),

+     ("provides", "R", 200),

+     ("obsoletes", "cabal2spec", 200),

+     ("conflicts", "mariadb", 200),

+     ("enhances", "httpd", 200),

+     ("recommends", "flac", 200),

+     ("suggests", "R-tools", 200),

+     ("supplements", "(hunspell and langpacks-fr)", 200),

+ ])

+ async def test_view_property_koji(cli, action, package, status_code):

+     resp = await cli.get('/koji/%s/%s' % (action, package))

+     assert resp.status == status_code

+     if status_code == 200:

+         json.loads(await resp.text())

@@ -0,0 +1,114 @@ 

+ #!/usr/bin/env python3

+ # -*- coding: utf-8 -*-

+ 

+ #

+ # Copyright © 2019  Red Hat, Inc.

+ #

+ # This copyrighted material is made available to anyone wishing to use,

+ # modify, copy, or redistribute it subject to the terms and conditions

+ # of the GNU General Public License v.2, or (at your option) any later

+ # version.  This program is distributed in the hope that it will be

+ # useful, but WITHOUT ANY WARRANTY expressed or implied, including the

+ # implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR

+ # PURPOSE.  See the GNU General Public License for more details.  You

+ # should have received a copy of the GNU General Public License along

+ # with this program; if not, write to the Free Software Foundation,

+ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

+ #

+ # Any Red Hat trademarks that are incorporated in the source

+ # code or documentation are not subject to the GNU General Public

+ # License and may only be used or replicated with the express permission

+ # of Red Hat, Inc.

+ #

+ 

+ '''

+ Tests for mdapi.

+ 

+ '''

+ import os

+ import shutil

+ import subprocess

+ import sys

+ import tempfile

+ 

+ import pytest

+ from aiohttp import web

+ 

+ sys.path.insert(0, os.path.join(os.path.dirname(

+     os.path.abspath(__file__)), '..'))

+ 

+ import mdapi

+ import mdapi.lib

+ 

+ HERE = os.path.join(os.path.dirname(os.path.abspath(__file__)))

+ 

+ 

+ @pytest.fixture

+ def cli(loop, aiohttp_client):

+     mdapi.CONFIG['DB_FOLDER'] = '.'

+     app = web.Application()

+     app = mdapi._set_routes(app)

+     return loop.run_until_complete(aiohttp_client(app))

+ 

+ 

+ async def test_view_index_page(cli):

+     resp = await cli.get('/')

+     assert resp.status == 200

+     header = r"""

+                _             _

+               | |           (_)

+  _ __ ___   __| | __ _ _ __  _

+ | '_ ` _ \ / _` |/ _` | '_ \| |

+ | | | | | | (_| | (_| | |_) | |

+ |_| |_| |_|\__,_|\__,_| .__/|_|

+                       | |

+                       |_|

+ """

+     output = await resp.text()

+     assert header in output

+ 

+ 

+ async def test_view_branches(cli):

+     resp = await cli.get('/branches')

+     assert resp.status == 200

+     assert '[]' == await resp.text()

+ 

+ 

+ async def test_view_pkg_rawhide(cli):

+     resp = await cli.get('/rawhide/pkg/kernel')

+     assert resp.status == 400

+     assert '400: Bad Request' == await resp.text()

+ 

+ 

+ async def test_view_pkg_rawhide_invalid(cli):

+     resp = await cli.get('/rawhide/pkg/invalidpackagename')

+     assert resp.status == 400

+     assert '400: Bad Request' == await resp.text()

+ 

+ 

+ async def test_view_srcpkg_rawhide(cli):

+     resp = await cli.get('/rawhide/srcpkg/python-natsort')

+     assert resp.status == 400

+     assert '400: Bad Request' == await resp.text()

+ 

+ 

+ async def test_view_file_list_rawhide(cli):

+     resp = await cli.get('/rawhide/files/kernel-core')

+     assert resp.status == 400

+     assert '400: Bad Request' == await resp.text()

+ 

+ 

+ async def test_view_changelog_rawhide(cli):

+     resp = await cli.get('/rawhide/changelog/kernel')

+     assert resp.status == 400

+     assert '400: Bad Request' == await resp.text()

+ 

+ 

+ @pytest.mark.parametrize("action", [

+     "requires", "provides", "obsoletes", "conflicts",

+     "enhances", "recommends", "suggests", "supplements",

+ ])

+ async def test_view_property_koji(cli, action):

+     resp = await cli.get('/koji/%s/R' % action)

+     assert resp.status == 400

+     assert '400: Bad Request' == await resp.text()

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

+ [tox]

+ envlist = py3

+ skipsdist = True

+ 

+ [testenv]

+ usedevelop = True

+ passenv = HOME

+ deps =

+     -rrequirements.txt

+     pytest

+     pytest-cov

+     pytest-aiohttp

+     mock

+ setenv =

+     PYTHONPATH={toxinidir}

+ commands =

+     py.test -s -vv --cov=mdapi {posargs}

no initial comment

We might want to use assertIN here so that we tests only if rawhide is there for example. That way we are sure that the test will work.

rebased onto 46934f2

6 years ago

Pull-Request has been merged by pingou

6 years ago