def test_add_view_obs_points_obs_lines_add(self, mock_messagebar):
        db_utils.sql_alter_db('''DROP VIEW IF EXISTS view_obs_points;''')
        db_utils.sql_alter_db('''DROP VIEW IF EXISTS view_obs_lines;''')
        db_utils.sql_alter_db(
            '''DELETE FROM views_geometry_columns WHERE view_name IN ('view_obs_points', 'view_obs_lines');'''
        )

        assert not any([
            db_utils.verify_table_exists('view_obs_points'),
            db_utils.verify_table_exists('view_obs_lines')
        ])
        views_geometry_columns = db_utils.sql_load_fr_db(
            '''SELECT view_name FROM views_geometry_columns WHERE view_name IN ('view_obs_points', 'view_obs_lines') ORDER BY view_name;'''
        )[1]
        print(str(views_geometry_columns))
        assert views_geometry_columns == []

        utils.add_view_obs_points_obs_lines()
        print(str(mock_messagebar.mock_calls))
        assert call.info(
            bar_msg=
            'Views added. Please reload layers (Midvatten>Load default db-layers to qgis or "F7").'
        ) in mock_messagebar.mock_calls
        assert all([
            db_utils.verify_table_exists('view_obs_points'),
            db_utils.verify_table_exists('view_obs_lines')
        ])
        views_geometry_columns = db_utils.sql_load_fr_db(
            '''SELECT view_name FROM views_geometry_columns WHERE view_name IN ('view_obs_points', 'view_obs_lines') ORDER BY view_name;'''
        )[1]
        print(str(views_geometry_columns))
        assert views_geometry_columns == [('view_obs_lines', ),
                                          ('view_obs_points', )]
 def export_2_csv(self,exportfolder):
     dbconnection = db_utils.DbConnectionManager()
     dbconnection.connect2db() #establish connection to the current midv db
     self.curs = dbconnection.cursor#get a cursor
     self.exportfolder = exportfolder
     self.write_data(self.to_csv, self.ID_obs_points, defs.get_subset_of_tables_fr_db(category='obs_points'),
                     lambda x: db_utils.verify_table_exists(x, dbconnection=dbconnection))
     self.write_data(self.to_csv, self.ID_obs_lines, defs.get_subset_of_tables_fr_db(category='obs_lines'),
                     lambda x: db_utils.verify_table_exists(x, dbconnection=dbconnection))
     self.write_data(self.zz_to_csv, 'no_obsids', defs.get_subset_of_tables_fr_db(category='data_domains'),
                     lambda x: db_utils.verify_table_exists(x, dbconnection=dbconnection))
     dbconnection.closedb()
    def test_add_view_obs_points_obs_lines_readd(self, mock_messagebar):
        assert all([db_utils.verify_table_exists('view_obs_points'), db_utils.verify_table_exists('view_obs_lines')])
        views_geometry_columns = db_utils.sql_load_fr_db('''SELECT view_name FROM views_geometry_columns WHERE view_name IN ('view_obs_points', 'view_obs_lines') ORDER BY view_name;''')[1]
        print(str(views_geometry_columns))
        assert views_geometry_columns == [('view_obs_lines',), ('view_obs_points',)]

        utils.add_view_obs_points_obs_lines()
        print(str(mock_messagebar.mock_calls))
        assert call.info(bar_msg='Views added. Please reload layers (Midvatten>Load default db-layers to qgis or "F7").') in mock_messagebar.mock_calls

        assert all([db_utils.verify_table_exists('view_obs_points'), db_utils.verify_table_exists('view_obs_lines')])
        views_geometry_columns = db_utils.sql_load_fr_db('''SELECT view_name FROM views_geometry_columns WHERE view_name IN ('view_obs_points', 'view_obs_lines') ORDER BY view_name;''')[1]
        print(str(views_geometry_columns))
        assert views_geometry_columns == [('view_obs_lines',), ('view_obs_points',)]
 def write_data(self, to_writer, obsids, ptabs, replace=False):
     for tname in ptabs:
         if not db_utils.verify_table_exists(tname, dbconnection=self.source_dbconnection):
             utils.MessagebarAndLog.info(bar_msg=ru(QCoreApplication.translate('ExportData', "Table %s didn't exist. Skipping it."))%tname)
             continue
         if not obsids:
             to_writer(tname, obsids, replace)
         else:
             nr_of_rows = self.get_number_of_rows(obsids, tname)
             if nr_of_rows > 0: #only go on if there are any observations for this obsid
                 to_writer(tname, obsids, replace)
Exemple #5
0
 def export_2_csv(self, exportfolder):
     dbconnection = db_utils.DbConnectionManager()
     dbconnection.connect2db()  #establish connection to the current midv db
     self.curs = dbconnection.cursor  #get a cursor
     self.exportfolder = exportfolder
     self.write_data(
         self.to_csv, self.ID_obs_points,
         defs.get_subset_of_tables_fr_db(category='obs_points'),
         lambda x: db_utils.verify_table_exists(x,
                                                dbconnection=dbconnection))
     self.write_data(
         self.to_csv, self.ID_obs_lines,
         defs.get_subset_of_tables_fr_db(category='obs_lines'),
         lambda x: db_utils.verify_table_exists(x,
                                                dbconnection=dbconnection))
     self.write_data(
         self.zz_to_csv, u'no_obsids',
         defs.get_subset_of_tables_fr_db(category='data_domains'),
         lambda x: db_utils.verify_table_exists(x,
                                                dbconnection=dbconnection))
     dbconnection.closedb()
Exemple #6
0
 def test_verify_table_exists(self):
     exists = db_utils.verify_table_exists(u'obs_points')
     assert exists
 def test_verify_table_exists(self):
     exists = db_utils.verify_table_exists('obs_points')
     assert exists