diff --git a/tests/openedx_tagging/core/tagging/import_export/test_template.py b/tests/openedx_tagging/core/tagging/import_export/test_template.py index 067a338c..c25c4b28 100644 --- a/tests/openedx_tagging/core/tagging/import_export/test_template.py +++ b/tests/openedx_tagging/core/tagging/import_export/test_template.py @@ -13,6 +13,7 @@ from openedx_tagging.core.tagging.import_export import api as import_api from .mixins import TestImportExportMixin +from ..utils import pretty_format_tags @ddt.ddt @@ -47,44 +48,36 @@ def test_import_template(self, template_file, parser_format): replace=True, ), import_api.get_last_import_log(self.taxonomy) - imported_tags = [ - { - "external_id": tag.external_id, - "value": tag.value, - "parent": tag.parent.external_id if tag.parent else None, - } - for tag in get_tags(self.taxonomy) - ] - assert imported_tags == [ - {'external_id': "ELECTRIC", 'parent': None, 'value': 'Electronic instruments'}, - {'external_id': 'PERCUSS', 'parent': None, 'value': 'Percussion instruments'}, - {'external_id': 'STRINGS', 'parent': None, 'value': 'String instruments'}, - {'external_id': 'WINDS', 'parent': None, 'value': 'Wind instruments'}, - {'external_id': 'SYNTH', 'parent': 'ELECTRIC', 'value': 'Synthesizer'}, - {'external_id': 'THERAMIN', 'parent': 'ELECTRIC', 'value': 'Theramin'}, - {'external_id': 'CHORD', 'parent': 'PERCUSS', 'value': 'Chordophone'}, - {'external_id': 'BELLS', 'parent': 'PERCUSS', 'value': 'Idiophone'}, - {'external_id': 'DRUMS', 'parent': 'PERCUSS', 'value': 'Membranophone'}, - {'external_id': 'BOW', 'parent': 'STRINGS', 'value': 'Bowed strings'}, - {'external_id': 'PLUCK', 'parent': 'STRINGS', 'value': 'Plucked strings'}, - {'external_id': 'BRASS', 'parent': 'WINDS', 'value': 'Brass'}, - {'external_id': 'WOODS', 'parent': 'WINDS', 'value': 'Woodwinds'}, - {'external_id': 'CELLO', 'parent': 'BOW', 'value': 'Cello'}, - {'external_id': 'VIOLIN', 'parent': 'BOW', 'value': 'Violin'}, - {'external_id': 'TRUMPET', 'parent': 'BRASS', 'value': 'Trumpet'}, - {'external_id': 'TUBA', 'parent': 'BRASS', 'value': 'Tuba'}, - {'external_id': 'PIANO', 'parent': 'CHORD', 'value': 'Piano'}, + assert pretty_format_tags(get_tags(self.taxonomy), external_id=True, usage_count=False) == [ + 'Electronic instruments (ELECTRIC) (None) (children: 2)', + ' Synthesizer (SYNTH) (Electronic instruments) (children: 0)', + ' Theramin (THERAMIN) (Electronic instruments) (children: 0)', + 'Percussion instruments (PERCUSS) (None) (children: 3)', + ' Chordophone (CHORD) (Percussion instruments) (children: 1)', + ' Piano (PIANO) (Chordophone) (children: 0)', + ' Idiophone (BELLS) (Percussion instruments) (children: 2)', + ' Celesta (CELESTA) (Idiophone) (children: 0)', + ' Hi-hat (HI-HAT) (Idiophone) (children: 0)', + ' Membranophone (DRUMS) (Percussion instruments) (children: 2)', + ' Cajón (CAJÓN) (Membranophone) (children: 1)', # This tag is present in the import files, but it will be omitted from get_tags() # because it sits beyond TAXONOMY_MAX_DEPTH. - # {'external_id': 'PYLE', 'parent': 'CAJÓN', 'value': 'Pyle Stringed Jam Cajón'}, - {'external_id': 'CELESTA', 'parent': 'BELLS', 'value': 'Celesta'}, - {'external_id': 'HI-HAT', 'parent': 'BELLS', 'value': 'Hi-hat'}, - {'external_id': 'CAJÓN', 'parent': 'DRUMS', 'value': 'Cajón'}, - {'external_id': 'TABLA', 'parent': 'DRUMS', 'value': 'Tabla'}, - {'external_id': 'BANJO', 'parent': 'PLUCK', 'value': 'Banjo'}, - {'external_id': 'HARP', 'parent': 'PLUCK', 'value': 'Harp'}, - {'external_id': 'MANDOLIN', 'parent': 'PLUCK', 'value': 'Mandolin'}, - {'external_id': 'CLARINET', 'parent': 'WOODS', 'value': 'Clarinet'}, - {'external_id': 'FLUTE', 'parent': 'WOODS', 'value': 'Flute'}, - {'external_id': 'OBOE', 'parent': 'WOODS', 'value': 'Oboe'}, + # Pyle Stringed Jam Cajón (PYLE) (Cajón) (children: 0) + ' Tabla (TABLA) (Membranophone) (children: 0)', + 'String instruments (STRINGS) (None) (children: 2)', + ' Bowed strings (BOW) (String instruments) (children: 2)', + ' Cello (CELLO) (Bowed strings) (children: 0)', + ' Violin (VIOLIN) (Bowed strings) (children: 0)', + ' Plucked strings (PLUCK) (String instruments) (children: 3)', + ' Banjo (BANJO) (Plucked strings) (children: 0)', + ' Harp (HARP) (Plucked strings) (children: 0)', + ' Mandolin (MANDOLIN) (Plucked strings) (children: 0)', + 'Wind instruments (WINDS) (None) (children: 2)', + ' Brass (BRASS) (Wind instruments) (children: 2)', + ' Trumpet (TRUMPET) (Brass) (children: 0)', + ' Tuba (TUBA) (Brass) (children: 0)', + ' Woodwinds (WOODS) (Wind instruments) (children: 3)', + ' Clarinet (CLARINET) (Woodwinds) (children: 0)', + ' Flute (FLUTE) (Woodwinds) (children: 0)', + ' Oboe (OBOE) (Woodwinds) (children: 0)', ] diff --git a/tests/openedx_tagging/core/tagging/test_models.py b/tests/openedx_tagging/core/tagging/test_models.py index af2c812b..c3ff48ae 100644 --- a/tests/openedx_tagging/core/tagging/test_models.py +++ b/tests/openedx_tagging/core/tagging/test_models.py @@ -13,6 +13,7 @@ from openedx_tagging.core.tagging import api from openedx_tagging.core.tagging.models import LanguageTaxonomy, ObjectTag, Tag, Taxonomy +from .utils import pretty_format_tags def get_tag(value): @@ -316,22 +317,11 @@ def test_depth_1_queries(self) -> None: ################## - @staticmethod - def _pretty_format_result(result) -> list[str]: - """ - Format a result to be more human readable. - """ - return [ - f"{t['depth'] * ' '}{t['value']} ({t['parent_value']}) " + - f"(used: {t['usage_count']}, children: {t['child_count']})" - for t in result - ] - def test_get_all(self) -> None: """ Test getting all of the tags in the taxonomy, using get_filtered_tags() """ - result = self._pretty_format_result(self.taxonomy.get_filtered_tags()) + result = pretty_format_tags(self.taxonomy.get_filtered_tags()) assert result == [ "Archaea (None) (used: 0, children: 3)", " DPANN (Archaea) (used: 0, children: 0)", @@ -360,7 +350,7 @@ def test_search(self) -> None: Search the whole taxonomy (up to max depth) for a given term. Should return all tags that match the term as well as their ancestors. """ - result = self._pretty_format_result(self.taxonomy.get_filtered_tags(search_term="ARCH")) + result = pretty_format_tags(self.taxonomy.get_filtered_tags(search_term="ARCH")) assert result == [ "Archaea (None) (used: 0, children: 3)", # Matches the value of this root tag, ARCHaea " Euryarchaeida (Archaea) (used: 0, children: 0)", # Matches the value of this child tag @@ -374,7 +364,7 @@ def test_search_2(self) -> None: Another search test, that matches a tag deeper in the taxonomy to check that all its ancestors are returned by the search. """ - result = self._pretty_format_result(self.taxonomy.get_filtered_tags(search_term="chordata")) + result = pretty_format_tags(self.taxonomy.get_filtered_tags(search_term="chordata")) assert result == [ "Eukaryota (None) (used: 0, children: 5)", " Animalia (Eukaryota) (used: 0, children: 7)", @@ -432,14 +422,14 @@ def test_usage_count(self) -> None: api.tag_object(object_id="obj03", taxonomy=self.taxonomy, tags=["Bacteria"]) api.tag_object(object_id="obj04", taxonomy=self.taxonomy, tags=["Eubacteria"]) # Now the API should reflect these usage counts: - result = self._pretty_format_result(self.taxonomy.get_filtered_tags(search_term="bacteria")) + result = pretty_format_tags(self.taxonomy.get_filtered_tags(search_term="bacteria")) assert result == [ "Bacteria (None) (used: 3, children: 2)", " Archaebacteria (Bacteria) (used: 0, children: 0)", " Eubacteria (Bacteria) (used: 1, children: 0)", ] # Same with depth=1, which uses a different query internally: - result1 = self._pretty_format_result(self.taxonomy.get_filtered_tags(search_term="bacteria", depth=1)) + result1 = pretty_format_tags(self.taxonomy.get_filtered_tags(search_term="bacteria", depth=1)) assert result1 == [ "Bacteria (None) (used: 3, children: 2)", ] diff --git a/tests/openedx_tagging/core/tagging/utils.py b/tests/openedx_tagging/core/tagging/utils.py new file mode 100644 index 00000000..fb24187d --- /dev/null +++ b/tests/openedx_tagging/core/tagging/utils.py @@ -0,0 +1,24 @@ +""" +Useful utilities for testing tagging and taxonomy code. +""" + + +def pretty_format_tags(result, parent=True, external_id=False, usage_count=True) -> list[str]: + """ + Format the result of get_filtered_tags() to be more human readable. + + Also works with other wrappers around get_filtered_tags, like api.get_tags() + """ + pretty_results = [] + for t in result: + line = f"{t['depth'] * ' '}{t['value']} " + if external_id: + line += f"({t['external_id']}) " + if parent: + line += f"({t['parent_value']}) " + line += "(" + if usage_count: + line += f"used: {t.get('usage_count', '?')}, " + line += f"children: {t['child_count']})" + pretty_results.append(line) + return pretty_results