def setUp(self, mock_locale): self.iface = DummyInterface() self.midvatten = midvatten(self.iface) try: os.remove(TEMP_DB_PATH) except OSError: pass mock_locale.return_value.answer = u'ok' mock_locale.return_value.value = u'sv_SE' self.midvatten.new_db()
def setUp(self): self.iface = DummyInterface()
class TestFieldLoggerImporterDb(object): answer_yes = mock_answer('yes') answer_no = mock_answer('no') CRS_question = MockUsingReturnValue([3006]) mocked_iface = MockQgisUtilsIface() #Used for not getting messageBar errors mock_askuser = MockReturnUsingDictIn({u'It is a strong': answer_no.get_v(), u'Please note!\nThere are ': answer_yes.get_v()}, 1) skip_popup = MockUsingReturnValue('') @mock.patch('create_db.utils.NotFoundQuestion') @mock.patch('midvatten_utils.askuser', answer_yes.get_v) @mock.patch('create_db.PyQt4.QtGui.QInputDialog.getInteger', CRS_question.get_v) @mock.patch('create_db.PyQt4.QtGui.QFileDialog.getSaveFileName', DBPATH_QUESTION.get_v) def setUp(self, mock_locale): self.iface = DummyInterface() self.midvatten = midvatten(self.iface) try: os.remove(TEMP_DB_PATH) except OSError: pass mock_locale.return_value.answer = u'ok' mock_locale.return_value.value = u'sv_SE' self.midvatten.new_db() def tearDown(self): #Delete database os.remove(TEMP_DB_PATH) @mock.patch('midvatten_utils.QgsProject.instance', MOCK_DBPATH.get_v) def test_staff_not_given(self): utils.sql_alter_db(u'''INSERT INTO obs_points ("obsid") VALUES ("Rb1")''') f = [ u"Location;date_time;value;comment\n", u"Rb1202.sample;30-03-2016;15:31:30;hej2;s.comment\n", u"Rb1608.level;30-03-2016;15:34:40;testc;l.comment\n", u"Rb1615.flow;30-03-2016;15:30:09;357;f.Accvol.m3\n", u"Rb1615.flow;30-03-2016;15:30:09;gick bra;f.comment\n", u"Rb1608.level;30-03-2016;15:34:13;ergv;l.comment\n", u"Rb1608.level;30-03-2016;15:34:13;555;l.meas.m\n", u"Rb1512.sample;30-03-2016;15:31:30;899;s.turbiditet.FNU\n", u"Rb1505.quality;30-03-2016;15:29:26;hej;q.comment\n", u"Rb1505.quality;30-03-2016;15:29:26;863;q.konduktivitet.µS/cm\n", u"Rb1512.quality;30-03-2016;15:30:39;test;q.comment\n", u"Rb1512.quality;30-03-2016;15:30:39;67;q.syre.mg/L\n", u"Rb1512.quality;30-03-2016;15:30:39;8;q.temperatur.grC\n", u"Rb1512.quality;30-03-2016;15:30:40;58;q.syre.%\n", ] with utils.tempinput(''.join(f)) as filename: @mock.patch('midvatten_utils.QgsProject.instance', MOCK_DBPATH.get_v) @mock.patch('import_fieldlogger.utils.QtGui.QFileDialog.getOpenFileNames') @mock.patch('import_fieldlogger.utils.QtGui.QInputDialog.getText') @mock.patch('import_fieldlogger.utils.MessagebarAndLog') @mock.patch('midvatten_utils.QgsProject.instance', MOCK_DBPATH.get_v) def _test_staff_not_given(self, filename, mock_MessagebarAndLog, mock_charset, mock_savefilename ): mock_charset.return_value = ('utf-8', True) mock_savefilename.return_value = [filename] ms = MagicMock() ms.settingsdict = OrderedDict() importer = FieldloggerImport(self.iface.mainWindow(), ms) importer.parse_observations_and_populate_gui() importer.start_import(importer.observations) mock_MessagebarAndLog.critical.assert_called_with(bar_msg=u'Import error, staff not given') @mock.patch('midvatten_utils.QgsProject.instance', MOCK_DBPATH.get_v) def test_full_integration_test_to_db(self): utils.sql_alter_db(u'''INSERT INTO obs_points ("obsid") VALUES ("Rb1202")''') utils.sql_alter_db(u'''INSERT INTO obs_points ("obsid") VALUES ("Rb1608")''') utils.sql_alter_db(u'''INSERT INTO obs_points ("obsid") VALUES ("Rb1615")''') utils.sql_alter_db(u'''INSERT INTO obs_points ("obsid") VALUES ("Rb1505")''') utils.sql_alter_db(u'''INSERT INTO obs_points ("obsid") VALUES ("Rb1512")''') utils.sql_alter_db(u'''INSERT INTO zz_staff ("staff") VALUES ("teststaff")''') utils.sql_alter_db(u'''INSERT or ignore INTO zz_flowtype ("type") VALUES ("Accvol")''') f = [ u"Location;date_time;value;comment\n", u"Rb1202.sample;30-03-2016;15:31:30;hej2;s.comment\n", u"Rb1608.level;30-03-2016;15:34:40;testc;l.comment\n", u"Rb1615.flow;30-03-2016;15:30:09;357;f.Accvol.m3\n", u"Rb1615.flow;30-03-2016;15:30:09;gick bra;f.comment\n", u"Rb1608.level;30-03-2016;15:34:13;ergv;l.comment\n", u"Rb1608.level;30-03-2016;15:34:13;555;l.meas.m\n", u"Rb1512.sample;30-03-2016;15:31:30;899;s.turbiditet.FNU\n", u"Rb1505.quality;30-03-2016;15:29:26;hej;q.comment\n", u"Rb1505.quality;30-03-2016;15:29:26;863;q.konduktivitet.µS/cm\n", u"Rb1512.quality;30-03-2016;15:30:39;test;q.comment\n", u"Rb1512.quality;30-03-2016;15:30:39;67;q.syre.mg/L\n", u"Rb1512.quality;30-03-2016;15:30:39;8;q.temperatur.grC\n", u"Rb1512.quality;30-03-2016;15:30:40;58;q.syre.%\n", ] with utils.tempinput(''.join(f)) as filename: @mock.patch('midvatten_utils.QgsProject.instance', MOCK_DBPATH.get_v) @mock.patch('import_fieldlogger.utils.askuser') @mock.patch('import_fieldlogger.utils.NotFoundQuestion') @mock.patch('import_fieldlogger.utils.QtGui.QFileDialog.getOpenFileNames') @mock.patch('import_fieldlogger.utils.QtGui.QInputDialog.getText') @mock.patch('import_fieldlogger.utils.MessagebarAndLog') @mock.patch('midvatten_utils.QgsProject.instance', MOCK_DBPATH.get_v) def _full_integration_test_to_db(self, filename, mock_MessagebarAndLog, mock_charset, mock_savefilename, mock_ask_instrument, mock_vacuum): mock_vacuum.return_value.result = 1 mock_charset.return_value = ('utf-8', True) mock_savefilename.return_value = [filename] mock_ask_instrument.return_value.value = u'testid' ms = MagicMock() ms.settingsdict = OrderedDict() importer = FieldloggerImport(self.iface.mainWindow(), ms) importer.parse_observations_and_populate_gui() #Set settings: for setting in importer.settings: if isinstance(setting, import_fieldlogger.StaffQuestion): setting.staff = u'teststaff' stored_settings = [[u's.comment', [[u'import_method', u'comments']]], [u'l.comment', [[u'import_method', u'comments']]], [u'f.comment', [[u'import_method', u'comments']]], [u'q.comment', [[u'import_method', u'comments']]], [u'l.meas.m', [[u'import_method', u'w_levels']]], [u'f.Accvol.m3', [[u'import_method', u'w_flow'], [u'flowtype', u'Accvol'], [u'unit', u'm3']]], [u's.turbiditet.FNU', [[u'import_method', u'w_qual_field'], [u'parameter', u'turbiditet'], [u'unit', u'FNU'], [u'depth', u''], [u'instrument', u'testid']]], [u'q.konduktivitet.µS/cm', [[u'import_method', u'w_qual_field'], [u'parameter', u'konduktivitet'], [u'unit', u'µS/cm'], [u'depth', u''], [u'instrument', u'testid']]], [u'q.syre.mg/L', [[u'import_method', u'w_qual_field'], [u'parameter', u'syre'], [u'unit', u'mg/L'], [u'depth', u''], [u'instrument', u'testid']]], [u'q.syre.%', [[u'import_method', u'w_qual_field'], [u'parameter', u'syre'], [u'unit', u'%'], [u'depth', u''], [u'instrument', u'testid']]], [u'q.temperatur.grC', [[u'import_method', u'w_qual_field'], [u'parameter', u'temperatur'], [u'unit', u'grC'], [u'depth', u''], [u'instrument', u'testid']]]] importer.set_parameters_using_stored_settings(stored_settings, importer.parameter_imports) importer.start_import(importer.observations) _full_integration_test_to_db(self, filename) test_string = create_test_string(dict([(k, utils.sql_load_fr_db(u'select * from %s'%k)) for k in (u'w_levels', u'w_qual_field', u'w_flow', u'zz_staff', u'comments')])) reference_string = u'{comments: (True, [(Rb1202, 2016-03-30 15:31:30, hej2, teststaff), (Rb1608, 2016-03-30 15:34:40, testc, teststaff)]), w_flow: (True, [(Rb1615, testid, Accvol, 2016-03-30 15:30:09, 357.0, m3, gick bra)]), w_levels: (True, [(Rb1608, 2016-03-30 15:34:13, 555.0, None, None, ergv)]), w_qual_field: (True, [(Rb1512, teststaff, 2016-03-30 15:30:39, testid, syre, 67.0, 67, mg/L, None, test), (Rb1512, teststaff, 2016-03-30 15:31:30, testid, turbiditet, 899.0, 899, FNU, None, None), (Rb1505, teststaff, 2016-03-30 15:29:26, testid, konduktivitet, 863.0, 863, µS/cm, None, hej), (Rb1512, teststaff, 2016-03-30 15:30:40, testid, syre, 58.0, 58, %, None, None), (Rb1512, teststaff, 2016-03-30 15:30:39, testid, temperatur, 8.0, 8, grC, None, test)]), zz_staff: (True, [(teststaff, None)])}' assert test_string == reference_string @mock.patch('midvatten_utils.QgsProject.instance', MOCK_DBPATH.get_v) def test_full_into_zz_flowtype(self): utils.sql_alter_db(u'''INSERT INTO obs_points ("obsid") VALUES ("2")''') #utils.sql_alter_db(u'''INSERT INTO obs_points ("obsid") VALUES ("5")''') f = [u'LOCATION;DATE;TIME;VALUE;TYPE\n', u'5.2892.level;12-12-2016;10:02:49;comment;l.comment\n', u'5.2892.level;12-12-2016;10:02:49;123;meas.m\n', u'5.2892.level;12-12-2016;10:02:57;onlycomment;l.comment\n', u'2.2892.flow;12-12-2016;10:03:07;123;Aveflow.m3/s\n', u'2.2892.flow;12-12-2016;10:03:15;onlycomment;f.comment\n', u'2.2892.comment;12-12-2016;10:03:24;onlycomment;comment\n'] with utils.tempinput(''.join(f)) as filename: @mock.patch('midvatten_utils.QgsProject.instance', MOCK_DBPATH.get_v) @mock.patch('import_fieldlogger.utils.askuser') @mock.patch('import_fieldlogger.utils.NotFoundQuestion') @mock.patch('import_fieldlogger.utils.QtGui.QFileDialog.getOpenFileNames') @mock.patch('import_fieldlogger.utils.QtGui.QInputDialog.getText') @mock.patch('import_fieldlogger.utils.MessagebarAndLog') @mock.patch('midvatten_utils.QgsProject.instance', MOCK_DBPATH.get_v) def _test(self, filename, mock_MessagebarAndLog, mock_charset, mock_savefilename, mock_ask_instrument, mock_askuser): mock_charset.return_value = ('utf-8', True) mock_savefilename.return_value = [filename] mock_ask_instrument.return_value.value = u'testid' def side_effect(*args, **kwargs): mock_result = mock.MagicMock() if len(args) > 1: if args[1].startswith(u'Do you want to confirm'): mock_result.result = 0 return mock_result elif args[1].startswith(u'Do you want to import all'): mock_result.result = 0 return mock_result elif args[1].startswith(u'Please note!\nForeign keys'): mock_result.result = 1 return mock_result elif args[1].startswith(u'Please note!\nThere are'): mock_result.result = 1 return mock_result elif args[1].startswith(u'It is a strong recommendation'): mock_result.result = 0 return mock_result mock_askuser.side_effect = side_effect ms = MagicMock() ms.settingsdict = OrderedDict() importer = FieldloggerImport(self.iface.mainWindow(), ms) importer.parse_observations_and_populate_gui() #Set settings: for setting in importer.settings: if isinstance(setting, import_fieldlogger.StaffQuestion): setting.staff = u'teststaff' stored_settings = [[u'f.comment', [[u'import_method', u'comments']]], [u'Aveflow.m3/s', [[u'import_method', u'w_flow'], [u'flowtype', u'Momflow2'], [u'unit', u'aunit']]]] importer.set_parameters_using_stored_settings(stored_settings, importer.parameter_imports) importer.start_import(importer.observations) _test(self, filename) test_string = create_test_string(dict([(k, utils.sql_load_fr_db(u'select * from %s'%k)) for k in (u'w_flow', u'zz_staff', u'comments', u'zz_flowtype')])) reference_string = u'{comments: (True, [(2, 2016-12-12 10:03:15, onlycomment, teststaff)]), w_flow: (True, [(2, testid, Momflow2, 2016-12-12 10:03:07, 123.0, aunit, None)]), zz_flowtype: (True, [(Accvol, Accumulated volume), (Momflow, Momentary flow rate), (Aveflow, Average flow since last reading), (Momflow2, None)]), zz_staff: (True, [(teststaff, None)])}' assert test_string == reference_string
def setUp(self): self.iface = DummyInterface() widget = QtGui.QWidget() self.calcave = w_flow_calc_aveflow.Calcave(widget)