コード例 #1
0
    def test_load_hard_coded_settings(self, mock_messagebar, mock_join,
                                      mock_hardcoded_template):
        self.midvatten.ms.settingsdict['secplot_loaded_template'] = ''
        self.midvatten.ms.settingsdict['secplot_templates'] = ''
        mock_join.return_value = ''
        test_dict = {"hardcoded": 1}
        mock_hardcoded_template.return_value = test_dict

        secplottemplates = PlotTemplates(
            self.sectionplot, self.template_list, self.edit_button,
            self.load_button, self.save_as_button, self.import_button,
            self.remove_button, self.template_folder,
            'secplot_templates', 'secplot_loaded_template',
            defs.secplot_default_template(), self.midvatten.ms)
        test = utils.anything_to_string_representation(
            secplottemplates.loaded_template)
        reference = utils.anything_to_string_representation(test_dict)

        assert call.warning(
            bar_msg=
            'Default template not found, loading hard coded default template.'
        ) in mock_messagebar.mock_calls
        assert call.info(
            log_msg='Loaded template from default hard coded template.'
        ) in mock_messagebar.mock_calls
        assert test == reference
コード例 #2
0
    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)"]'''
コード例 #3
0
    def test_load_default_file(self):
        default_file = '''{'Axes_set_ylabel': {'fontsize': 10}, 'wlevels_Axes_plot': {'DEFAULT': {'marker': 'v', 'markersize': 6, 'linewidth': 1, 'linestyle': '-'}}, 'geology_Axes_bar': {'edgecolor': 'black'}, 'obsid_Axes_bar': {'edgecolor': 'black', 'linewidth': 0.5, 'fill': False}, 'dems_Axes_plot': {'DEFAULT': {'marker': 'None', 'linewidth': 1, 'linestyle': '-'}}, 'Axes_set_xlabel': {'fontsize': 10}, 'Axes_set_ylim': None, 'plot_width': None, 'grid_Axes_grid': {'color': '0.65', 'b': True, 'linestyle': '-', 'which': 'both'}, 'legend_Axes_legend': {'loc': 0, 'framealpha': 1, 'fontsize': 10}, 'legend_Frame_set_fill': False, 'plot_height': None, 'layer_Axes_annotate': {'va': 'center', 'xytext': (5, 0), 'fontsize': 9, 'bbox': {'alpha': 0.6, 'fc': 'white', 'boxstyle': 'square,pad=0.05', 'edgecolor': 'white'}, 'ha': 'left', 'textcoords': 'offset points'}, 'ticklabels_Text_set_fontsize': {'fontsize': 10}, 'legend_Text_set_fontsize': 10, 'Figure_subplots_adjust': {}, 'Axes_set_xlim': None, 'obsid_Axes_annotate': {'va': 'top', 'xytext': (0, 10), 'fontsize': 9, 'bbox': {'alpha': 0.4, 'fc': 'white', 'boxstyle': 'square,pad=0.05', 'edgecolor': 'white'}, 'rotation': 0, 'ha': 'center', 'textcoords': 'offset points'}, 'drillstop_Axes_plot': {'color': 'black', 'marker': '^', 'markersize': 8, 'linestyle': ''}, 'legend_Frame_set_facecolor': '1'}'''
        as_dict = ast.literal_eval(default_file)

        self.midvatten.ms.settingsdict['secplot_loaded_template'] = ''

        with utils.tempinput(default_file, 'utf-8') as f1:

            @mock.patch('midvatten_utils.MessagebarAndLog')
            @mock.patch('os.path.join')
            def _test(self, filename, mock_join, mock_messagebar):
                mock_join.return_value = filename
                self.midvatten.ms.settingsdict['secplot_templates'] = filename
                secplottemplates = PlotTemplates(
                    self.sectionplot, self.template_list, self.edit_button,
                    self.load_button, self.save_as_button, self.import_button,
                    self.remove_button, self.template_folder,
                    'secplot_templates', 'secplot_loaded_template',
                    defs.secplot_default_template(), self.midvatten.ms)

                return secplottemplates, mock_messagebar

            secplottemplates, mock_messagebar = _test(self, f1)

        assert call.info(log_msg='Loaded template from default template file.'
                         ) in mock_messagebar.mock_calls
        assert utils.anything_to_string_representation(
            secplottemplates.loaded_template
        ) == utils.anything_to_string_representation(as_dict)
コード例 #4
0
    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_calselected(self, mock_messagebar, mock_getselectedobjectnames, mock_iface):
        mock_getselectedobjectnames.return_value = ['1']

        db_utils.sql_alter_db('''INSERT INTO obs_points (obsid) VALUES ('1')''')
        db_utils.sql_alter_db('''INSERT INTO obs_points (obsid) VALUES ('2')''')
        db_utils.sql_alter_db('''INSERT INTO w_flow (obsid, instrumentid, flowtype, date_time, reading, unit) VALUES ('1', 'inst1', 'Accvol', '2019-02-02 00:00', 2.0, 'm3')''')
        db_utils.sql_alter_db('''INSERT INTO w_flow (obsid, instrumentid, flowtype, date_time, reading, unit) VALUES ('1', 'inst1', 'Accvol', '2019-02-01 00:00', 1.0, 'm3')''')
        db_utils.sql_alter_db('''INSERT INTO w_flow (obsid, instrumentid, flowtype, date_time, reading, unit) VALUES ('1', 'inst1', 'Accvol', '2019-02-04 00:00', 10.0, 'm3')''')
        db_utils.sql_alter_db('''INSERT INTO w_flow (obsid, instrumentid, flowtype, date_time, reading, unit) VALUES ('1', 'inst1', 'Accvol', '2019-02-03 00:00', 5.0, 'm3')''')
        db_utils.sql_alter_db('''INSERT INTO w_flow (obsid, instrumentid, flowtype, date_time, reading, unit) VALUES ('2', 'inst2', 'Accvol', '2019-02-04 00:00', 10.0, 'm3')''')
        db_utils.sql_alter_db('''INSERT INTO w_flow (obsid, instrumentid, flowtype, date_time, reading, unit) VALUES ('2', 'inst2', 'Accvol', '2019-02-03 00:00', 5.0, 'm3')''')

        widget = QtWidgets.QWidget()
        calcave = w_flow_calc_aveflow.Calcave(widget)
        calcave.FromDateTime.setDateTime(date_utils.datestring_to_date('2000-01-01 00:00:00'))
        calcave.calcselected()
        print(str(mock_messagebar.mock_calls))
        #insert or ignore into w_flow(obsid,instrumentid,flowtype,date_time,reading,unit) values('%s','%s','Aveflow','%s','%s','l/s')
        res = db_utils.sql_load_fr_db('''SELECT obsid, instrumentid, flowtype, date_time, ROUND(reading, 4), unit FROM w_flow ORDER BY obsid, flowtype, date_time''')[1]
        test = utils.anything_to_string_representation(res)


        print(test)
        reference = '[("1", "inst1", "Accvol", "2019-02-01 00:00", 1.0, "m3", ), ("1", "inst1", "Accvol", "2019-02-02 00:00", 2.0, "m3", ), ("1", "inst1", "Accvol", "2019-02-03 00:00", 5.0, "m3", ), ("1", "inst1", "Accvol", "2019-02-04 00:00", 10.0, "m3", ), ("1", "inst1", "Aveflow", "2019-02-02 00:00", 0.0116, "l/s", ), ("1", "inst1", "Aveflow", "2019-02-03 00:00", 0.0347, "l/s", ), ("1", "inst1", "Aveflow", "2019-02-04 00:00", 0.0579, "l/s", ), ("2", "inst2", "Accvol", "2019-02-03 00:00", 5.0, "m3", ), ("2", "inst2", "Accvol", "2019-02-04 00:00", 10.0, "m3", )]'
        #result_list = self.calcave.observations
        #reference_list = ['1', '2']
        assert test == reference
