Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db: properly filter PRs by project and pr_id #441

Merged
merged 2 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packit_service/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ class PullRequest(Base):
def get_or_create(cls, pr_id: int, namespace: str, repo_name: str) -> "PullRequest":
session = get_sa_session()
project = GitProject.get_or_create(namespace=namespace, repo_name=repo_name)
pr = session.query(PullRequest).filter_by(pr_id=pr_id).first()
pr = (
session.query(PullRequest)
.filter_by(pr_id=pr_id, project_id=project.id)
.first()
)
if not pr:
pr = PullRequest()
pr.pr_id = pr_id
Expand Down
47 changes: 45 additions & 2 deletions tests_requre/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,32 @@
"""
import pytest

from packit_service.models import CoprBuild, get_sa_session, SRPMBuild
from packit_service.models import (
CoprBuild,
get_sa_session,
SRPMBuild,
PullRequest,
GitProject,
)

TARGET = "fedora-42-x86_64"


def clean_db():
s = get_sa_session()
s.query(CoprBuild).delete()
s.query(PullRequest).delete()
s.query(GitProject).delete()
s.commit()


@pytest.fixture()
def a_copr_build():
s = get_sa_session()
s.query(CoprBuild).delete()
s.commit()
srpm_build = SRPMBuild.create("asd\nqwe\n")
return CoprBuild.get_or_create(
yield CoprBuild.get_or_create(
pr_id=1,
build_id="123456",
commit_sha="687abc76d67d",
Expand All @@ -54,6 +68,7 @@ def a_copr_build():
status="pending",
srpm_build=srpm_build,
)
clean_db()


def test_create_copr_build(a_copr_build):
Expand Down Expand Up @@ -94,3 +109,31 @@ def test_copr_build_set_build_logs_url(a_copr_build):
assert a_copr_build.build_logs_url == url
b = CoprBuild.get_by_build_id(a_copr_build.build_id, TARGET)
assert b.build_logs_url == url


def test_get_or_create_pr():
clean_db()
s = get_sa_session()

try:
expected_pr = PullRequest.get_or_create(
pr_id=42, namespace="clapton", repo_name="layla"
)
actual_pr = PullRequest.get_or_create(
pr_id=42, namespace="clapton", repo_name="layla"
)

assert s.query(PullRequest).count() == 1
assert expected_pr.project_id == actual_pr.project_id

expected_pr = PullRequest.get_or_create(
pr_id=42, namespace="clapton", repo_name="cocaine"
)
actual_pr = PullRequest.get_or_create(
pr_id=42, namespace="clapton", repo_name="cocaine"
)

assert s.query(PullRequest).count() == 2
assert expected_pr.project_id == actual_pr.project_id
finally:
clean_db()