@@ -550,6 +550,37 @@ def test_tree_sort(self) -> None:
550550 " azure (ALPHABET) (children: 0)" ,
551551 ]
552552
553+ def test_descendant_counts (self ) -> None :
554+ """
555+ Test getting the descendant count on a taxonomy known to cause aggregation
556+ bugs unless the aggregations are correctly specified with distinct=True
557+
558+ https://docs.djangoproject.com/en/5.0/topics/db/aggregation/#combining-multiple-aggregations
559+ """
560+ taxonomy = api .create_taxonomy ("ESDC Subset" )
561+ api .add_tag_to_taxonomy (taxonomy , "Interests" ) # root tag
562+ api .add_tag_to_taxonomy (taxonomy , "Holland Codes" , parent_tag_value = "Interests" ) # child tag
563+ # Create the grandchild tag:
564+ g_tag = api .add_tag_to_taxonomy (taxonomy , "Interests - Holland Codes" , parent_tag_value = "Holland Codes" )
565+ # Create the 6 great-grandchild tags:
566+ api .add_tag_to_taxonomy (taxonomy , "Artistic" , parent_tag_value = g_tag .value )
567+ api .add_tag_to_taxonomy (taxonomy , "Conventional" , parent_tag_value = g_tag .value )
568+ api .add_tag_to_taxonomy (taxonomy , "Enterprising" , parent_tag_value = g_tag .value )
569+ api .add_tag_to_taxonomy (taxonomy , "Investigative" , parent_tag_value = g_tag .value )
570+ api .add_tag_to_taxonomy (taxonomy , "Realistic" , parent_tag_value = g_tag .value )
571+ api .add_tag_to_taxonomy (taxonomy , "Social" , parent_tag_value = g_tag .value )
572+
573+ result = pretty_format_tags (taxonomy .get_filtered_tags (depth = 1 , include_counts = True ))
574+ assert result == [
575+ "Interests (None) (used: 0, children: 1 + 7)" , # 1 child + 1 grandchild and 6 great grandchild tags = 8 total
576+ ]
577+ result2 = pretty_format_tags (taxonomy .get_filtered_tags (depth = None , include_counts = True ))
578+ assert result2 == [
579+ "Interests (None) (used: 0, children: 1 + 7)" ,
580+ " Holland Codes (Interests) (used: 0, children: 1 + 6)" ,
581+ " Interests - Holland Codes (Holland Codes) (used: 0, children: 6)" ,
582+ ]
583+
553584
554585class TestFilteredTagsFreeTextTaxonomy (TestCase ):
555586 """
0 commit comments