コード例 #6
0
    def test_load_default_file(self):
        default_file = '''{'Axes_set_ylabel': {'fontsize': 10}, 'wlevels_Axes_plot': {'DEFAULT': {'marker': 'v', 'markersize': 6, 'linewidth': 1, 'linestyle': '-'}}, 'geology_Axes_bar': {'edgecolor': 'black'}, 'obsid_Axes_bar': {'edgecolor': 'black', 'linewidth': 0.5, 'fill': False}, 'dems_Axes_plot': {'DEFAULT': {'marker': 'None', 'linewidth': 1, 'linestyle': '-'}}, 'Axes_set_xlabel': {'fontsize': 10}, 'Axes_set_ylim': None, 'plot_width': None, 'grid_Axes_grid': {'color': '0.65', 'b': True, 'linestyle': '-', 'which': 'both'}, 'legend_Axes_legend': {'loc': 0, 'framealpha': 1, 'fontsize': 10}, 'legend_Frame_set_fill': False, 'plot_height': None, 'layer_Axes_annotate': {'va': 'center', 'xytext': (5, 0), 'fontsize': 9, 'bbox': {'alpha': 0.6, 'fc': 'white', 'boxstyle': 'square,pad=0.05', 'edgecolor': 'white'}, 'ha': 'left', 'textcoords': 'offset points'}, 'ticklabels_Text_set_fontsize': {'fontsize': 10}, 'legend_Text_set_fontsize': 10, 'Figure_subplots_adjust': {}, 'Axes_set_xlim': None, 'obsid_Axes_annotate': {'va': 'top', 'xytext': (0, 10), 'fontsize': 9, 'bbox': {'alpha': 0.4, 'fc': 'white', 'boxstyle': 'square,pad=0.05', 'edgecolor': 'white'}, 'rotation': 0, 'ha': 'center', 'textcoords': 'offset points'}, 'drillstop_Axes_plot': {'color': 'black', 'marker': '^', 'markersize': 8, 'linestyle': ''}, 'legend_Frame_set_facecolor': '1'}'''
        as_dict = ast.literal_eval(default_file)

        self.midvatten.ms.settingsdict['secplot_loaded_template'] = ''

        with utils.tempinput(default_file, 'utf-8') as f1:

            @mock.patch('midvatten_utils.MessagebarAndLog')
            @mock.patch('os.path.join')
            def _test(self, filename, mock_join, mock_messagebar):
                mock_join.return_value = filename
                self.midvatten.ms.settingsdict['secplot_templates'] = filename
                secplottemplates = PlotTemplates(self.sectionplot, self.template_list, self.edit_button,
                                                 self.load_button,
                                                 self.save_as_button, self.import_button, self.remove_button,
                                                 self.template_folder, 'secplot_templates', 'secplot_loaded_template',
                                                 defs.secplot_default_template(), self.midvatten.ms)

                return secplottemplates, mock_messagebar

            secplottemplates, mock_messagebar = _test(self, f1)

        assert call.info(log_msg='Loaded template from default template file.') in mock_messagebar.mock_calls
        assert utils.anything_to_string_representation(secplottemplates.loaded_template) == utils.anything_to_string_representation(as_dict)
コード例 #7
0
    def test_convert_comma_to_points_for_double_columns(self):
        file_data = [['obsid', 'date_time', 'reading'], ['obs1,1', '2017-04-12 11:03', '123,456']]

        #(6, 'comment', 'text', 0, None, 0)
        tables_columns = ((0, 'obsid', 'text', 0, None, 0), (1, 'reading', 'double', 0, None, 0))
        test_string = utils.anything_to_string_representation(GeneralCsvImportGui.convert_comma_to_points_for_double_columns(file_data, tables_columns))
        reference = '[["obsid", "date_time", "reading"], ["obs1,1", "2017-04-12 11:03", "123.456"]]'
        assert test_string == reference
コード例 #8
0
 def set_db(self):
     self.midvsettingsdialogdock.ms.settingsdict[
         'database'] = utils.anything_to_string_representation(
             {u'postgis': {
                 u'connection': self.connection
             }})
     self.midvsettingsdialogdock.ms.save_settings('database')
     self.midvsettingsdialogdock.load_plot_settings()
コード例 #9
0
    def test_import_files_load(self):
        afile = '''{"loaded_file": 2}'''
        as_dict = ast.literal_eval(afile)

        self.midvatten.ms.settingsdict['secplot_loaded_template'] = ''

        with utils.tempinput(afile, 'utf-8') as f1:

            @mock.patch('midvatten_utils.select_files')
            @mock.patch('midvatten_utils.MessagebarAndLog')
            @mock.patch('os.path.join')
            def _test(self, filename, mock_join, mock_messagebar,
                      mock_select_files):
                mock_join.return_value = ''
                mock_select_files.return_value = [filename]
                self.midvatten.ms.settingsdict['secplot_templates'] = ''
                secplottemplates = PlotTemplates(
                    self.sectionplot, self.template_list, self.edit_button,
                    self.load_button, self.save_as_button, self.import_button,
                    self.remove_button, self.template_folder,
                    'secplot_templates', 'secplot_loaded_template',
                    defs.secplot_default_template(), self.midvatten.ms)
                secplottemplates.import_templates()
                item = [
                    self.template_list.item(idx)
                    for idx in range(self.template_list.count())
                ][0]
                item.setSelected(True)
                secplottemplates.load()

                return secplottemplates, mock_messagebar, filename

            secplottemplates, mock_messagebar, reference_filename = _test(
                self, f1)

        items = [
            self.template_list.item(idx)
            for idx in range(self.template_list.count())
        ]
        filename = items[0].filename
        assert filename == reference_filename

        test = utils.anything_to_string_representation(
            secplottemplates.loaded_template)
        reference = utils.anything_to_string_representation(as_dict)
        assert test == reference
コード例 #10
0
    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)"]'''
コード例 #11
0
def db_setup_as_string():
    tables = db_utils.get_tables()
    #tables = db_utils.sql_load_fr_db(r"""SELECT tbl_name FROM sqlite_master WHERE (type='table' or type='view') and not (name in""" + db_utils.sqlite_internal_tables() + r""") ORDER BY tbl_name""")[1]
    res = []
    for table in sorted(tables):
        res.append((table,))
        table_info = db_utils.get_table_info(table)
        res.append(table_info)
    return utils.anything_to_string_representation(res)
コード例 #12
0
    def test_load_hard_coded_settings(self, mock_messagebar, mock_join, mock_hardcoded_template):
        self.midvatten.ms.settingsdict['secplot_loaded_template'] = ''
        self.midvatten.ms.settingsdict['secplot_templates'] = ''
        mock_join.return_value = ''
        test_dict = {"hardcoded": 1}
        mock_hardcoded_template.return_value = test_dict

        secplottemplates = PlotTemplates(self.sectionplot, self.template_list, self.edit_button,
                                         self.load_button,
                                         self.save_as_button, self.import_button, self.remove_button,
                                         self.template_folder, 'secplot_templates', 'secplot_loaded_template',
                                         defs.secplot_default_template(), self.midvatten.ms)
        test = utils.anything_to_string_representation(secplottemplates.loaded_template)
        reference = utils.anything_to_string_representation(test_dict)

        assert call.warning(bar_msg='Default template not found, loading hard coded default template.') in mock_messagebar.mock_calls
        assert call.info(log_msg='Loaded template from default hard coded template.') in mock_messagebar.mock_calls
        assert test == reference
コード例 #13
0
    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
コード例 #14
0
def db_setup_as_string():
    tables = db_utils.get_tables()
    #tables = db_utils.sql_load_fr_db(r"""SELECT tbl_name FROM sqlite_master WHERE (type='table' or type='view') and not (name in""" + db_utils.sqlite_internal_tables() + r""") ORDER BY tbl_name""")[1]
    res = []
    for table in sorted(tables):
        res.append((table, ))
        table_info = db_utils.get_table_info(table)
        res.append(table_info)
    return utils.anything_to_string_representation(res)
コード例 #15
0
    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
コード例 #16
0
    def test_plot_section_p_label_lengths_with_geology_changed_label(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.find_layer')
        @mock.patch('midvatten_utils.getselectedobjectnames', autospec=True)
        @mock.patch('qgis.utils.iface', autospec=True)
        def _test(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.secplot_templates.loaded_template['geology_Axes_bar'] = {'sand': {'label': 'sandtest', 'edgecolor': 'black', 'zorder': 5},
                                                                                  'grus': {'label': 'grustest', 'edgecolor': 'black', 'zorder': 5},
                                                                                  'DEFAULT': {'edgecolor': 'black', 'zorder': 5}}
            print("before: " + str(self.myplot.secplot_templates.loaded_template['geology_Axes_bar']))
            self.myplot.Stratigraphy_radioButton.setChecked(True)
            self.myplot.Legend_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))
        labels = [p.get_label() for p in self.myplot.p]
        assert len(self.myplot.skipped_bars) == len(self.myplot.labels)
        assert len(self.myplot.skipped_bars) == 4
        assert anything_to_string_representation(labels) == '''["sandtest", "grustest", "2015", "drillstop like %berg%", "_container2"]'''
        assert anything_to_string_representation(self.myplot.water_level_labels_duplicate_check) == '''["2015"]'''
コード例 #17
0
    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
コード例 #18
0
    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
コード例 #19
0
 def select_file(self):
     """ Open a dialog to locate the sqlite file and some more..."""
     dbpath, __ = QFileDialog.getOpenFileName(None, str("Select database:"), "*.sqlite")
     if dbpath:  # Only get new db name if not cancelling the FileDialog
         self.dbpath = dbpath
         self.midvsettingsdialogdock.ms.settingsdict['database'] = utils.anything_to_string_representation({'spatialite': {'dbpath': dbpath}})
         self.midvsettingsdialogdock.ms.save_settings('database')
         self.midvsettingsdialogdock.load_plot_settings()
         warn_about_old_database()
     else:  # debug
         utils.MessagebarAndLog.info(log_msg=ru(QCoreApplication.translate('SpatialiteSettings', "DB selection cancelled and still using database path %s"))%utils.returnunicode(self.midvsettingsdialogdock.ms.settingsdict['database']))
