Skip to content

Commit

Permalink
Updated models from merge
Browse files Browse the repository at this point in the history
  • Loading branch information
SalmanAsh committed Jul 12, 2024
2 parents 276b709 + 519dfdd commit f7f88b4
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 91 deletions.
10 changes: 5 additions & 5 deletions api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.25 on 2024-07-11 10:16
# Generated by Django 3.2.25 on 2024-07-12 15:33

from django.db import migrations, models
import django.db.models.deletion
Expand All @@ -15,7 +15,7 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='Contributor',
fields=[
('id', models.IntegerField(primary_key=True, serialize=False)),
('id', models.IntegerField(help_text="The contributor's GitHub user-ID.", primary_key=True, serialize=False)),
('email', models.EmailField(max_length=254, verbose_name='email')),
('name', models.TextField(verbose_name='name')),
('location', models.TextField(verbose_name='location')),
Expand All @@ -31,8 +31,8 @@ class Migration(migrations.Migration):
name='Repository',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('gh_id', models.IntegerField(verbose_name='GitHub ID')),
('points', models.IntegerField(default=0, verbose_name='points')),
('gh_id', models.IntegerField(help_text='Github ID of the repo a contributor has contributed to.', verbose_name='GitHub ID')),
('points', models.IntegerField(default=0, help_text='Story points the contributor closed for this repository.', verbose_name='points')),
('contributor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.contributor')),
],
options={
Expand All @@ -45,7 +45,7 @@ class Migration(migrations.Migration):
name='AgreementSignature',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('agreement_id', models.CharField(max_length=40, verbose_name='agreement id')),
('agreement_id', models.CharField(help_text='Commit ID of the contribution agreement in workspace.', max_length=40, verbose_name='agreement id')),
('signed_at', models.DateTimeField(verbose_name='signed at')),
('contributor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.contributor')),
],
Expand Down
8 changes: 6 additions & 2 deletions api/models/agreement_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ class AgreementSignature(models.Model):
contributor_id: int
contributor = models.ForeignKey(Contributor, on_delete=models.CASCADE)

agreement_id = models.CharField(_("agreement id"), max_length=40)
agreement_id = models.CharField(
_("agreement id"),
max_length=40,
help_text=_("Commit ID of the contribution agreement in workspace."),
)
signed_at = models.DateTimeField(_("signed at"))

class Meta(TypedModelMeta):
Expand All @@ -31,6 +35,6 @@ class Meta(TypedModelMeta):
verbose_name_plural = _("agreement signatures")

