def test_type_check_boolean(session): if session.bind.name == "postgresql": pytest.skip("type checks not working on postgres") factories.GlobalSettingsFactory(use_1d_flow=True) factories.GlobalSettingsFactory(use_1d_flow=1) # factories.GlobalSettingsFactory(use_1d_flow='true') # factories.GlobalSettingsFactory(use_1d_flow='1') # factories.GlobalSettingsFactory(use_1d_flow=1.0) type_check = TypeCheck(models.GlobalSetting.use_1d_flow) invalid_rows = type_check.get_invalid(session) assert len(invalid_rows) == 0
def test_file_exists_check_filesystem_ok(session, tmp_path): (tmp_path / "somefile").touch() factories.GlobalSettingsFactory(dem_file="somefile") session.model_checker_context.base_path = tmp_path check = FileExistsCheck(column=models.GlobalSetting.dem_file) invalid_rows = check.get_invalid(session) assert len(invalid_rows) == 0
def test_length_geom_linestring_in_4326(session): if session.bind.name == "postgresql": pytest.skip( "Postgres already has a constrain that checks on the length") factories.GlobalSettingsFactory(epsg_code=28992) channel_too_short = factories.ChannelFactory( the_geom="SRID=4326;LINESTRING(" "-0.38222938832999598 -0.13872236685816669, " "-0.38222930900909202 -0.13872236685816669)", ) factories.ChannelFactory(the_geom="SRID=4326;LINESTRING(" "-0.38222938468305784 -0.13872235682908687, " "-0.38222931083256106 -0.13872235591735235, " "-0.38222930992082654 -0.13872207236791409, " "-0.38222940929989008 -0.13872235591735235)", ) q = Query(models.Channel).filter( geo_func.ST_Length( geo_func.ST_Transform( models.Channel.the_geom, Query(models.GlobalSetting.epsg_code).limit(1))) < 0.05) check_length_linestring = QueryCheck( column=models.Channel.the_geom, invalid=q, message= "Length of the v2_channel is too short, should be at least 0.05m", ) errors = check_length_linestring.get_invalid(session) assert len(errors) == 1 assert errors[0].id == channel_too_short.id
def test_global_settings_use_1d_flow_and_no_1d_elements(session): factories.GlobalSettingsFactory(use_1d_flow=1) factories.GlobalSettingsFactory(use_1d_flow=0) query_1d_nodes_and_no_use_1d_flow = Query(models.GlobalSetting).filter( models.GlobalSetting.use_1d_flow == False, Query(func.count(models.ConnectionNode.id) > 0).label("1d_count"), ) check_use_1d_flow_has_1d = QueryCheck( column=models.GlobalSetting.use_1d_flow, invalid=query_1d_nodes_and_no_use_1d_flow, message= "GlobalSettings.use_1d_flow must be set to True when there are 1d " "elements", ) errors = check_use_1d_flow_has_1d.get_invalid(session) assert len(errors) == 0
def test_fk_check_both_null(session): factories.GlobalSettingsFactory(control_group_id=None) assert session.query(models.GlobalSetting).first().id is not None assert session.query( models.GlobalSetting.control_group_id).scalar() is None assert session.query(models.ControlGroup.id).scalar() is None fk_check = ForeignKeyCheck(models.ControlGroup.id, models.GlobalSetting.control_group_id) invalid_rows = fk_check.get_invalid(session) assert len(invalid_rows) == 0
def test_global_settings_start_date(session): if session.bind.name == "postgresql": pytest.skip("Can't insert wrong datatype in postgres") factories.GlobalSettingsFactory(start_date="1991-08-27") wrong_start_date = factories.GlobalSettingsFactory( start_date="asdf18:00:00") check_start_date = QueryCheck( column=models.GlobalSetting.start_date, invalid=Query(models.GlobalSetting).filter( func.date(models.GlobalSetting.start_date) == None, models.GlobalSetting.start_date != None, ), message="GlobalSettings.start_date is an invalid, make sure it has the " "following format: 'YYYY-MM-DD'", ) errors = check_start_date.get_invalid(session) assert len(errors) == 1 assert errors[0].id == wrong_start_date.id
def test_conditional_checks(session): global_settings1 = factories.GlobalSettingsFactory( dem_obstacle_detection=True, dem_obstacle_height=-5) factories.GlobalSettingsFactory(dem_obstacle_detection=False, dem_obstacle_height=-5) query = Query(models.GlobalSetting).filter( models.GlobalSetting.dem_obstacle_height <= 0, models.GlobalSetting.dem_obstacle_detection == True, ) conditional_range_check_to_query_check = QueryCheck( column=models.GlobalSetting.dem_obstacle_height, invalid=query, message="GlobalSetting.dem_obstacle_height should be larger than 0 " "when GlobalSetting.dem_obstacle_height is True.", ) invalids_querycheck = conditional_range_check_to_query_check.get_invalid( session) assert len(invalids_querycheck) == 1 assert invalids_querycheck[0].id == global_settings1.id
def test_file_exists_check_skip(session): # no context, no check, no invalid records factories.GlobalSettingsFactory(dem_file="some/file") check = FileExistsCheck(column=models.GlobalSetting.dem_file) invalid_rows = check.get_invalid(session) assert len(invalid_rows) == 0
def test_file_exists_check_context_ignore(session): factories.GlobalSettingsFactory(dem_file="") session.model_checker_context.available_rasters = {"frict_coef_file"} check = FileExistsCheck(column=models.GlobalSetting.dem_file) invalid_rows = check.get_invalid(session) assert len(invalid_rows) == 0