diff --git a/krill/sample/forms.py b/krill/sample/forms.py
index 4c0c06c..551c1d7 100644
--- a/krill/sample/forms.py
+++ b/krill/sample/forms.py
@@ -2,6 +2,7 @@
from .models.sample import Sample
from .models.aliquot import Aliquot, AliquotLocation, AliquotType, AliquotDisposition
from .models.source import Source
+from .widgets import RemoteSelect, aliquot_choice_label, mark_comboboxes
class SampleForm(forms.ModelForm):
class Meta:
@@ -26,6 +27,10 @@ class Meta:
'notes': forms.Textarea(attrs={'rows': 4}),
}
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ mark_comboboxes(self)
+
def clean(self):
cleaned = super().clean()
warn, alert = cleaned.get('low_count_warn'), cleaned.get('low_count_alert')
@@ -72,6 +77,7 @@ class Meta:
]
widgets = {
'sample_date': forms.DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
+ 'parent': RemoteSelect('sample:aliquot_options'),
}
help_texts = {
'parent': 'Parent aliquot, if this is a derivative',
@@ -85,8 +91,10 @@ def __init__(self, *args, **kwargs):
from storage.models.storage import Box
super().__init__(*args, **kwargs)
self.fields['box'].queryset = Box.objects.all().order_by('name')
- self.fields['parent'].queryset = Aliquot.objects.select_related('sample').all()
+ self.fields['parent'].queryset = Aliquot.objects.select_related('sample', 'aliquot_type')
+ self.fields['parent'].label_from_instance = aliquot_choice_label
self.fields['disposition'].required = True
+ mark_comboboxes(self)
def clean(self):
"""Validate box assignment fields"""
@@ -177,3 +185,4 @@ def __init__(self, *args, **kwargs):
from storage.models.storage import Box
super().__init__(*args, **kwargs)
self.fields['box'].queryset = Box.objects.all()
+ mark_comboboxes(self)
diff --git a/krill/sample/tests/test_combobox.py b/krill/sample/tests/test_combobox.py
new file mode 100644
index 0000000..ff427fc
--- /dev/null
+++ b/krill/sample/tests/test_combobox.py
@@ -0,0 +1,225 @@
+import datetime
+
+from django.contrib.staticfiles import finders
+from django.db import connection
+from django.test.utils import CaptureQueriesContext
+from django.urls import reverse
+
+from ..forms import AliquotForm, AliquotMoveForm, SampleForm
+from ..models.aliquot import Aliquot, AliquotType
+from ..models.sample import Sample
+from ..widgets import RemoteSelect, aliquot_choice_label
+from .test_views import SampleViewTest
+
+
+class ComboboxFixture(SampleViewTest):
+ def setUp(self):
+ super().setUp()
+ Aliquot.objects.all().delete()
+ self.url = reverse('sample:aliquot_options')
+ self.client.force_login(self.user)
+
+ def make_aliquot(self, sample=None, **kwargs):
+ return Aliquot.objects.create(
+ sample=sample or self.sample, aliquot_type=kwargs.pop('aliquot_type', self.aliquot_type),
+ disposition=self.stored_disposition, **kwargs)
+
+ def search(self, q):
+ return self.client.get(self.url, {'q': q}).json()
+
+
+class AliquotLabelTest(ComboboxFixture):
+ def test_label_has_sample_number_type_passage_and_date(self):
+ a = self.make_aliquot(passage='p8', sample_date=datetime.date(2026, 3, 5))
+ self.assertEqual(aliquot_choice_label(a), f'Test Sample #{a.pk} · Test Type · p8 · 2026-03-05')
+
+ def test_label_skips_what_is_missing(self):
+ a = Aliquot.objects.create(sample=self.sample, disposition=self.stored_disposition)
+ self.assertEqual(aliquot_choice_label(a), f'Test Sample #{a.pk}')
+
+
+class SearchEndpointTest(ComboboxFixture):
+ def test_requires_login(self):
+ self.client.logout()
+ self.assertEqual(self.client.get(self.url, {'q': 'x'}).status_code, 302)
+
+ def test_blank_query_returns_nothing(self):
+ self.make_aliquot()
+ for q in ('', ' '):
+ with self.subTest(q=repr(q)):
+ self.assertEqual(self.search(q), {'results': [], 'more': False})
+
+ def test_matches_sample_name_ignoring_case(self):
+ a = self.make_aliquot()
+ self.make_aliquot(sample=Sample.objects.create(name='Other', source=self.source))
+ self.assertEqual([r['id'] for r in self.search('test sam')['results']], [a.pk])
+
+ def test_matches_passage_and_type(self):
+ by_passage = self.make_aliquot(passage='p8')
+ other_type = AliquotType.objects.create(name='Pellet')
+ by_type = self.make_aliquot(aliquot_type=other_type)
+ self.assertEqual([r['id'] for r in self.search('p8')['results']], [by_passage.pk])
+ self.assertEqual([r['id'] for r in self.search('pellet')['results']], [by_type.pk])
+
+ def test_every_word_must_match_so_sample_and_passage_combine(self):
+ hit = self.make_aliquot(passage='p8')
+ self.make_aliquot(passage='p9')
+ self.make_aliquot(sample=Sample.objects.create(name='Other', source=self.source), passage='p8')
+ # 'test' would also match every aliquot through its type, 'Test Type'; 'sample' is only in the name
+ self.assertEqual([r['id'] for r in self.search('sample p8')['results']], [hit.pk])
+
+ def test_aliquot_number_with_or_without_hash(self):
+ target = self.make_aliquot()
+ self.make_aliquot()
+ for q in (str(target.pk), f'#{target.pk}'):
+ with self.subTest(q=q):
+ self.assertEqual(self.search(q)['results'][0]['id'], target.pk)
+
+ def test_an_exact_number_is_listed_first_even_when_others_also_match(self):
+ sample = Sample.objects.create(name='Line 7', source=self.source) # "7" also matches this name
+ many = [self.make_aliquot(sample=sample) for _ in range(25)]
+ target = self.make_aliquot(sample=self.sample)
+ number = str(many[3].pk)
+ self.assertEqual(self.search(number)['results'][0]['id'], many[3].pk)
+ self.assertNotEqual(target.pk, many[3].pk)
+
+ def test_limit_and_more_flag(self):
+ for _ in range(25):
+ self.make_aliquot()
+ data = self.search('test')
+ self.assertEqual(len(data['results']), 20)
+ self.assertTrue(data['more'])
+
+ def test_no_more_flag_at_the_limit(self):
+ for _ in range(20):
+ self.make_aliquot()
+ data = self.search('test')
+ self.assertEqual(len(data['results']), 20)
+ self.assertFalse(data['more'])
+
+ def test_deleted_aliquots_are_not_offered(self):
+ gone = self.make_aliquot()
+ gone.deleted = True
+ gone.save()
+ self.assertEqual(self.search('test')['results'], [])
+
+ def test_results_are_labelled_and_ordered_by_sample_then_newest(self):
+ zebra = Sample.objects.create(name='Zebra', source=self.source)
+ a = self.make_aliquot(sample=zebra)
+ older, newer = self.make_aliquot(passage='p1'), self.make_aliquot(passage='p1')
+ results = self.search('')['results'] or self.search('p1')['results']
+ self.assertEqual([r['id'] for r in results], [newer.pk, older.pk])
+ self.assertEqual(results[0]['label'], f'Test Sample #{newer.pk} · Test Type · p1')
+ self.assertEqual(self.search('zebra')['results'][0]['id'], a.pk)
+
+ def test_the_query_count_does_not_grow_with_the_results(self):
+ self.make_aliquot()
+ with CaptureQueriesContext(connection) as few:
+ self.search('test')
+ for _ in range(15):
+ self.make_aliquot()
+ with CaptureQueriesContext(connection) as many:
+ self.search('test')
+ self.assertEqual(len(few), len(many))
+
+
+class RemoteSelectFormTest(ComboboxFixture):
+ def test_the_parent_select_renders_only_a_blank_option(self):
+ for _ in range(30):
+ self.make_aliquot()
+ html = str(AliquotForm()['parent'])
+ self.assertEqual(html.count('', html)
+
+ def test_an_edited_aliquot_shows_its_current_parent(self):
+ parent = self.make_aliquot(passage='p3')
+ child = self.make_aliquot(parent=parent)
+ html = str(AliquotForm(instance=child)['parent'])
+ self.assertIn(f'', html)
+ select = html[html.index('name="parent"'):]
+ select = select[:select.index('')]
+ self.assertEqual(select.count('