def cog(ctx, check=False): """ Executes cog on _internal/alfacase/schema.py to generate the schema for strictyaml. """ ctx.run(command=f"cog -rc {schema_file_path()}", warn=True) from alfasim_sdk._internal.alfacase.generate_schema import ( get_all_classes_that_needs_schema, ) from alfasim_sdk._internal.alfacase.case_description import CaseDescription from alfasim_sdk._internal.alfacase.generate_case_description_docstring import ( generate_definition, ) for class_ in get_all_classes_that_needs_schema(CaseDescription): output = generate_definition(class_) Path(alfacase_definitions_path() / f"{class_.__name__}.txt").write_text(output) from alfasim_sdk._internal.alfacase.generate_case_description_docstring import ( generate_list_of_units, CATEGORIES_USED_ON_DESCRIPTION, ) for category in CATEGORIES_USED_ON_DESCRIPTION: output = generate_list_of_units(category) category_for_path = category.replace(" ", "_") Path( alfacase_definitions_path() / f"list_of_unit_for_{category_for_path}.txt" ).write_text(output) if check: check_cog(ctx)
def cog(ctx, check=False): """ Executes cog - on `_internal/alfacase/case_description.py`; - on `_internal/alfacase/schema.py` to generate the schema for strictyaml; - generate some documentation files; """ ctx.run(command=f"cog -rc {case_description_source_file_path()}", warn=True) ctx.run(command=f"cog -rc {schema_file_path()}", warn=True) from alfasim_sdk._internal.alfacase.generate_schema import ( get_all_classes_that_needs_schema, ) from alfasim_sdk._internal.alfacase.case_description import CaseDescription from alfasim_sdk._internal.alfacase.generate_case_description_docstring import ( generate_definition, ) alfacase_definitions_path().mkdir(parents=True, exist_ok=True) alfacase_definitions_cogged_files = set() def write_alfacase_definitions_cogged_file(filename, content): alfacase_definitions_cogged_files.add(filename) Path(alfacase_definitions_path() / filename).write_text(content) for class_ in get_all_classes_that_needs_schema(CaseDescription): output = generate_definition(class_) write_alfacase_definitions_cogged_file(f"{class_.__name__}.txt", output) from alfasim_sdk._internal.alfacase.generate_case_description_docstring import ( generate_list_of_units, CATEGORIES_USED_ON_DESCRIPTION, ) for category in CATEGORIES_USED_ON_DESCRIPTION: output = generate_list_of_units(category) category_for_path = category.replace(" ", "_") write_alfacase_definitions_cogged_file( f"list_of_unit_for_{category_for_path}.txt", output) for filepath in alfacase_definitions_path().glob("*.txt"): filename = filepath.name if filename not in alfacase_definitions_cogged_files: print(f"Removing {filepath}") filepath.unlink() if check: check_cog(ctx)
def test_generate_schema_for_all_cases(file_regression): """ This check helps ensure that the YAMLSchema is being changed consciously because whenever CaseDescription is changed, the schema will change as well. If this test fails, make sure that the following steps were made: - Update the CaseDescription defined on alfasim_core.common_testing.filled_case_descriptions !! filled_case_descriptions helps you to find errors on the YAML import !! After that, you can update this test with --force-regen Note: Currently the schema doesn't have a version number, but in the future whenever this tests break, the version the number will be necessary to be changed and the changelog updated as well. Dev note: This test is also a facility to help debug the output generate from COG """ list_of_classes_that_needs_schema = get_all_classes_that_needs_schema( CaseDescription ) output = [ generate_alfacase_schema(class_) for class_ in list_of_classes_that_needs_schema ] file_regression.check("\n".join(output), encoding="utf-8")
import pytest from alfasim_sdk._internal.alfacase import case_description from alfasim_sdk._internal.alfacase.generate_case_description_docstring import ( generate_definition, ) from alfasim_sdk._internal.alfacase.generate_schema import ( get_all_classes_that_needs_schema, ) ALL_CLASSES_THAT_NEEDS_SCHEMA = get_all_classes_that_needs_schema( case_description.CaseDescription) # # Useful for debugging # ALL_CLASSES_THAT_NEEDS_SCHEMA = [case_description.PositionalPipeTrendDescription] @pytest.mark.parametrize("class_", ALL_CLASSES_THAT_NEEDS_SCHEMA) def test_generate_case_description_docstring(class_, file_regression): output = generate_definition(class_) file_regression.check(output, encoding="utf-8")
def test_get_cases_class(): """ Smoke test to ensure that the descriptions classes didn't change the name. If the is broken due to renaming or removal be aware of backward compatibility with ALFAcase files. If only Descriptions are being added, you don't need to worry and just update the list above. """ list_of_classes_that_needs_schema = get_all_classes_that_needs_schema( CaseDescription ) obtained = {class_.__name__ for class_ in list_of_classes_that_needs_schema} expected = { "AnnulusDescription", "BipDescription", "CaseDescription", "CaseOutputDescription", "CasingDescription", "CasingSectionDescription", "CompositionDescription", "CompressorEquipmentDescription", "CompressorPressureTableDescription", "CvTableDescription", "EnvironmentDescription", "EnvironmentPropertyDescription", "EquipmentDescription", "FluidDescription", "FormationDescription", "FormationLayerDescription", "GasLiftValveEquipmentDescription", "HeatSourceEquipmentDescription", "HeavyComponentDescription", "IPRCurveDescription", "IPRModelsDescription", "InitialConditionsDescription", "InitialPressuresDescription", "InitialTemperaturesDescription", "InitialTracersMassFractionsDescription", "InitialVelocitiesDescription", "InitialVolumeFractionsDescription", "InternalNodePropertiesDescription", "LengthAndElevationDescription", "LightComponentDescription", "LinearIPRDescription", "MassSourceEquipmentDescription", "MassSourceNodePropertiesDescription", "MaterialDescription", "NodeDescription", "NumericalOptionsDescription", "OpenHoleDescription", "OpeningCurveDescription", "PackerDescription", "PhysicsDescription", "PipeDescription", "PipeSegmentsDescription", "PressureContainerDescription", "PressureNodePropertiesDescription", "ProfileDescription", "ProfileOutputDescription", "PumpEquipmentDescription", "PvtModelCompositionalDescription", "PvtModelCorrelationDescription", "PvtModelsDescription", "ReferencedPressureContainerDescription", "ReferencedTemperaturesContainerDescription", "ReferencedTracersMassFractionsContainerDescription", "ReferencedVelocitiesContainerDescription", "ReferencedVolumeFractionsContainerDescription", "ReservoirInflowEquipmentDescription", "SeparatorNodePropertiesDescription", "SpeedCurveDescription", "TableIPRDescription", "TablePumpDescription", "TemperaturesContainerDescription", "TimeOptionsDescription", "TracerModelConstantCoefficientsDescription", "TracersDescription", "TracersMassFractionsContainerDescription", "TrendOutputDescription", "TubingDescription", "ValveEquipmentDescription", "VelocitiesContainerDescription", "VolumeFractionsContainerDescription", "WallDescription", "WallLayerDescription", "WellDescription", "XAndYDescription", } assert ( obtained == expected ), f"""