コード例 #20
0
    def ask_and_update_settings(self, objects_with_get_settings, settingskey, msg=''):

        old_string = utils.anything_to_string_representation(self.update_stored_settings(objects_with_get_settings))

        new_string = qgis.PyQt.QtWidgets.QInputDialog.getText(None, ru(QCoreApplication.translate('ExportToFieldLogger', "Edit settings string")), msg,
                                                           qgis.PyQt.QtWidgets.QLineEdit.Normal, old_string)
        if not new_string[1]:
            return False

        new_string_text = ru(new_string[0])

        self.update_settings(new_string_text, settingskey)
コード例 #21
0
    def test_load_from_msettings(self, mock_messagebar):
        test_str = '''{"test": 1}'''
        self.midvatten.ms.settingsdict['secplot_loaded_template'] = test_str
        self.midvatten.ms.settingsdict['secplot_templates'] = ''

        secplottemplates = PlotTemplates(self.sectionplot, self.template_list, self.edit_button, self.load_button,
                                               self.save_as_button, self.import_button, self.remove_button,
                                               self.template_folder, 'secplot_templates', 'secplot_loaded_template',
                                               defs.secplot_default_template(), self.midvatten.ms)

        assert call.info(log_msg='Loaded template from midvatten settings secplot_loaded_template.') in mock_messagebar.mock_calls
        assert utils.anything_to_string_representation(secplottemplates.loaded_template) == test_str
コード例 #22
0
 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))
コード例 #23
0
    def test_convert_comma_to_points_for_double_columns(self):
        file_data = [['obsid', 'date_time', 'reading'],
                     ['obs1,1', '2017-04-12 11:03', '123,456']]

        #(6, 'comment', 'text', 0, None, 0)
        tables_columns = ((0, 'obsid', 'text', 0, None, 0),
                          (1, 'reading', 'double', 0, None, 0))
        test_string = utils.anything_to_string_representation(
            GeneralCsvImportGui.convert_comma_to_points_for_double_columns(
                file_data, tables_columns))
        reference = '[["obsid", "date_time", "reading"], ["obs1,1", "2017-04-12 11:03", "123.456"]]'
        assert test_string == reference
コード例 #24
0
    def test_stratigraphy_missing_h_gs(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, h_toc, geometry) VALUES ('2', NULL, 10, ST_GeomFromText('POINT(6720727 016568)', 3006))''')
        db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, geometry) VALUES ('3', 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')''')
        db_utils.sql_alter_db('''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geology, geoshort, capacity, development) VALUES ('2', 1, 0, 1, 'sand', 'sand', '3', 'j')''')
        db_utils.sql_alter_db('''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geology, geoshort, capacity, development) VALUES ('2', 2, 1, 4.5, 'morän', 'morän', '3', 'j')''')
        db_utils.sql_alter_db('''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geology, geoshort, capacity, development) VALUES ('3', 1, 0, 2, 'sand', 'sand', '3', 'j')''')
        db_utils.sql_alter_db('''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geology, geoshort, capacity, development) VALUES ('3', 2, 2, 6, 'morän', 'morän', '3', 'j')''')
        self.create_and_select_vlayer()

        dlg = Stratigraphy(self.iface, self.vlayer, self.ms.settingsdict)

        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) == 1
        print(str(mock_skippopup.mock_calls))
        print(str(mock_messagebar.mock_calls))
        assert mock_skippopup.mock_calls == [mock.call('Warning, h_gs is missing. See messagebar.')]
        assert mock_messagebar.mock_calls == [mock.call.warning(bar_msg="Obsid 2: using h_gs '' failed, using 'h_toc' instead.", duration=90, log_msg='False'),
                                              mock.call.warning(bar_msg="Obsid 3: using h_gs '' failed, using '-1' instead.", duration=90, log_msg='False')]
        print("test: " + test)
        assert test == """{"1": SURVEY('1', 5.000000, '<QgsPointXY: POINT(633466 711659)>'), "2": SURVEY('2', 10.000000, '<QgsPointXY: POINT(6720727 16568)>'), "3": SURVEY('3', -1.000000, '<QgsPointXY: POINT(6720728 16569)>')}"""
        print("test_survey " + test_survey)
        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)"]'''
