def test_single_value_sql5(self): m_execute = Mock() m_fetchall = Mock(return_value=((17, 22),)) m_cursor = MagicMock() m_cursor.execute = m_execute m_cursor.fetchall = m_fetchall conn = MagicMock() conn.cursor.return_value.__enter__.return_value = m_cursor dbutil.single_row_sql(conn, "select 17, 22", (1, 2, 3)) assert conn.cursor.call_count == 1 assert m_cursor.execute.call_count == 1 m_cursor.execute.assert_called_once_with("select 17, 22", (1, 2, 3))
def test_single_value_sql4(self): m_execute = Mock() m_fetchall = Mock(return_value=None) m_cursor = MagicMock() m_cursor.execute = m_execute m_cursor.fetchall = m_fetchall conn = MagicMock() conn.cursor.return_value.__enter__.return_value = m_cursor with pytest.raises(dbutil.SQLDidNotReturnSingleRow): dbutil.single_row_sql(conn, 'select 17, 22', (1, 2, 3)) assert conn.cursor.call_count == 1 assert m_cursor.execute.call_count == 1 m_cursor.execute.assert_called_once_with("select 17, 22", (1, 2, 3))
def __getitem__(self, key): """return the job info or raise a KeyError""" sql = """ SELECT next_run, first_run, last_run, last_success, depends_on, error_count, last_error, ongoing FROM cron_job WHERE app_name = %s """ columns = ('next_run', 'first_run', 'last_run', 'last_success', 'depends_on', 'error_count', 'last_error', 'ongoing') try: with transaction_context(self.database_class) as conn: record = single_row_sql(conn, sql, (key, )) except SQLDidNotReturnSingleRow: raise KeyError(key) row = dict(zip(columns, record)) # Unserialize last_error into a Python dict if row['last_error']: row['last_error'] = json.loads(row['last_error']) return row
def update_crashstats_signature(self, signature, report_date, report_build): with transaction_context(self.database) as connection: # Pull the data from the db. If it's there, then do an update. If it's # not there, then do an insert. try: sql = """ SELECT signature, first_build, first_date FROM crashstats_signature WHERE signature=%s """ sig = single_row_sql(connection, sql, (signature, )) sql = """ UPDATE crashstats_signature SET first_build=%s, first_date=%s WHERE signature=%s """ params = (min(sig[1], int(report_build)), min(sig[2], string_to_datetime(report_date)), sig[0]) except SQLDidNotReturnSingleRow: sql = """ INSERT INTO crashstats_signature (signature, first_build, first_date) VALUES (%s, %s, %s) """ params = (signature, report_build, report_date) execute_no_results(connection, sql, params)
def __getitem__(self, key): """return the job info or raise a KeyError""" sql = """ SELECT next_run, first_run, last_run, last_success, depends_on, error_count, last_error, ongoing FROM cron_job WHERE app_name = %s """ columns = ( 'next_run', 'first_run', 'last_run', 'last_success', 'depends_on', 'error_count', 'last_error', 'ongoing' ) try: with transaction_context(self.database_class) as conn: record = single_row_sql(conn, sql, (key,)) except SQLDidNotReturnSingleRow: raise KeyError(key) row = dict(zip(columns, record)) # Unserialize last_error into a Python dict if row['last_error']: row['last_error'] = json.loads(row['last_error']) return row
def update_crashstats_signature(self, signature, report_date, report_build): with transaction_context(self.database) as connection: # Pull the data from the db. If it's there, then do an update. If it's # not there, then do an insert. try: sql = """ SELECT signature, first_build, first_date FROM crashstats_signature WHERE signature=%s """ sig = single_row_sql(connection, sql, (signature,)) sql = """ UPDATE crashstats_signature SET first_build=%s, first_date=%s WHERE signature=%s """ params = ( min(sig[1], int(report_build)), min(sig[2], string_to_datetime(report_date)), sig[0] ) except SQLDidNotReturnSingleRow: sql = """ INSERT INTO crashstats_signature (signature, first_build, first_date) VALUES (%s, %s, %s) """ params = (signature, report_build, report_date) execute_no_results(connection, sql, params)