Skip to content

Commit

Permalink
Return only distinct VariantExperiment objects
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorjerse committed Dec 12, 2024
1 parent eeb0a15 commit 16e03ad
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Added
-----
- Add filtering ``Variant`` by ``VariantExperiment`` and ``VariantCall``
- Use generic permission filters when filtering variants by related models
- Return only distinct ``VariantExperiment`` objects


===================
Expand Down
5 changes: 5 additions & 0 deletions resolwe_bio/variants/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,8 @@ class Meta:
"timestamp": DATETIME_LOOKUPS,
"variant_data_source": TEXT_LOOKUPS,
}

@property
def qs(self):
"""Always return distinct queryset."""
return super().qs.distinct()
26 changes: 26 additions & 0 deletions resolwe_bio/variants/tests/test_variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,32 @@ def setUp(self) -> None:
self.view = VariantExperimentViewSet.as_view({"get": "list"})
return super().setUp()

def test_distinct(self):
"""Test only distynct experiments are returned."""
# Create another VariantCall that points to the first sample and experiment.
self.sample.set_permission(Permission.VIEW, self.contributor)
VariantCall.objects.create(
sample=self.sample,
variant=self.variants[0],
quality=0.7,
depth_norm_quality=0.7,
alternative_allele_depth=1,
depth=15,
filter="filter 1",
genotype="1",
genotype_quality=1,
experiment=self.experiments[0],
)
# Filter by sample with permissions. Make sure no duplicates are returned.
request = APIRequestFactory().get(
"/variantexperiment", {"variant_calls__sample": self.sample.id}
)
force_authenticate(request, self.contributor)
response = self.view(request)
expected = VariantExperimentSerializer(self.experiments, many=True).data
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertCountEqual(response.data, expected)

def test_filter(self):
# Set the permission on sample.
self.sample.set_permission(Permission.EDIT, self.contributor)
Expand Down

0 comments on commit 16e03ad

Please sign in to comment.