コード例 #25
0
    def test_plot_section_with_w_levels_duplicate_label(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')''')

        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(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
            gui_utils.set_combobox(self.myplot.wlvltableComboBox, 'w_levels')
            self.myplot.datetimetextEdit.append('2015')
            self.myplot.datetimetextEdit.append('2015')
            self.myplot.secplot_templates.loaded_template['wlevels_Axes_plot'] = {'2015': {'label': '1', 'linestyle': '-', 'linewidth': 1, 'marker': 'v', 'markersize': 6, 'zorder': 8},
                                                                                  '2015_2': {'label': '2', 'linestyle': '-', 'linewidth': 1, 'marker': 'v', 'markersize': 6, 'zorder': 8},
                                                                                  'DEFAULT': {'label': 'DEFAULT', 'linestyle': '-', 'linewidth': 1, 'marker': 'v', 'markersize': 6, 'zorder': 8}}
            self.myplot.draw_plot()

        _test(self)

        print(str(mock_messagebar.mock_calls))
        assert not mock_messagebar.warning.called
        assert not mock_messagebar.critical.called
        labels = [p.get_label() for p in self.myplot.p]
        assert anything_to_string_representation(labels) == '''["1", "2", "drillstop like %berg%", "_container0"]'''
        assert anything_to_string_representation(self.myplot.water_level_labels_duplicate_check) == '''["2015", "2015_2"]'''
コード例 #26
0
    def test_import_files_load(self):
        afile = '''{"loaded_file": 2}'''
        as_dict = ast.literal_eval(afile)

        self.midvatten.ms.settingsdict['secplot_loaded_template'] = ''

        with utils.tempinput(afile, 'utf-8') as f1:

            @mock.patch('midvatten_utils.select_files')
            @mock.patch('midvatten_utils.MessagebarAndLog')
            @mock.patch('os.path.join')
            def _test(self, filename, mock_join, mock_messagebar, mock_select_files):
                mock_join.return_value = ''
                mock_select_files.return_value = [filename]
                self.midvatten.ms.settingsdict['secplot_templates'] = ''
                secplottemplates = PlotTemplates(self.sectionplot, self.template_list, self.edit_button,
                                                 self.load_button,
                                                 self.save_as_button, self.import_button, self.remove_button,
                                                 self.template_folder, 'secplot_templates', 'secplot_loaded_template',
                                                 defs.secplot_default_template(), self.midvatten.ms)
                secplottemplates.import_templates()
                item = [self.template_list.item(idx) for idx in range(self.template_list.count())][0]
                item.setSelected(True)
                secplottemplates.load()

                return secplottemplates, mock_messagebar, filename

            secplottemplates, mock_messagebar, reference_filename = _test(self, f1)

        items = [self.template_list.item(idx) for idx in range(self.template_list.count())]
        filename = items[0].filename
        assert filename == reference_filename

        test = utils.anything_to_string_representation(secplottemplates.loaded_template)
        reference = utils.anything_to_string_representation(as_dict)
        assert test == reference
コード例 #27
0
    def test_piper_plot_default_settings(self, mock_showplot):
        mock_ms = mock.MagicMock()
        mock_ms.settingsdict = {r"""piper_cl""": '',
                                r"""piper_hco3""": '',
                                r"""piper_so4""": '',
                                r"""piper_na""": '',
                                r"""piper_k""": '',
                                r"""piper_ca""": '',
                                r"""piper_mg""": ''}
        mock_active_layer = mock.MagicMock()
        piperplot = piper.PiperPlot(mock_ms, mock_active_layer)
        piperplot.create_parameter_selection()

        test = utils.anything_to_string_representation(piperplot.ParameterList)
        ref = '''["(lower(parameter) like '%klorid%' or lower(parameter) like '%chloride%')", "(lower(parameter) like '%alkalinitet%' or lower(parameter) like '%alcalinity%')", "(lower(parameter) like '%sulfat%' or lower(parameter) like '%sulphat%')", "(lower(parameter) like '%natrium%')", "(lower(parameter) like '%kalium%' or lower(parameter) like '%potassium%')", "(lower(parameter) like '%kalcium%' or lower(parameter) like '%calcium%')", "(lower(parameter) like '%magnesium%')"]'''
        assert test == ref
コード例 #28
0
    def test_piper_plot_user_chosen_settings(self, mock_showplot):
        mock_ms = mock.MagicMock()
        mock_ms.settingsdict = {r"""piper_cl""": 'cl',
                                r"""piper_hco3""": 'hco3',
                                r"""piper_so4""": 'so4',
                                r"""piper_na""": 'na',
                                r"""piper_k""": 'k',
                                r"""piper_ca""": 'ca',
                                r"""piper_mg""": 'mg'}
        mock_active_layer = mock.MagicMock()
        piperplot = piper.PiperPlot(mock_ms, mock_active_layer)
        piperplot.create_parameter_selection()

        test = utils.anything_to_string_representation(piperplot.ParameterList)
        ref = '''["parameter = 'cl'", "parameter = 'hco3'", "parameter = 'so4'", "parameter = 'na'", "parameter = 'k'", "parameter = 'ca'", "parameter = 'mg'"]'''
        assert test == ref
コード例 #29
0
    def test_piper_plot_user_chosen_settings(self, mock_showplot):
        mock_ms = mock.MagicMock()
        mock_ms.settingsdict = {r"""piper_cl""": 'cl',
                                r"""piper_hco3""": 'hco3',
                                r"""piper_so4""": 'so4',
                                r"""piper_na""": 'na',
                                r"""piper_k""": 'k',
                                r"""piper_ca""": 'ca',
                                r"""piper_mg""": 'mg'}
        mock_active_layer = mock.MagicMock()
        piperplot = piper.PiperPlot(mock_ms, mock_active_layer)
        piperplot.create_parameter_selection()

        test = utils.anything_to_string_representation(piperplot.ParameterList)
        ref = '''["parameter = 'cl'", "parameter = 'hco3'", "parameter = 'so4'", "parameter = 'na'", "parameter = 'k'", "parameter = 'ca'", "parameter = 'mg'"]'''
        assert test == ref
コード例 #30
0
    def test_piper_plot_default_settings(self, mock_showplot):
        mock_ms = mock.MagicMock()
        mock_ms.settingsdict = {r"""piper_cl""": '',
                                r"""piper_hco3""": '',
                                r"""piper_so4""": '',
                                r"""piper_na""": '',
                                r"""piper_k""": '',
                                r"""piper_ca""": '',
                                r"""piper_mg""": ''}
        mock_active_layer = mock.MagicMock()
        piperplot = piper.PiperPlot(mock_ms, mock_active_layer)
        piperplot.create_parameter_selection()

        test = utils.anything_to_string_representation(piperplot.ParameterList)
        ref = '''["(lower(parameter) like '%klorid%' or lower(parameter) like '%chloride%')", "(lower(parameter) like '%alkalinitet%' or lower(parameter) like '%alcalinity%')", "(lower(parameter) like '%sulfat%' or lower(parameter) like '%sulphat%')", "(lower(parameter) like '%natrium%')", "(lower(parameter) like '%kalium%' or lower(parameter) like '%potassium%')", "(lower(parameter) like '%kalcium%' or lower(parameter) like '%calcium%')", "(lower(parameter) like '%magnesium%')"]'''
        assert test == ref
コード例 #31
0
    def test_prepare_qgis2threejs(self, mock_iface, mock_messagebar):
        self.init_qgis()

        dbconnection = db_utils.DbConnectionManager()
        dbconnection.execute(
            '''INSERT INTO obs_points (obsid, h_gs, geometry) VALUES ('1', 1, ST_GeomFromText('POINT(1 1)', 3006)); '''
        )
        dbconnection.execute(
            '''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geoshort) VALUES ('1', 1, 0, 1, 'torv'); '''
        )
        dbconnection.execute(
            '''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geoshort) VALUES ('1', 2, 1, 2, 'fyll'); '''
        )
        dbconnection.commit_and_closedb()
        #print(str(db_utils.sql_load_fr_db('''SELECT * FROM stratigraphy;''')))

        canvas = MagicMock()
        mock_iface.mapCanvas.return_value = canvas

        self.midvatten.prepare_layers_for_qgis2threejs()

        layers = [
            'strat_torv', 'strat_fyll', 'strat_lera', 'strat_silt',
            'strat_finsand', 'strat_mellansand', 'strat_sand',
            'strat_grovsand', 'strat_fingrus', 'strat_mellangrus',
            'strat_grus', 'strat_grovgrus', 'strat_morn', 'strat_berg',
            'strat_obs_p_for_qgsi2threejs'
        ]

        view_contents = []
        for l in layers:
            if l != 'strat_obs_p_for_qgsi2threejs':
                view_contents.append(
                    db_utils.sql_load_fr_db(
                        '''SELECT rowid, obsid, z_coord, height, ST_AsText(geometry) FROM {};'''
                        .format(l))[1])
        view_contents.append(
            db_utils.sql_load_fr_db(
                '''SELECT rowid, obsid, ST_AsText(geometry) FROM {};'''.format(
                    'strat_obs_p_for_qgsi2threejs'))[1])
        test = utils.anything_to_string_representation(view_contents)
        print(str(test))
        ref = '''[[(1, "1", 1.0, -1.0, "POINT(1 1)", )], [(2, "1", 0.0, -1.0, "POINT(1 1)", )], [], [], [], [], [], [], [], [], [], [], [], [], [(1, "1", "POINT(1 1)", )]]'''
        assert test == ref

        assert not mock_messagebar.mock_calls
コード例 #32
0
class MidvattenTestPostgisNotCreated(object):
    ALL_POSTGIS_SETTINGS = {u'nosetests': {u'estimatedMetadata': u'false', u'username': u'henrik3', u'publicOnly': u'false', u'service': u'', u'database': u'nosetests', u'dontResolveType': u'false', u'saveUsername': u'true', u'sslmode': u'1', u'host': u'127.0.0.1', u'authcfg': u'', u'geometryColumnsOnly': u'false', u'allowGeometrylessTables': u'false', u'password': u'0000', u'savePassword': u'false', u'port': u'5432'}}
    TEMP_DB_SETTINGS = {u'postgis': {u'connection': u'nosetests/127.0.0.1:5432/nosetests'}}
    SETTINGS_DATABASE = (utils.anything_to_string_representation(TEMP_DB_SETTINGS), True)

    mock_postgis_connections = mock.MagicMock()
    mock_postgis_connections.return_value = ALL_POSTGIS_SETTINGS

    mock_instance_settings_database = mock.MagicMock()
    mock_instance_settings_database.return_value.readEntry.return_value = SETTINGS_DATABASE

    def __init__(self):
        self.TEMP_DB_SETTINGS = MidvattenTestPostgisNotCreated.TEMP_DB_SETTINGS
        self.SETTINGS_DATABASE = MidvattenTestPostgisNotCreated.SETTINGS_DATABASE
        pass

    @mock.patch('db_utils.get_postgis_connections', mock_postgis_connections)
    @mock.patch('midvatten_utils.QgsProject.instance', mock_instance_settings_database)
    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))

    @mock.patch('db_utils.get_postgis_connections', mock_postgis_connections)
    @mock.patch('midvatten_utils.QgsProject.instance', mock_instance_settings_database)
    @mock.patch('midvatten_utils.MessagebarAndLog')
    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))
コード例 #33
0
    def test_prepare_qgis2threejs(self, mock_iface, mock_messagebar):
        self.init_qgis()

        dbconnection = db_utils.DbConnectionManager()
        dbconnection.execute('''INSERT INTO obs_points (obsid, h_gs, geometry) VALUES ('1', 1, ST_GeomFromText('POINT(1 1)', 3006)); ''')
        dbconnection.execute('''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geoshort) VALUES ('1', 1, 0, 1, 'torv'); ''')
        dbconnection.execute('''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geoshort) VALUES ('1', 2, 1, 2, 'fyll'); ''')
        dbconnection.commit_and_closedb()
        #print(str(db_utils.sql_load_fr_db('''SELECT * FROM stratigraphy;''')))


        canvas = MagicMock()
        mock_iface.mapCanvas.return_value = canvas

        self.midvatten.prepare_layers_for_qgis2threejs()

        layers = ['strat_torv',
                    'strat_fyll',
                    'strat_lera',
                    'strat_silt',
                    'strat_finsand',
                    'strat_mellansand',
                    'strat_sand',
                    'strat_grovsand',
                    'strat_fingrus',
                    'strat_mellangrus',
                    'strat_grus',
                    'strat_grovgrus',
                    'strat_morn',
                    'strat_berg',
                    'strat_obs_p_for_qgsi2threejs']

        view_contents = []
        for l in layers:
            if l != 'strat_obs_p_for_qgsi2threejs':
                view_contents.append(db_utils.sql_load_fr_db('''SELECT rowid, obsid, z_coord, height, ST_AsText(geometry) FROM {};'''.format(l))[1])
        view_contents.append(
            db_utils.sql_load_fr_db('''SELECT rowid, obsid, ST_AsText(geometry) FROM {};'''.format('strat_obs_p_for_qgsi2threejs'))[1])
        test = utils.anything_to_string_representation(view_contents)
        print(str(test))
        ref = '''[[(1, "1", 1.0, -1.0, "POINT(1 1)", )], [(2, "1", 0.0, -1.0, "POINT(1 1)", )], [], [], [], [], [], [], [], [], [], [], [], [], [(1, "1", "POINT(1 1)", )]]'''
        assert test == ref

        assert not mock_messagebar.mock_calls
