def delete_selected_range(self, table_name): """ Deletes the current selected range from the database from w_levels_logger :return: De """ current_loaded_obsid = self.obsid selected_obsid = self.load_obsid_and_init() if current_loaded_obsid != selected_obsid: utils.pop_up_info(ru(QCoreApplication.translate('Calibrlogger', "Error!\n The obsid selection has been changed but the plot has not been updated. No deletion done.\nUpdating plot."))) self.update_plot() return elif selected_obsid is None: utils.pop_up_info(ru(QCoreApplication.translate('Calibrlogger', "Error!\n No obsid was selected. No deletion done.\nUpdating plot."))) self.update_plot() return fr_d_t = str((self.FromDateTime.dateTime().toPyDateTime() - datetime.datetime(1970,1,1)).total_seconds()) to_d_t = str((self.ToDateTime.dateTime().toPyDateTime() - datetime.datetime(1970,1,1)).total_seconds()) sql_list = [] sql_list.append(r"""DELETE FROM "%s" """%table_name) sql_list.append(r"""WHERE obsid = '%s' """%selected_obsid) # This %s is db formatting for seconds. It is not used as python formatting! sql_list.append(r"""AND CAST(strftime('%s', date_time) AS NUMERIC) """) sql_list.append(r""" > '%s' """%fr_d_t) # This %s is db formatting for seconds. It is not used as python formatting! sql_list.append(r"""AND CAST(strftime('%s', date_time) AS NUMERIC) """) sql_list.append(r""" < '%s' """%to_d_t) sql = ''.join(sql_list) really_delete = utils.Askuser("YesNo", ru(QCoreApplication.translate('Calibrlogger', "Do you want to delete the period %s to %s for obsid %s from table %s?"))%(str(self.FromDateTime.dateTime().toPyDateTime()), str(self.ToDateTime.dateTime().toPyDateTime()), selected_obsid, table_name)).result if really_delete: utils.start_waiting_cursor() db_utils.sql_alter_db(sql) utils.stop_waiting_cursor() self.update_plot()
def calculateaveflow(self): utils.start_waiting_cursor() date_from = self.FromDateTime.dateTime().toPyDateTime() date_to = self.ToDateTime.dateTime().toPyDateTime() #Identify distinct set of obsid and instrumentid with Accvol-data and within the user-defined date_time-interval: sql= """SELECT DISTINCT obsid, instrumentid FROM (SELECT * FROM w_flow WHERE flowtype = 'Accvol' AND date_time >= '%s' AND date_time <= '%s' AND obsid IN (%s))"""%(date_from,date_to, utils.sql_unicode_list(self.observations)) #utils.pop_up_info(sql)#debug uniqueset = db_utils.sql_load_fr_db(sql)[1] # The unique set of obsid and instrumentid is kept in uniqueset negativeflow = False for pyobsid, pyinstrumentid in uniqueset: sql= """select date_time, reading from w_flow where flowtype = 'Accvol' and obsid='%s' and instrumentid='%s' and date_time >='%s' and date_time <='%s' order by date_time"""%(pyobsid,pyinstrumentid,date_from,date_to) recs = db_utils.sql_load_fr_db(sql)[1] """Transform data to a numpy.recarray""" My_format = [('date_time', datetime.datetime), ('values', float)] #Define format with help from function datetime table = np.array(recs, dtype=My_format) #NDARRAY table2=table.view(np.recarray) # RECARRAY Makes the two columns into callable objects, i.e. write table2.values for j, row in enumerate(table2):#This is where Aveflow is calculated for each obs and also written to db if j>0:#first row is "start-value" for Accvol and there is no Aveflow to be calculated Volume = (table2.values[j] - table2.values[j-1])*1000#convert to L since Accvol is supposed to be in m3 """ Get help from function datestr2num to get date and time into float""" DeltaTime = 24*3600*(datestr2num(table2.date_time[j]) - datestr2num(table2.date_time[j-1]))#convert to seconds since numtime is days Aveflow = Volume/DeltaTime#L/s if Aveflow<0: negativeflow = True sql = """insert or ignore into w_flow(obsid,instrumentid,flowtype,date_time,reading,unit) values('%s','%s','Aveflow','%s','%s','l/s')"""%(pyobsid,pyinstrumentid,table2.date_time[j],Aveflow) db_utils.sql_alter_db(sql) if negativeflow: utils.MessagebarAndLog.info(bar_msg=ru(QCoreApplication.translate('Calcave', "Please notice that negative flow was encountered."))) utils.stop_waiting_cursor() self.close()
def adjust_trend_func(self): obsid = self.load_obsid_and_init() if obsid is None: return None data = {'obsid': obsid, 'adjust_start_date': long_dateformat(self.FromDateTime.dateTime().toPyDateTime()), 'adjust_end_date': long_dateformat(self.ToDateTime.dateTime().toPyDateTime()), 'L1_date': db_utils.cast_date_time_as_epoch(date_time=long_dateformat(self.L1_date.dateTime().toPyDateTime())), 'L2_date': db_utils.cast_date_time_as_epoch(date_time=long_dateformat(self.L2_date.dateTime().toPyDateTime())), 'M1_date': db_utils.cast_date_time_as_epoch(date_time=long_dateformat(self.M1_date.dateTime().toPyDateTime())), 'M2_date': db_utils.cast_date_time_as_epoch(date_time=long_dateformat(self.M2_date.dateTime().toPyDateTime())), 'L1_level': str(float(self.L1_level.text())), 'L2_level': str(float(self.L2_level.text())), 'M1_level': str(float(self.M1_level.text())), 'M2_level': str(float(self.M2_level.text())), 'date_as_numeric': db_utils.cast_date_time_as_epoch()} sql = """ UPDATE w_levels_logger SET level_masl = level_masl - ( ((({L1_level} - {L2_level}) / ({L1_date} - {L2_date})) - (({M1_level} - {M2_level}) / ({M1_date} - {M2_date}))) * ({date_as_numeric} - {L1_date}) ) WHERE obsid = '{obsid}' AND date_time > '{adjust_start_date}' AND date_time < '{adjust_end_date}' """.format(**data) utils.start_waiting_cursor() db_utils.sql_alter_db(sql) utils.stop_waiting_cursor() self.update_plot()
def test_export_spatialite_zz_tables(self, mock_skip_popup, mock_iface, mock_find_layer, mock_newdbpath, mock_verify, mock_locale, mock_createdb_crs_question, mock_messagebar): mock_find_layer.return_value.crs.return_value.authid.return_value = 'EPSG:3006' mock_createdb_crs_question.return_value = [3006, True] dbconnection = db_utils.DbConnectionManager() mock_newdbpath.return_value = (EXPORT_DB_PATH, '') mock_verify.return_value = 0 """ insert into zz_strat(geoshort,strata) values('land fill','fyll'); insert into zz_stratigraphy_plots (strata,color_mplot,hatch_mplot,color_qt,brush_qt) values('torv','DarkGray','+','darkGray','NoBrush'); insert into zz_capacity (capacity,explanation) values('6 ','mycket god'); insert into zz_capacity (capacity,explanation) values('6+','mycket god'); insert into zz_capacity_plots (capacity,color_qt) values('', 'gray'); """ db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry) VALUES ('P1', ST_GeomFromText('POINT(633466 711659)', 3006))''', dbconnection=dbconnection) dbconnection.execute('''PRAGMA foreign_keys='off' ''') dbconnection.execute('''UPDATE zz_strat SET strata = 'filling' WHERE geoshort = 'land fill' ''') dbconnection.execute('''INSERT INTO zz_stratigraphy_plots (strata,color_mplot,hatch_mplot,color_qt,brush_qt) values ('filling','Yellow','+','darkGray','NoBrush') ''') dbconnection.execute('''UPDATE zz_stratigraphy_plots SET color_mplot = 'OrangeFIX' WHERE strata = 'made ground' ''') dbconnection.execute('''UPDATE zz_capacity SET explanation = 'anexpl' WHERE capacity = 0 ''') dbconnection.execute('''UPDATE zz_capacity_plots SET color_qt = 'whiteFIX' WHERE capacity = 0 ''') #print(str(dbconnection.execute_and_fetchall('select * from zz_strat'))) dbconnection.commit_and_closedb() print("Before export") mock_locale.return_value.answer = 'ok' mock_locale.return_value.value = 'en_US' self.midvatten.export_spatialite() sql_list = ['''SELECT geoshort, strata FROM zz_strat WHERE geoshort IN ('land fill', 'rock') ''', '''SELECT strata, color_mplot FROM zz_stratigraphy_plots WHERE strata IN ('made ground', 'rock', 'filling') ''', '''SELECT capacity, explanation FROM zz_capacity WHERE capacity IN (0, 1)''', '''SELECT capacity, color_qt FROM zz_capacity_plots WHERE capacity IN (0, 1) '''] conn = db_utils.connect_with_spatialite_connect(EXPORT_DB_PATH) curs = conn.cursor() test_list = [] for sql in sql_list: test_list.append('\n' + sql + '\n') test_list.append(curs.execute(sql).fetchall()) conn.commit() conn.close() test_string = utils_for_tests.create_test_string(test_list) reference_string = ['''[''', '''SELECT geoshort, strata FROM zz_strat WHERE geoshort IN ('land fill', 'rock') ''', ''', [(land fill, filling), (rock, rock)], ''', '''SELECT strata, color_mplot FROM zz_stratigraphy_plots WHERE strata IN ('made ground', 'rock', 'filling') ''', ''', [(filling, Yellow), (made ground, OrangeFIX), (rock, red)], ''', '''SELECT capacity, explanation FROM zz_capacity WHERE capacity IN (0, 1)''', ''', [(0, anexpl), (1, above gwl)], ''', '''SELECT capacity, color_qt FROM zz_capacity_plots WHERE capacity IN (0, 1) ''', ''', [(0, whiteFIX), (1, red)]]'''] reference_string = '\n'.join(reference_string) assert test_string == reference_string
def test_stratigraphy(self, mock_skippopup, mock_messagebar): """ :param mock_skippopup: :param mock_messagebar: :return: """ db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, h_gs, geometry) VALUES ('1', 5, ST_GeomFromText('POINT(633466 711659)', 3006))''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, h_gs, geometry) VALUES ('2', 10, ST_GeomFromText('POINT(6720727 016568)', 3006))''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, h_gs, geometry) VALUES ('3', 20, ST_GeomFromText('POINT(6720728 016569)', 3006))''') db_utils.sql_alter_db('''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geology, geoshort, capacity, development) VALUES ('1', 1, 0, 1, 'sand', 'sand', '3', 'j')''') db_utils.sql_alter_db('''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geology, geoshort, capacity, development) VALUES ('1', 2, 1, 4.5, 'morän', 'morän', '3', 'j')''') self.create_and_select_vlayer() #print(str(self.vlayer.isValid())) #print(str(db_utils.sql_load_fr_db('select * from obs_points'))) #print(str(db_utils.sql_load_fr_db('select * from stratigraphy'))) dlg = Stratigraphy(self.iface, self.vlayer, self.ms.settingsdict) #print(str(mock_messagebar.mock_calls)) #print(str(mock_skippopup.mock_calls)) dlg.showSurvey() test = utils.anything_to_string_representation(dlg.data) test_survey = utils.anything_to_string_representation(repr(dlg.data['1'])) test_strata = utils.anything_to_string_representation(utils.returnunicode(dlg.data['1'].strata, keep_containers=True)) assert len(mock_skippopup.mock_calls) == 0 print(str(mock_messagebar.mock_calls)) assert len(mock_messagebar.mock_calls) == 0 assert test == """{"1": SURVEY('1', 5.000000, '<QgsPointXY: POINT(633466 711659)>')}""" assert test_survey == '''"SURVEY('1', 5.000000, '<QgsPointXY: POINT(633466 711659)>')"''' print("test_strata: " + test_strata) assert test_strata == '''["strata(1, '3', 'sand', 'sand', 0.000000-1.000000)", "strata(2, '3', 'morän', 'moran', 1.000000-4.500000)"]'''
def test_calibrlogger_adjust_trend(self, mock_messagebar): db_utils.sql_alter_db("INSERT INTO obs_points (obsid) VALUES ('rb1')") db_utils.sql_alter_db("INSERT INTO w_levels_logger (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-01 00:00', 100)") db_utils.sql_alter_db("INSERT INTO w_levels_logger (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-10 00:00', 200)") db_utils.sql_alter_db("INSERT INTO w_levels (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-01 00:00', 200)") db_utils.sql_alter_db("INSERT INTO w_levels (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-10 00:00', 100)") calibrlogger = Calibrlogger(self.iface.mainWindow(), self.ms) gui_utils.set_combobox(calibrlogger.combobox_obsid, 'rb1 (uncalibrated)') calibrlogger.update_plot() calibrlogger.FromDateTime.setDateTime(date_utils.datestring_to_date('2000-01-01 00:00:00')) calibrlogger.L1_date.setDateTime(date_utils.datestring_to_date('2017-02-01 00:00')) calibrlogger.L2_date.setDateTime(date_utils.datestring_to_date('2017-02-10 00:00')) calibrlogger.M1_date.setDateTime(date_utils.datestring_to_date('2017-02-01 00:00')) calibrlogger.M2_date.setDateTime(date_utils.datestring_to_date('2017-02-10 00:00')) calibrlogger.adjust_trend_func() res = db_utils.sql_load_fr_db('SELECT * FROM w_levels_logger') l = list(res[1][1]) l[5] = '%.11e'%Decimal(l[5]) res[1][1] = tuple(l) test = utils_for_tests.create_test_string(res) print(mock_messagebar.mock_calls) print(test) ref = '(True, [(rb1, 2017-02-01 00:00, None, None, None, 100.0, None), (rb1, 2017-02-10 00:00, None, None, None, -2.84217094304e-14, None)])' assert test == ref
def tearDown(self, mock_messagebar): #mocked_instance.return_value.readEntry.return_value = self.SETTINGS_DATABASE #Clear the database try: db_utils.sql_alter_db(u'DROP SCHEMA public CASCADE;') db_utils.sql_alter_db(u'CREATE SCHEMA public;') except Exception as e: print("Failure resetting db: " + str(e)) print("MidvattenTestPostgisNotCreated tearDownproblem: " + str(mock_messagebar.mock_calls))
def tearDown(self, mock_messagebar): #mocked_instance.return_value.readEntry.return_value = self.SETTINGS_DATABASE #Clear the database try: db_utils.sql_alter_db('DROP SCHEMA public CASCADE;') db_utils.sql_alter_db('CREATE SCHEMA public;') except Exception as e: print("Failure resetting db: " + str(e)) print("MidvattenTestPostgisNotCreated tearDownproblem: " + str(mock_messagebar.mock_calls))
def test_wlvllogg_import_from_diveroffice_files_cancel(self): files = [('Location=rb2', 'Date/time,Water head[cm],Temperature[°C]', '2016/03/15 10:30:00,1,10', '2016/03/15 11:00:00,11,101')] db_utils.sql_alter_db( '''INSERT INTO obs_points (obsid) VALUES ('Rb1')''') DiverofficeImport.charsetchoosen = 'utf-8' with utils.tempinput('\n'.join(files[0]), DiverofficeImport.charsetchoosen) as f1: filenames = [f1] utils_askuser_answer_no_obj = MockUsingReturnValue(None) utils_askuser_answer_no_obj.result = 0 utils_askuser_answer_no = MockUsingReturnValue( utils_askuser_answer_no_obj) @mock.patch('import_data_to_db.utils.NotFoundQuestion') @mock.patch('db_utils.QgsProject.instance', utils_for_tests.MidvattenTestSpatialiteNotCreated. mock_instance_settings_database) @mock.patch('import_data_to_db.utils.Askuser') @mock.patch('qgis.utils.iface', autospec=True) @mock.patch('qgis.PyQt.QtWidgets.QInputDialog.getText') @mock.patch('import_data_to_db.utils.pop_up_info', autospec=True) @mock.patch('import_data_to_db.utils.select_files') def _test_wlvllogg_import_from_diveroffice_files( self, filenames, mock_filenames, mock_skippopup, mock_encoding, mock_iface, mock_askuser, mock_notfoundquestion): mock_notfoundquestion.return_value.answer = 'cancel' mock_notfoundquestion.return_value.value = 'rb1' mock_notfoundquestion.return_value.reuse_column = 'location' mock_filenames.return_value = filenames mock_encoding.return_value = ['utf-8'] ms = MagicMock() ms.settingsdict = OrderedDict() importer = DiverofficeImport(self.iface.mainWindow(), ms) importer.select_files_and_load_gui() importer.import_all_data.checked = True importer.confirm_names.checked = False answer = importer.start_import( importer.files, importer.skip_rows.checked, importer.confirm_names.checked, importer.import_all_data.checked) return answer answer = _test_wlvllogg_import_from_diveroffice_files( self, filenames) test_string = utils_for_tests.create_test_string( db_utils.sql_load_fr_db( '''SELECT obsid, date_time, head_cm, temp_degc, cond_mscm, level_masl, comment FROM w_levels_logger''' )) reference_string = r'''(True, [])''' assert test_string == reference_string
def tearDown(self, mock_messagebar): #Clear the database try: db_utils.sql_alter_db('DROP SCHEMA public CASCADE;') db_utils.sql_alter_db('CREATE SCHEMA public;') except Exception as e: print("Failure resetting db: " + str(e)) print("MidvattenTestPostgisNotCreated tearDownproblem: " + str(mock_messagebar.mock_calls)) super().tearDown()
def test_only_moran(self): db_utils.sql_alter_db('DELETE FROM zz_strat') db_utils.sql_alter_db('DELETE FROM zz_stratigraphy_plots') db_utils.sql_alter_db("""INSERT INTO zz_strat(geoshort, strata) VALUES('morän', 'morän')""") db_utils.sql_alter_db("""INSERT INTO zz_strat(geoshort, strata) VALUES('moran', 'morän')""") db_utils.sql_alter_db("""INSERT INTO zz_stratigraphy_plots(strata, color_mplot, hatch_mplot, color_qt, brush_qt) VALUES('morän', 'theMPcolor', '/', 'theQTcolor', 'thePattern')""") test_string = utils.anything_to_string_representation(midvatten_defs.geocolorsymbols()) reference_string = '''{"moran": ("thePattern", "theQTcolor", ), "morän": ("thePattern", "theQTcolor", )}''' assert test_string == reference_string
def test_warn_about_view_obs_lines_missing(self, mock_messagebar, mock_latest_version): mock_latest_version.return_value = '0.0.1' db_utils.sql_alter_db('''DROP VIEW view_obs_lines;''') utils.warn_about_old_database() print(str(mock_messagebar.mock_calls)) assert call.warning( bar_msg= 'Database is missing view_obs_points or view_obs_lines! Add these using Midvatten>Database Management>Add view_obs_points as workaround for qgis bug #20633.', duration=60) in mock_messagebar.mock_calls
def test_wlvllogg_import_from_diveroffice_files(self): files = [('Location=rb1', 'Date/time,Water head[cm],Temperature[°C]', '2016/03/15 10:30:00,1,10', '2016/03/15 11:00:00,11,101'), ('Location=rb2', 'Date/time,Water head[cm],Temperature[°C]', '2016/04/15 10:30:00,2,20', '2016/04/15 11:00:00,21,201'), ('Location=rb3', 'Date/time,Water head[cm],Temperature[°C],Conductivity[mS/cm]', '2016/05/15 10:30:00,3,30,5', '2016/05/15 11:00:00,31,301,6') ] db_utils.sql_alter_db('''INSERT INTO obs_points (obsid) VALUES ('rb1')''') DiverofficeImport.charsetchoosen = 'utf-8' with utils.tempinput('\n'.join(files[0]), DiverofficeImport.charsetchoosen) as f1: with utils.tempinput('\n'.join(files[1]), DiverofficeImport.charsetchoosen) as f2: with utils.tempinput('\n'.join(files[2]), DiverofficeImport.charsetchoosen) as f3: filenames = [f1, f2, f3] utils_askuser_answer_no_obj = MockUsingReturnValue(None) utils_askuser_answer_no_obj.result = 0 utils_askuser_answer_no = MockUsingReturnValue(utils_askuser_answer_no_obj) @mock.patch('import_data_to_db.utils.NotFoundQuestion') @mock.patch('db_utils.QgsProject.instance', utils_for_tests.MidvattenTestPostgisNotCreated.mock_instance_settings_database) @mock.patch('db_utils.get_postgis_connections', utils_for_tests.MidvattenTestPostgisNotCreated.mock_postgis_connections) @mock.patch('import_data_to_db.utils.Askuser') @mock.patch('qgis.utils.iface', autospec=True) @mock.patch('qgis.PyQt.QtWidgets.QInputDialog.getText') @mock.patch('import_data_to_db.utils.pop_up_info', autospec=True) @mock.patch('import_data_to_db.utils.select_files') def _test_wlvllogg_import_from_diveroffice_files(self, filenames, mock_filenames, mock_skippopup, mock_encoding, mock_iface, mock_askuser, mock_notfoundquestion): mock_notfoundquestion.return_value.answer = 'ok' mock_notfoundquestion.return_value.value = 'rb1' mock_notfoundquestion.return_value.reuse_column = 'location' mock_filenames.return_value = filenames mock_encoding.return_value = ['utf-8'] ms = MagicMock() ms.settingsdict = OrderedDict() importer = DiverofficeImport(self.iface.mainWindow(), ms) importer.select_files_and_load_gui() importer.start_import(importer.files, importer.skip_rows.checked, importer.confirm_names.checked, importer.import_all_data.checked) _test_wlvllogg_import_from_diveroffice_files(self, filenames) test_string = utils_for_tests.create_test_string(db_utils.sql_load_fr_db('''SELECT obsid, date_time, head_cm, temp_degc, cond_mscm, level_masl, comment FROM w_levels_logger''')) reference_string = r'''(True, [(rb1, 2016-03-15 10:30:00, 1.0, 10.0, None, None, None), (rb1, 2016-03-15 11:00:00, 11.0, 101.0, None, None, None), (rb1, 2016-04-15 10:30:00, 2.0, 20.0, None, None, None), (rb1, 2016-04-15 11:00:00, 21.0, 201.0, None, None, None), (rb1, 2016-05-15 10:30:00, 3.0, 30.0, 5.0, None, None), (rb1, 2016-05-15 11:00:00, 31.0, 301.0, 6.0, None, None)])''' assert test_string == reference_string
def test_interlab4_connection_table(self): db_utils.sql_alter_db('''INSERT INTO zz_staff (staff) VALUES ('DV')''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid) VALUES ('obsid1')''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid) VALUES ('obsid2')''') db_utils.sql_alter_db('''INSERT INTO zz_interlab4_obsid_assignment (specifik_provplats, provplatsnamn, obsid) VALUES ('Demo', 'Demo1 vattenverk', 'obsid1')''') db_utils.sql_alter_db('''INSERT INTO zz_interlab4_obsid_assignment (specifik_provplats, provplatsnamn, obsid) VALUES ('Demo', 'Demo2 vattenverk', 'obsid2')''') interlab4_lines = ( '#Interlab', '#Version=4.0', '#Tecken=UTF-8', '#Textavgränsare=Nej', '#Decimaltecken=,', '#Provadm', 'Lablittera;Namn;Adress;Postnr;Ort;Kommunkod;Projekt;Laboratorium;Provtyp;Provtagare;Registertyp;ProvplatsID;Provplatsnamn;Specifik provplats;Provtagningsorsak;Provtyp;Provtypspecifikation;Bedömning;Kemisk bedömning;Mikrobiologisk bedömning;Kommentar;År;Provtagningsdatum;Provtagningstid;Inlämningsdatum;Inlämningstid;', 'DM-990908-2773;MFR;PG Vejdes väg 15;351 96;Växjö;0780;Demoproj;Demo-Laboratoriet;NSG;DV;;;Demo1 vattenverk;Demo;;Dricksvatten enligt SLVFS 2001:30;Utgående;Nej;Tjänligt;;;2010;2010-09-07;10:15;2010-09-07;14:15;', 'DM-990908-2774;MFR;PG Vejdes väg 15;351 96;Växjö;0780;Demoproj;Demo-Laboratoriet;NSG;DV;;;Demo2 vattenverk;Demo;;Dricksvatten enligt SLVFS 2001:30;Utgående;Nej;Tjänligt;;;2010;2010-09-07;10:15;2010-09-07;14:15;', '#Provdat', 'Lablittera;Metodbeteckning;Parameter;Mätvärdetext;Mätvärdetal;Mätvärdetalanm;Enhet;Rapporteringsgräns;Detektionsgräns;Mätosäkerhet;Mätvärdespår;Parameterbedömning;Kommentar;', 'DM-990908-2773;SS-EN ISO 7887-1/4;Kalium;<2,5;2,5;;mg/l Pt;;;;;;;', 'DM-990908-2773;SS-EN ISO 7887-1/4;Kalium;<1;1;;mg/l Pt;;;;;;;', 'DM-990908-2774;SS-EN ISO 7887-1/4;Kalium;<15;15;;mg/l Pt;;;;;;;', '#S**t' ) with utils.tempinput('\n'.join(interlab4_lines), 'utf-8') as filename: @mock.patch('midvatten_utils.NotFoundQuestion') @mock.patch('import_data_to_db.utils.Askuser', mocks_for_tests.mock_askuser.get_v) @mock.patch('qgis.utils.iface', autospec=True) @mock.patch('import_data_to_db.utils.pop_up_info', autospec=True) @mock.patch('import_data_to_db.qgis.PyQt.QtWidgets.QFileDialog.getOpenFileNames') def _test(self, filename, mock_filenames, mock_skippopup, mock_iface, mock_not_found_question): #mock_not_found_question.return_value.answer = 'ok' #mock_not_found_question.return_value.value = 'anobsid' #mock_not_found_question.return_value.reuse_column = 'obsid' mock_filenames.return_value = [[filename]] importer = Interlab4Import(self.iface.mainWindow(), self.midvatten.ms) importer.init_gui() importer.select_files_button.click() importer.use_obsid_assignment_table.setChecked(True) importer.start_import_button.click() _test(self, filename) test_string = utils_for_tests.create_test_string(db_utils.sql_load_fr_db('''SELECT * FROM w_qual_lab''')) reference_string = r'''(True, [(obsid1, None, DM-990908-2773, Demoproj, DV, 2010-09-07 10:15:00, SS-EN ISO 7887-1/4, Kalium, 2.5, <2,5, mg/l Pt, provtyp: Dricksvatten enligt SLVFS 2001:30. provtypspecifikation: Utgående. bedömning: Nej. kemisk bedömning: Tjänligt. provplatsnamn: Demo1 vattenverk. specifik provplats: Demo), (obsid1, None, DM-990908-2773, Demoproj, DV, 2010-09-07 10:15:00, SS-EN ISO 7887-1/4, Kalium (dubblett 1), 1.0, <1, mg/l Pt, provtyp: Dricksvatten enligt SLVFS 2001:30. provtypspecifikation: Utgående. bedömning: Nej. kemisk bedömning: Tjänligt. provplatsnamn: Demo1 vattenverk. specifik provplats: Demo), (obsid2, None, DM-990908-2774, Demoproj, DV, 2010-09-07 10:15:00, SS-EN ISO 7887-1/4, Kalium, 15.0, <15, mg/l Pt, provtyp: Dricksvatten enligt SLVFS 2001:30. provtypspecifikation: Utgående. bedömning: Nej. kemisk bedömning: Tjänligt. provplatsnamn: Demo2 vattenverk. specifik provplats: Demo)])''' print(reference_string) print(test_string) assert test_string == reference_string
def test_interlab4_full_test_to_db_staff_0(self): db_utils.sql_alter_db( u'''INSERT INTO obs_points (obsid) VALUES ('anobsid')''') interlab4_lines = ( '#Interlab', '#Version=4.0', '#Tecken=UTF-8', '#Textavgränsare=Nej', '#Decimaltecken=,', '#Provadm', 'Lablittera;Namn;Adress;Postnr;Ort;Kommunkod;Projekt;Laboratorium;Provtyp;Provtagare;Registertyp;ProvplatsID;Provplatsnamn;Specifik provplats;Provtagningsorsak;Provtyp;Provtypspecifikation;Bedömning;Kemisk bedömning;Mikrobiologisk bedömning;Kommentar;År;Provtagningsdatum;Provtagningstid;Inlämningsdatum;Inlämningstid;', 'DM-990908-2773;MFR;PG Vejdes väg 15;351 96;Växjö;0780;Demoproj;Demo-Laboratoriet;NSG;0;;Demo1 vattenverk;;Föreskriven regelbunden undersökning enligt SLVFS 2001:30;Dricksvatten enligt SLVFS 2001:30;Utgående;Nej;Tjänligt;;;;2010;2010-09-07;10:15;2010-09-07;14:15;', '#Provdat', 'Lablittera;Metodbeteckning;Parameter;Mätvärdetext;Mätvärdetal;Mätvärdetalanm;Enhet;Rapporteringsgräns;Detektionsgräns;Mätosäkerhet;Mätvärdespår;Parameterbedömning;Kommentar;', 'DM-990908-2773;SS-EN ISO 7887-1/4;Kalium;<2,5;2,5;;mg/l Pt;;;;;;;', 'DM-990908-2773;SS-EN ISO 7887-1/4;Kalium;<1;1;;mg/l Pt;;;;;;;', '#S**t') with utils.tempinput(u'\n'.join(interlab4_lines), 'utf-8') as filename: @mock.patch('midvatten_utils.NotFoundQuestion') @mock.patch('db_utils.QgsProject.instance', utils_for_tests.MidvattenTestSpatialiteNotCreated. mock_instance_settings_database) @mock.patch('import_data_to_db.utils.Askuser', mocks_for_tests.mock_askuser.get_v) @mock.patch('qgis.utils.iface', autospec=True) @mock.patch('import_data_to_db.utils.pop_up_info', autospec=True) @mock.patch( 'import_data_to_db.qgis.PyQt.QtWidgets.QFileDialog.getOpenFileNames' ) def _test(self, filename, mock_filenames, mock_skippopup, mock_iface, mock_not_found_question): mock_not_found_question.return_value.answer = 'ok' mock_not_found_question.return_value.value = 'anobsid' mock_not_found_question.return_value.reuse_column = 'obsid' mock_filenames.return_value = [[filename]] importer = Interlab4Import(self.iface.mainWindow(), self.ms) importer.parse_observations_and_populate_gui() importer.start_import( importer.all_lab_results, importer.metadata_filter.get_selected_lablitteras()) _test(self, filename) test_string = utils_for_tests.create_test_string( db_utils.sql_load_fr_db(u'''SELECT * FROM w_qual_lab''')) reference_string = '''(True, [(anobsid, None, DM-990908-2773, Demoproj, 0, 2010-09-07 10:15:00, SS-EN ISO 7887-1/4, Kalium, 2.5, <2,5, mg/l Pt, provtagningsorsak: Dricksvatten enligt SLVFS 2001:30. provtyp: Utgående. provtypspecifikation: Nej. bedömning: Tjänligt. provplatsid: Demo1 vattenverk. specifik provplats: Föreskriven regelbunden undersökning enligt SLVFS 2001:30), (anobsid, None, DM-990908-2773, Demoproj, 0, 2010-09-07 10:15:00, SS-EN ISO 7887-1/4, Kalium (dubblett 1), 1.0, <1, mg/l Pt, provtagningsorsak: Dricksvatten enligt SLVFS 2001:30. provtyp: Utgående. provtypspecifikation: Nej. bedömning: Tjänligt. provplatsid: Demo1 vattenverk. specifik provplats: Föreskriven regelbunden undersökning enligt SLVFS 2001:30)])''' print(reference_string) print(test_string) assert test_string == reference_string test_string = utils_for_tests.create_test_string( db_utils.sql_load_fr_db(u'''SELECT * FROM zz_staff''')) reference_string = '(True, [(0, None)])' assert test_string == reference_string
def test_only_moran(self): db_utils.sql_alter_db('DELETE FROM zz_strat') db_utils.sql_alter_db('DELETE FROM zz_stratigraphy_plots') db_utils.sql_alter_db("""INSERT INTO zz_strat(geoshort, strata) VALUES('morän', 'morän')""") db_utils.sql_alter_db("""INSERT INTO zz_strat(geoshort, strata) VALUES('moran', 'morän')""") db_utils.sql_alter_db("""INSERT INTO zz_stratigraphy_plots(strata, color_mplot, hatch_mplot, color_qt, brush_qt) VALUES('morän', 'theMPcolor', '/', 'theQTcolor', 'thePattern')""") test_string = utils.anything_to_string_representation(midvatten_defs.geocolorsymbols()) reference_string = '''{"moran": ("thePattern", "theQTcolor", ), "morän": ("thePattern", "theQTcolor", )}''' print(test_string) assert test_string == reference_string
def setUp(self): super().setUp() QgsProject.instance().writeEntry("Midvatten", 'database', utils.anything_to_string_representation(MidvattenTestPostgisNotCreated.TEMP_DB_SETTINGS)) qs = QSettings() for k, v in MidvattenTestPostgisNotCreated.ALL_POSTGIS_SETTINGS['nosetests'].items(): qs.setValue('PostgreSQL/connections/{}/{}'.format('nosetests', k), v) #Clear the database try: db_utils.sql_alter_db('DROP SCHEMA public CASCADE;') db_utils.sql_alter_db('CREATE SCHEMA public;') except Exception as e: print("Failure resetting db: " + str(e))
def test_calcall(self): db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, h_toc) VALUES ('rb1', 1)''') db_utils.sql_alter_db('''INSERT into w_levels (obsid, meas, date_time) VALUES ('rb1', 222, '2005-01-01 00:00:00')''') self.calclvl.FromDateTime = QtWidgets.QDateTimeEdit() self.calclvl.FromDateTime.setDateTime(datestring_to_date('2000-01-01 00:00:00')) self.calclvl.ToDateTime = QtWidgets.QDateTimeEdit() self.calclvl.ToDateTime.setDateTime(datestring_to_date('2010-01-01 00:00:00')) self.calclvl.calcall() test_string = utils_for_tests.create_test_string( db_utils.sql_load_fr_db('SELECT obsid, date_time, meas, h_toc, level_masl FROM w_levels')) reference_string = '(True, [(rb1, 2005-01-01 00:00:00, 222.0, 1.0, -221.0)])' assert test_string == reference_string
def test_calcall(self, mock_messagebar): db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, h_toc) VALUES ('rb1', 1)''') db_utils.sql_alter_db('''INSERT into w_levels (obsid, meas, date_time) VALUES ('rb1', 222, '2005-01-01 00:00:00')''') self.calclvl.FromDateTime = QtWidgets.QDateTimeEdit() self.calclvl.FromDateTime.setDateTime(datestring_to_date('2000-01-01 00:00:00')) self.calclvl.ToDateTime = QtWidgets.QDateTimeEdit() self.calclvl.ToDateTime.setDateTime(datestring_to_date('2010-01-01 00:00:00')) self.calclvl.calcall() test_string = utils_for_tests.create_test_string( db_utils.sql_load_fr_db('SELECT obsid, date_time, meas, h_toc, level_masl FROM w_levels')) reference_string = '(True, [(rb1, 2005-01-01 00:00:00, 222.0, 1.0, -221.0)])' assert test_string == reference_string
def test_calibrlogger_adjust_trend(self, mock_messagebar): db_utils.sql_alter_db("INSERT INTO obs_points (obsid) VALUES ('rb1')") db_utils.sql_alter_db( "INSERT INTO w_levels_logger (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-01 00:00', 100)" ) db_utils.sql_alter_db( "INSERT INTO w_levels_logger (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-10 00:00', 200)" ) db_utils.sql_alter_db( "INSERT INTO w_levels (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-01 00:00', 200)" ) db_utils.sql_alter_db( "INSERT INTO w_levels (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-10 00:00', 100)" ) calibrlogger = Calibrlogger(self.iface.mainWindow(), self.ms) gui_utils.set_combobox(calibrlogger.combobox_obsid, 'rb1 (uncalibrated)') calibrlogger.update_plot() calibrlogger.FromDateTime.setDateTime( date_utils.datestring_to_date('2000-01-01 00:00:00')) calibrlogger.L1_date.setDateTime( date_utils.datestring_to_date('2017-02-01 00:00')) calibrlogger.L2_date.setDateTime( date_utils.datestring_to_date('2017-02-10 00:00')) calibrlogger.M1_date.setDateTime( date_utils.datestring_to_date('2017-02-01 00:00')) calibrlogger.M2_date.setDateTime( date_utils.datestring_to_date('2017-02-10 00:00')) calibrlogger.L1_level.setText('100') calibrlogger.L2_level.setText('200') calibrlogger.M1_level.setText('200') calibrlogger.M2_level.setText('100') calibrlogger.adjust_trend_func() res = db_utils.sql_load_fr_db( 'SELECT obsid, date_time, head_cm, temp_degc, cond_mscm, level_masl, comment FROM w_levels_logger' ) l = list(res[1][1]) l[5] = '%.11e' % Decimal(l[5]) res[1][1] = tuple(l) test = utils_for_tests.create_test_string(res) print(mock_messagebar.mock_calls) ref = '(True, [(rb1, 2017-02-01 00:00, None, None, None, 100.0, None), (rb1, 2017-02-10 00:00, None, None, None, -2.84217094304e-14, None)])' print("Ref") print(ref) print("Test") print(test) assert test == ref
def test_plot_section(self, mock_messagebar): """For now, the test only initiates the plot. Check that it does not crash """ db_utils.sql_alter_db('''INSERT INTO obs_lines (obsid, geometry) VALUES ('L1', ST_GeomFromText('LineString(633466.711659 6720684.24498, 633599.530455 6720727.016568)', 3006))''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry) VALUES ('P1', ST_GeomFromText('POINT(633466 711659)', 3006))''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry) VALUES ('P2', ST_GeomFromText('POINT(6720727 016568)', 3006))''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry) VALUES ('P3', ST_GeomFromText('POINT(6720728 016569)', 3006))''') self.create_and_select_vlayer() @mock.patch('db_utils.QgsProject.instance', utils_for_tests.MidvattenTestPostgisNotCreated.mock_instance_settings_database) @mock.patch('db_utils.get_postgis_connections', utils_for_tests.MidvattenTestPostgisNotCreated.mock_postgis_connections) @mock.patch('midvatten_utils.getselectedobjectnames', autospec=True) @mock.patch('qgis.utils.iface', autospec=True) def _test_plot_section(self, mock_iface, mock_getselectedobjectnames): mock_iface.mapCanvas.return_value.currentLayer.return_value = self.vlayer mock_getselectedobjectnames.return_value = ('P1', 'P2', 'P3') mock_mapcanvas = mock_iface.mapCanvas.return_value mock_mapcanvas.layerCount.return_value = 0 self.midvatten.plot_section() self.myplot = self.midvatten.myplot self.myplot.drillstoplineEdit.setText("%berg%") self.myplot.draw_plot() self.selected_obsids = self.myplot.selected_obsids _test_plot_section(self) assert """call.info(log_msg='Settings {""" in str(mock_messagebar.mock_calls) assert self.myplot.drillstoplineEdit.text() == '%berg%' assert utils_for_tests.create_test_string(self.myplot.selected_obsids) == "['P1' 'P2' 'P3']" assert not mock_messagebar.warning.called assert not mock_messagebar.critical.called assert len(self.myplot.p) == len(self.myplot.Labels)
def setUp(self): #self.iface = mock.MagicMock() self.dummy_iface = DummyInterface2() self.iface = self.dummy_iface.mock self.midvatten = midvatten(self.iface) self.ms = mock.MagicMock() self.ms.settingsdict = OrderedDict() #Clear the database try: db_utils.sql_alter_db('DROP SCHEMA public CASCADE;') db_utils.sql_alter_db('CREATE SCHEMA public;') except Exception as e: print("Failure resetting db: " + str(e))
def test_plot_section_length_along_slope(self, mock_messagebar): db_utils.sql_alter_db('''INSERT INTO obs_lines (obsid, geometry) VALUES ('L1', ST_GeomFromText('LineString(2 0, 10 10)', 3006))''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry) VALUES ('P1', ST_GeomFromText('POINT(1 0)', 3006))''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry) VALUES ('P2', ST_GeomFromText('POINT(3 0)', 3006))''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry) VALUES ('P3', ST_GeomFromText('POINT(5 0)', 3006))''') self.create_and_select_vlayer() @mock.patch('midvatten_utils.getselectedobjectnames', autospec=True) @mock.patch('qgis.utils.iface', autospec=True) def _test(midvatten, vlayer, mock_iface, mock_getselectedobjectnames): mock_iface.mapCanvas.return_value.currentLayer.return_value = vlayer mock_getselectedobjectnames.return_value = ('P1', 'P2', 'P3') mock_mapcanvas = mock_iface.mapCanvas.return_value mock_mapcanvas.layerCount.return_value = 0 midvatten.plot_section() self.myplot = midvatten.myplot self.myplot.drillstoplineEdit.setText("%berg%") self.myplot.draw_plot() _test(self.midvatten, self.vlayer) test_string = utils_for_tests.create_test_string(self.myplot.LengthAlong) assert any([test_string == "[ 0. 0.62469505 1.87408514]", test_string == "[0. 0.62469505 1.87408514]"]) assert not mock_messagebar.warning.called assert not mock_messagebar.critical.called
def test_plot_section(self, mock_messagebar): """For now, the test only initiates the plot. Check that it does not crash """ db_utils.sql_alter_db('''INSERT INTO obs_lines (obsid, geometry) VALUES ('L1', ST_GeomFromText('LineString(633466.711659 6720684.24498, 633599.530455 6720727.016568)', 3006))''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry) VALUES ('P1', ST_GeomFromText('POINT(633466 711659)', 3006))''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry) VALUES ('P2', ST_GeomFromText('POINT(6720727 016568)', 3006))''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry) VALUES ('P3', ST_GeomFromText('POINT(6720728 016569)', 3006))''') self.create_and_select_vlayer() @mock.patch('midvatten_utils.find_layer') @mock.patch('midvatten_utils.getselectedobjectnames', autospec=True) @mock.patch('qgis.utils.iface', autospec=True) def _test_plot_section(self, mock_iface, mock_getselectedobjectnames, mock_findlayer): mock_iface.mapCanvas.return_value.currentLayer.return_value = self.vlayer mock_findlayer.return_value.isEditable.return_value = False mock_getselectedobjectnames.return_value = ('P1', 'P2', 'P3') mock_mapcanvas = mock_iface.mapCanvas.return_value mock_mapcanvas.layerCount.return_value = 0 self.midvatten.plot_section() self.myplot = self.midvatten.myplot self.myplot.drillstoplineEdit.setText("%berg%") self.myplot.draw_plot() self.selected_obsids = self.myplot.selected_obsids _test_plot_section(self) assert """call.info(log_msg='Settings {""" in str(mock_messagebar.mock_calls) assert self.myplot.drillstoplineEdit.text() == '%berg%' assert utils_for_tests.create_test_string(self.myplot.selected_obsids) == "['P1' 'P2' 'P3']" assert not mock_messagebar.warning.called assert not mock_messagebar.critical.called print("self.myplot.p {} self.myplot.labels {}".format(str(self.myplot.p), str(self.myplot.labels))) assert len(self.myplot.p) - 1 == len(self.myplot.labels) # The bars should not be labeled, so there is one less label than plot.
def test_plot_section_length_along(self, mock_messagebar): """For now, the test only initiates the plot. Check that it does not crash """ db_utils.sql_alter_db('''INSERT INTO obs_lines (obsid, geometry) VALUES ('L1', ST_GeomFromText('LineString(0 0, 1 0, 10 0)', 3006))''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry) VALUES ('P1', ST_GeomFromText('POINT(1 0)', 3006))''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry) VALUES ('P2', ST_GeomFromText('POINT(3 5)', 3006))''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry) VALUES ('P3', ST_GeomFromText('POINT(5 10)', 3006))''') self.create_and_select_vlayer() @mock.patch('midvatten_utils.getselectedobjectnames', autospec=True) @mock.patch('qgis.utils.iface', autospec=True) def _test(midvatten, vlayer, mock_iface, mock_getselectedobjectnames): mock_iface.mapCanvas.return_value.currentLayer.return_value = vlayer mock_getselectedobjectnames.return_value = ('P1', 'P2', 'P3') mock_mapcanvas = mock_iface.mapCanvas.return_value mock_mapcanvas.layerCount.return_value = 0 midvatten.plot_section() myplot = midvatten.myplot myplot.drillstoplineEdit.setText("%berg%") myplot.draw_plot() return myplot myplot = _test(self.midvatten, self.vlayer) test_string = utils_for_tests.create_test_string(myplot.LengthAlong) assert any([test_string == "[ 1. 3. 5.]", test_string == "[1. 3. 5.]"]) assert mock.call.info(log_msg='Hidden features, obsids and length along section:\nP1;P2;P3\\1.0;3.0;5.0') in mock_messagebar.mock_calls assert not mock_messagebar.warning.called assert not mock_messagebar.critical.called
def test_calc_selected(self, mock_selected_obsids): mock_selected_obsids.return_value = [u'rb1'] db_utils.sql_alter_db( u'''INSERT INTO obs_points (obsid, h_toc) VALUES ('rb1', 1)''') db_utils.sql_alter_db( u'''INSERT into w_levels (obsid, meas, date_time) VALUES ('rb1', 222, '2005-01-01 00:00:00')''' ) db_utils.sql_alter_db( u'''INSERT INTO obs_points (obsid, h_toc) VALUES ('rb2', 4)''') db_utils.sql_alter_db( u'''INSERT into w_levels (obsid, meas, date_time) VALUES ('rb2', 444, '2005-01-01 00:00:00')''' ) self.calclvl.FromDateTime = QtGui.QDateTimeEdit() self.calclvl.FromDateTime.setDateTime( datestring_to_date(u'2000-01-01 00:00:00')) self.calclvl.ToDateTime = QtGui.QDateTimeEdit() self.calclvl.ToDateTime.setDateTime( datestring_to_date(u'2010-01-01 00:00:00')) self.calclvl.calcselected() test_string = utils_for_tests.create_test_string( db_utils.sql_load_fr_db( u'SELECT obsid, date_time, meas, h_toc, level_masl FROM w_levels ORDER BY obsid' )) reference_string = u'(True, [(rb1, 2005-01-01 00:00:00, 222.0, 1.0, -221.0), (rb2, 2005-01-01 00:00:00, 444.0, None, None)])' assert test_string == reference_string
def setUp(self): #self.iface = mock.MagicMock() self.dummy_iface = DummyInterface2() self.iface = self.dummy_iface.mock self.midvatten = midvatten(self.iface) self.ms = mock.MagicMock() self.ms.settingsdict = OrderedDict() #Clear the database try: db_utils.sql_alter_db(u'DROP SCHEMA public CASCADE;') db_utils.sql_alter_db(u'CREATE SCHEMA public;') except Exception as e: print("Failure resetting db: " + str(e))
def test_plot_section_with_depth(self, mock_messagebar): db_utils.sql_alter_db( u'''INSERT INTO obs_lines (obsid, geometry) VALUES ('L1', ST_GeomFromText('LINESTRING(633466.711659 6720684.24498, 633599.530455 6720727.016568)', 3006))''' ) db_utils.sql_alter_db( u'''INSERT INTO obs_points (obsid, geometry, length) VALUES ('P1', ST_GeomFromText('POINT(633466 711659)', 3006), 2)''' ) db_utils.sql_alter_db( u'''INSERT INTO obs_points (obsid, geometry, length) VALUES ('P2', ST_GeomFromText('POINT(6720727 016568)', 3006), '1')''' ) db_utils.sql_alter_db( u'''INSERT INTO obs_points (obsid, geometry, length) VALUES ('P3', ST_GeomFromText('POINT(6720727 016568)', 3006), NULL)''' ) self.create_and_select_vlayer() @mock.patch('midvatten_utils.getselectedobjectnames', autospec=True) @mock.patch('qgis.utils.iface', autospec=True) def _test(self, mock_iface, mock_getselectedobjectnames): mock_iface.mapCanvas.return_value.currentLayer.return_value = self.vlayer mock_getselectedobjectnames.return_value = (u'P1', u'P2', u'P3') mock_mapcanvas = mock_iface.mapCanvas.return_value mock_mapcanvas.layerCount.return_value = 0 self.midvatten.plot_section() self.myplot = self.midvatten.myplot _test(self) print(str(mock_messagebar.mock_calls)) assert not mock_messagebar.warning.called assert not mock_messagebar.critical.called
def calculateaveflow(self): PyQt4.QtGui.QApplication.setOverrideCursor(PyQt4.QtCore.Qt.WaitCursor) date_from = self.FromDateTime.dateTime().toPyDateTime() date_to = self.ToDateTime.dateTime().toPyDateTime() #Identify distinct set of obsid and instrumentid with Accvol-data and within the user-defined date_time-interval: sql = """select distinct obsid, instrumentid from(select * from w_flow where flowtype = "Accvol" and date_time >="%s" and date_time <="%s" and obsid IN %s)""" % ( date_from, date_to, (str(self.observations)).encode('utf-8').replace('[', '(').replace( ']', ')')) #utils.pop_up_info(sql)#debug uniqueset = db_utils.sql_load_fr_db(sql)[ 1] # The unique set of obsid and instrumentid is kept in uniqueset negativeflow = False for pyobsid, pyinstrumentid in uniqueset: sql = """select date_time, reading from w_flow where flowtype = 'Accvol' and obsid='%s' and instrumentid='%s' and date_time >='%s' and date_time <='%s' order by date_time""" % ( pyobsid, pyinstrumentid, date_from, date_to) recs = db_utils.sql_load_fr_db(sql)[1] """Transform data to a numpy.recarray""" My_format = [('date_time', datetime.datetime), ('values', float) ] #Define format with help from function datetime table = np.array(recs, dtype=My_format) #NDARRAY table2 = table.view( np.recarray ) # RECARRAY Makes the two columns into callable objects, i.e. write table2.values for j, row in enumerate( table2 ): #This is where Aveflow is calculated for each obs and also written to db if j > 0: #first row is "start-value" for Accvol and there is no Aveflow to be calculated Volume = ( table2.values[j] - table2.values[j - 1] ) * 1000 #convert to L since Accvol is supposed to be in m3 """ Get help from function datestr2num to get date and time into float""" DeltaTime = 24 * 3600 * ( datestr2num(table2.date_time[j]) - datestr2num(table2.date_time[j - 1]) ) #convert to seconds since numtime is days Aveflow = Volume / DeltaTime #L/s if Aveflow < 0: negativeflow = True sql = """insert or ignore into w_flow(obsid,instrumentid,flowtype,date_time,reading,unit) values('%s','%s','Aveflow','%s','%s','l/s')""" % ( pyobsid, pyinstrumentid, table2.date_time[j], Aveflow) db_utils.sql_alter_db(sql) if negativeflow: utils.MessagebarAndLog.info(bar_msg=ru( QCoreApplication.translate( u'Calcave', u"Please notice that negative flow was encountered."))) PyQt4.QtGui.QApplication.restoreOverrideCursor() self.close()
def test_missing_colors_patterns(self): db_utils.sql_alter_db('DELETE FROM zz_strat') db_utils.sql_alter_db('DELETE FROM zz_stratigraphy_plots') db_utils.sql_alter_db("""INSERT INTO zz_strat(geoshort, strata) VALUES('nostrata', 'noshort')""") db_utils.sql_alter_db("""INSERT INTO zz_stratigraphy_plots(strata, color_mplot, hatch_mplot, color_qt, brush_qt) VALUES('moran', 'theMPcolor', '/', 'theQTcolor', 'thePattern')""") test_string = utils.anything_to_string_representation(midvatten_defs.geocolorsymbols()) reference_string = '''{"nostrata": ("NoBrush", "white", )}''' assert test_string == reference_string
def test_interlab4_full_test_to_db_staff_0(self): db_utils.sql_alter_db(u'''INSERT INTO obs_points (obsid) VALUES ('anobsid')''') interlab4_lines = ( '#Interlab', '#Version=4.0', '#Tecken=UTF-8', '#Textavgränsare=Nej', '#Decimaltecken=,', '#Provadm', 'Lablittera;Namn;Adress;Postnr;Ort;Kommunkod;Projekt;Laboratorium;Provtyp;Provtagare;Registertyp;ProvplatsID;Provplatsnamn;Specifik provplats;Provtagningsorsak;Provtyp;Provtypspecifikation;Bedömning;Kemisk bedömning;Mikrobiologisk bedömning;Kommentar;År;Provtagningsdatum;Provtagningstid;Inlämningsdatum;Inlämningstid;', 'DM-990908-2773;MFR;PG Vejdes väg 15;351 96;Växjö;0780;Demoproj;Demo-Laboratoriet;NSG;0;;Demo1 vattenverk;;Föreskriven regelbunden undersökning enligt SLVFS 2001:30;Dricksvatten enligt SLVFS 2001:30;Utgående;Nej;Tjänligt;;;;2010;2010-09-07;10:15;2010-09-07;14:15;', '#Provdat', 'Lablittera;Metodbeteckning;Parameter;Mätvärdetext;Mätvärdetal;Mätvärdetalanm;Enhet;Rapporteringsgräns;Detektionsgräns;Mätosäkerhet;Mätvärdespår;Parameterbedömning;Kommentar;', 'DM-990908-2773;SS-EN ISO 7887-1/4;Kalium;<2,5;2,5;;mg/l Pt;;;;;;;', 'DM-990908-2773;SS-EN ISO 7887-1/4;Kalium;<1;1;;mg/l Pt;;;;;;;', '#S**t' ) with utils.tempinput(u'\n'.join(interlab4_lines), 'utf-8') as filename: @mock.patch('midvatten_utils.NotFoundQuestion') @mock.patch('db_utils.QgsProject.instance', utils_for_tests.MidvattenTestSpatialiteNotCreated.mock_instance_settings_database) @mock.patch('import_data_to_db.utils.Askuser', mocks_for_tests.mock_askuser.get_v) @mock.patch('qgis.utils.iface', autospec=True) @mock.patch('import_data_to_db.utils.pop_up_info', autospec=True) @mock.patch('import_data_to_db.qgis.PyQt.QtWidgets.QFileDialog.getOpenFileNames') def _test(self, filename, mock_filenames, mock_skippopup, mock_iface, mock_not_found_question): mock_not_found_question.return_value.answer = 'ok' mock_not_found_question.return_value.value = 'anobsid' mock_not_found_question.return_value.reuse_column = 'obsid' mock_filenames.return_value = [[filename]] importer = Interlab4Import(self.iface.mainWindow(), self.ms) importer.parse_observations_and_populate_gui() importer.start_import(importer.all_lab_results, importer.metadata_filter.get_selected_lablitteras()) _test(self, filename) test_string = utils_for_tests.create_test_string(db_utils.sql_load_fr_db(u'''SELECT * FROM w_qual_lab''')) reference_string = '''(True, [(anobsid, None, DM-990908-2773, Demoproj, 0, 2010-09-07 10:15:00, SS-EN ISO 7887-1/4, Kalium, 2.5, <2,5, mg/l Pt, provtagningsorsak: Dricksvatten enligt SLVFS 2001:30. provtyp: Utgående. provtypspecifikation: Nej. bedömning: Tjänligt. provplatsid: Demo1 vattenverk. specifik provplats: Föreskriven regelbunden undersökning enligt SLVFS 2001:30), (anobsid, None, DM-990908-2773, Demoproj, 0, 2010-09-07 10:15:00, SS-EN ISO 7887-1/4, Kalium (dubblett 1), 1.0, <1, mg/l Pt, provtagningsorsak: Dricksvatten enligt SLVFS 2001:30. provtyp: Utgående. provtypspecifikation: Nej. bedömning: Tjänligt. provplatsid: Demo1 vattenverk. specifik provplats: Föreskriven regelbunden undersökning enligt SLVFS 2001:30)])''' print(reference_string) print(test_string) assert test_string == reference_string test_string = utils_for_tests.create_test_string(db_utils.sql_load_fr_db(u'''SELECT * FROM zz_staff''')) reference_string = '(True, [(0, None)])' assert test_string == reference_string
def test_calibrlogger_add_to_level_masl(self, mock_messagebar): db_utils.sql_alter_db("INSERT INTO obs_points (obsid) VALUES ('rb1')") db_utils.sql_alter_db("INSERT INTO w_levels_logger (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-01 00:00', 100)") calibrlogger = Calibrlogger(self.iface.mainWindow(), self.ms) calibrlogger.update_plot() calibrlogger.FromDateTime.setDateTime(date_utils.datestring_to_date('2000-01-01 00:00:00')) calibrlogger.Add2Levelmasl.setText('50') gui_utils.set_combobox(calibrlogger.combobox_obsid, 'rb1 (uncalibrated)') calibrlogger.add_to_level_masl() test = utils_for_tests.create_test_string(db_utils.sql_load_fr_db('SELECT * FROM w_levels_logger')) ref = '(True, [(rb1, 2017-02-01 00:00, None, None, None, 150.0, None)])' print(test) assert test == ref
def test_calibrlogger_add_to_level_masl(self, mock_messagebar): db_utils.sql_alter_db(u"INSERT INTO obs_points (obsid) VALUES ('rb1')") db_utils.sql_alter_db(u"INSERT INTO w_levels_logger (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-01 00:00', 100)") calibrlogger = Calibrlogger(self.iface.mainWindow(), self.ms) calibrlogger.update_plot() calibrlogger.FromDateTime.setDateTime(date_utils.datestring_to_date(u'2000-01-01 00:00:00')) calibrlogger.Add2Levelmasl.setText(u'50') gui_utils.set_combobox(calibrlogger.combobox_obsid, u'rb1 (uncalibrated)') calibrlogger.add_to_level_masl() test = utils_for_tests.create_test_string(db_utils.sql_load_fr_db(u'SELECT * FROM w_levels_logger')) ref = u'(True, [(rb1, 2017-02-01 00:00, None, None, None, 150.0, None)])' print(test) assert test == ref
def test_stratigraphy(self, mock_skippopup, mock_messagebar): """ :param mock_skippopup: :param mock_messagebar: :return: """ db_utils.sql_alter_db( '''INSERT INTO obs_points (obsid, h_gs, geometry) VALUES ('1', 5, ST_GeomFromText('POINT(633466 711659)', 3006))''' ) db_utils.sql_alter_db( '''INSERT INTO obs_points (obsid, h_gs, geometry) VALUES ('2', 10, ST_GeomFromText('POINT(6720727 016568)', 3006))''' ) db_utils.sql_alter_db( '''INSERT INTO obs_points (obsid, h_gs, geometry) VALUES ('3', 20, ST_GeomFromText('POINT(6720728 016569)', 3006))''' ) db_utils.sql_alter_db( '''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geology, geoshort, capacity, development) VALUES ('1', 1, 0, 1, 'sand', 'sand', '3', 'j')''' ) db_utils.sql_alter_db( '''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geology, geoshort, capacity, development) VALUES ('1', 2, 1, 4.5, 'morän', 'morän', '3', 'j')''' ) self.create_and_select_vlayer() #print(str(self.vlayer.isValid())) #print(str(db_utils.sql_load_fr_db('select * from obs_points'))) #print(str(db_utils.sql_load_fr_db('select * from stratigraphy'))) dlg = Stratigraphy(self.iface, self.vlayer, self.midvatten.ms.settingsdict) #print(str(mock_messagebar.mock_calls)) #print(str(mock_skippopup.mock_calls)) dlg.showSurvey() test = utils.anything_to_string_representation(dlg.data) test_survey = utils.anything_to_string_representation( repr(dlg.data['1'])) test_strata = utils.anything_to_string_representation( utils.returnunicode(dlg.data['1'].strata, keep_containers=True)) assert len(mock_skippopup.mock_calls) == 0 print(str(mock_messagebar.mock_calls)) assert len(mock_messagebar.mock_calls) == 0 assert test == """{"1": SURVEY('1', 5.000000, '<QgsPointXY: POINT(633466 711659)>')}""" assert test_survey == '''"SURVEY('1', 5.000000, '<QgsPointXY: POINT(633466 711659)>')"''' print("test_strata: " + test_strata) assert test_strata == '''["strata(1, '3', 'sand', 'sand', 0.000000-1.000000)", "strata(2, '3', 'morän', 'moran', 1.000000-4.500000)"]'''
def test_calc_selected_overwrite(self, mock_selected_obsids): mock_selected_obsids.return_value = ['rb1', 'rb2'] db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, h_toc) VALUES ('rb1', 1)''') db_utils.sql_alter_db('''INSERT into w_levels (obsid, meas, date_time) VALUES ('rb1', 222, '2005-01-01 00:00:00')''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, h_toc) VALUES ('rb2', 4)''') db_utils.sql_alter_db('''INSERT into w_levels (obsid, meas, date_time) VALUES ('rb2', 444, '2005-01-01 00:00:00')''') db_utils.sql_alter_db('''INSERT into w_levels (obsid, meas, level_masl, date_time) VALUES ('rb2', 555, 667, '2005-01-02 00:00:00')''') self.calclvl.FromDateTime = QtWidgets.QDateTimeEdit() self.calclvl.FromDateTime.setDateTime(datestring_to_date('2000-01-01 00:00:00')) self.calclvl.ToDateTime = QtWidgets.QDateTimeEdit() self.calclvl.ToDateTime.setDateTime(datestring_to_date('2010-01-01 00:00:00')) self.calclvl.calcselected() test_string = utils_for_tests.create_test_string( db_utils.sql_load_fr_db('SELECT obsid, date_time, meas, h_toc, level_masl FROM w_levels ORDER BY obsid')) reference_string = '(True, [(rb1, 2005-01-01 00:00:00, 222.0, 1.0, -221.0), (rb2, 2005-01-01 00:00:00, 444.0, 4.0, -440.0), (rb2, 2005-01-02 00:00:00, 555.0, 4.0, -551.0)])' print(test_string) assert test_string == reference_string
def drop_db_views(self): # TODO: Update to support PostGIS sql1="delete from views_geometry_columns where view_name = 'strat_obs_p_for_qgsi2threejs'" sql2="drop view if exists strat_obs_p_for_qgsi2threejs" db_utils.sql_alter_db(sql1, dbconnection=self.dbconnection) db_utils.sql_alter_db(sql2, dbconnection=self.dbconnection) placeholder_sign = db_utils.placeholder_sign(self.dbconnection) sql1="delete from views_geometry_columns where view_name = %s"%placeholder_sign sql2="drop view if exists " for key in self.strat_layers_dict: db_utils.sql_alter_db(sql1, dbconnection=self.dbconnection, all_args=[(key,)]) db_utils.sql_alter_db(sql2 + key, dbconnection=self.dbconnection)
def calc(self, obsids): fr_d_t = self.FromDateTime.dateTime().toPyDateTime() to_d_t = self.ToDateTime.dateTime().toPyDateTime() sql = """SELECT obsid FROM obs_points WHERE obsid IN ({}) AND h_toc IS NULL""".format(', '.join(["'{}'".format(x) for x in obsids])) obsid_with_h_toc_null = db_utils.sql_load_fr_db(sql)[1] if obsid_with_h_toc_null: obsid_with_h_toc_null = [x[0] for x in obsid_with_h_toc_null] if self.checkBox_stop_if_null.isChecked(): any_nulls = [obsid for obsid in obsids if obsid in obsid_with_h_toc_null] if any_nulls: utils.pop_up_info(ru(QCoreApplication.translate('Calclvl', 'Adjustment aborted! There seems to be NULL values in your table obs_points, column h_toc.')), ru(QCoreApplication.translate('Calclvl', 'Error'))) return None else: obsids = [obsid for obsid in obsids if obsid not in obsid_with_h_toc_null] if not obsids: utils.pop_up_info(ru(QCoreApplication.translate('Calclvl', 'Adjustment aborted! All h_tocs were NULL.')), ru(QCoreApplication.translate('Calclvl', 'Error'))) return None formatted_obsids = ', '.join(["'{}'".format(x) for x in obsids]) where_args = {'fr_dt': str(fr_d_t), 'to_dt': str(to_d_t), 'obsids': formatted_obsids} where_sql = """meas IS NOT NULL AND date_time >= '{fr_dt}' AND date_time <= '{to_dt}' AND obsid IN ({obsids})""".format(**where_args) if not self.checkBox_overwrite_prev.isChecked(): where_sql += """ AND level_masl IS NULL """ sql1 = """UPDATE w_levels SET h_toc = (SELECT obs_points.h_toc FROM obs_points WHERE w_levels.obsid = obs_points.obsid) WHERE {}""".format(where_sql) self.updated_h_tocs = self.log_msg(where_sql) db_utils.sql_alter_db(sql1) where_sql += """ AND h_toc IS NOT NULL""" sql2 = """UPDATE w_levels SET level_masl = h_toc - meas WHERE h_toc IS NOT NULL AND {}""".format(where_sql) self.updated_level_masl = self.log_msg(where_sql) db_utils.sql_alter_db(sql2) utils.MessagebarAndLog.info(bar_msg=ru(QCoreApplication.translate('Calclvl', 'Calculation done, see log message panel')), log_msg=ru(QCoreApplication.translate('Calclvl', 'H_toc added and level_masl calculated for\nobsid;min date;max date;calculated number of measurements: \n%s'))%(self.updated_level_masl)) self.close()
def test_calc_selected_dont_overwrite(self, mock_selected_obsids, mock_messagebar): mock_selected_obsids.return_value = ['rb1', 'rb2'] db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, h_toc) VALUES ('rb1', 1)''') db_utils.sql_alter_db('''INSERT into w_levels (obsid, meas, date_time) VALUES ('rb1', 222, '2005-01-01 00:00:00')''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, h_toc) VALUES ('rb2', 4)''') db_utils.sql_alter_db('''INSERT into w_levels (obsid, meas, date_time) VALUES ('rb2', 444, '2005-01-01 00:00:00')''') db_utils.sql_alter_db('''INSERT into w_levels (obsid, meas, level_masl, date_time) VALUES ('rb2', 555, 667, '2005-01-02 00:00:00')''') self.calclvl.FromDateTime = QtWidgets.QDateTimeEdit() self.calclvl.FromDateTime.setDateTime(datestring_to_date('2000-01-01 00:00:00')) self.calclvl.ToDateTime = QtWidgets.QDateTimeEdit() self.calclvl.ToDateTime.setDateTime(datestring_to_date('2010-01-01 00:00:00')) self.calclvl.checkBox_overwrite_prev.setChecked(False) self.calclvl.calcselected() #self.checkBox_skipnulls test_string = utils_for_tests.create_test_string( db_utils.sql_load_fr_db('SELECT obsid, date_time, meas, h_toc, level_masl FROM w_levels ORDER BY obsid, date_time')) reference_string = '(True, [(rb1, 2005-01-01 00:00:00, 222.0, 1.0, -221.0), (rb2, 2005-01-01 00:00:00, 444.0, 4.0, -440.0), (rb2, 2005-01-02 00:00:00, 555.0, None, 667.0)])' print(str(mock_messagebar.mock_calls)) print(test_string) assert test_string == reference_string
def test_calibrlogger_set_log_pos(self, mock_messagebar): db_utils.sql_alter_db("INSERT INTO obs_points (obsid) VALUES ('rb1')") db_utils.sql_alter_db( "INSERT INTO w_levels (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-01 00:00', 100)" ) db_utils.sql_alter_db( "INSERT INTO w_levels_logger (obsid, date_time, head_cm) VALUES ('rb1', '2017-02-01 00:00', 100)" ) calibrlogger = Calibrlogger(self.iface.mainWindow(), self.ms) calibrlogger.update_plot() calibrlogger.FromDateTime.setDateTime( date_utils.datestring_to_date('2000-01-01 00:00:00')) calibrlogger.LoggerPos.setText('2') gui_utils.set_combobox(calibrlogger.combobox_obsid, 'rb1 (uncalibrated)') calibrlogger.set_logger_pos() print(str(mock_messagebar.mock_calls)) test = utils_for_tests.create_test_string( db_utils.sql_load_fr_db('SELECT * FROM w_levels_logger')) ref = '(True, [(rb1, 2017-02-01 00:00, 100.0, None, None, 3.0, None)])' print(test) assert test == ref
def drop_db_views(self): sql1 = "delete from views_geometry_columns where view_name = 'strat_obs_p_for_qgsi2threejs'" sql2 = "drop view if exists strat_obs_p_for_qgsi2threejs" db_utils.sql_alter_db(sql1, dbconnection=self.dbconnection) db_utils.sql_alter_db(sql2, dbconnection=self.dbconnection) placeholder_sign = db_utils.placeholder_sign(self.dbconnection) sql1 = "delete from views_geometry_columns where view_name = %s" % placeholder_sign sql2 = "drop view if exists " for key in self.strat_layers_dict: db_utils.sql_alter_db(sql1, dbconnection=self.dbconnection, all_args=[(key, )]) db_utils.sql_alter_db(sql2 + key, dbconnection=self.dbconnection)
def test_calibrlogger_calc_best_fit_add_no_matches_same_to_date( self, mock_messagebar, skip_popup): db_utils.sql_alter_db("INSERT INTO obs_points (obsid) VALUES ('rb1')") db_utils.sql_alter_db( "INSERT INTO w_levels (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-01 00:00', 100)" ) db_utils.sql_alter_db( "INSERT INTO w_levels_logger (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-01 01:00', 50)" ) calibrlogger = Calibrlogger(self.iface.mainWindow(), self.ms) calibrlogger.update_plot() calibrlogger.loggerpos_masl_or_offset_state = 2 calibrlogger.FromDateTime.setDateTime( date_utils.datestring_to_date('2010-02-01 01:00')) calibrlogger.ToDateTime.setDateTime( date_utils.datestring_to_date('2017-02-01 01:00')) gui_utils.set_combobox(calibrlogger.combobox_obsid, 'rb1 (uncalibrated)') calibrlogger.bestFitSearchRadius.setText('2 hours') calibrlogger.calc_best_fit() test = utils_for_tests.create_test_string( db_utils.sql_load_fr_db('SELECT * FROM w_levels_logger')) ref = '(True, [(rb1, 2017-02-01 01:00, None, None, None, 50.0, None)])' print(test) assert test == ref
def test_add_view_obs_points_obs_lines_add(self, mock_messagebar): db_utils.sql_alter_db('''DROP VIEW IF EXISTS view_obs_points;''') db_utils.sql_alter_db('''DROP VIEW IF EXISTS view_obs_lines;''') db_utils.sql_alter_db( '''DELETE FROM views_geometry_columns WHERE view_name IN ('view_obs_points', 'view_obs_lines');''' ) assert not any([ db_utils.verify_table_exists('view_obs_points'), db_utils.verify_table_exists('view_obs_lines') ]) views_geometry_columns = db_utils.sql_load_fr_db( '''SELECT view_name FROM views_geometry_columns WHERE view_name IN ('view_obs_points', 'view_obs_lines') ORDER BY view_name;''' )[1] print(str(views_geometry_columns)) assert views_geometry_columns == [] utils.add_view_obs_points_obs_lines() print(str(mock_messagebar.mock_calls)) assert call.info( bar_msg= 'Views added. Please reload layers (Midvatten>Load default db-layers to qgis or "F7").' ) in mock_messagebar.mock_calls assert all([ db_utils.verify_table_exists('view_obs_points'), db_utils.verify_table_exists('view_obs_lines') ]) views_geometry_columns = db_utils.sql_load_fr_db( '''SELECT view_name FROM views_geometry_columns WHERE view_name IN ('view_obs_points', 'view_obs_lines') ORDER BY view_name;''' )[1] print(str(views_geometry_columns)) assert views_geometry_columns == [('view_obs_lines', ), ('view_obs_points', )]
def test_strat_symbology(self, mock_messagebar): db_utils.sql_alter_db( '''INSERT INTO obs_points (obsid, h_gs, geometry) VALUES ('1', 5, ST_GeomFromText('POINT(1 2)', 3006))''' ) db_utils.sql_alter_db( '''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geology, geoshort, capacity, development) VALUES ('1', 1, 0, 1, 'sand', 'sand', '3', 'j')''' ) db_utils.sql_alter_db( '''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geology, geoshort, capacity, development) VALUES ('1', 2, 1, 4.5, 'morän', 'morän', '3', 'j')''' ) @mock.patch('qgis.utils.iface', autospec=True) def _test(self, mock_iface): mock_mapcanvas = mock_iface.mapCanvas.return_value mock_mapcanvas.layerCount.return_value = 0 self.midvatten.load_strat_symbology() self.ss = self.midvatten.strat_symbology try: self.ss.create_symbology() except: print(str(mock_messagebar.mock_calls)) raise _test(self) root = QgsProject.instance().layerTreeRoot() test = utils.anything_to_string_representation( utils_for_tests.recursive_children(root)) ref = '["", "", [["Midvatten strat symbology", "", [["Rings", "", [["Bedrock", True, []], ["Layers", "", [["Geology", True, []], ["Hydro", True, []]]]]], ["Static bars", "", [["W levels", True, []], ["Bedrock", True, []], ["Layers", "", [["Geology", True, []], ["Hydro", True, []]]]]], ["Bars", "", [["W levels", True, []], ["Bedrock", True, []], ["Layers", "", [["Geology", True, []], ["Hydro", True, []]]]]]]]]]' assert test == ref assert mock_messagebar.mock_calls == []
def test_calc_selected_dont_overwrite_dont_skip_nulls(self, mock_selected_obsids, mock_messagebar, mock_skippopup): mock_selected_obsids.return_value = ['rb1', 'rb2'] db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, h_toc) VALUES ('rb1', 1)''') db_utils.sql_alter_db('''INSERT into w_levels (obsid, meas, date_time) VALUES ('rb1', 222, '2005-01-01 00:00:00')''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, h_toc) VALUES ('rb2', NULL)''') db_utils.sql_alter_db('''INSERT into w_levels (obsid, meas, date_time) VALUES ('rb2', 444, '2005-01-01 00:00:00')''') db_utils.sql_alter_db('''INSERT into w_levels (obsid, meas, level_masl, date_time) VALUES ('rb2', 555, 667, '2005-01-02 00:00:00')''') self.calclvl.FromDateTime = QtWidgets.QDateTimeEdit() self.calclvl.FromDateTime.setDateTime(datestring_to_date('2000-01-01 00:00:00')) self.calclvl.ToDateTime = QtWidgets.QDateTimeEdit() self.calclvl.ToDateTime.setDateTime(datestring_to_date('2010-01-01 00:00:00')) self.calclvl.checkBox_overwrite_prev.setChecked(False) self.calclvl.calcselected() #self.checkBox_skipnulls test_string = utils_for_tests.create_test_string( db_utils.sql_load_fr_db('SELECT obsid, date_time, meas, h_toc, level_masl FROM w_levels ORDER BY obsid, date_time')) reference_string = '(True, [(rb1, 2005-01-01 00:00:00, 222.0, None, None), (rb2, 2005-01-01 00:00:00, 444.0, None, None), (rb2, 2005-01-02 00:00:00, 555.0, None, 667.0)])' print(str(mock_messagebar.mock_calls)) print(test_string) assert test_string == reference_string
def test_vlayer(self, mock_skippopup, mock_messagebar): """ :param mock_skippopup: :param mock_messagebar: :return: """ for obsid in [1, 2, 3]: db_utils.sql_alter_db('''INSERT INTO obs_points (obsid) VALUES ({})'''.format(str(obsid))) self.create_vlayer() self.select_features() feature_ids = [feature.id() for feature in self.vlayer.getFeatures()] reference_ids = (1, 2, 3) assert self.vlayer.isValid() assert len(feature_ids) == len(reference_ids) assert tuple(feature_ids) == reference_ids assert tuple(sorted([x for x in self.vlayer.selectedFeatureIds()])) == reference_ids assert tuple(sorted([x.id() for x in self.vlayer.getSelectedFeatures()])) == reference_ids assert tuple(sorted([x.id() for x in self.vlayer.getFeatures(feature_ids)])) == reference_ids assert self.vlayer.featureCount() == 3
def test_calibrlogger_adjust_trend(self, mock_messagebar): db_utils.sql_alter_db(u"INSERT INTO obs_points (obsid) VALUES ('rb1')") db_utils.sql_alter_db(u"INSERT INTO w_levels_logger (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-01 00:00', 100)") db_utils.sql_alter_db(u"INSERT INTO w_levels_logger (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-10 00:00', 200)") db_utils.sql_alter_db(u"INSERT INTO w_levels (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-01 00:00', 200)") db_utils.sql_alter_db(u"INSERT INTO w_levels (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-10 00:00', 100)") calibrlogger = Calibrlogger(self.iface.mainWindow(), self.ms) gui_utils.set_combobox(calibrlogger.combobox_obsid, u'rb1 (uncalibrated)') calibrlogger.update_plot() calibrlogger.FromDateTime.setDateTime(date_utils.datestring_to_date(u'2000-01-01 00:00:00')) calibrlogger.L1_date.setDateTime(date_utils.datestring_to_date(u'2017-02-01 00:00')) calibrlogger.L2_date.setDateTime(date_utils.datestring_to_date(u'2017-02-10 00:00')) calibrlogger.M1_date.setDateTime(date_utils.datestring_to_date(u'2017-02-01 00:00')) calibrlogger.M2_date.setDateTime(date_utils.datestring_to_date(u'2017-02-10 00:00')) calibrlogger.adjust_trend_func() test = utils_for_tests.create_test_string(db_utils.sql_load_fr_db(u'SELECT * FROM w_levels_logger')) print(mock_messagebar.mock_calls) print(test) ref = u'(True, [(rb1, 2017-02-01 00:00, None, None, None, 100.0, None), (rb1, 2017-02-10 00:00, None, None, None, -2.84217094304e-14, None)])' assert test == ref
def test_export_spatialite_with_umlauts(self, mock_skip_popup, mock_iface, mock_find_layer, mock_newdbpath, mock_verify, mock_selection, mock_locale, mock_createdb_crs_question, mock_messagebar): mock_selection.return_value = ('åäö', ) mock_find_layer.return_value.crs.return_value.authid.return_value = 'EPSG:3006' mock_createdb_crs_question.return_value = [3006, True] mock_newdbpath.return_value = (EXPORT_DB_PATH, '') mock_verify.return_value = 0 db_utils.sql_alter_db( '''INSERT INTO obs_points (obsid, geometry) VALUES ('åäö', ST_GeomFromText('POINT(633466 711659)', 3006))''' ) db_utils.sql_alter_db('''INSERT INTO zz_staff (staff) VALUES ('s1')''') db_utils.sql_alter_db( '''INSERT INTO comments (obsid, date_time, staff, comment) VALUES ('åäö', '2015-01-01 00:00:00', 's1', 'comment1')''' ) mock_locale.return_value.answer = 'ok' mock_locale.return_value.value = 'sv_SE' self.midvatten.export_spatialite() sql_list = [ '''select obsid, ST_AsText(geometry) from obs_points''', '''select staff from zz_staff''', '''select obsid, date_time, staff, comment from comments''' ] conn = db_utils.connect_with_spatialite_connect(EXPORT_DB_PATH) curs = conn.cursor() test_list = [] for sql in sql_list: test_list.append('\n' + sql + '\n') test_list.append(curs.execute(sql).fetchall()) conn.commit() conn.close() test_string = utils_for_tests.create_test_string(test_list) reference_string = [ '''[''', '''select obsid, ST_AsText(geometry) from obs_points''', ''', [(åäö, POINT(633466 711659))], ''', '''select staff from zz_staff''', ''', [(s1)], ''', '''select obsid, date_time, staff, comment from comments''', ''', [(åäö, 2015-01-01 00:00:00, s1, comment1)]]''' ] reference_string = '\n'.join(reference_string) print("Ref") print(reference_string) print("Test") print(test_string) assert test_string == reference_string
def test_calibrlogger_last_calibration(self, mock_messagebar): db_utils.sql_alter_db("INSERT INTO obs_points (obsid) VALUES ('rb1')") db_utils.sql_alter_db("INSERT INTO w_levels_logger (obsid, date_time, head_cm, level_masl) VALUES ('rb1', '2017-02-01 00:00', 50, 100)") db_utils.sql_alter_db("INSERT INTO w_levels_logger (obsid, date_time, head_cm, level_masl) VALUES ('rb1', '2017-03-01 00:00', 100, NULL)") calibrlogger = Calibrlogger(self.iface.mainWindow(), self.ms) calibrlogger.update_plot() test = utils_for_tests.create_test_string(calibrlogger.getlastcalibration(calibrlogger.selected_obsid)) ref = '[(2017-02-01 00:00, 99.5)]' assert test == ref
def test_calibrlogger_last_calibration(self, mock_messagebar): db_utils.sql_alter_db(u"INSERT INTO obs_points (obsid) VALUES ('rb1')") db_utils.sql_alter_db(u"INSERT INTO w_levels_logger (obsid, date_time, head_cm, level_masl) VALUES ('rb1', '2017-02-01 00:00', 50, 100)") db_utils.sql_alter_db(u"INSERT INTO w_levels_logger (obsid, date_time, head_cm, level_masl) VALUES ('rb1', '2017-03-01 00:00', 100, NULL)") calibrlogger = Calibrlogger(self.iface.mainWindow(), self.ms) calibrlogger.update_plot() test = utils_for_tests.create_test_string(calibrlogger.getlastcalibration(calibrlogger.selected_obsid)) ref = u'[(2017-02-01 00:00, 99.5)]' assert test == ref
def test_calibrlogger_set_last_calibration_zero(self, mock_messagebar): db_utils.sql_alter_db("INSERT INTO obs_points (obsid) VALUES ('rb1')") db_utils.sql_alter_db("INSERT INTO w_levels_logger (obsid, date_time, head_cm, level_masl) VALUES ('rb1', '2017-02-01 00:00', 100, 1)") db_utils.sql_alter_db("INSERT INTO w_levels_logger (obsid, date_time, head_cm, level_masl) VALUES ('rb1', '2017-03-01 00:00', 100, NULL)") calibrlogger = Calibrlogger(self.iface.mainWindow(), self.ms) """(level_masl - (head_cm/100))""" calibrlogger.update_plot() res = calibrlogger.getlastcalibration(calibrlogger.selected_obsid) test = utils_for_tests.create_test_string(calibrlogger.INFO.text()) ref = 'Last pos. for logger in rb1 was 0.0 masl at 2017-02-01 00:00' assert test == ref
def test_plot_section_with_depth(self, mock_messagebar): db_utils.sql_alter_db('''INSERT INTO obs_lines (obsid, geometry) VALUES ('L1', ST_GeomFromText('LineString(633466.711659 6720684.24498, 633599.530455 6720727.016568)', 3006))''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry, length) VALUES ('P1', ST_GeomFromText('POINT(633466 711659)', 3006), 2)''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry, length) VALUES ('P2', ST_GeomFromText('POINT(6720727 016568)', 3006), '1')''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry, length) VALUES ('P3', ST_GeomFromText('POINT(6720727 016568)', 3006), NULL)''') self.create_and_select_vlayer() @mock.patch('midvatten_utils.getselectedobjectnames', autospec=True) @mock.patch('qgis.utils.iface', autospec=True) def _test_plot_section_with_depth(self, mock_iface, mock_getselectedobjectnames): mock_iface.mapCanvas.return_value.currentLayer.return_value = self.vlayer mock_getselectedobjectnames.return_value = ('P1', 'P2', 'P3') mock_mapcanvas = mock_iface.mapCanvas.return_value mock_mapcanvas.layerCount.return_value = 0 self.midvatten.plot_section() self.myplot = self.midvatten.myplot _test_plot_section_with_depth(self) assert not mock_messagebar.warning.called assert not mock_messagebar.critical.called
def update_level_masl_from_level_masl(self, obsid, fr_d_t, to_d_t, newzref): utils.start_waiting_cursor() """ Updates the level masl using newzref :param obsid: (str) The obsid :param fr_d_t: (datetime) start of calibration :param to_d_t: (datetime) end of calibration :param newzref: (int/float/str [m]) The correction that should be made against the head [m] :return: None """ sql =r"""UPDATE w_levels_logger SET level_masl = """ sql += str(newzref) sql += """ + level_masl WHERE obsid = '""" sql += obsid sql += """' AND level_masl IS NOT NULL""" # Sqlite seems to have problems with date comparison date_time >= a_date, so they have to be converted into total seconds first. date_time_as_epoch = db_utils.cast_date_time_as_epoch() sql += """ AND %s > %s"""%(date_time_as_epoch, str((fr_d_t - datetime.datetime(1970,1,1)).total_seconds())) sql += """ AND %s < %s""" % (date_time_as_epoch, str((to_d_t - datetime.datetime(1970, 1, 1)).total_seconds())) dummy = db_utils.sql_alter_db(sql) utils.stop_waiting_cursor()
def test_add_view_obs_points_obs_lines_add(self, mock_messagebar): db_utils.sql_alter_db('''DROP VIEW IF EXISTS view_obs_points;''') db_utils.sql_alter_db('''DROP VIEW IF EXISTS view_obs_lines;''') db_utils.sql_alter_db('''DELETE FROM views_geometry_columns WHERE view_name IN ('view_obs_points', 'view_obs_lines');''') assert not any([db_utils.verify_table_exists('view_obs_points'), db_utils.verify_table_exists('view_obs_lines')]) views_geometry_columns = db_utils.sql_load_fr_db('''SELECT view_name FROM views_geometry_columns WHERE view_name IN ('view_obs_points', 'view_obs_lines') ORDER BY view_name;''')[1] print(str(views_geometry_columns)) assert views_geometry_columns == [] utils.add_view_obs_points_obs_lines() print(str(mock_messagebar.mock_calls)) assert call.info(bar_msg='Views added. Please reload layers (Midvatten>Load default db-layers to qgis or "F7").') in mock_messagebar.mock_calls assert all([db_utils.verify_table_exists('view_obs_points'), db_utils.verify_table_exists('view_obs_lines')]) views_geometry_columns = db_utils.sql_load_fr_db('''SELECT view_name FROM views_geometry_columns WHERE view_name IN ('view_obs_points', 'view_obs_lines') ORDER BY view_name;''')[1] print(str(views_geometry_columns)) assert views_geometry_columns == [('view_obs_lines',), ('view_obs_points',)]
def test_calibrlogger_calc_best_fit_log_pos_out_of_radius(self, mock_messagebar, skip_popup): db_utils.sql_alter_db("INSERT INTO obs_points (obsid) VALUES ('rb1')") db_utils.sql_alter_db("INSERT INTO w_levels (obsid, date_time, level_masl) VALUES ('rb1', '2017-02-01 00:00', 100)") db_utils.sql_alter_db("INSERT INTO w_levels_logger (obsid, date_time, head_cm) VALUES ('rb1', '2017-03-01 00:00', 50)") calibrlogger = Calibrlogger(self.iface.mainWindow(), self.ms) calibrlogger.update_plot() calibrlogger.loggerpos_masl_or_offset_state = 1 calibrlogger.FromDateTime.setDateTime(date_utils.datestring_to_date('2000-01-01 00:00:00')) gui_utils.set_combobox(calibrlogger.combobox_obsid, 'rb1 (uncalibrated)') calibrlogger.calc_best_fit() test = utils_for_tests.create_test_string(db_utils.sql_load_fr_db('SELECT * FROM w_levels_logger')) ref = '(True, [(rb1, 2017-03-01 00:00, 50.0, None, None, None, None)])' print(test) assert test == ref
def test_export_spatialite_with_umlauts(self, mock_skip_popup, mock_iface, mock_find_layer, mock_newdbpath, mock_verify, mock_selection, mock_locale, mock_createdb_crs_question, mock_messagebar): mock_selection.return_value = ('åäö', ) mock_find_layer.return_value.crs.return_value.authid.return_value = 'EPSG:3006' mock_createdb_crs_question.return_value = [3006, True] mock_newdbpath.return_value = (EXPORT_DB_PATH, '') mock_verify.return_value = 0 db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry) VALUES ("åäö", ST_GeomFromText('POINT(633466 711659)', 3006))''') db_utils.sql_alter_db('''INSERT INTO zz_staff (staff) VALUES ('s1')''') db_utils.sql_alter_db('''INSERT INTO comments (obsid, date_time, staff, comment) VALUES ('åäö', '2015-01-01 00:00:00', 's1', 'comment1')''') mock_locale.return_value.answer = 'ok' mock_locale.return_value.value = 'sv_SE' self.midvatten.export_spatialite() sql_list = ['''select obsid, ST_AsText(geometry) from obs_points''', '''select staff from zz_staff''', '''select obsid, date_time, staff, comment from comments'''] conn = db_utils.connect_with_spatialite_connect(EXPORT_DB_PATH) curs = conn.cursor() test_list = [] for sql in sql_list: test_list.append('\n' + sql + '\n') test_list.append(curs.execute(sql).fetchall()) conn.commit() conn.close() test_string = utils_for_tests.create_test_string(test_list) reference_string = ['''[''', '''select obsid, ST_AsText(geometry) from obs_points''', ''', [(åäö, POINT(633466 711659))], ''', '''select staff from zz_staff''', ''', [(s1)], ''', '''select obsid, date_time, staff, comment from comments''', ''', [(åäö, 2015-01-01 00:00:00, s1, comment1)]]'''] reference_string = '\n'.join(reference_string) assert test_string == reference_string
def test_plot_section_p_label_lengths_with_geology(self, mock_messagebar): db_utils.sql_alter_db('''INSERT INTO obs_lines (obsid, geometry) VALUES ('1', ST_GeomFromText('LINESTRING(633466.711659 6720684.24498, 633599.530455 6720727.016568)', 3006))''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry, length) VALUES ('P1', ST_GeomFromText('POINT(633466 711659)', 3006), 2)''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry, length) VALUES ('P2', ST_GeomFromText('POINT(6720727 016568)', 3006), '1')''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry, length) VALUES ('P3', ST_GeomFromText('POINT(6720727 016568)', 3006), NULL)''') db_utils.sql_alter_db('''INSERT INTO w_levels (obsid, date_time, meas, h_toc, level_masl) VALUES ('P1', '2015-01-01 00:00:00', '15', '200', '185')''') db_utils.sql_alter_db('''INSERT INTO w_levels (obsid, date_time, meas, h_toc, level_masl) VALUES ('P2', '2015-01-01 00:00:00', '17', '200', '183')''') db_utils.sql_alter_db('''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geoshort) VALUES ('P1', 1, 0, 1, 'sand')''') db_utils.sql_alter_db('''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geoshort) VALUES ('P1', 2, 1, 2, 'gravel')''') self.create_and_select_vlayer() @mock.patch('midvatten_utils.getselectedobjectnames', autospec=True) @mock.patch('qgis.utils.iface', autospec=True) def _test(self, mock_iface, mock_getselectedobjectnames): mock_iface.mapCanvas.return_value.currentLayer.return_value = self.vlayer mock_getselectedobjectnames.return_value = ('P1', 'P2', 'P3') mock_mapcanvas = mock_iface.mapCanvas.return_value mock_mapcanvas.layerCount.return_value = 0 self.midvatten.plot_section() self.myplot = self.midvatten.myplot self.myplot.Stratigraphy_checkBox.setChecked(True) gui_utils.set_combobox(self.myplot.wlvltableComboBox, 'w_levels') self.myplot.datetimetextEdit.append('2015') self.myplot.draw_plot() _test(self) print(str(mock_messagebar.mock_calls)) print(str(self.myplot.p)) print(str(self.myplot.Labels)) assert len(self.myplot.skipped_bars) == len(self.myplot.Labels) assert len(self.myplot.skipped_bars) == 4
def test_plot_section_with_w_levels(self, mock_messagebar): db_utils.sql_alter_db('''INSERT INTO obs_lines (obsid, geometry) VALUES ('L1', ST_GeomFromText('LINESTRING(633466.711659 6720684.24498, 633599.530455 6720727.016568)', 3006))''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry, length) VALUES ('P1', ST_GeomFromText('POINT(633466 711659)', 3006), 2)''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry, length) VALUES ('P2', ST_GeomFromText('POINT(6720727 016568)', 3006), '1')''') db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry, length) VALUES ('P3', ST_GeomFromText('POINT(6720727 016568)', 3006), NULL)''') db_utils.sql_alter_db('''INSERT INTO w_levels (obsid, date_time, meas, h_toc, level_masl) VALUES ('P1', '2015-01-01 00:00:00', '15', '200', '185')''') db_utils.sql_alter_db('''INSERT INTO w_levels (obsid, date_time, meas, h_toc, level_masl) VALUES ('P2', '2015-01-01 00:00:00', '17', '200', '183')''') self.create_and_select_vlayer() @mock.patch('midvatten_utils.getselectedobjectnames', autospec=True) @mock.patch('qgis.utils.iface', autospec=True) def _test(self, mock_iface, mock_getselectedobjectnames): mock_iface.mapCanvas.return_value.currentLayer.return_value = self.vlayer mock_getselectedobjectnames.return_value = ('P1', 'P2', 'P3') mock_mapcanvas = mock_iface.mapCanvas.return_value mock_mapcanvas.layerCount.return_value = 0 self.midvatten.plot_section() self.myplot = self.midvatten.myplot gui_utils.set_combobox(self.myplot.wlvltableComboBox, 'w_levels') self.myplot.datetimetextEdit.append('2015') self.myplot.draw_plot() _test(self) assert not mock_messagebar.warning.called assert not mock_messagebar.critical.called