Skip to content

Commit

Permalink
Add a unit test that fails due to this bug
Browse files Browse the repository at this point in the history
The bug occurs when a workspace data object has a second foreign
key to workspace. The inlineformset_factory method does not know
which fk it should be referring to. Add a custom workspace data
model with this foreign key. Add an adapter and a form. Add a test
for WorkspaceCreate where the exception is raised.
  • Loading branch information
amstilp committed Nov 7, 2023
1 parent 9c67840 commit 00c11e9
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 19 deletions.
15 changes: 15 additions & 0 deletions anvil_consortium_manager/tests/test_app/adapters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from anvil_consortium_manager.adapters.account import BaseAccountAdapter
from anvil_consortium_manager.adapters.workspace import BaseWorkspaceAdapter
from anvil_consortium_manager.forms import WorkspaceForm
from anvil_consortium_manager.tables import WorkspaceTable

from . import filters, forms, models, tables

Expand Down Expand Up @@ -39,3 +41,16 @@ def get_autocomplete_queryset(self, queryset, q):

def get_autocomplete_label(self, account):
return "TEST {}".format(account.email)


class TestForeignKeyWorkspaceAdapter(BaseWorkspaceAdapter):
"""Adapter for TestForeignKeyWorkspace."""

name = "Test foreign key workspace"
type = "test_fk"
description = "Workspace type for testing"
list_table_class = WorkspaceTable
workspace_form_class = WorkspaceForm
workspace_data_model = models.TestForeignKeyWorkspaceData
workspace_data_form_class = forms.TestForeignKeyWorkspaceDataForm
workspace_detail_template_name = "workspace_detail.html"
8 changes: 8 additions & 0 deletions anvil_consortium_manager/tests/test_app/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ def clean_name(self):
if name and name == "test-fail":
raise ValidationError("Workspace name cannot be 'test-fail'!")
return name


class TestForeignKeyWorkspaceDataForm(forms.ModelForm):
"""Form for a TestForeignKeyWorkspace object."""

class Meta:
model = models.TestForeignKeyWorkspaceData
fields = ("other_workspace", "workspace")
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Generated by Django 4.2.7 on 2023-11-07 20:38

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import simple_history.models


class Migration(migrations.Migration):

dependencies = [
('anvil_consortium_manager', '0013_alter_anvilprojectmanageraccess_options'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('test_app', '0003_testworkspacedata_alter_study_name'),
]

operations = [
migrations.CreateModel(
name='TestForeignKeyWorkspaceData',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('other_workspace', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='test_foreign_key_workspaces', to='anvil_consortium_manager.workspace')),
('workspace', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='anvil_consortium_manager.workspace')),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='HistoricalTestForeignKeyWorkspaceData',
fields=[
('id', models.IntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')),
('history_id', models.AutoField(primary_key=True, serialize=False)),
('history_date', models.DateTimeField(db_index=True)),
('history_change_reason', models.CharField(max_length=100, null=True)),
('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)),
('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)),
('other_workspace', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='anvil_consortium_manager.workspace')),
('workspace', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='anvil_consortium_manager.workspace')),
],
options={
'verbose_name': 'historical test foreign key workspace data',
'verbose_name_plural': 'historical test foreign key workspace datas',
'ordering': ('-history_date', '-history_id'),
'get_latest_by': ('history_date', 'history_id'),
},
bases=(simple_history.models.HistoricalChanges, models.Model),
),
]
8 changes: 8 additions & 0 deletions anvil_consortium_manager/tests/test_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ class ProtectedWorkspaceData(models.Model):
"""Model to test having a protected foreign key to DefaultWorkspaceData."""

workspace_data = models.ForeignKey(DefaultWorkspaceData, on_delete=models.PROTECT)


class TestForeignKeyWorkspaceData(BaseWorkspaceData):
"""Custom model with a second fk to Workspace."""

other_workspace = models.ForeignKey(
Workspace, related_name="test_foreign_key_workspaces", on_delete=models.PROTECT
)
Loading

0 comments on commit 00c11e9

Please sign in to comment.