コード例 #34
0
 def select_file(self):
     """ Open a dialog to locate the sqlite file and some more..."""
     dbpath = QFileDialog.getOpenFileName(None, str("Select database:"),
                                          "*.sqlite")
     if dbpath:  # Only get new db name if not cancelling the FileDialog
         self.dbpath = dbpath
         self.midvsettingsdialogdock.ms.settingsdict[
             'database'] = utils.anything_to_string_representation(
                 {u'spatialite': {
                     u'dbpath': dbpath
                 }})
         self.midvsettingsdialogdock.ms.save_settings('database')
         self.midvsettingsdialogdock.load_plot_settings()
         #self.midvsettingsdialogdock.LoadAndSelectLastSettings()
     else:  # debug
         utils.MessagebarAndLog.info(log_msg=ru(
             QCoreApplication.translate(
                 u'SpatialiteSettings',
                 u"DB selection cancelled and still using database path %s")
         ) % utils.returnunicode(
             self.midvsettingsdialogdock.ms.settingsdict['database']))
コード例 #35
0
    def ask_for_stored_settings(self, stored_settings):
        old_string = utils.anything_to_string_representation(
            stored_settings,
            itemjoiner=',\n',
            pad='    ',
            dictformatter='{\n%s}',
            listformatter='[\n%s]',
            tupleformatter='(\n%s, )')

        msg = ru(
            QCoreApplication.translate(
                'DrillreportUi',
                'Replace the settings string with a new settings string.'))
        new_string = qgis.PyQt.QtWidgets.QInputDialog.getText(
            None,
            ru(
                QCoreApplication.translate('DrillreportUi',
                                           "Edit settings string")), msg,
            qgis.PyQt.QtWidgets.QLineEdit.Normal, old_string)
        if not new_string[1]:
            raise utils.UserInterruptError()

        new_string_text = ru(new_string[0])
        if not new_string_text:
            return {}

        try:
            as_dict = ast.literal_eval(new_string_text)
        except Exception as e:
            utils.MessagebarAndLog.warning(bar_msg=ru(
                QCoreApplication.translate(
                    'DrillreportUi',
                    'Translating string to dict failed, see log message panel')
            ),
                                           log_msg=str(e))
            raise utils.UsageError()
        else:
            return as_dict
コード例 #36
0
    def ask_for_stored_settings(self, stored_settings):
        old_string = utils.anything_to_string_representation(stored_settings, itemjoiner=',\n', pad='    ',
                                                             dictformatter='{\n%s}',
                                                             listformatter='[\n%s]', tupleformatter='(\n%s, )')

        msg = ru(QCoreApplication.translate('DrillreportUi', 'Replace the settings string with a new settings string.'))
        new_string = qgis.PyQt.QtWidgets.QInputDialog.getText(None, ru(QCoreApplication.translate('DrillreportUi', "Edit settings string")), msg,
                                                           qgis.PyQt.QtWidgets.QLineEdit.Normal, old_string)
        if not new_string[1]:
            raise utils.UserInterruptError()

        new_string_text = ru(new_string[0])
        if not new_string_text:
            return {}

        try:
            as_dict = ast.literal_eval(new_string_text)
        except Exception as e:
            utils.MessagebarAndLog.warning(bar_msg=ru(QCoreApplication.translate('DrillreportUi', 'Translating string to dict failed, see log message panel')),
                                           log_msg=str(e))
            raise utils.UsageError()
        else:
            return as_dict
コード例 #37
0
def export_fieldlogger_defaults():
    current_locale = utils.getcurrentlocale()[0]

    if current_locale != 'sv_SE':
        input_field_browser =  [
            [0, (("input_field_list",[
            "Accvol.m3;numberDecimal|numberSigned; ",
            "DO.mg/L;numberDecimal|numberSigned; ",
            "Momflow.l/s;numberDecimal|numberSigned; ",
            "Momflow.m3/h;numberDecimal|numberSigned; ",
            "comment;text;Obsid related comment",
            "cond.µS/cm;numberDecimal|numberSigned; ",
            "f.comment;text;Measurement related comment",
            "l.comment;text;Measurement related comment",
            "meas.m;numberDecimal|numberSigned;depth to water",
            "pH;numberDecimal|numberSigned; ",
            "q.comment;text;Measurement related comment",
            "redox.mV;numberDecimal|numberSigned; ",
            "s.comment;text;Measurement related comment",
            "temp.°C;numberDecimal|numberSigned; ",
            "turb.FNU;numberDecimal|numberSigned; "
            ]),)]]
        input_fields_groups = [
            [0, (("input_field_group_list",
                  ["meas.m;numberDecimal|numberSigned;depth to water",
                   "l.comment;text;Measurement related comment"]),
                ("sublocation_suffix", "level"))],
            [1, (("input_field_group_list",
                  ["comment;text;Obsid related comment"]),
                 ("sublocation_suffix", "comment"))],
            [2, (("input_field_group_list",
                  ["cond.µS/cm;numberDecimal|numberSigned; ",
                   "DO.mg/L;numberDecimal|numberSigned; ",
                   "pH;numberDecimal|numberSigned; ",
                   "redox.mV;numberDecimal|numberSigned; ",
                   "temp.°C;numberDecimal|numberSigned; ",
                   "turb.FNU;numberDecimal|numberSigned; ",
                   "q.comment;text;Measurement related comment"]),
                 ("sublocation_suffix", "quality"))],
            [3, (("input_field_group_list",
                  ["temp.°C;numberDecimal|numberSigned; ",
                   "turb.FNU;numberDecimal|numberSigned; ",
                   "s.comment;text;Measurement related comment"]),
                 ("sublocation_suffix", "sample"))],
            [4, (("input_field_group_list",
                  ["Accvol.m3;numberDecimal|numberSigned; ",
                   "Momflow.l/s;numberDecimal|numberSigned; ",
                   "Momflow.m3/h;numberDecimal|numberSigned; ",
                   "f.comment;text;Measurement related comment"]),
                 ("sublocation_suffix", "flow"))]]
    else:
        input_field_browser = [[0, (("input_field_list", [
            "Accvol.m3;numberDecimal|numberSigned; ",
            "DO.mg/L;numberDecimal|numberSigned; ",
            "Momflow.l/s;numberDecimal|numberSigned; ",
            "Momflow.m3/h;numberDecimal|numberSigned; ",
            "f.kommentar;text;mätrelaterad kommentar",
            "k.kommentar;text;mätrelaterad kommentar",
            "kommentar;text;obsidrelaterad kommentar",
            "kond.µS/cm;numberDecimal|numberSigned; ",
            "meas.m;numberDecimal|numberSigned;djup till vatten",
            "n.kommentar;text;mätrelaterad kommentar",
            "nedmätning.m;numberDecimal|numberSigned;djup till vatten",
            "p.kommentar;text;mätrelaterad kommentar", "pH;numberDecimal|numberSigned; ",
            "redox.mV;numberDecimal|numberSigned; ", "temp.°C;numberDecimal|numberSigned; ",
            "turb.FNU;numberDecimal|numberSigned; "
            ], ), )]]

        input_fields_groups = [
            [0, (("input_field_group_list",
               ["nedmätning.m;numberDecimal|numberSigned;djup till vatten",
                "n.kommentar;text;mätrelaterad kommentar"]),
              ("sublocation_suffix", "nivå"))],
            [1, (("input_field_group_list",
                  ["kommentar;text;obsidrelaterad kommentar"]),
                 ("sublocation_suffix", "kommentar"))],
            [2, (("input_field_group_list",
                  ["kond.µS/cm;numberDecimal|numberSigned; ",
                   "DO.mg/L;numberDecimal|numberSigned; ",
                   "pH;numberDecimal|numberSigned; ",
                   "redox.mV;numberDecimal|numberSigned; ",
                   "temp.°C;numberDecimal|numberSigned; ",
                   "turb.FNU;numberDecimal|numberSigned; ",
                   "k.kommentar;text;mätrelaterad kommentar"]),
                 ("sublocation_suffix", "kvalitet"))],
            [3, (("input_field_group_list",
                  ["temp.°C;numberDecimal|numberSigned; ",
                   "turb.FNU;numberDecimal|numberSigned; ",
                   "p.kommentar;text;mätrelaterad kommentar"]),
                 ("sublocation_suffix", "prov"))],
            [4, (("input_field_group_list",
                  ["Accvol.m3;numberDecimal|numberSigned; ",
                   "Momflow.l/s;numberDecimal|numberSigned; ",
                   "Momflow.m3/h;numberDecimal|numberSigned; ",
                   "f.kommentar;text;mätrelaterad kommentar"],),
                 ("sublocation_suffix", "flöde"))]]

    input_field_browser = utils.anything_to_string_representation(input_field_browser)
    input_fields_groups = utils.anything_to_string_representation(input_fields_groups)
    return input_field_browser, input_fields_groups
