예제 #1
0
def test_two_times(mocker):
	db = DataAccessHelper.createdb('postgresql://*****:*****@localhost:5432/det_autoup')
	DataAccessHelper.add_class_groups(db)
	susdata = [{'tablename': 'csAmz_150km', 
				'shpname': 'csAmz_150km_epsg_4326', 
				'as_attribute_name': 'id'}]
	for sudata in susdata:
		DataAccessHelper.add_spatial_unit(db, sudata['tablename'], sudata['shpname'])
	startdate = date(2021, 2, 28)
	enddate = date(2021, 1, 1)
	DataAccessHelper.determine_risk_indicators(db, startdate, enddate)
	deter_repo = DeterRepository()
	deter_url = 'postgresql://*****:*****@localhost:5432/DETER-B'
	date_now = datetime.now()
	yesterday = (date_now - timedelta(days=1))	
	DataAccessHelper.del_deter_data(deter_url, date_now)  # clean if last test fail
	DataAccessHelper.del_deter_data(deter_url, yesterday)  # clean if last test fail
	rirepo = RiskIndicatorsRepository(susdata[0]['tablename'], susdata[0]['as_attribute_name'], db)	
	indicators_1 = rirepo.list()
	assert len(indicators_1) == 357
	assert indicators_1[0].date.isoformat() == '2021-02-28'
	assert indicators_1[0].percentage == 0.0004478673808316384
	assert indicators_1[0].area == 0.10077016068752663
	assert indicators_1[0].classname == 'DS'
	uc = DeterDailyUpdate(deter_repo, 1)
	mocker.patch.object(DeterDailyUpdate, '_get_enddate', return_value=date(2021, 1, 1))
	uc.execute(db)	
	assert uc.last_update() == '2021-02-28'
	DataAccessHelper.add_deter_data(deter_url, 5, yesterday)
	while uc.last_update() == startdate.isoformat():
		time.sleep(1)		
	assert uc.last_update() == yesterday.date().isoformat()
	indicators_2 = rirepo.list()
	assert len(indicators_2) == 358	
	yesterday_indicator = get_newest_indicator(indicators_2, yesterday.date())
	assert yesterday_indicator.date.isoformat() == yesterday.date().isoformat()
	assert round(yesterday_indicator.percentage, 5) == round(0.0028141977493854964, 5)
	assert round(yesterday_indicator.area, 5) == 0.63319
	assert yesterday_indicator.classname == 'DS'	
	DataAccessHelper.add_deter_data(deter_url, 5, date_now)
	while uc.last_update() == yesterday.date().isoformat():
		time.sleep(1)	
	assert uc.last_update() == date_now.date().isoformat()
	indicators_3 = rirepo.list()
	assert len(indicators_3) == 359	
	now_indicator = get_newest_indicator(indicators_3, date_now.date())
	assert now_indicator.date.isoformat() == date_now.date().isoformat()
	assert round(now_indicator.percentage, 5) == round(0.0028141977493854964, 5)
	assert round(now_indicator.area, 5) == 0.63319
	assert now_indicator.classname == 'DS'				
	uc.terminate()
	DataAccessHelper.del_deter_data(deter_url, yesterday)
	DataAccessHelper.del_deter_data(deter_url, date_now)
	db.drop()
def test_uc_historical():
    db = setdb(
        'postgresql://*****:*****@localhost:5432/det_risk_uc_historic')
    set_class_groups(db)
    deter_repo = DeterRepository()
    deter_hist = DeterHistoricalRepository()
    units_repo = SpatialUnitInfoRepository(db)
    units = units_repo.list()
    sutablename = units[0].dataname
    as_attribute_name = units[0].as_attribute_name
    surepo = SpatialUnitDynamicMapperFactory.instance()\
       .create_spatial_unit(sutablename, as_attribute_name)
    su = surepo.get()
    startdate = datetime.date(2020, 2, 1)
    enddate = datetime.date(2019, 11, 1)
    groups_repo = DeterClassGroupRepository(db)
    class_groups = groups_repo.list()
    uc = DetermineRiskIndicators(su, deter_repo, deter_hist, class_groups,
                                 startdate, enddate)
    model_indicators = uc.execute()
    rirepo = RiskIndicatorsRepository(sutablename, as_attribute_name, db)
    rirepo.save(model_indicators)
    indicators = rirepo.list()
    indicators_per_feature = {}
    expected_total_per_feature = determine_risk_indicators_results.total_per_feature_historical
    for ind in indicators:
        if ind.feature.id not in indicators_per_feature:
            indicators_per_feature[ind.feature.id] = 0
        indicators_per_feature[ind.feature.id] += ind.percentage
    assert len(model_indicators) == len(indicators) == 1350
    assert (len(indicators_per_feature) == len(expected_total_per_feature) ==
            177)
    # for i in indicators_per_feature:
    # 	print(f'{{\'id\': {i}, \'percentage\': {indicators_per_feature[i]}}},')
    for i in range(len(expected_total_per_feature)):
        assert expected_total_per_feature[i]['id'] in indicators_per_feature
        assert (round(expected_total_per_feature[i]['percentage'], 5) == round(
            indicators_per_feature[expected_total_per_feature[i]['id']], 5))
    # for i in indicators:
    # 	print(f'{{\'id\': {i.feature.id}, \'percentage\': {i.percentage},'
    # 			+ f'\'classname\': \'{i.classname}\', \'date\': \'{i.date}\'}},')
    expected_classname = determine_risk_indicators_results.deter_historical
    gp = Geoprocessing()
    for i in range(len(indicators)):
        assert expected_classname[i]['id'] == indicators[i].feature.id
        assert round(expected_classname[i]['percentage'],
                     5) == round(indicators[i].percentage, 5)
        assert expected_classname[i]['date'] == str(indicators[i].date)
        assert int(indicators[i].area * 100 / indicators[i].percentage)\
          == int(gp.degrees_to_km2(indicators[i].feature.geom.area))
    db.drop()