|
12 | 12 | import textwrap |
13 | 13 | import unittest |
14 | 14 | import warnings |
| 15 | +from copy import copy |
15 | 16 | from pathlib import Path |
16 | 17 | from unittest import mock |
17 | 18 |
|
@@ -593,6 +594,119 @@ def test_api_scenario_generation(self): |
593 | 594 | # Restore the default runner |
594 | 595 | kh.set_runner(default_runner) |
595 | 596 |
|
| 597 | + def test_data_path_deprecation_in_api_method(self): |
| 598 | + """Tests if core.api issues deprecation warning when legacy data |
| 599 | + paths are used for secondary tables |
| 600 | + """ |
| 601 | + # Set the root directory of these tests |
| 602 | + test_resources_dir = os.path.join(resources_dir(), "scenario_generation", "api") |
| 603 | + |
| 604 | + # Use the test runner that only compares the scenarios |
| 605 | + default_runner = kh.get_runner() |
| 606 | + test_runner = ScenarioWriterRunner(self, test_resources_dir) |
| 607 | + kh.set_runner(test_runner) |
| 608 | + |
| 609 | + # Obtain mock arguments for each API call |
| 610 | + method_test_args = self._build_mock_api_method_parameters() |
| 611 | + |
| 612 | + # Define legacy additional data tables path |
| 613 | + str_legacy_additional_data_tables = { |
| 614 | + "Customer`Services": "ServicesBidon.csv", |
| 615 | + "Customer`Services`Usages": "UsagesBidon.csv", |
| 616 | + "Customer`Address": "AddressBidon.csv", |
| 617 | + } |
| 618 | + |
| 619 | + # Test for each dataset mock parameters |
| 620 | + for method_name, method_full_args in method_test_args.items(): |
| 621 | + # Use bytes for deploy_model's additional_data_tables |
| 622 | + if method_name == "deploy_model": |
| 623 | + legacy_additional_data_tables = { |
| 624 | + bytes(key, encoding="ascii"): bytes(value, encoding="ascii") |
| 625 | + for key, value in str_legacy_additional_data_tables.items() |
| 626 | + } |
| 627 | + else: |
| 628 | + legacy_additional_data_tables = str_legacy_additional_data_tables |
| 629 | + # Set the runners test name |
| 630 | + test_runner.test_name = method_name |
| 631 | + |
| 632 | + # Clean the directory for this method's tests |
| 633 | + cleanup_dir(test_runner.output_scenario_dir, "*/output/*._kh", verbose=True) |
| 634 | + for dataset, dataset_method_args in method_full_args.items(): |
| 635 | + # Test only for the Customer dataset |
| 636 | + if dataset != "Customer": |
| 637 | + continue |
| 638 | + |
| 639 | + test_runner.subtest_name = dataset |
| 640 | + with self.subTest(method=method_name): |
| 641 | + # Get the API function and its args and kwargs |
| 642 | + method = getattr(kh, method_name) |
| 643 | + dataset_args = dataset_method_args["args"] |
| 644 | + dataset_kwargs = dataset_method_args["kwargs"] |
| 645 | + |
| 646 | + # Skip the test if `additional_data_tables` is not an |
| 647 | + # API call kwarg |
| 648 | + if "additional_data_tables" not in dataset_kwargs: |
| 649 | + continue |
| 650 | + |
| 651 | + # Store current additional data_tables |
| 652 | + current_additional_data_tables = copy( |
| 653 | + dataset_kwargs["additional_data_tables"] |
| 654 | + ) |
| 655 | + |
| 656 | + # Update the `additional_data_tables` kwargs to use |
| 657 | + # legacy paths |
| 658 | + dataset_kwargs["additional_data_tables"] = copy( |
| 659 | + legacy_additional_data_tables |
| 660 | + ) |
| 661 | + |
| 662 | + # Test that using legacy paths entails a deprecation warning |
| 663 | + with warnings.catch_warnings(record=True) as warning_list: |
| 664 | + method(*dataset_args, **dataset_kwargs) |
| 665 | + |
| 666 | + # Build current-legacy data path map |
| 667 | + legacy_to_current_data_paths = {} |
| 668 | + for ( |
| 669 | + data_path, |
| 670 | + data_file_path, |
| 671 | + ) in current_additional_data_tables.items(): |
| 672 | + for ( |
| 673 | + leg_data_path, |
| 674 | + leg_data_file_path, |
| 675 | + ) in legacy_additional_data_tables.items(): |
| 676 | + if leg_data_file_path == data_file_path: |
| 677 | + legacy_to_current_data_paths[leg_data_path] = data_path |
| 678 | + break |
| 679 | + |
| 680 | + # Check the warning message |
| 681 | + self.assertEqual( |
| 682 | + len(warning_list), len(legacy_additional_data_tables) |
| 683 | + ) |
| 684 | + warning = warning_list[0] |
| 685 | + for warning in warning_list: |
| 686 | + self.assertTrue(issubclass(warning.category, UserWarning)) |
| 687 | + warning_message = warning.message |
| 688 | + self.assertEqual(len(warning_message.args), 1) |
| 689 | + message = warning_message.args[0] |
| 690 | + self.assertTrue( |
| 691 | + "'`'-based dictionary data path" in message |
| 692 | + and "deprecated" in message |
| 693 | + ) |
| 694 | + |
| 695 | + # Check legacy data path is replaced with the current |
| 696 | + # data path |
| 697 | + for legacy_data_path in legacy_additional_data_tables: |
| 698 | + expected_legacy_data_path = legacy_to_current_data_paths[ |
| 699 | + legacy_data_path |
| 700 | + ] |
| 701 | + if f"'{legacy_data_path}'" in message: |
| 702 | + self.assertTrue( |
| 703 | + f"'{expected_legacy_data_path}'" in message |
| 704 | + ) |
| 705 | + break |
| 706 | + |
| 707 | + # Restore the default runner |
| 708 | + kh.set_runner(default_runner) |
| 709 | + |
596 | 710 | def test_unknown_argument_in_api_method(self): |
597 | 711 | """Tests if core.api raises ValueError when an unknown argument is passed""" |
598 | 712 | # Obtain mock arguments for each API call |
|
0 commit comments