Skip to content

Commit

Permalink
pylint anitya/tests/lib
Browse files Browse the repository at this point in the history
  • Loading branch information
lmilbaum authored and mergify[bot] committed Jun 23, 2023
1 parent 679051f commit 4d5680a
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 105 deletions.
11 changes: 7 additions & 4 deletions anitya/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _configure_db(db_uri="sqlite://"):
db_uri (str): The URI to use when creating the engine. This defaults
to an in-memory SQLite database.
"""
global engine
global engine # pylint: disable=W0603
engine = create_engine(db_uri)

if db_uri.startswith("sqlite://"):
Expand All @@ -93,7 +93,10 @@ def begin_event(conn):
@event.listens_for(Session, "after_transaction_end")
def restart_savepoint(session, transaction):
"""Allow tests to call rollback on the session."""
if transaction.nested and not transaction._parent.nested:
if (
transaction.nested
and not transaction._parent.nested # pylint: disable=W0212
):
session.expire_all()
session.begin_nested()

Expand Down Expand Up @@ -130,7 +133,7 @@ class DatabaseTestCase(AnityaTestCase):
"""

def setUp(self):
super(DatabaseTestCase, self).setUp()
super().setUp()

# We don't want our SQLAlchemy session thrown away post-request because that rolls
# back the transaction and no database assertions can be made.
Expand Down Expand Up @@ -162,7 +165,7 @@ def tearDown(self):
self.transaction.rollback()
self.connection.close()
Session.remove()
super(DatabaseTestCase, self).tearDown()
super().tearDown()


def create_distro(session):
Expand Down
4 changes: 4 additions & 0 deletions anitya/tests/db/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@


class SetEcosystemBackendTests(DatabaseTestCase):
"""SetEcosystemBackendTests"""

