From 37ca716c6e963be0052633c37ed80cacca2b6456 Mon Sep 17 00:00:00 2001 From: Xander Vertegaal Date: Sat, 3 Feb 2024 12:04:10 +0100 Subject: [PATCH] Fix tests; add related_name to dob and dod --- .../0004_persondateofdeath_persondateofbirth.py | 6 +++--- backend/person/models.py | 8 ++++++-- backend/person/tests/test_person_models.py | 5 +++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/backend/person/migrations/0004_persondateofdeath_persondateofbirth.py b/backend/person/migrations/0004_persondateofdeath_persondateofbirth.py index 6417021b..f0cd9d07 100644 --- a/backend/person/migrations/0004_persondateofdeath_persondateofbirth.py +++ b/backend/person/migrations/0004_persondateofdeath_persondateofbirth.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.7 on 2024-02-02 15:17 +# Generated by Django 4.2.7 on 2024-02-03 10:53 import django.core.validators from django.db import migrations, models @@ -21,7 +21,7 @@ class Migration(migrations.Migration): ('year_lower', models.IntegerField(default=400, help_text='The earliest possible year for this value', validators=[django.core.validators.MinValueValidator(400), django.core.validators.MaxValueValidator(800)])), ('year_upper', models.IntegerField(default=800, help_text='The latest possible year for this value', validators=[django.core.validators.MinValueValidator(400), django.core.validators.MaxValueValidator(800)])), ('year_exact', models.IntegerField(blank=True, help_text='The exact year of the value (if known). This will override the values in the lower and upper bounds fields.', null=True, validators=[django.core.validators.MinValueValidator(400), django.core.validators.MaxValueValidator(800)])), - ('person', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='person.person')), + ('person', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='date_of_death', to='person.person')), ], options={ 'abstract': False, @@ -36,7 +36,7 @@ class Migration(migrations.Migration): ('year_lower', models.IntegerField(default=400, help_text='The earliest possible year for this value', validators=[django.core.validators.MinValueValidator(400), django.core.validators.MaxValueValidator(800)])), ('year_upper', models.IntegerField(default=800, help_text='The latest possible year for this value', validators=[django.core.validators.MinValueValidator(400), django.core.validators.MaxValueValidator(800)])), ('year_exact', models.IntegerField(blank=True, help_text='The exact year of the value (if known). This will override the values in the lower and upper bounds fields.', null=True, validators=[django.core.validators.MinValueValidator(400), django.core.validators.MaxValueValidator(800)])), - ('person', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='person.person')), + ('person', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='date_of_birth', to='person.person')), ], options={ 'abstract': False, diff --git a/backend/person/models.py b/backend/person/models.py index 5189ecbe..149bf05e 100644 --- a/backend/person/models.py +++ b/backend/person/models.py @@ -65,7 +65,9 @@ class PersonDateOfBirth(LettercraftDate, Field, models.Model): A relationship between a person and their date of birth. """ - person = models.OneToOneField(Person, on_delete=models.CASCADE) + person = models.OneToOneField( + Person, related_name="date_of_birth", on_delete=models.CASCADE + ) def __str__(self): if self.year_exact: @@ -79,7 +81,9 @@ class PersonDateOfDeath(LettercraftDate, Field, models.Model): A relationship between a person and their date of death. """ - person = models.OneToOneField(Person, on_delete=models.CASCADE) + person = models.OneToOneField( + Person, related_name="date_of_death", on_delete=models.CASCADE + ) def __str__(self): if self.year_exact: diff --git a/backend/person/tests/test_person_models.py b/backend/person/tests/test_person_models.py index ed66b237..8fb958ec 100644 --- a/backend/person/tests/test_person_models.py +++ b/backend/person/tests/test_person_models.py @@ -4,6 +4,7 @@ def test_person_names(person_unnamed, person_single_name, person_multiple_names) assert person_single_name.__str__() == "Bert" assert person_multiple_names.__str__() == "Bert (aka Ernie, Oscar)" + def test_person_date_of_birth(person_with_exact_dob, person_with_approx_dob): - assert person_with_exact_dob.dates_of_birth.first().__str__() == "born in 512" - assert person_with_approx_dob.dates_of_birth.first().__str__() == "born c. 500–525" \ No newline at end of file + assert person_with_exact_dob.date_of_birth.__str__().endswith("born in 512") + assert person_with_approx_dob.date_of_birth.__str__().endswith("born c. 500–525")