コード例 #38
0
def export_fieldlogger_defaults():
    current_locale = utils.getcurrentlocale(
        print_error_message_in_bar=False)[0]

    if current_locale != 'sv_SE':
        input_field_browser = [[
            0,
            (("input_field_list", [
                "Accvol.m3;numberDecimal|numberSigned; ",
                "DO.mg/L;numberDecimal|numberSigned; ",
                "Momflow.l/s;numberDecimal|numberSigned; ",
                "Momflow.m3/h;numberDecimal|numberSigned; ",
                "comment;text;Obsid related comment",
                "cond.µS/cm;numberDecimal|numberSigned; ",
                "f.comment;text;Measurement related comment",
                "l.comment;text;Measurement related comment",
                "meas.m;numberDecimal|numberSigned;depth to water",
                "pH;numberDecimal|numberSigned; ",
                "q.comment;text;Measurement related comment",
                "redox.mV;numberDecimal|numberSigned; ",
                "s.comment;text;Measurement related comment",
                "temp.°C;numberDecimal|numberSigned; ",
                "turb.FNU;numberDecimal|numberSigned; "
            ]), )
        ]]
        input_fields_groups = [
            [
                0,
                (("input_field_group_list", [
                    "meas.m;numberDecimal|numberSigned;depth to water",
                    "l.comment;text;Measurement related comment"
                ]), ("sublocation_suffix", "level"))
            ],
            [
                1,
                (("input_field_group_list",
                  ["comment;text;Obsid related comment"]),
                 ("sublocation_suffix", "comment"))
            ],
            [
                2,
                (("input_field_group_list", [
                    "cond.µS/cm;numberDecimal|numberSigned; ",
                    "DO.mg/L;numberDecimal|numberSigned; ",
                    "pH;numberDecimal|numberSigned; ",
                    "redox.mV;numberDecimal|numberSigned; ",
                    "temp.°C;numberDecimal|numberSigned; ",
                    "turb.FNU;numberDecimal|numberSigned; ",
                    "q.comment;text;Measurement related comment"
                ]), ("sublocation_suffix", "quality"))
            ],
            [
                3,
                (("input_field_group_list", [
                    "temp.°C;numberDecimal|numberSigned; ",
                    "turb.FNU;numberDecimal|numberSigned; ",
                    "s.comment;text;Measurement related comment"
                ]), ("sublocation_suffix", "sample"))
            ],
            [
                4,
                (("input_field_group_list", [
                    "Accvol.m3;numberDecimal|numberSigned; ",
                    "Momflow.l/s;numberDecimal|numberSigned; ",
                    "Momflow.m3/h;numberDecimal|numberSigned; ",
                    "f.comment;text;Measurement related comment"
                ]), ("sublocation_suffix", "flow"))
            ]
        ]
    else:
        input_field_browser = [[
            0,
            ((
                "input_field_list",
                [
                    "Accvol.m3;numberDecimal|numberSigned; ",
                    "DO.mg/L;numberDecimal|numberSigned; ",
                    "Momflow.l/s;numberDecimal|numberSigned; ",
                    "Momflow.m3/h;numberDecimal|numberSigned; ",
                    "f.kommentar;text;mätrelaterad kommentar",
                    "k.kommentar;text;mätrelaterad kommentar",
                    "kommentar;text;obsidrelaterad kommentar",
                    "kond.µS/cm;numberDecimal|numberSigned; ",
                    "meas.m;numberDecimal|numberSigned;djup till vatten",
                    "n.kommentar;text;mätrelaterad kommentar",
                    "nedmätning.m;numberDecimal|numberSigned;djup till vatten",
                    "p.kommentar;text;mätrelaterad kommentar",
                    "pH;numberDecimal|numberSigned; ",
                    "redox.mV;numberDecimal|numberSigned; ",
                    "temp.°C;numberDecimal|numberSigned; ",
                    "turb.FNU;numberDecimal|numberSigned; "
                ],
            ), )
        ]]

        input_fields_groups = [
            [
                0,
                (("input_field_group_list", [
                    "nedmätning.m;numberDecimal|numberSigned;djup till vatten",
                    "n.kommentar;text;mätrelaterad kommentar"
                ]), ("sublocation_suffix", "nivå"))
            ],
            [
                1,
                (("input_field_group_list",
                  ["kommentar;text;obsidrelaterad kommentar"]),
                 ("sublocation_suffix", "kommentar"))
            ],
            [
                2,
                (("input_field_group_list", [
                    "kond.µS/cm;numberDecimal|numberSigned; ",
                    "DO.mg/L;numberDecimal|numberSigned; ",
                    "pH;numberDecimal|numberSigned; ",
                    "redox.mV;numberDecimal|numberSigned; ",
                    "temp.°C;numberDecimal|numberSigned; ",
                    "turb.FNU;numberDecimal|numberSigned; ",
                    "k.kommentar;text;mätrelaterad kommentar"
                ]), ("sublocation_suffix", "kvalitet"))
            ],
            [
                3,
                (("input_field_group_list", [
                    "temp.°C;numberDecimal|numberSigned; ",
                    "turb.FNU;numberDecimal|numberSigned; ",
                    "p.kommentar;text;mätrelaterad kommentar"
                ]), ("sublocation_suffix", "prov"))
            ],
            [
                4,
                ((
                    "input_field_group_list",
                    [
                        "Accvol.m3;numberDecimal|numberSigned; ",
                        "Momflow.l/s;numberDecimal|numberSigned; ",
                        "Momflow.m3/h;numberDecimal|numberSigned; ",
                        "f.kommentar;text;mätrelaterad kommentar"
                    ],
                ), ("sublocation_suffix", "flöde"))
            ]
        ]

    input_field_browser = utils.anything_to_string_representation(
        input_field_browser)
    input_fields_groups = utils.anything_to_string_representation(
        input_fields_groups)
    return input_field_browser, input_fields_groups
コード例 #39
0
 def set_db(self):
     if self.connection:
         self.midvsettingsdialogdock.ms.settingsdict['database'] = utils.anything_to_string_representation({'postgis': {'connection': self.connection}})
         self.midvsettingsdialogdock.ms.save_settings('database')
         self.midvsettingsdialogdock.load_plot_settings()
         warn_about_old_database()
コード例 #40
0
    def populate_postgis_db(self, verno, user_select_CRS='y', EPSG_code='4326'):

        dbconnection = db_utils.DbConnectionManager()
        db_settings = dbconnection.db_settings
        if not isinstance(db_settings, str):
            self.db_settings = ru(utils.anything_to_string_representation(dbconnection.db_settings))
        else:
            self.db_settings = ru(db_settings)
        if dbconnection.dbtype != 'postgis':
            raise utils.UsageError('Database type postgis not selected, check Midvatten settings!')

        dbconnection.execute('CREATE EXTENSION IF NOT EXISTS postgis;')

        result = dbconnection.execute_and_fetchall('select version(), PostGIS_full_version();')

        versionstext = ', '.join(result[0])

        utils.stop_waiting_cursor()
        set_locale = self.ask_for_locale()
        utils.start_waiting_cursor()

        if user_select_CRS=='y':
            utils.stop_waiting_cursor()
            EPSGID=str(self.ask_for_CRS(set_locale)[0])
            utils.start_waiting_cursor()
        else:
            EPSGID=EPSG_code

        if EPSGID=='0' or not EPSGID:
            raise utils.UserInterruptError()

        filenamestring = "create_db.sql"

        SQLFile = os.path.join(os.sep,os.path.dirname(__file__),"..","definitions",filenamestring)
        qgisverno = Qgis.QGIS_VERSION#We want to store info about which qgis-version that created the db
        replace_word_replace_with = [
            ('CHANGETORELEVANTEPSGID', ru(EPSGID)),
            ('CHANGETOPLUGINVERSION', ru(verno)),
            ('CHANGETOQGISVERSION', ru(qgisverno)),
            ('CHANGETODBANDVERSION', 'PostGIS version %s' % ru(versionstext)),
            ('CHANGETOLOCALE', ru(set_locale)),
            ('double', 'double precision'),
            ('"', ''),
            ('rowid as rowid', 'CTID as rowid'),
            ('POSTGIS ', '')]

        created_tables_sqls = {}
        with open(SQLFile, 'r') as f:
            f.readline()  # first line is encoding info....
            lines = [ru(line) for line in f]
        sql_lines = ['{};'.format(l) for l in ' '.join(lines).split(';') if l]
        for linenr, line in enumerate(sql_lines):
            if all([line, not line.startswith("#"), 'InitSpatialMetadata' not in line, 'SPATIALITE' not in line,
                    line.replace(';', '').strip().replace('\n', '').replace('\r', '')]):
                sql = self.replace_words(line, replace_word_replace_with)
                try:
                    dbconnection.execute(sql)
                except:
                    try:
                        print(str(sql))
                        print("numlines: " + str(len(sql_lines)))
                        print("Error on line nr {}".format(str(linenr)))
                        print("before " + sql_lines[linenr - 1])
                        if linenr + 1 < len(sql_lines):
                            print("after " + sql_lines[linenr + 1 ])
                    except:
                        pass
                    raise
                else:
                    _sql = sql.lstrip('\r').lstrip('\n').lstrip()
                    if _sql.startswith('CREATE TABLE'):
                        tablename = ' '.join(_sql.split()).split()[2]
                        created_tables_sqls[tablename] = sql

            #lines = [self.replace_words(line.decode('utf-8').rstrip('\n').rstrip('\r'), replace_word_replace_with) for line in f if all([line,not line.startswith("#"), 'InitSpatialMetadata' not in line])]
        #db_utils.sql_alter_db(lines)

        self.insert_datadomains(set_locale, dbconnection)

        execute_sqlfile(get_full_filename('insert_obs_points_triggers_postgis.sql'), dbconnection)

        execute_sqlfile(get_full_filename('insert_functions_postgis.sql'), dbconnection)

        self.add_metadata_to_about_db(dbconnection, created_tables_sqls)

        dbconnection.vacuum()

        dbconnection.commit_and_closedb()

        """
        #The intention is to keep layer styles in the database by using the class AddLayerStyles but due to limitations in how layer styles are stored in the database, I will put this class on hold for a while.

        #Finally add the layer styles info into the data base
        AddLayerStyles(dbpath)
        """
        utils.stop_waiting_cursor()
