Skip to content

Commit

Permalink
Convert test main script in algorithm_utils to unit test (#3864)
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisrall authored Jan 8, 2024
1 parent cfecf36 commit e55ad26
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
11 changes: 0 additions & 11 deletions ludwig/utils/algorithms_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import logging

from ludwig.constants import TIED

logger = logging.getLogger(__name__)


def topological_sort(graph_unsorted):
"""Repeatedly go through all of the nodes in the graph, moving each of the nodes that has all its edges
Expand Down Expand Up @@ -86,10 +82,3 @@ def topological_sort_feature_dependencies(features):
dependencies_graph[feature["name"]] = dependencies
output_features_dict[feature["name"]] = feature
return [output_features_dict[node[0]] for node in topological_sort(dependencies_graph)]


if __name__ == "__main__":
graph_unsorted = [(2, []), (5, [11]), (11, [2, 9, 10]), (7, [11, 8]), (9, []), (10, []), (8, [9]), (3, [10, 8])]
logger.info(topological_sort(graph_unsorted))
graph_unsorted = [("macro", ["action", "contact_type"]), ("contact_type", None), ("action", ["contact_type"])]
logger.info(topological_sort(graph_unsorted))
20 changes: 20 additions & 0 deletions tests/ludwig/utils/test_algorithm_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest

from ludwig.utils.algorithms_utils import topological_sort


@pytest.mark.parametrize(
"unsorted,sorted",
[
(
[(2, []), (5, [11]), (11, [2, 9, 10]), (7, [11, 8]), (9, []), (10, []), (8, [9]), (3, [10, 8])],
[(2, []), (9, []), (10, []), (8, [9]), (3, [10, 8]), (11, [2, 9, 10]), (7, [11, 8]), (5, [11])],
),
(
[("macro", ["action", "contact_type"]), ("contact_type", None), ("action", ["contact_type"])],
[("contact_type", []), ("action", ["contact_type"]), ("macro", ["action", "contact_type"])],
),
],
)
def test_topological_sort(unsorted: list, sorted: list) -> None:
assert topological_sort(unsorted) == sorted

0 comments on commit e55ad26

Please sign in to comment.