Skip to content

Commit

Permalink
Order tables appropriately in dbgap app
Browse files Browse the repository at this point in the history
Add sensible default orderings to the tables in the dbgap app. This
way it is easier for users to find information because tables are not
ordered by pk.
  • Loading branch information
amstilp committed Sep 25, 2023
1 parent e2f6281 commit d3fa401
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions primed/dbgap/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Meta:
"dbgap_phs",
"studies",
)
order_by = ("dbgap_phs",)

def render_dbgap_phs(self, value):
return "phs{0:06d}".format(value)
Expand Down Expand Up @@ -140,6 +141,7 @@ class Meta:
"dbgap_project_id",
"principal_investigator",
)
order_by = ("dbgap_project_id",)


class dbGaPDataAccessSnapshotTable(tables.Table):
Expand All @@ -151,6 +153,7 @@ class Meta:
"pk",
"created",
)
order_by = ("-created",)

pk = tables.Column(linkify=True, verbose_name="Details", orderable=False)
number_approved_dars = tables.columns.Column(
Expand Down Expand Up @@ -235,6 +238,7 @@ class Meta:
"dbgap_consent_abbreviation",
"dbgap_current_status",
)
order_by = ("dbgap_dar_id",)


class dbGaPDataAccessRequestSummaryTable(tables.Table):
Expand Down
35 changes: 35 additions & 0 deletions primed/dbgap/tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.db.models import Count
from django.test import TestCase
from django.utils import timezone
from freezegun import freeze_time

from .. import models, tables
from . import factories
Expand Down Expand Up @@ -50,6 +51,14 @@ def test_number_workspaces(self):
self.assertEqual(table.rows[1].get_cell("number_workspaces"), 1)
self.assertEqual(table.rows[2].get_cell("number_workspaces"), 2)

def test_ordering(self):
"""Instances are ordered alphabetically by dbgap_phs."""
instance_1 = self.model_factory.create(dbgap_phs=2)
instance_2 = self.model_factory.create(dbgap_phs=1)
table = self.table_class(self.model.objects.all())
self.assertEqual(table.data[0], instance_2)
self.assertEqual(table.data[1], instance_1)


class dbGaPWorkspaceTableTest(TestCase):
model = acm_models.Workspace
Expand Down Expand Up @@ -380,6 +389,14 @@ def test_last_update_two_snapshots(self):
latest_snapshot.get_absolute_url(), table.rows[0].get_cell("last_update")
)

def test_ordering(self):
"""Instances are ordered alphabetically by dbgap_project_id."""
instance_1 = self.model_factory.create(dbgap_project_id=2)
instance_2 = self.model_factory.create(dbgap_project_id=1)
table = self.table_class(self.model.objects.all())
self.assertEqual(table.data[0], instance_2)
self.assertEqual(table.data[1], instance_1)


class dbGaPDataAccessSnapshotTableTest(TestCase):
model = models.dbGaPDataAccessSnapshot
Expand Down Expand Up @@ -436,6 +453,16 @@ def test_number_approved_dars(self):
self.assertEqual(table.rows[0].get_cell_value("number_approved_dars"), 1)
self.assertEqual(table.rows[1].get_cell_value("number_approved_dars"), 2)

def test_ordering(self):
"""Instances are ordered by decreasing snapshot date."""
with freeze_time("2020-01-01"):
instance_1 = self.model_factory.create()
with freeze_time("2021-12-12"):
instance_2 = self.model_factory.create()
table = self.table_class(self.model.objects.all())
self.assertEqual(table.data[0], instance_2)
self.assertEqual(table.data[1], instance_1)


class dbGaPDataAccessRequestTableTest(TestCase):
model = models.dbGaPDataAccessRequest
Expand Down Expand Up @@ -508,6 +535,14 @@ def test_two_matching_workspaces(self):
self.assertIn(str(workspace_1), value)
self.assertIn(str(workspace_2), value)

def test_ordering(self):
"""Instances are ordered alphabetically by dbgap_dar_id."""
instance_1 = self.model_factory.create(dbgap_dar_id=2)
instance_2 = self.model_factory.create(dbgap_dar_id=1)
table = self.table_class(self.model.objects.all())
self.assertEqual(table.data[0], instance_2)
self.assertEqual(table.data[1], instance_1)


class dbGaPDataAccessRequestSummaryTable(TestCase):

Expand Down
5 changes: 5 additions & 0 deletions requirements/local.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ setuptools==65.5.1 # https://github.com/pypa/setuptools
django-debug-toolbar==3.4.0 # https://github.com/jazzband/django-debug-toolbar
django-coverage-plugin==2.0.3 # https://github.com/nedbat/django_coverage_plugin
pytest-django==4.5.2 # https://github.com/pytest-dev/pytest-django

# My additions
# ------------------------------------------------------------------------------
# For setting time when running tests.
freezegun==1.2.2 # https://github.com/spulec/freezegun

0 comments on commit d3fa401

Please sign in to comment.