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

Use null_distincts for IssueLink constraints #2539

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions .taskcluster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ tasks:
- "git clone --quiet ${repository} /src && cd /src && git checkout ${head_rev} -b checks &&
cd /src/tools && ${pip_install} . &&
cd /src/backend && ${pip_install} . && ${pip_install} -r requirements-dev.txt &&
./ci/setup_postgres.sh &&
./manage.py test && ./manage.py makemigrations --check --noinput --dry-run -v 3"
env:
DATABASE_URL: postgres://[email protected]/code-review
metadata:
name: "Code Review Backend checks: unit tests"
description: Check python code with Django tests
Expand Down
5 changes: 5 additions & 0 deletions backend/ci/pg_hba.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Database administrative login by Unix domain socket
local all postgres peer

# Only tester user can connect locally, without password
host all tester 127.0.0.1/32 trust
19 changes: 19 additions & 0 deletions backend/ci/setup_postgres.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash -ex
export PG_VERSION=15

# Silent apt
export DEBIAN_FRONTEND=noninteractive

# Install postgresql
apt update
apt install -qq -y postgresql-$PG_VERSION

# Setup access rights
cp $(dirname $0)/pg_hba.conf /etc/postgresql/$PG_VERSION/main/pg_hba.conf

# Start postgresql
pg_ctlcluster $PG_VERSION main start

# Create user & database
su postgres -c 'createuser --createdb tester'
su postgres -c 'createdb --owner=tester code-review'
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 5.1.1 on 2024-11-25 14:35

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("issues", "0014_unique_hash"),
]

operations = [
migrations.RemoveConstraint(
model_name="issuelink",
name="issue_link_unique_revision",
),
migrations.RemoveConstraint(
model_name="issuelink",
name="issue_link_unique_diff",
),
migrations.AddConstraint(
model_name="issuelink",
constraint=models.UniqueConstraint(
fields=("issue", "revision", "diff", "line", "nb_lines", "char"),
name="issue_link_unique",
nulls_distinct=False,
),
),
]
10 changes: 2 additions & 8 deletions backend/code_review_backend/issues/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,10 @@ class IssueLink(models.Model):

class Meta:
constraints = [
# Two constraints are required as Null values are not compared for unicity
models.UniqueConstraint(
fields=["issue", "revision", "line", "nb_lines", "char"],
name="issue_link_unique_revision",
condition=Q(diff__isnull=True),
),
models.UniqueConstraint(
fields=["issue", "revision", "diff", "line", "nb_lines", "char"],
name="issue_link_unique_diff",
condition=Q(diff__isnull=False),
name="issue_link_unique",
nulls_distinct=False,
),
]

Expand Down
61 changes: 61 additions & 0 deletions backend/code_review_backend/issues/tests/test_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import datetime
from unittest.mock import patch

from django.db.utils import IntegrityError
from django.test import TestCase
from django.urls import reverse
from rest_framework import status
Expand Down Expand Up @@ -211,3 +212,63 @@ def test_list_repository_issues_date_no_match(self):
data = response.json()
self.assertEqual(data["count"], 0)
self.assertEqual(data["results"], [])

def test_issue_link_unicity_all_values(self):
"""
Check that the unique constraints are respected when all positioning values are set
"""
self.revision.issue_links.create(
issue=self.err_issue, line=20, nb_lines=2, char=1
)
with self.assertRaises(IntegrityError):
self.revision.issue_links.create(
issue=self.err_issue, line=20, nb_lines=2, char=1
)

def test_issue_link_unicity_no_line(self):
"""
Check that the unique constraints are respected when line is not set
"""
self.revision.issue_links.create(
issue=self.err_issue, line=None, nb_lines=2, char=1
)
with self.assertRaises(IntegrityError):
self.revision.issue_links.create(
issue=self.err_issue, line=None, nb_lines=2, char=1
)

def test_issue_link_unicity_no_nb_lines(self):
"""
Check that the unique constraints are respected when nb_lines is not set
"""
self.revision.issue_links.create(
issue=self.err_issue, line=100, nb_lines=None, char=1
)
with self.assertRaises(IntegrityError):
self.revision.issue_links.create(
issue=self.err_issue, line=100, nb_lines=None, char=1
)

def test_issue_link_unicity_no_char(self):
"""
Check that the unique constraints are respected when char is not set
"""
self.revision.issue_links.create(
issue=self.err_issue, line=100, nb_lines=42, char=None
)
with self.assertRaises(IntegrityError):
self.revision.issue_links.create(
issue=self.err_issue, line=100, nb_lines=42, char=None
)

def test_issue_link_unicity_no_position(self):
"""
Check that the unique constraints are respected when no positioning args are set
"""
self.revision.issue_links.create(
issue=self.err_issue, line=None, nb_lines=None, char=None
)
with self.assertRaises(IntegrityError):
self.revision.issue_links.create(
issue=self.err_issue, line=None, nb_lines=None, char=None
)