Skip to content

Commit

Permalink
frontend: stop using deprecated Query.get method
Browse files Browse the repository at this point in the history
Fix #3612
  • Loading branch information
FrostyX committed Feb 11, 2025
1 parent cacc583 commit b81778f
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 47 deletions.
3 changes: 2 additions & 1 deletion frontend/coprs_frontend/coprs/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
except ImportError:
from flask_wtf import Form as FlaskForm

from coprs import db
from coprs import app
from coprs import exceptions
from coprs import helpers
Expand Down Expand Up @@ -1387,7 +1388,7 @@ def _validate_batch_opts(form, field):
return

build_id = int(build_id)
build = models.Build.query.get(build_id)
build = db.session.get(models.Build, build_id)
if not build:
raise wtforms.ValidationError(
"Build {} not found".format(build_id))
Expand Down
2 changes: 1 addition & 1 deletion frontend/coprs_frontend/coprs/logic/batches_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def get_batch_or_create(cls, build_id, requestor, modify=False):
# We don't want to create a new batch if one already exists, but there's
# the concurrency problem so we need to lock the build instance for
# writing.
build = db.session.query(Build).get(build_id)
build = db.session.get(Build, build_id)
if not build:
raise BadRequest("Build {} doesn't exist".format(build_id))

Expand Down
10 changes: 5 additions & 5 deletions frontend/coprs_frontend/tests/request_test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from bs4 import BeautifulSoup
from werkzeug.datastructures import FileStorage

from coprs import models
from coprs import models, db


# pylint: disable=consider-using-with
Expand Down Expand Up @@ -191,7 +191,7 @@ def _submit_url_build(self, project, urls, build_options):

def rebuild_all_packages(self, project_id, package_names=None):
""" There's a button "rebuild-all" in web-UI, hit that button """
copr = models.Copr.query.get(project_id)
copr = db.session.get(models.Copr, project_id)
if not package_names:
packages = copr.packages
package_names = [p.name for p in packages]
Expand All @@ -207,7 +207,7 @@ def rebuild_all_packages(self, project_id, package_names=None):
return resp