def __str__(self):
cont = f"Contributor {self.contributor} signed"
cont = f"Contributor {self.contributor.pk} signed"
repo = f"{self.agreement_id[:7]} at {self.signed_at}"
return f"{cont} {repo}"
35 changes: 7 additions & 28 deletions api/models/agreement_signature_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
"""

from codeforlife.tests import ModelTestCase
from django.db import IntegrityError

from .agreement_signature import AgreementSignature
from .contributor import Contributor


class TestAgreementSignature(ModelTestCase[AgreementSignature]):
Expand All @@ -17,36 +15,17 @@ class TestAgreementSignature(ModelTestCase[AgreementSignature]):

def setUp(self):
self.agreement_signature = AgreementSignature.objects.get(pk=1)
self.contributor1 = Contributor.objects.get(pk=1)

def test_str(self):
"""Parsing a contributor object instance to returns its name."""
"""
Parsing an agreement-signature instance to a string
that returns the contributor's primary key,
the first 7 characters of the agreement's commit ID
and the timestamp of when the agreement was signed.
"""
commit_id = self.agreement_signature.agreement_id[:7]
time = self.agreement_signature.signed_at
cont = f"Contributor {self.agreement_signature.contributor} signed"
cont = f"Contributor {self.agreement_signature.contributor.pk} signed"
repo = f"{commit_id} at {time}"
expected_str = f"{cont} {repo}"
assert str(self.agreement_signature) == expected_str

def test_unique_fields(self):
"""Test the unique fields functionality"""
new_contributor = Contributor.objects.create(
id=738237,
email="[email protected]",
name="new contributor",
location="london",
html_url="https://github.com/newcontributor",
avatar_url="https://contributornew.github.io/",
)
AgreementSignature.objects.create(
contributor=new_contributor,
agreement_id="pyu66uehr8dgd43vc37232fef0898df3f3f31fga",
signed_at="2024-01-02T12:00:00Z",
)

with self.assertRaises(IntegrityError):
AgreementSignature.objects.create(
contributor=new_contributor,
agreement_id="pyu66uehr8dgd43vc37232fef0898df3f3f31fga",
signed_at="2024-01-02T12:00:00Z",
)
4 changes: 3 additions & 1 deletion api/models/contributor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
class Contributor(models.Model):
"""A contributor that contributes to a repo"""

id = models.IntegerField(primary_key=True)
id = models.IntegerField(
primary_key=True, help_text=_("The contributor's GitHub user-ID.")
)
email = models.EmailField(_("email"))
name = models.TextField(_("name"))
location = models.TextField(_("location"))
Expand Down
20 changes: 7 additions & 13 deletions api/models/contributor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,12 @@ class TestContributor(ModelTestCase[Contributor]):
fixtures = ["contributors"]

def setUp(self):
self.contributor1 = Contributor.objects.get(pk=1)
self.contributor2 = Contributor.objects.get(pk=2)
self.contributor3 = Contributor.objects.get(pk=3)
self.contributor = Contributor.objects.get(pk=1)

def test_str(self):
"""Parsing a contributor object instance to returns its name."""
name = self.contributor1.name
email = self.contributor1.email
assert str(self.contributor1) == f"{name} <{email}>"

def test_fields(self):
"""Check the correct fields"""
assert self.contributor1.email == "[email protected]"
assert self.contributor2.email == "[email protected]"
assert self.contributor3.email == "[email protected]"
"""
Parsing a contributor instance to a string returns its name and email.
"""
name = self.contributor.name
email = self.contributor.email
assert str(self.contributor) == f"{name} <{email}>"
15 changes: 11 additions & 4 deletions api/models/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,25 @@


class Repository(models.Model):
"""A repository to contribute to"""
"""A repository that a contributor has contributed to."""

contributor_id: int
contributor = models.ForeignKey(Contributor, on_delete=models.CASCADE)

gh_id = models.IntegerField(_("GitHub ID"))
points = models.IntegerField(_("points"), default=0)
gh_id = models.IntegerField(
_("GitHub ID"),
help_text=_("Github ID of the repo a contributor has contributed to."),
)
points = models.IntegerField(
_("points"),
default=0,
help_text=_("Story points the contributor closed for this repository."),
)

class Meta(TypedModelMeta):
unique_together = ["contributor", "gh_id"]
verbose_name = _("repository")
verbose_name_plural = _("repositories")

def __str__(self):
return f"{self.contributor}: {self.gh_id}"
return f"{self.contributor.pk}:{self.gh_id}"
44 changes: 6 additions & 38 deletions api/models/repository_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"""

from codeforlife.tests import ModelTestCase
from django.db import IntegrityError

from .contributor import Contributor
from .repository import Repository


Expand All @@ -17,42 +15,12 @@ class TestRepository(ModelTestCase[Repository]):

def setUp(self):
self.repository = Repository.objects.get(pk=1)
self.contributor1 = Contributor.objects.get(pk=1)
self.contributor2 = Contributor.objects.get(pk=2)

def test_str(self):
"""Parsing a contributor object instance to returns its name."""
expected = f"{self.repository.contributor}: {self.repository.gh_id}"
"""
Parsing a repository instance to a string
returns the contributor's primary key and
the repository's GitHub ID.
"""
expected = f"{self.repository.contributor.pk}:{self.repository.gh_id}"
assert str(self.repository) == expected

def test_default_value(self):
"""check default value of points if not assigned"""
new_contributor = Contributor.objects.create(
id=425525,
email="[email protected]",
name="new contributor",
location="london",
html_url="https://github.com/newcontributor",
avatar_url="https://contributornew.github.io/",
)
repository = Repository.objects.create(
contributor=new_contributor, gh_id=432079567
)
assert repository.points == 0

def test_unique_fields(self):
"""Test the unique fields functionality"""
new_contributor = Contributor.objects.create(
id=5134,
email="[email protected]",
name="new contributor",
location="london",
html_url="https://github.com/newcontributor",
avatar_url="https://contributornew.github.io/",
)
Repository.objects.create(contributor=new_contributor, gh_id=432079567)

with self.assertRaises(IntegrityError):
Repository.objects.create(
contributor=self.contributor1, gh_id=10274252
)

0 comments on commit f7f88b4

Please sign in to comment.