def test_create_xlsx_from_record_array_with_valid_xlsx_name( test_fixture_handler: FixtureHandler, test_temp_folder: Path, ): """ Description: ----------- We use a nested list of model records and use pandas to create an excel file. We store it temporarily in /tests/temp. Passed xlsx file name is equal to RecordsArray's string representation. Expectation: ----------- Test should create one file in /tests/temp, which gets deleted afterwards. Fails: ------ """ commodity_product_model_field_names = get_field_names( model_import_path="orbitaz.app.models.commodity_product", model_class_name_in_camel_case="CommodityProduct", ) test_file_path = test_temp_folder / "commodity_product.xlsx" test_fixture_handler.create_xlsx_from_record_array( array=CommodityProductArray, xlsx_fixtures_file_path=test_file_path, model_field_names=commodity_product_model_field_names, ) assert test_file_path.is_file() == True, "File not found" return
def test_create_dataframe_fixtures_from_xlsx_files( test_fixture_handler: FixtureHandler, test_temp_folder: Path): """ Description: ----------- We test wether the columns from excel files are getting parsed correctly into dataframe columns. Expectation: ----------- Dataframe gets parsed correctly and df columns matches the commodity product django model fields. Further, the fixture cache dict keys are equal to the assigned json fixtures folder path + model name. Fails: ------ """ commodity_product_model_field_names = get_field_names( model_import_path="orbitaz.app.models.commodity_product", model_class_name_in_camel_case="CommodityProduct", ) test_fixture_handler.create_xlsx_from_record_array( array=CommodityProductArray, xlsx_fixtures_file_path=test_temp_folder / "commodity_product.xlsx", model_field_names=commodity_product_model_field_names, ) test_dataframe_with_fixtures = ( test_fixture_handler.create_dataframe_with_fixtures_from_xlsx_files( xlsx_fixtures_file_path=test_temp_folder / "commodity_product.xlsx", json_fixtures_folder_path=test_temp_folder, )) assert (list(test_dataframe_with_fixtures.keys())[0] == test_temp_folder / "commodity_product.json" ), "Fixture cache keys did not get assigned properly" assert set(test_dataframe_with_fixtures[ test_temp_folder / "commodity_product.json"].columns) == set([ "commodity", "product", "vat", "source", "start_time", "granularity", "point_of_origin", "unit", ]), "Columns did not get parsed right from excel to dataframe" return
def test_dump_json_fixtures_from_blueprint( test_fixture_handler: FixtureHandler, test_temp_folder: Path): """ Description: ----------- We test wether the fixture file gets dumped correctly. Expectation: ----------- The temp json fixture folder contains a json file Fails: ------ """ commodity_product_model_field_names = get_field_names( model_import_path="orbitaz.app.models.commodity_product", model_class_name_in_camel_case="CommodityProduct", ) test_fixture_handler.create_xlsx_from_record_array( array=CommodityProductArray, xlsx_fixtures_file_path=test_temp_folder / "commodity_product.xlsx", model_field_names=commodity_product_model_field_names, ) test_fixtures_cache = ( test_fixture_handler.create_dataframe_with_fixtures_from_xlsx_files( xlsx_fixtures_file_path=test_temp_folder / "commodity_product.xlsx", json_fixtures_folder_path=test_temp_folder, )) test_fixtures_blueprint = test_fixture_handler.create_fixtures_blueprints( app_name="app", model_class_name_in_lower_case="CommodityProduct", fixtures_cache=test_fixtures_cache, ) test_fixture_handler.dump_json_fixtures_from_blueprint( fixtures_blueprint=test_fixtures_blueprint) test_json_fixtures_folder = test_temp_folder / "commodity_product.json" assert test_json_fixtures_folder.is_file() == True, "File not found" return
def test_get_model_field_names(): """ Description: ----------- Test wether the field names from commodity product database model getting received correctly. Expectation: ----------- Matches the following field names: [ "commodity", "product", "vat", "source", "start_time", "granularity", "point_of_origin", "unit", ] Fails: ------ If model field names changes or new fields get added. """ test_commodity_product_model_field_names = get_field_names( model_import_path="orbitaz.app.models.commodity_product", model_class_name_in_camel_case="CommodityProduct", ) assert test_commodity_product_model_field_names == [ "commodity", "product", "vat", "source", "start_time", "granularity", "point_of_origin", "unit", ] return
def setup_fixtures( app_name: str, fixture_source: str, model_name_in_snake_case: Path, ) -> None: # Fixture path for xlsx formatted fixtures xlsx_fixtures_file_name = ".".join([model_name_in_snake_case, "xlsx"]) # Fixture path for xlsx formatted fixtures xlsx_fixtures_file_path = FIXTURES_DIR_XLSX / ".".join( [model_name_in_snake_case, "xlsx"]) # Reformatted model name model_name_in_camel_case = reformat_snake_to_camel_case( model_name_in_snake_case) # Should be either "arrays" or "xlsx", otherwise code breaks is_fixture_source_valid(fixture_source=fixture_source) # If fixture source is not xlsx, parse the hard-coded model records # Or if excel file does not exist if "arrays" in fixture_source or is_file( xlsx_fixtures_file_path) == False: # We use this path to get a database class model and its field names model_import_path = ".".join([ "orbitaz.app.models", model_name_in_snake_case, ]) model_field_names = get_field_names( model_import_path=model_import_path, model_class_name_in_camel_case=model_name_in_camel_case, ) # Assure that the xlsx file names matches your model names. is_xlsx_file_name_in_array_class_name( array=CommodityProductArray, xlsx_fixtures_file_path=xlsx_fixtures_file_path, ) # Storing python lists with records as excel file FixtureHandler.create_xlsx_from_record_array( array=CommodityProductArray, xlsx_fixtures_file_path=xlsx_fixtures_file_path, model_field_names=model_field_names, ) logger.info( f"\nSuccessfully stored xlsx fixtures @files/fixtures/xlsx/commodity_products.xlsx" ) # Make sure you created a parseable xlsx file is_file(target_file_path=xlsx_fixtures_file_path) # Creating an intermediate storage for path and record information fixtures_cache = FixtureHandler.create_dataframe_with_fixtures_from_xlsx_files( xlsx_fixtures_file_path=xlsx_fixtures_file_path, json_fixtures_folder_path=FIXTURES_DIR_JSON, ) # Create a dict datastructure with model field data fixtures_blueprint = FixtureHandler.create_fixtures_blueprints( app_name=app_name, model_class_name_in_lower_case=model_name_in_snake_case.replace( "_", ""), fixtures_cache=fixtures_cache, ) # Dump model field data as json, which can be used as the actual fixture FixtureHandler.dump_json_fixtures_from_blueprint( fixtures_blueprint=fixtures_blueprint) logger.info( f"\nSuccessfully stored json fixtures @app/fixtures/{model_name_in_snake_case}.json" ) return