def resubmit_build_id(self, build_id):
build = models.Build.query.get(build_id)
build = db.session.get(models.Build, build_id)
path = f"/coprs/{build.copr.full_name}/new_build_rebuild/{build_id}"
response = self.client.post(
path,
Expand Down Expand Up @@ -439,7 +439,7 @@ def finish_srpm_and_import(self, build_id, package_name=None, pkg_version="1"):
Given the build_id, finish the source build, import it, and move the
corresponding BuildChroot instances to /pending-jobs/.
"""
build = models.Build.query.get(build_id)
build = db.session.get(models.Build, build_id)
if not package_name:
package_name = build.package.name

Expand All @@ -456,7 +456,7 @@ def finish_srpm_and_import(self, build_id, package_name=None, pkg_version="1"):
}).status_code == 200

# import srpm to appropriate branches
build = models.Build.query.get(build_id)
build = db.session.get(models.Build, build_id)
branch_commits = {}
for bch in build.build_chroots:
branch_commits[bch.mock_chroot.distgit_branch.name] = "4dc328232"
Expand Down
4 changes: 2 additions & 2 deletions frontend/coprs_frontend/tests/test_anitya.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ def test_pypi(self):
# Submit another build (not finished). This one though has the
# pypi_package_version field specified (could be submitted by anitya).
resp = self.api3.rebuild_package("inprogress-match", "python-umap-pytorch")
build = models.Build.query.get(int(resp.json["id"]))
build = self.db.session.get(models.Build, int(resp.json["id"]))
build.source_json = json.dumps({"pypi_package_version": "0.0.5"})
db.session.commit()

# Submit yet another build (not finished). This one though has the
# pypi_package_version field specified, and is older so we want to
# re-build).
resp = self.api3.rebuild_package("inprogress-older", "python-umap-pytorch")
build = models.Build.query.get(int(resp.json["id"]))
build = self.db.session.get(models.Build, int(resp.json["id"]))
build.source_json = json.dumps({"pypi_package_version": "0.0.4"})
db.session.commit()

Expand Down
6 changes: 3 additions & 3 deletions frontend/coprs_frontend/tests/test_apiv3/test_copr_chroot.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_v3_edit_chroot_reset(self):
)

# Make sure all the chroot attributes are configured
chroot = self.models.CoprChroot.query.get(1)
chroot = self.db.session.get(self.models.CoprChroot, 1)
assert chroot.isolation == "nspawn"
assert chroot.buildroot_pkgs == "pkg1 pkg2 pkg3"

Expand All @@ -82,7 +82,7 @@ def test_v3_edit_chroot_reset(self):
reset_fields=["additional_packages"]
)
assert response.status_code == 200
chroot = self.models.CoprChroot.query.get(1)
chroot = self.db.session.get(self.models.CoprChroot, 1)
assert chroot.isolation == "nspawn"
assert chroot.buildroot_pkgs is None

Expand All @@ -92,7 +92,7 @@ def test_v3_edit_chroot_reset(self):
chrootname,
reset_fields=["additional_packages", "isolation"]
)
chroot = self.models.CoprChroot.query.get(1)
chroot = self.db.session.get(self.models.CoprChroot, 1)
assert chroot.isolation == "unchanged"
assert chroot.buildroot_pkgs is None

Expand Down
4 changes: 2 additions & 2 deletions frontend/coprs_frontend/tests/test_apiv3/test_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_v3_packages(self, chroots, case):
"package_name": form_data["package_name"],
}
self.post_api3_with_auth(endpoint, rebuild_data, user)
build = self.models.Build.query.get(1)
build = self.db.session.get(self.models.Build, 1)
assert json.loads(build.source_json) == expected_source_dict
assert build.source_type == BuildSourceEnum(source_type_text)

Expand All @@ -75,7 +75,7 @@ def _assert_default_chroots(test_build):
_assert_default_chroots(build)
rebuild_data["chroots"] = chroots
self.post_api3_with_auth(endpoint, rebuild_data, user)
build = self.models.Build.query.get(2)
build = self.db.session.get(self.models.Build, 2)
assert json.loads(build.source_json) == expected_source_dict
if "fedora-18-x86_64" in chroots or chroots == []:
_assert_default_chroots(build)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_edit_project_chroot_api(self):
response = self.api3.post(route, data)
assert response.status_code == 200

chroot = models.CoprChroot.query.get(chroot.id)
chroot = self.db.session.get(self.models.CoprChroot, chroot.id)
assert chroot.isolation == "nspawn"
assert chroot.bootstrap == "off"
assert chroot.buildroot_pkgs == "foo bar"
Expand Down
9 changes: 5 additions & 4 deletions frontend/coprs_frontend/tests/test_apiv3/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def test_isolation_config(self, store, read):
assert Copr.query.one().isolation == read

def _get_copr_id_data(self, copr_id):
data = copy.deepcopy(self.models.Copr.query.get(copr_id).__dict__)
copr = self.db.session.get(self.models.Copr, copr_id)
data = copy.deepcopy(copr.__dict__)
data.pop("_sa_instance_state")
data.pop("latest_indexed_data_update")
return data
Expand Down Expand Up @@ -186,16 +187,16 @@ def test_fedora_review_setting(self):
project without specifying it.
"""
self.db.session.add(self.c1)
copr = Copr.query.get(self.c1.id)
copr = self.db.session.get(Copr, self.c1.id)
assert not copr.fedora_review
assert not copr.delete_after_days

route = "/api_3/project/edit/{}".format(self.c1.full_name)
self.api3.post(route, {"fedora_review": True})
assert Copr.query.get(self.c1.id).fedora_review
assert self.db.session.get(Copr, self.c1.id).fedora_review

self.api3.post(route, {"delete_after_days": 5})
copr = Copr.query.get(self.c1.id)
copr = self.db.session.get(Copr, self.c1.id)
assert copr.fedora_review
assert copr.delete_after_days == 5

Expand Down
2 changes: 1 addition & 1 deletion frontend/coprs_frontend/tests/test_comps.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_edit_project_chroot_comps(self, request_type):
bootstrap_image="image",
)