def test_set_backend(self):
"""Assert the ecosystem gets set to correct backend."""
project = models.Project(
Expand Down Expand Up @@ -81,6 +83,8 @@ def test_set_wrong_backend(self):


class SetEcosystemHomepageTests(DatabaseTestCase):
"""SetEcosystemHomepageTests"""

def test_set_backend(self):
"""Assert the ecosystem gets set to correct backend, even when homepage is changed."""
project = models.Project(
Expand Down
8 changes: 7 additions & 1 deletion anitya/tests/db/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# 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.
"""test meta"""
import unittest

import mock
Expand All @@ -26,9 +27,12 @@


class InitalizeTests(unittest.TestCase):
"""InitalizeTests"""

@mock.patch("anitya.db.meta.create_engine")
@mock.patch("anitya.db.meta.Session")
def test_initialize(self, mock_session, mock_create_engine):
"""test_initialize"""
config = {"DB_URL": "postgresql://postgres:pass@localhost/mydb"}
engine = meta.initialize(config)
mock_create_engine.assert_called_once_with(config["DB_URL"], echo=False)
Expand All @@ -39,6 +43,7 @@ def test_initialize(self, mock_session, mock_create_engine):
@mock.patch("anitya.db.meta.event.listen")
@mock.patch("anitya.db.meta.Session")
def test_initalize_sqlite(self, mock_session, mock_listen, mock_create_engine):
"""test_initalize_sqlite"""
config = {"DB_URL": "sqlite://", "SQL_DEBUG": True}
engine = meta.initialize(config)
mock_create_engine.assert_called_once_with(config["DB_URL"], echo=True)
Expand All @@ -52,7 +57,7 @@ class BaseQueryPaginateTests(DatabaseTestCase):
"""Tests for the BaseQuery queries."""

def setUp(self):
super(BaseQueryPaginateTests, self).setUp()
super().setUp()
self.query = meta.BaseQuery(models.Project, session=self.session)

def test_defaults(self):
Expand Down Expand Up @@ -114,6 +119,7 @@ def test_order_by(self):
self.assertEqual(page.items[2].name, "subsurface")

def test_as_dict(self):
"""test_as_dict"""
expected_dict = {
"items_per_page": 1,
"page": 1,
Expand Down
5 changes: 2 additions & 3 deletions anitya/tests/test_alembic.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ def test_alembic_history(self):
This test runs the `alembic history | grep ' (head), '` command,
and ensure it returns only one line.
"""
proc1 = subprocess.Popen(
proc1 = subprocess.Popen( # pylint: disable=R1732
["alembic", "history"], cwd=REPO_PATH, stdout=subprocess.PIPE
)
proc2 = subprocess.Popen(
proc2 = subprocess.Popen( # pylint: disable=R1732
["grep", " (head), "], stdin=proc1.stdout, stdout=subprocess.PIPE
)

stdout = proc2.communicate()[0]
stdout = stdout.strip().split(b"\n")
self.assertEqual(len(stdout), 1)
Expand Down
14 changes: 9 additions & 5 deletions anitya/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def setUp(self):
def test_default_config(self):
"""Assert when no configuration is provided, :data:`anitya.config.config` is used."""
application = app.create()
for key in anitya_config:
self.assertEqual(anitya_config[key], application.config[key])
for key, value in anitya_config.items():
self.assertEqual(value, application.config[key])

def test_email_config(self):
"""Assert a SMTPHandler is added to the anitya logger when ``EMAIL_ERRORS=True``."""
Expand Down Expand Up @@ -76,8 +76,10 @@ def test_db_config(self):


class IntegrityErrorHandlerTests(base.DatabaseTestCase):
"""IntegrityErrorHandlerTests"""

def setUp(self):
super(IntegrityErrorHandlerTests, self).setUp()
super().setUp()
session = Session()
self.user = models.User(email="[email protected]", username="user")
session.add(self.user)
Expand Down Expand Up @@ -129,8 +131,10 @@ def test_no_social_auth(self):


class AuthExceptionHandlerTests(base.DatabaseTestCase):
def setUp(self):
super(AuthExceptionHandlerTests, self).setUp()
"""AuthExceptionHandlerTests"""

def setUp(self): # pylint: disable=W0246
super().setUp()

def test_exception_handling(self):
"""Assert an AuthException results in a 400 error"""
Expand Down
6 changes: 3 additions & 3 deletions anitya/tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LoadUserFromRequestTests(DatabaseTestCase):
"""Tests for the :class:`anitya.authentication.SessionInterface`` class."""

def setUp(self):
super(LoadUserFromRequestTests, self).setUp()
super().setUp()
self.app = self.flask_app.test_client()
session = Session()
self.user = models.User(email="[email protected]", username="user")
Expand Down Expand Up @@ -78,7 +78,7 @@ class LoadUserFromSessionTests(DatabaseTestCase):
"""Tests for the :func:`anitya.authentication.load_user_from_session`` functions."""

def setUp(self):
super(LoadUserFromSessionTests, self).setUp()
super().setUp()

session = Session()
self.user = models.User(email="[email protected]", username="user")
Expand Down Expand Up @@ -110,7 +110,7 @@ class RequireTokenTests(DatabaseTestCase):
"""Tests for the :func:`anitya.authentication.require_token` decorator."""

def setUp(self):
super(RequireTokenTests, self).setUp()
super().setUp()
self.app = self.flask_app.test_client()
session = Session()
self.user = models.User(email="[email protected]", username="user")
Expand Down
25 changes: 15 additions & 10 deletions anitya/tests/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ def test_session_removed_post_request(self):


class SettingsTests(DatabaseTestCase):
"""SettingsTests"""

def setUp(self):
"""Set up the Flask testing environnment"""
super(SettingsTests, self).setUp()
super().setUp()
self.app = self.flask_app.test_client()
self.user = models.User(email="[email protected]", username="user")
user_social_auth = social_models.UserSocialAuth(
Expand Down Expand Up @@ -124,7 +126,7 @@ def test_delete_token(self):
data = {"csrf_token": csrf_token}

output = c.post(
"/settings/tokens/delete/{}/".format(token.token),
f"/settings/tokens/delete/{token.token}/",
data=data,
follow_redirects=True,
)
Expand Down Expand Up @@ -165,7 +167,7 @@ def test_delete_token_invalid_csrf(self):
data = {"csrf_token": "not a valid token"}

output = c.post(
"/settings/tokens/delete/{}/".format(token.token),
f"/settings/tokens/delete/{token.token}/",
data=data,
follow_redirects=True,
)
Expand All @@ -181,7 +183,7 @@ class NewProjectTests(DatabaseTestCase):

def setUp(self):
"""Set up the Flask testing environnment"""
super(NewProjectTests, self).setUp()
super().setUp()
create_distro(self.session)
self.app = self.flask_app.test_client()
self.user = models.User(email="[email protected]", username="user")
Expand Down Expand Up @@ -321,6 +323,7 @@ def test_new_project_invalid_homepage(self):

@mock.patch("anitya.lib.utilities.check_project_release")
def test_new_project_with_check_release(self, patched):
"""test_new_project_with_check_release"""
output = self.app.get("/project/new", follow_redirects=True)
with login_user(self.flask_app, self.user):
with self.flask_app.test_client() as c:
Expand Down Expand Up @@ -441,7 +444,7 @@ class FlaskTest(DatabaseTestCase):

def setUp(self):
"""Set up the environnment, ran before every tests."""
super(FlaskTest, self).setUp()
super().setUp()

self.flask_app.config["TESTING"] = True
self.app = self.flask_app.test_client()
Expand Down Expand Up @@ -830,9 +833,11 @@ def test_projects_updated_incorrect_status(self):


class EditProjectTests(DatabaseTestCase):
"""EditProjectTests"""

def setUp(self):
"""Set up the Flask testing environnment"""
super(EditProjectTests, self).setUp()
super().setUp()
self.app = self.flask_app.test_client()
# Make a user to login with
self.user = models.User(email="[email protected]", username="user")
Expand Down Expand Up @@ -1012,7 +1017,7 @@ class MapProjectTests(DatabaseTestCase):
"""Tests for the :func:`anitya.ui.map_project` view."""

def setUp(self):
super(MapProjectTests, self).setUp()
super().setUp()
create_distro(self.session)
create_project(self.session)
self.client = self.flask_app.test_client()
Expand Down Expand Up @@ -1120,7 +1125,7 @@ class EditProjectMappingTests(DatabaseTestCase):
"""Tests for the :func:`anitya.ui.edit_project_mapping` view."""

def setUp(self):
super(EditProjectMappingTests, self).setUp()
super().setUp()

# Set up a mapping to edit
session = Session()
Expand Down Expand Up @@ -1283,7 +1288,7 @@ class AddDistroTests(DatabaseTestCase):
"""Tests for the :func:`anitya.admin.add_distro` view function."""

def setUp(self):
super(AddDistroTests, self).setUp()
super().setUp()

# Add a regular user and an admin user
session = Session()
Expand Down Expand Up @@ -1365,7 +1370,7 @@ class FlagProjecTests(DatabaseTestCase):
"""Tests for the :func:`anitya.ui.flag_project` view."""

def setUp(self):
super(FlagProjecTests, self).setUp()
super().setUp()

create_project(self.session)
self.user = models.User(email="[email protected]", username="user")
Expand Down
Loading

0 comments on commit 4d5680a

Please sign in to comment.