コード例 #41
0
    def test_stratigraphy_missing_h_gs(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, h_toc, geometry) VALUES ('2', NULL, 10, ST_GeomFromText('POINT(6720727 016568)', 3006))'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO obs_points (obsid, geometry) VALUES ('3', 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')'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geology, geoshort, capacity, development) VALUES ('2', 1, 0, 1, 'sand', 'sand', '3', 'j')'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geology, geoshort, capacity, development) VALUES ('2', 2, 1, 4.5, 'morän', 'morän', '3', 'j')'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geology, geoshort, capacity, development) VALUES ('3', 1, 0, 2, 'sand', 'sand', '3', 'j')'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO stratigraphy (obsid, stratid, depthtop, depthbot, geology, geoshort, capacity, development) VALUES ('3', 2, 2, 6, 'morän', 'morän', '3', 'j')'''
        )
        self.create_and_select_vlayer()

        dlg = Stratigraphy(self.iface, self.vlayer, self.ms.settingsdict)

        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) == 1
        print(str(mock_skippopup.mock_calls))
        print(str(mock_messagebar.mock_calls))
        assert mock_skippopup.mock_calls == [
            mock.call('Warning, h_gs is missing. See messagebar.')
        ]
        assert mock_messagebar.mock_calls == [
            mock.call.warning(
                bar_msg="Obsid 2: using h_gs '' failed, using 'h_toc' instead.",
                duration=90,
                log_msg='False'),
            mock.call.warning(
                bar_msg="Obsid 3: using h_gs '' failed, using '-1' instead.",
                duration=90,
                log_msg='False')
        ]
        print("test: " + test)
        assert test == """{"1": SURVEY('1', 5.000000, '<QgsPointXY: POINT(633466 711659)>'), "2": SURVEY('2', 10.000000, '<QgsPointXY: POINT(6720727 16568)>'), "3": SURVEY('3', -1.000000, '<QgsPointXY: POINT(6720728 16569)>')}"""
        print("test_survey " + test_survey)
        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)"]'''
コード例 #42
0
    def test_piper_plot_get_data(self, mock_showplot, mock_selected,
                                 mock_messagebar):

        db_utils.sql_alter_db(
            '''INSERT INTO obs_points (obsid, type, geometry) VALUES ('P1', 'well', ST_GeomFromText('POINT(633466 711659)', 3006))'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO obs_points (obsid, type, geometry) VALUES ('P2', 'notwell', ST_GeomFromText('POINT(6720727 016568)', 3006))'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P1', '1', 'chloride', '1', 'mg/l', '2017-01-01')'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P1', '1', 'alcalinity', '2', 'mg/l', '2017-01-01')'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P1', '1', 'sulphat', '3', 'mg/l', '2017-01-01')'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P1', '1', 'natrium', '4', 'mg/l', '2017-01-01')'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P1', '1', 'kalium', '5', 'mg/l', '2017-01-01')'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P1', '1', 'kalcium', '6', 'mg/l', '2017-01-01')'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P1', '1', 'magnesium', '7', 'mg/l', '2017-01-01')'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P2', '2', 'chloride', '10', 'mg/l', '2017-01-01')'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P2', '2', 'alcalinity', '20', 'mg/l', '2017-01-01')'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P2', '2', 'sulphat', '30', 'mg/l', '2017-01-01')'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P2', '2', 'natrium', '40', 'mg/l', '2017-01-01')'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P2', '2', 'kalium', '50', 'mg/l', '2017-01-01')'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P2', '2', 'kalcium', '60', 'mg/l', '2017-01-01')'''
        )
        db_utils.sql_alter_db(
            '''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P2', '2', 'magnesium', '70', 'mg/l', '2017-01-01')'''
        )
        """
        Manual calculation:
        (factor1 * reading_num) / factor2 = meq 
        1		1	/	35,453	=	0,028206357713029
        1		2	/	61,0168	=	0,032777857901431
        2	*	3	/	96,063	=	0,062459011273852

        1		4	/	22,9898	=	0,173990204351495
        1		5	/	39,0983	=	0,127882797973313
        sum                         0,301873002

        2	*	6	/	40,078	=	0,299416138529867
        2	*	7	/	24,305	=	0,576013166015223
                    /			
        1		10	/	35,453	=	0,282063577130285
        1		20	/	61,0168	=	0,327778579014304
        2	*	30	/	96,063	=	0,624590112738515

        1		40	/	22,9898	=	1,73990204351495
        1		50	/	39,0983	=	1,27882797973313
        sum                         3,018730023

        2	*	60	/	40,078	=	2,99416138529867
        2	*	70	/	24,305	=	5,76013166015223

        """

        mock_ms = mock.MagicMock()
        mock_ms.settingsdict = {
            r"""piper_cl""": '',
            r"""piper_hco3""": '',
            r"""piper_so4""": '',
            r"""piper_na""": '',
            r"""piper_k""": '',
            r"""piper_ca""": '',
            r"""piper_mg""": ''
        }
        mock_active_layer = mock.MagicMock()
        mock_selected.return_value = ['P1', 'P2']
        piperplot = piper.PiperPlot(mock_ms, mock_active_layer)
        piperplot.create_parameter_selection()
        piperplot.ms.settingsdict['piper_markers'] = 'obsid'
        piperplot.get_data_and_make_plot()
        data = piperplot.obsnp_nospecformat
        print("data: " + str(data))
        for l in data:
            for idx in range(3, 9):
                l[idx] = '{0:.10f}'.format(float(l[idx]))
        print("data: " + str(data))

        test_paramlist = utils.anything_to_string_representation(
            piperplot.ParameterList)
        ref_paramlist = '''["(lower(parameter) like '%klorid%' or lower(parameter) like '%chloride%')", "(lower(parameter) like '%alkalinitet%' or lower(parameter) like '%alcalinity%')", "(lower(parameter) like '%sulfat%' or lower(parameter) like '%sulphat%')", "(lower(parameter) like '%natrium%')", "(lower(parameter) like '%kalium%' or lower(parameter) like '%potassium%')", "(lower(parameter) like '%kalcium%' or lower(parameter) like '%calcium%')", "(lower(parameter) like '%magnesium%')"]'''
        assert test_paramlist == ref_paramlist

        test_data = tuple([tuple(_) for _ in data])

        ref_data = (('P1', '2017-01-01', 'well', '0.0282063577',
                     '0.0327778579', '0.0624590113', '0.3018730023',
                     '0.2994161385', '0.5760131660'),
                    ('P2', '2017-01-01', 'notwell', '0.2820635771',
                     '0.3277785790', '0.6245901127', '3.0187300232',
                     '2.9941613853', '5.7601316602'))

        print("test")
        print(test_data)
        print("REF")
        print(ref_data)
        assert test_data == ref_data

        assert len(mock_messagebar.mock_calls) == 0