chroot = models.CoprChroot.query.get(chroot_id)
chroot = self.db.session.get(models.CoprChroot, chroot_id)
data = "<some><xml></xml></some>\n"
assert chroot.comps == data
assert chroot.bootstrap_image == "image"
Expand Down
6 changes: 3 additions & 3 deletions frontend/coprs_frontend/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ def test_orm_object_clone(self):
in Copr database.
"""
# "fedora-17-x86_64" from "user2/foocopr"
original = self.models.CoprChroot.query.get(2)
original = self.db.session.get(self.models.CoprChroot, 2)
# "user1/foocopr"
target_copr = self.models.Copr.query.get(1)
target_copr = self.db.session.get(self.models.Copr, 1)

assert "fedora-17-x86_64" not in \
[m.name for m in target_copr.mock_chroots]
Expand All @@ -175,7 +175,7 @@ def test_orm_object_clone(self):
self.db.session.add(copy)
self.db.session.commit()

target_copr = self.models.Copr.query.get(1)
target_copr = self.db.session.get(self.models.Copr, 1)
assert "fedora-17-x86_64" in \
[m.name for m in target_copr.mock_chroots]

Expand Down
11 changes: 6 additions & 5 deletions frontend/coprs_frontend/tests/test_logic/test_batch_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_normal_batch_operation_failures(self):
assert "Not a valid integer" in error

# drop the finished build from batch
build = models.Build.query.get(1)
build = self.db.session.get(models.Build, 1)
build.batch = None
self.db.session.commit()
error = self._submit({"with_build_id": 1})
Expand All @@ -117,7 +117,8 @@ def test_less_likely_batch_problems(self):
# existing build
BatchesLogic.get_batch_or_create(2, self.transaction_user)
# permission problem
user = models.User.query.get(2)
user = self.db.session.get(models.User, 2)

with pytest.raises(BadRequest) as error:
BatchesLogic.get_batch_or_create(2, user, modify=True)
assert "The batch 2 belongs to project user1/test" in str(error)
Expand All @@ -126,11 +127,11 @@ def test_less_likely_batch_problems(self):
def test_cant_group_others_build(self):
self._prepare_project_with_batches()
# de-assign the build from batch
build = models.Build.query.get(1)
build = self.db.session.get(models.Build, 1)
build.batch = None
self.db.session.commit()

user = models.User.query.get(2)
user = self.db.session.get(models.User, 2)
with pytest.raises(BadRequest) as error:
BatchesLogic.get_batch_or_create(1, user, modify=True)
assert "Build 1 is not yet in any batch" in str(error)
Expand Down Expand Up @@ -217,7 +218,7 @@ def _add_one_large_batch(self, projectname, builds=1000, after_build=None):

self.db.session.commit()

build = models.Build.query.get(batch_build_id)
build = self.db.session.get(models.Build, batch_build_id)
batch = build.batch
mock_chroot = build.build_chroots[0].mock_chroot

Expand Down
27 changes: 13 additions & 14 deletions frontend/coprs_frontend/tests/test_logic/test_builds_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,13 @@ def test_build_queue_6(self, f_users, f_coprs, f_mock_chroots, f_db):
data = BuildsLogic.get_pending_build_tasks().all()
assert len(data) == 0

@staticmethod
@pytest.mark.usefixtures("f_users", "f_coprs", "f_mock_chroots", "f_builds",
"f_db")
def test_build_queue_7():
def test_build_queue_7(self):
assert len(BuildsLogic.get_pending_srpm_build_tasks().all()) == 0
models.Build.query.get(1).source_status = StatusEnum("pending")
models.Build.query.get(2).source_status = StatusEnum("starting")
models.Build.query.get(3).source_status = StatusEnum("running")
self.db.session.get(models.Build, 1).source_status = StatusEnum("pending")
self.db.session.get(models.Build, 2).source_status = StatusEnum("starting")
self.db.session.get(models.Build, 3).source_status = StatusEnum("running")
assert len(BuildsLogic.get_pending_srpm_build_tasks().all()) == 1
assert len(BuildsLogic.get_pending_srpm_build_tasks(data_type="for_backend").all()) == 3

Expand Down Expand Up @@ -470,7 +469,7 @@ def test_package_assigned_to_build_initially(self):
bootstrap="on")
self.web_ui.create_distgit_package("test", "tar")
self.api3.rebuild_package("test", "tar")
build = models.Build.query.get(1)
build = self.db.session.get(models.Build, 1)
assert build.package.name == "tar"

@TransactionDecorator("u1")
Expand All @@ -480,7 +479,7 @@ def test_rebuild_all_packages(self):
bootstrap="on")
self.web_ui.create_distgit_package("test", "tar")
self.web_ui.create_distgit_package("test", "cpio")
copr = models.Copr.query.get(1)
copr = self.db.session.get(models.Copr, 1)
self.web_ui.rebuild_all_packages(copr.id)

builds = models.Build.query.all()
Expand All @@ -506,7 +505,7 @@ def test_package_not_updated_after_source_ready(self):
self.web_ui.create_distgit_package("test", "copr-cli")
self.api3.rebuild_package("test", "copr-cli")

build = models.Build.query.get(1)
build = self.db.session.get(models.Build, 1)
assert build.status == StatusEnum("pending")

form_data = {
Expand All @@ -526,7 +525,7 @@ def test_package_not_updated_after_source_ready(self):
assert len(importing) == 1
assert importing[0]['pkg_name'] == "copr-cli"

build = models.Build.query.get(1)
build = self.db.session.get(models.Build, 1)
assert build.status == StatusEnum("importing")
assert build.package.name == "copr-cli"

Expand All @@ -536,7 +535,7 @@ def test_package_set_when_source_ready(self):
self.web_ui.new_project("test", ["fedora-rawhide-i386"])
self.web_ui.submit_url_build("test")

build = models.Build.query.get(1)
build = self.db.session.get(models.Build, 1)
assert len(build.build_chroots) == 0
assert build.source_status == StatusEnum("pending")
assert build.package is None
Expand All @@ -554,7 +553,7 @@ def test_package_set_when_source_ready(self):
}

self.backend.update(form_data)
build = models.Build.query.get(1)
build = self.db.session.get(models.Build, 1)
assert len(build.build_chroots) == 1
assert build.source_status == StatusEnum("importing")
assert build.package.name == "foo"
Expand Down Expand Up @@ -602,7 +601,7 @@ def save_field(_field, file_path):
with open(file_path, "w", encoding="utf-8") as fd:
fd.write(content)

user = models.User.query.get(1)
user = self.db.session.get(models.User, 1)
copr = models.Copr.query.first()

with mock.patch("coprs.logic.builds_logic.save_form_file_field_to",
Expand Down Expand Up @@ -631,7 +630,7 @@ def save_field(_field, file_path):
}

self.backend.update(form_data)
build = models.Build.query.get(1)
build = self.db.session.get(models.Build, 1)
assert build.source_state == "failed" if fail else "importing"

# Removed upon failure, otherwise exists!
Expand All @@ -653,7 +652,7 @@ def save_field(_field, file_path):
"reponame": "test/foo"
}))
assert r.status_code == 200
build = models.Build.query.get(1)
build = self.db.session.get(models.Build, 1)
assert build.source_state == "succeeded"
assert not os.path.exists(storage)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def test_delete_expired_coprs(self, f_users, f_mock_chroots, f_coprs, f_builds,
# some builds are not finished, nothing deleted yet
assert len([c for c in query.all() if c.deleted]) == 0

b = self.db.session.query(models.Build).get(3)
b = self.db.session.get(models.Build, 3)
b.canceled = True

ComplexLogic.delete_expired_projects()
Expand All @@ -120,7 +120,7 @@ def test_delete_expired_coprs(self, f_users, f_mock_chroots, f_coprs, f_builds,
assert len([c for c in query.all() if c.deleted]) == 1

# test that build is deleted as well
assert not self.db.session.query(models.Build).get(3)
assert not self.db.session.get(models.Build, 3)


class TestProjectForking(CoprsTestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_coprdir_cleanup_no_removal(self):
def test_coprdir_cleanup_no_build(self):
self.api3.new_project("test-pr-dirs", ["fedora-17-i386"])
self.pr_trigger.build_package("test-pr-dirs", "testpkg", 1)
build = models.Build.query.get(1)
build = db.session.get(models.Build, 1)
db.session.delete(build)
models.Action.query.delete()
db.session.commit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def test_copr_detail_allows_submitter_to_cancel_build(

# And now cancel the build.
self.web_ui.cancel_build(self.c2.name, build_id)
build = models.Build.query.get(build_id)
build = self.db.session.get(models.Build, build_id)
assert build.state == "canceled"


Expand Down Expand Up @@ -1147,7 +1147,7 @@ def test_fedora_review_project(self):
follow_redirects=False,
)
assert "user1/test-fedora-review/add_build" in resp.headers["Location"]
copr = self.models.Copr.query.get(1)
copr = self.db.session.get(self.models.Copr, 1)
assert copr.full_name == "user1/test-fedora-review"
assert len(copr.active_chroots) == 1
assert copr.active_chroots[0].name == "fedora-rawhide-x86_64"
Expand Down

0 comments on commit b81778f

Please sign in to comment.