def test_query_csv3(self): """Test query_csv with multiple commands.""" results = query_csv(['update foo set a=(a+b+c)', 'select a from foo'], self.foo) self.assertMatch(results, """ a 6 """)
def test_query_csv1(self): """Test query_csv with single table.""" results = query_csv('select * from foo', self.foo) self.assertMatch( results, """ a, b, c 1, 2, 3 """)
def test_query_csv2(self): """Test query_csv with multiple tables.""" results = query_csv('select * from foo union all ' 'select * from bar', [self.foo, self.bar]) self.assertMatch( results, """ a, b, c 1, 2, 3 4, 5, 6 """)
def _get_distinct_sample_ids_from(self, file_path): '''Given a file_path, this returns a set of unique sample ids.''' file_name = self._get_file_name_without_extension(file_path) unique_sample_ids = query_csv(self.sql['distinct_sample_id'].format(self.fields['sample_id'], file_name), [file_path], TEMPDB) if len(unique_sample_ids) > 0: #: remove header cell unique_sample_ids.pop(0) return unique_sample_ids
def _get_samples_for_id(self, sample_id_set, file_path, config=None): '''Given a `(id,)` styled set of sample ids, this will return the sample rows for the given csv file_path. The format will be an array of dictionaries with the key being the destination field name and the value being the source csv value. This is only invoked for result files. ''' file_name = self._get_file_name_without_extension(file_path) samples_for_id = query_csv(self.sql['sample_id'].format(file_name, self.fields['sample_id'], sample_id_set[0]), [file_path], TEMPDB) return self._etl_column_names(samples_for_id, config or self.result_config)
def _get_wqx_duplicate_ids(self, file_path): '''Given the file_path, return the list of stripped station ids that have the _WQX suffix''' file_name = None try: file_name = self._get_file_name_without_extension(file_path) except AttributeError: #: most likely list is not a string error #: we are sending in the updated stations stations = file_path if file_name: rows = query_csv(self.sql['wqxids'].format(self.fields['monitoring_location_id'], file_name), [file_path], TEMPDB) if len(rows) > 0: rows.pop(0) return set([re.sub(self.wqx_re, '-', row[0]) for row in rows]) return set([re.sub(self.wqx_re, '-', station['StationId']) for station in filter(lambda x: self.wqx_re.search(x['StationId']), stations)])
def get(self, query): result_query = interpret_query_for(query) dummy_query = 'select count(*) from population' result_data = query_csv(dummy_query, CSV_DATABASE[0]) return {'result_query': result_query, 'result_data': result_data}
def test_pretty_print2(self): fp = StringIO() results = query_csv('select count(*) from foo', self.foo) pretty_print(results, fp) self.assertEqual(fp.getvalue(), ' count(*)\n==========\n 1 \n')
def test_pretty_print1(self): fp = StringIO() results = query_csv('select * from foo', self.foo) pretty_print(results, fp) self.assertEqual(fp.getvalue(), ' a | b | c\n===========\n 1 | 2 | 3\n')