コード例 #43
0
    def create_new_spatialite_db(self, verno, user_select_CRS='y', EPSG_code='4326', delete_srids=True):  #CreateNewDB(self, verno):
        """Open a new DataBase (create an empty one if file doesn't exists) and set as default DB"""

        utils.stop_waiting_cursor()
        set_locale = self.ask_for_locale()
        utils.start_waiting_cursor()

        if user_select_CRS=='y':
            utils.stop_waiting_cursor()
            EPSGID=str(self.ask_for_CRS(set_locale)[0])
            utils.start_waiting_cursor()
        else:
            EPSGID=EPSG_code

        if EPSGID=='0' or not EPSGID:
            raise utils.UserInterruptError()
        # If a CRS is selectd, go on and create the database

        #path and name of new db
        utils.stop_waiting_cursor()
        dbpath = ru(utils.get_save_file_name_no_extension(parent=None, caption="New DB",
                                                                    directory="midv_obsdb.sqlite",
                                                                    filter="Spatialite (*.sqlite)"))

        utils.start_waiting_cursor()

        if os.path.exists(dbpath):
            utils.MessagebarAndLog.critical(
                bar_msg=ru(QCoreApplication.translate('NewDb', 'A database with the chosen name already existed. Cancelling...')))
            utils.stop_waiting_cursor()
            return ''

        #Create the database
        conn = db_utils.connect_with_spatialite_connect(dbpath)
        conn.close()

        self.db_settings = ru(utils.anything_to_string_representation({'spatialite': {'dbpath': dbpath}}))

        #dbconnection = db_utils.DbConnectionManager(self.db_settings)
        try:
            # creating/connecting the test_db
            dbconnection = db_utils.DbConnectionManager(self.db_settings)
            dbconnection.execute("PRAGMA foreign_keys = ON")    #Foreign key constraints are disabled by default (for backwards compatibility), so must be enabled separately for each database dbconnection separately.
        except Exception as e:
            utils.MessagebarAndLog.critical(bar_msg=ru(QCoreApplication.translate('NewDb', "Impossible to connect to selected DataBase, see log message panel")), log_msg=ru(QCoreApplication.translate('NewDb', 'Msg:\n') + str(e)))
            #utils.pop_up_info("Impossible to connect to selected DataBase")
            utils.stop_waiting_cursor()
            return ''
        d =dbconnection.connector
        #First, find spatialite version
        versionstext = dbconnection.execute_and_fetchall('select spatialite_version()')[0][0]
        # load sql syntax to initialise spatial metadata, automatically create GEOMETRY_COLUMNS and SPATIAL_REF_SYS
        # then the syntax defines a Midvatten project db according to the loaded .sql-file
        if not int(versionstext[0][0]) > 3: # which file to use depends on spatialite version installed
            utils.pop_up_info(ru(QCoreApplication.translate('NewDb', "Midvatten plugin needs spatialite4.\nDatabase can not be created")))
            utils.stop_waiting_cursor()
            return ''

        filenamestring = "create_db.sql"

        SQLFile = os.path.join(os.sep,os.path.dirname(__file__),"..","definitions",filenamestring)
        qgisverno = Qgis.QGIS_VERSION#We want to store info about which qgis-version that created the db
        replace_word_replace_with = [('CHANGETORELEVANTEPSGID', ru(EPSGID)),
                                    ('CHANGETOPLUGINVERSION', ru(verno)),
                                    ('CHANGETOQGISVERSION', ru(qgisverno)),
                                    ('CHANGETODBANDVERSION', 'SpatiaLite version %s'%ru(versionstext)),
                                    ('CHANGETOLOCALE', ru(set_locale)),
                                    (('SPATIALITE ', ''))]

        with open(SQLFile, 'r') as f:
            f.readline()  # first line is encoding info....
            lines = [ru(line) for line in f]
        sql_lines = ['{};'.format(l) for l in ' '.join(lines).split(';') if l]
        for line in sql_lines:
            if all([line, not line.startswith("#"), 'POSTGIS' not in line]):
                sql = self.replace_words(line, replace_word_replace_with)
                try:
                    dbconnection.execute(sql)
                except:
                    try:
                        print(str(sql))
                    except:
                        pass
                    raise

        if delete_srids:
            db_utils.delete_srids(dbconnection, EPSGID)


        self.insert_datadomains(set_locale, dbconnection)

        execute_sqlfile(get_full_filename("insert_obs_points_triggers.sql"), dbconnection)

        execute_sqlfile(get_full_filename('qgis3_obsp_fix.sql'), dbconnection)

        self.add_metadata_to_about_db(dbconnection)

        #FINISHED WORKING WITH THE DATABASE, CLOSE CONNECTIONS

        dbconnection.commit()
        dbconnection.vacuum()
        dbconnection.commit_and_closedb()

        #create SpatiaLite Connection in QGIS QSettings
        settings=qgis.PyQt.QtCore.QSettings()
        settings.beginGroup('/SpatiaLite/dbconnections')
        settings.setValue('%s/sqlitepath'%os.path.basename(dbpath),'%s'%dbpath)
        settings.endGroup()

        """
        #The intention is to keep layer styles in the database by using the class AddLayerStyles but due to limitations in how layer styles are stored in the database, I will put this class on hold for a while.

        #Finally add the layer styles info into the data base
        AddLayerStyles(dbpath)
        """

        utils.stop_waiting_cursor()
コード例 #44
0
    def test_piper_plot_get_data(self, mock_showplot, mock_selected, mock_messagebar):

        db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, type, geometry) VALUES ('P1', 'well', ST_GeomFromText('POINT(633466 711659)', 3006))''')
        db_utils.sql_alter_db('''INSERT INTO obs_points (obsid, type, geometry) VALUES ('P2', 'notwell', ST_GeomFromText('POINT(6720727 016568)', 3006))''')
        db_utils.sql_alter_db('''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P1', '1', 'chloride', '1', 'mg/l', '2017-01-01')''')
        db_utils.sql_alter_db('''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P1', '1', 'alcalinity', '2', 'mg/l', '2017-01-01')''')
        db_utils.sql_alter_db('''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P1', '1', 'sulphat', '3', 'mg/l', '2017-01-01')''')
        db_utils.sql_alter_db('''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P1', '1', 'natrium', '4', 'mg/l', '2017-01-01')''')
        db_utils.sql_alter_db('''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P1', '1', 'kalium', '5', 'mg/l', '2017-01-01')''')
        db_utils.sql_alter_db('''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P1', '1', 'kalcium', '6', 'mg/l', '2017-01-01')''')
        db_utils.sql_alter_db('''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P1', '1', 'magnesium', '7', 'mg/l', '2017-01-01')''')
        db_utils.sql_alter_db('''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P2', '2', 'chloride', '10', 'mg/l', '2017-01-01')''')
        db_utils.sql_alter_db('''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P2', '2', 'alcalinity', '20', 'mg/l', '2017-01-01')''')
        db_utils.sql_alter_db('''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P2', '2', 'sulphat', '30', 'mg/l', '2017-01-01')''')
        db_utils.sql_alter_db('''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P2', '2', 'natrium', '40', 'mg/l', '2017-01-01')''')
        db_utils.sql_alter_db('''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P2', '2', 'kalium', '50', 'mg/l', '2017-01-01')''')
        db_utils.sql_alter_db('''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P2', '2', 'kalcium', '60', 'mg/l', '2017-01-01')''')
        db_utils.sql_alter_db('''INSERT INTO w_qual_lab (obsid, report, parameter, reading_num, unit, date_time) VALUES ('P2', '2', 'magnesium', '70', 'mg/l', '2017-01-01')''')

        """
        Manual calculation:
        (factor1 * reading_num) / factor2 = meq 
        1		1	/	35,453	=	0,028206357713029
        1		2	/	61,0168	=	0,032777857901431
        2	*	3	/	96,063	=	0,062459011273852

        1		4	/	22,9898	=	0,173990204351495
        1		5	/	39,0983	=	0,127882797973313
        sum                         0,301873002

        2	*	6	/	40,078	=	0,299416138529867
        2	*	7	/	24,305	=	0,576013166015223
                    /			
        1		10	/	35,453	=	0,282063577130285
        1		20	/	61,0168	=	0,327778579014304
        2	*	30	/	96,063	=	0,624590112738515

        1		40	/	22,9898	=	1,73990204351495
        1		50	/	39,0983	=	1,27882797973313
        sum                         3,018730023

        2	*	60	/	40,078	=	2,99416138529867
        2	*	70	/	24,305	=	5,76013166015223

        """

        mock_ms = mock.MagicMock()
        mock_ms.settingsdict = {r"""piper_cl""": '',
                                r"""piper_hco3""": '',
                                r"""piper_so4""": '',
                                r"""piper_na""": '',
                                r"""piper_k""": '',
                                r"""piper_ca""": '',
                                r"""piper_mg""": ''}
        mock_active_layer = mock.MagicMock()
        mock_selected.return_value = ['P1', 'P2']
        piperplot = piper.PiperPlot(mock_ms, mock_active_layer)
        piperplot.create_parameter_selection()
        piperplot.ms.settingsdict['piper_markers'] = 'obsid'
        piperplot.get_data_and_make_plot()
        data = piperplot.obsnp_nospecformat
        print("data: " + str(data))
        for l in data:
            for idx in range(3, 9):
                l[idx] = '{0:.10f}'.format(float(l[idx]))
        print("data: " + str(data))

        test_paramlist = utils.anything_to_string_representation(piperplot.ParameterList)
        ref_paramlist = '''["(lower(parameter) like '%klorid%' or lower(parameter) like '%chloride%')", "(lower(parameter) like '%alkalinitet%' or lower(parameter) like '%alcalinity%')", "(lower(parameter) like '%sulfat%' or lower(parameter) like '%sulphat%')", "(lower(parameter) like '%natrium%')", "(lower(parameter) like '%kalium%' or lower(parameter) like '%potassium%')", "(lower(parameter) like '%kalcium%' or lower(parameter) like '%calcium%')", "(lower(parameter) like '%magnesium%')"]'''
        assert test_paramlist == ref_paramlist

        test_data = tuple([tuple(_) for _ in data])

        ref_data = (('P1', '2017-01-01', 'well', '0.0282063577', '0.0327778579', '0.0624590113', '0.3018730023',
                     '0.2994161385', '0.5760131660'), ('P2', '2017-01-01', 'notwell', '0.2820635771', '0.3277785790',
                                                       '0.6245901127', '3.0187300232', '2.9941613853', '5.7601316602'))

        print("test")
        print(test_data)
        print("REF")
        print(ref_data)
        assert test_data == ref_data

        assert len(mock_messagebar.mock_calls) == 0