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')
def test_prepare_w_levels_data_to_both(self): observations = [{'obsid': 'obs1', 'date_time': datestring_to_date('2016-01-01 00:00'), 'value': '123,4', 'level_masl': '567'}, {'obsid': 'obs1', 'date_time': datestring_to_date('2016-01-01 00:02'), 'meas': '897'}] test_string = create_test_string(FieldloggerImport.prepare_w_levels_data(observations)) reference_string = '[[obsid, date_time, meas, h_toc, level_masl, comment], [obs1, 2016-01-01 00:00:00, , , 567, ], [obs1, 2016-01-01 00:02:00, 897, , , ]]' print(test_string) assert test_string == reference_string
def test_prepare_w_qual_field_data_with_depth(self): observations = [{'obsid': 'obs1', 'date_time': datestring_to_date('2016-01-01 00:00'), 'parameter': 'par1', 'unit': 'unit1', 'staff': 'staff1', 'instrument': 'inst1', 'comment': 'comment1', 'value': '1.23', 'depth': '123.4'}] test_string = create_test_string(FieldloggerImport.prepare_w_qual_field_data(observations)) reference_string = '[[obsid, staff, date_time, instrument, parameter, reading_num, reading_txt, unit, depth, comment], [obs1, staff1, 2016-01-01 00:00:00, inst1, par1, 1.23, 1.23, unit1, 123.4, comment1]]' assert test_string == reference_string
def _test(self, filename, mock_MessagebarAndLog, mock_charset, mock_savefilename, mock_delimiter_question): mock_delimiter_question.return_value = (',', True) mock_charset.return_value = ('utf-8', True) mock_savefilename.return_value = [[filename]] test_string = FieldloggerImport.select_file_and_parse_rows(FieldloggerImport.parse_rows) return test_string
def _test(self, filename, mock_MessagebarAndLog, mock_charset, mock_savefilename ): mock_charset.return_value = ('utf-8', True) mock_savefilename.return_value = [[filename]] test_string = create_test_string(sorted(FieldloggerImport.select_file_and_parse_rows(FieldloggerImport.parse_rows), key=itemgetter('date_time', 'parametername', 'sublocation'))) return test_string
def test_prepare_w_flow_data(self, mock_flow_instruments, mock_instrument_not_found): mock_flow_instruments = [True, {}] mock_instrument_not_found.return_value.answer = u'ok' mock_instrument_not_found.return_value.value = u'inst1' observations = [{u'obsid': u'obs1', u'flowtype': u'atype', u'date_time': datestring_to_date(u'2016-01-01 00:00'), u'unit': u'aunit', u'value': u'123,4'}] test_string = create_test_string(FieldloggerImport.prepare_w_flow_data(observations)) reference_string = u'[[obsid, instrumentid, flowtype, date_time, reading, unit, comment], [obs1, inst1, atype, 2016-01-01 00:00:00, 123.4, aunit, ]]' assert test_string == reference_string
def test_prepare_w_flow_data(self, mock_flow_instruments, mock_instrument_not_found): mock_flow_instruments = [True, {}] mock_instrument_not_found.return_value.answer = 'ok' mock_instrument_not_found.return_value.value = 'inst1' observations = [{'sublocation': 'obs1.sub', 'obsid': 'obs1', 'flowtype': 'atype', 'date_time': datestring_to_date('2016-01-01 00:00'), 'unit': 'aunit', 'value': '123,4'}] test_string = create_test_string(FieldloggerImport.prepare_w_flow_data(observations)) reference_string = '[[obsid, instrumentid, flowtype, date_time, reading, unit, comment], [obs1, inst1, atype, 2016-01-01 00:00:00, 123.4, aunit, ]]' assert test_string == reference_string
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)
def test_prepare_w_flow_data_assert_only_ask_instrument_once(self, mock_flow_instruments, mock_instrument_not_found): mock_flow_instruments = [True, {}] mock_instrument_not_found.return_value.answer = u'ok' mock_instrument_not_found.return_value.value = u'inst1' observations = [{u'obsid': u'obs1', u'flowtype': u'atype', u'date_time': datestring_to_date(u'2016-01-01 00:00'), u'unit': u'aunit', u'value': u'123,4'}, {u'obsid': u'obs1', u'flowtype': u'atype', u'date_time': datestring_to_date(u'2016-01-02 00:00'), u'unit': u'aunit', u'value': u'223,4'}] test_string = create_test_string(FieldloggerImport.prepare_w_flow_data(observations)) mock_instrument_not_found.assert_called_once_with(combobox_label=u'Instrument id:s in database.\nThe last used instrument id for the current obsid is prefilled:', default_value=u'', dialogtitle=u'Submit instrument id', existing_list=[u''], msg=u'Submit the instrument id for the measurement:\n obs1, 2016-01-01 00:00:00, atype, aunit') reference_string = u'[[obsid, instrumentid, flowtype, date_time, reading, unit, comment], [obs1, inst1, atype, 2016-01-01 00:00:00, 123.4, aunit, ], [obs1, inst1, atype, 2016-01-02 00:00:00, 223.4, aunit, ]]' assert test_string == reference_string
def test_prepare_w_flow_data_assert_only_ask_instrument_once(self, mock_flow_instruments, mock_instrument_not_found): mock_flow_instruments = [True, {}] mock_instrument_not_found.return_value.answer = 'ok' mock_instrument_not_found.return_value.value = 'inst1' observations = [{'sublocation': 'obs1.sub', 'obsid': 'obs1', 'flowtype': 'atype', 'date_time': datestring_to_date('2016-01-01 00:00'), 'unit': 'aunit', 'value': '123,4'}, {'sublocation': 'obs1.sub', 'obsid': 'obs1', 'flowtype': 'atype', 'date_time': datestring_to_date('2016-01-02 00:00'), 'unit': 'aunit', 'value': '223,4'}] test_string = create_test_string(FieldloggerImport.prepare_w_flow_data(observations)) mock_instrument_not_found.assert_called_once_with(combobox_label='Instrument id:s in database for obsid obs1.\nThe last used instrument id for obsid obs1 is prefilled:', default_value='', dialogtitle='Submit instrument id', existing_list=[''], msg='Submit the instrument id for the measurement:\n obs1.sub, obs1, 2016-01-01 00:00:00, atype, aunit') reference_string = '[[obsid, instrumentid, flowtype, date_time, reading, unit, comment], [obs1, inst1, atype, 2016-01-01 00:00:00, 123.4, aunit, ], [obs1, inst1, atype, 2016-01-02 00:00:00, 223.4, aunit, ]]' assert test_string == reference_string
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)
def test_prepare_w_flow_data_assert_only_ask_instrument_twice(self, mock_flow_instruments, mock_instrument_not_found): mock_flow_instruments = [True, {}] mock_instrument_not_found.return_value.answer = 'ok' mock_instrument_not_found.return_value.value = 'inst1' observations = [{'sublocation': 'obs1.sub', 'obsid': 'obs1', 'flowtype': 'atype', 'date_time': datestring_to_date('2016-01-01 00:00'), 'unit': 'aunit', 'value': '123,4'}, {'sublocation': 'obs2.sub', 'obsid': 'obs2', 'flowtype': 'atype', 'date_time': datestring_to_date('2016-01-02 00:00'), 'unit': 'aunit', 'value': '223,4'}, {'sublocation': 'obs2.sub', 'obsid': 'obs2', 'flowtype': 'atype', 'date_time': datestring_to_date('2016-01-03 00:00'), 'unit': 'aunit', 'value': '223,4'}] test_string = create_test_string(FieldloggerImport.prepare_w_flow_data(observations)) expected_calls = [call(combobox_label='Instrument id:s in database for obsid obs1.\nThe last used instrument id for obsid obs1 is prefilled:', default_value='', dialogtitle='Submit instrument id', existing_list=[''], msg='Submit the instrument id for the measurement:\n obs1.sub, obs1, 2016-01-01 00:00:00, atype, aunit'), call(combobox_label='Instrument id:s in database for obsid obs2.\nThe last used instrument id for obsid obs2 is prefilled:', default_value='', dialogtitle='Submit instrument id', existing_list=[''], msg='Submit the instrument id for the measurement:\n obs2.sub, obs2, 2016-01-02 00:00:00, atype, aunit')] assert mock_instrument_not_found.mock_calls == expected_calls reference_string = '[[obsid, instrumentid, flowtype, date_time, reading, unit, comment], [obs1, inst1, atype, 2016-01-01 00:00:00, 123.4, aunit, ], [obs2, inst1, atype, 2016-01-02 00:00:00, 223.4, aunit, ], [obs2, inst1, atype, 2016-01-03 00:00:00, 223.4, aunit, ]]' assert test_string == reference_string
def _test(self, filename, mock_MessagebarAndLog, mock_charset, mock_savefilename ): mock_charset.return_value = ('utf-8', True) mock_savefilename.return_value = [filename] test_string = create_test_string(FieldloggerImport.select_file_and_parse_rows(FieldloggerImport.parse_rows)) return test_string
def test_parse_rows_skip_empty_rows(self): f = ['Br2;12-12-2016;15:33:30;123;w_lvl', 'Br1;12-12-2016;15:34:30;;w_lvl'] observations = FieldloggerImport.parse_rows(f) test = create_test_string(observations) reference = utils_for_tests.create_test_string([{'date_time': '2016-12-12 15:33:30', 'parametername': 'w_lvl', 'sublocation': 'Br2', 'value': '123'}]) assert test == reference