Skip to content

Commit

Permalink
fix sqlalchemy and pytest warnings (#502)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv authored Mar 14, 2022
1 parent 94a60fd commit 5f2832c
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 26 deletions.
4 changes: 2 additions & 2 deletions plugins/quetz_harvester/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from quetz.db_models import ApiKey, Profile, User
from quetz.jobs.runner import Supervisor
from quetz.rest_models import Channel, Package
from quetz.testing.mockups import TestWorker
from quetz.testing.mockups import MockWorker

pytest_plugins = "quetz.testing.fixtures"

Expand Down Expand Up @@ -51,7 +51,7 @@ def auth_client(client, user):

@pytest.fixture
def supervisor(config, db, dao):
manager = TestWorker(config, db, dao)
manager = MockWorker(config, db, dao)
supervisor = Supervisor(db, manager)
return supervisor

Expand Down
2 changes: 1 addition & 1 deletion quetz/authentication/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def register(self, auth: BaseAuthenticator):
)

if len(self.enabled_authenticators) > 1:
logger.warn(
logger.warning(
"You have registered multiple authentication providers."
"Please note that this is currently discouraged in production setups!"
)
Expand Down
3 changes: 3 additions & 0 deletions quetz/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class Upsert(Insert):
incr: increment
"""

inherit_cache = False

def __init__(self, table, values, index_elements, column, incr=1):
self.values = values
self.index_elements = index_elements
Expand Down Expand Up @@ -802,6 +804,7 @@ def create_version(
.with_for_update()
.filter(Package.channel_name == channel_name)
.filter(Package.name == package_name)
.join(PackageVersion)
.filter(PackageVersion.package_format == package_format)
.filter(PackageVersion.platform == platform)
).first()
Expand Down
21 changes: 12 additions & 9 deletions quetz/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ class ChannelMember(Base):
user_id = Column(UUID, ForeignKey('users.id'), primary_key=True, index=True)
role = Column(String)

channel = relationship(
'Channel', backref=backref("channel_members", cascade="all,delete-orphan")
)
channel = relationship('Channel', back_populates="members")

user = relationship(
'User', backref=backref("channel_members", cascade="all,delete-orphan")
)
Expand Down Expand Up @@ -210,9 +209,9 @@ class Channel(Base):
mirrors = relationship("ChannelMirror", cascade="all, delete", uselist=True)

members_count = column_property(
select([func.count(ChannelMember.user_id)]).where(
ChannelMember.channel_name == name
),
select([func.count(ChannelMember.user_id)])
.where(ChannelMember.channel_name == name)
.scalar_subquery(), # type: ignore
deferred=True,
)

Expand All @@ -224,7 +223,9 @@ def load_channel_metadata(self):
return {}

packages_count = column_property(
select([func.count(Package.name)]).where(Package.channel_name == name),
select([func.count(Package.name)])
.where(Package.channel_name == name)
.scalar_subquery(), # type: ignore
deferred=True,
)

Expand Down Expand Up @@ -263,9 +264,11 @@ class PackageMember(Base):
'Package',
backref=backref("members", cascade="all,delete"),
primaryjoin="and_(Package.name == foreign(PackageMember.package_name),"
"Package.channel_name == Channel.name)",
"Package.channel_name == PackageMember.channel_name)",
)
channel = relationship(
'Channel', backref=backref("package_members", cascade="all,delete")
)
channel = relationship('Channel')
user = relationship('User', backref=backref("packages", cascade="all,delete"))

def __repr__(self):
Expand Down
6 changes: 4 additions & 2 deletions quetz/jobs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ class Job(Base):
default=JobStatus.pending,
)

tasks = sa.orm.relationship('Task', cascade="all,delete-orphan", uselist=True)
tasks = sa.orm.relationship(
'Task', back_populates='job', cascade="all,delete-orphan", uselist=True
)

extra_args = sa.Column(sa.String(), nullable=True)

Expand All @@ -83,7 +85,7 @@ class Task(Base):
job_id = sa.Column(
sa.Integer, sa.ForeignKey("jobs.id", ondelete="cascade"), nullable=False
)
job = sa.orm.relationship("Job")
job = sa.orm.relationship("Job", back_populates='tasks')
package_version_id = sa.Column(
UUID, sa.ForeignKey("package_versions.id", ondelete="cascade"), nullable=True
)
Expand Down
2 changes: 2 additions & 0 deletions quetz/jobs/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@


class any_true(FunctionElement):
inherit_cache = True
name = "anytrue"
type = Boolean()

Expand All @@ -37,6 +38,7 @@ def pg_any(element, compiler, **kw):


class all_true(FunctionElement):
inherit_cache = True
name = "alltrue"
type = Boolean()

Expand Down
2 changes: 1 addition & 1 deletion quetz/tasks/workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def done(self):

async def wait(self, waittime=0.1):
while not self.done:
asyncio.sleep(waittime)
await asyncio.sleep(waittime)
if self.status == "failed":
raise self._future.exception()

Expand Down
2 changes: 1 addition & 1 deletion quetz/testing/mockups.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from quetz.tasks.workers import job_wrapper


class TestWorker:
class MockWorker:
"synchronous worker for testing"

def __init__(
Expand Down
4 changes: 2 additions & 2 deletions quetz/tests/api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from quetz.config import Config
from quetz.dao import Dao
from quetz.db_models import Identity, Profile, User
from quetz.db_models import Identity, PackageVersion, Profile, User
from quetz.rest_models import Channel, Package


Expand Down Expand Up @@ -128,7 +128,7 @@ def _make_package_version(filename, version_number, platform="linux-64"):
yield _make_package_version

for version in versions:
db.delete(version)
db.query(PackageVersion).filter(PackageVersion.id == version.id).delete()
db.commit()


Expand Down
4 changes: 2 additions & 2 deletions quetz/tests/api/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from quetz.config import Config
from quetz.jobs.models import Job
from quetz.jobs.runner import Supervisor
from quetz.testing.mockups import TestWorker
from quetz.testing.mockups import MockWorker


@pytest.fixture
Expand Down Expand Up @@ -135,7 +135,7 @@ def test_permissions_channel_endpoints(
@pytest.fixture
def sync_supervisor(db, dao, config):
"supervisor with synchronous test worker"
manager = TestWorker(config, db, dao)
manager = MockWorker(config, db, dao)
supervisor = Supervisor(db, manager)
return supervisor

Expand Down
7 changes: 6 additions & 1 deletion quetz/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ def user(db, user_without_profile):

yield user_without_profile

db.delete(profile)
db.query(Profile).filter(
Profile.name == profile.name,
Profile.avatar_url == profile.avatar_url,
Profile.user_id == user_without_profile.id,
).delete()

db.commit()


Expand Down
4 changes: 2 additions & 2 deletions quetz/tests/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from quetz.jobs.runner import Supervisor, mk_sql_expr, parse_conda_spec
from quetz.rest_models import Channel, Package
from quetz.tasks.workers import SubprocessWorker
from quetz.testing.mockups import TestWorker
from quetz.testing.mockups import MockWorker

pytest_plugins = ("pytest_asyncio",)

Expand Down Expand Up @@ -891,7 +891,7 @@ def package_version_job(db, user, package_version):
@pytest.fixture
def sync_supervisor(db, dao, config):
"supervisor with synchronous test worker"
manager = TestWorker(config, db, dao)
manager = MockWorker(config, db, dao)
supervisor = Supervisor(db, manager)
return supervisor

Expand Down
4 changes: 2 additions & 2 deletions quetz/tests/test_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
handle_repodata_package,
initial_sync_mirror,
)
from quetz.testing.mockups import TestWorker
from quetz.testing.mockups import MockWorker


@pytest.fixture
def job_supervisor(db, config, dao, dummy_remote_session_object):
manager = TestWorker(config, db, dao, dummy_remote_session_object)
manager = MockWorker(config, db, dao, dummy_remote_session_object)
supervisor = Supervisor(db, manager)
return supervisor

Expand Down
3 changes: 2 additions & 1 deletion quetz/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ def apply_custom_query(search_type, db, keywords, filters):
f'%{each_keyword}%'
)
keyword_conditions.append(each_keyword_condition)
query = db.filter(and_(*keyword_conditions))

query = db.filter(and_(True, *keyword_conditions))

for each_filter in filters:
key, values = each_filter
Expand Down

0 comments on commit 5f2832c

Please sign in to comment.