Skip to content
Snippets Groups Projects
Commit 2f73dc63 authored by Bastien Abadie's avatar Bastien Abadie
Browse files

Merge branch 'duplicate-children-count' into 'master'

Fix duplicates in children count with multiple parents

See merge request !689
parents f6a63d08 af997088
No related branches found
No related tags found
1 merge request!689Fix duplicates in children count with multiple parents
......@@ -67,7 +67,7 @@ def _fetch_children_count(elements):
ElementPath.objects
.filter(path__last__in=map(operator.attrgetter('id'), elements))
.values('path__last')
.annotate(count=Count('path__last'))
.annotate(count=Count('element_id', distinct=True))
.values_list('path__last', 'count')
)
......
......@@ -827,3 +827,31 @@ class TestElementsAPI(FixtureAPITestCase):
'Volume 1': 8,
}
)
def test_with_children_count_unique(self):
r"""
Ensure with_children_count only counts children elements once, not even when ElementPaths are duplicated.
Counts may have duplicates as soon as an element's parent has multiple parents:
A B Element | Path
\ / C | A
C C | B
| D | A C
D D | B C
Here, requesting the parents of D with children counts should return C with 1 child,
although there are two ElementPaths whose last ID the path is C's.
"""
act = Element.objects.get(name='Act 1')
act.add_parent(Element.objects.get(name='Volume 2'))
surface = Element.objects.get(name='Surface A')
with self.assertNumQueries(7):
response = self.client.get(
reverse('api:elements-parents', kwargs={'pk': str(surface.id)}),
data={'with_children_count': True},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertDictEqual(
{element['name']: element['children_count'] for element in response.json()['results']},
{'Act 1': 1},
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment