# Path: hecatoncheir/QueryResult.py
# class QueryResult():
#     query = None
#     column_names = None
#     resultset = None
# 
#     def __init__(self, query):
#         self.query = query
#         self.column_names = []
#         self.resultset = []
# 
#     def __repr__(self):
#         return u'QueryResult:' + repr({'query': self.query,
#                                        'column': self.column_names,
#                                        'result': self.resultset})
# 
# Path: hecatoncheir/exception.py
# class DriverError(Exception):
#     source = None
# 
#     def __init__(self, value, source=None):
#         self.value = value
#         self.source = source
# 
#     def __str__(self):
#         return self.value
# 
# class InternalError(Exception):
#     source = None
#     query = None
# 
#     def __init__(self, value, query=None, source=None):
#         self.value = value
#         self.query = query
#         self.source = source
# 
#     def __str__(self):
#         return self.value
# 
# class QueryError(Exception):
#     source = None
#     query = None
# 
#     def __init__(self, value, query, source=None):
#         self.value = value
#         self.query = query
#         self.source = source
# 
#     def __str__(self):
#         return self.value
# 
# class QueryTimeout(Exception):
#     source = None
#     query = None
# 
#     def __init__(self, value, query, source=None):
#         self.value = value
#         self.query = query
#         self.source = source
# 
#     def __str__(self):
#         return self.value
# 
# Path: hecatoncheir/bigquery/BigQueryDriver.py
# class BigQueryDriver(DbDriverBase.DbDriverBase):
#     project = None
#     conn = None
#     driver = None
# 
#     def __init__(self, project):
#         self.project = project
# 
#         name = "google.cloud.bigquery"
#         try:
#             self.driver = __import__(name, fromlist=['google.cloud'])
#         except Exception as e:
#             raise DriverError(
#                 u"Could not load the driver module: %s" % name, source=e)
# 
#     def connect(self):
#         try:
#             self.conn = self.driver.Client(project=self.project)
#         except Exception as ex:
#             raise DriverError(
#                 u"Could not connect to the server: %s" %
#                 ex.args[0].split('\n')[0], source=ex)
# 
#         return True
# 
#     def query_to_resultset(self, query, max_rows=10000, timeout=None):
#         """Build a QueryResult object from the query
# 
#         Args:
#             query (str): a query string to be executed.
#             max_rows (int): max rows which can be kept in a QueryResult object.
# 
#         Returns:
#             QueryResult: an object holding query, column names and result set.
#         """
#         assert query
#         assert isinstance(query, unicode)
#         log.trace('query_to_resultset: start query=%s' % query)
# 
#         res = QueryResult(query)
#         try:
#             if not self.conn:
#                 self.connect()
# 
#             query_job = self.conn.query(res.query)
#             assert query_job
#             res_iter = query_job.result(timeout=timeout)
#             assert query_job.state == 'DONE'
# 
#             desc = []
#             for f in query_job.query_results().schema:
#                 desc.append(f.name)
#             res.column_names = deepcopy(tuple(desc))
# 
#             for row in res_iter:
#                 # let's consider the memory size.
#                 if len(res.resultset) > max_rows:
#                     raise InternalError(
#                         u'Exceeded the record limit (%d) for QueryResult.' %
#                         max_rows, query=query)
#                 res.resultset.append(deepcopy(list(row)))
#         except InternalError as ex:
#             raise ex
#         except DriverError as ex:
#             raise ex
#         except Exception as ex:
#             msg = unicode(ex)
#             if msg == ('Operation did not complete within '
#                        'the designated timeout.'):
#                 raise QueryTimeout(
#                     "Query timeout: %s" % query,
#                     query=query, source=ex)
#             raise QueryError(
#                 "Could not execute a query: %s" % msg,
#                 query=query, source=ex)
# 
#         log.trace('query_to_resultset: end')
#         return res
# 
#     def disconnect(self):
#         if self.conn is None:
#             return False
# 
#         self.conn = None
#         return True


import os
import sys
import unittest
from hecatoncheir.QueryResult import QueryResult
from hecatoncheir.exception import DriverError, InternalError, QueryError, QueryTimeout
from hecatoncheir.bigquery import BigQueryDriver
        self.dbpass = "dqwbuser"

    def test_BigQueryDriver_001(self):
        bq = BigQueryDriver.BigQueryDriver('a', 'b', 'c')
        self.assertTrue(bq is not None)
        self.assertEqual('a', bq.project)
        self.assertEqual('b', bq.dbuser)
        self.assertEqual('c', bq.dbpass)

    def test_connect_001(self):
        # connection success
        bq = BigQueryDriver.BigQueryDriver(self.dbname, self.dbuser, self.dbpass)
        try:
            bq.connect()
        except DriverError as e:
            self.fail()
        self.assertIsNotNone(bq.conn)

    def test_connect_002(self):
        # connection failure
        # FIXME:
        bq = BigQueryDriver.BigQueryDriver(self.dbname, "nosuchuser", '')
        with self.assertRaises(DriverError) as cm:
            bq.connect()
        self.assertEqual('', cm.exception.value)

    def test_query_to_resultset_001(self):
        bq = BigQueryDriver.BigQueryDriver(self.dbname, self.dbuser, self.dbpass)
        try:
            bq.connect()

# Path: hecatoncheir/QueryResult.py
# class QueryResult():
#     query = None
#     column_names = None
#     resultset = None
# 
#     def __init__(self, query):
#         self.query = query
#         self.column_names = []
#         self.resultset = []
# 
#     def __repr__(self):
#         return u'QueryResult:' + repr({'query': self.query,
#                                        'column': self.column_names,
#                                        'result': self.resultset})
# 
# Path: hecatoncheir/exception.py
# class DriverError(Exception):
#     source = None
# 
#     def __init__(self, value, source=None):
#         self.value = value
#         self.source = source
# 
#     def __str__(self):
#         return self.value
# 
# class InternalError(Exception):
#     source = None
#     query = None
# 
#     def __init__(self, value, query=None, source=None):
#         self.value = value
#         self.query = query
#         self.source = source
# 
#     def __str__(self):
#         return self.value
# 
# class QueryError(Exception):
#     source = None
#     query = None
# 
#     def __init__(self, value, query, source=None):
#         self.value = value
#         self.query = query
#         self.source = source
# 
#     def __str__(self):
#         return self.value
# 
# class QueryTimeout(Exception):
#     source = None
#     query = None
# 
#     def __init__(self, value, query, source=None):
#         self.value = value
#         self.query = query
#         self.source = source
# 
#     def __str__(self):
#         return self.value
# 
# Path: hecatoncheir/bigquery/BigQueryDriver.py
# class BigQueryDriver(DbDriverBase.DbDriverBase):
#     project = None
#     conn = None
#     driver = None
# 
#     def __init__(self, project):
#         self.project = project
# 
#         name = "google.cloud.bigquery"
#         try:
#             self.driver = __import__(name, fromlist=['google.cloud'])
#         except Exception as e:
#             raise DriverError(
#                 u"Could not load the driver module: %s" % name, source=e)
# 
#     def connect(self):
#         try:
#             self.conn = self.driver.Client(project=self.project)
#         except Exception as ex:
#             raise DriverError(
#                 u"Could not connect to the server: %s" %
#                 ex.args[0].split('\n')[0], source=ex)
# 
#         return True
# 
#     def query_to_resultset(self, query, max_rows=10000, timeout=None):
#         """Build a QueryResult object from the query
# 
#         Args:
#             query (str): a query string to be executed.
#             max_rows (int): max rows which can be kept in a QueryResult object.
# 
#         Returns:
#             QueryResult: an object holding query, column names and result set.
#         """
#         assert query
#         assert isinstance(query, unicode)
#         log.trace('query_to_resultset: start query=%s' % query)
# 
#         res = QueryResult(query)
#         try:
#             if not self.conn:
#                 self.connect()
# 
#             query_job = self.conn.query(res.query)
#             assert query_job
#             res_iter = query_job.result(timeout=timeout)
#             assert query_job.state == 'DONE'
# 
#             desc = []
#             for f in query_job.query_results().schema:
#                 desc.append(f.name)
#             res.column_names = deepcopy(tuple(desc))
# 
#             for row in res_iter:
#                 # let's consider the memory size.
#                 if len(res.resultset) > max_rows:
#                     raise InternalError(
#                         u'Exceeded the record limit (%d) for QueryResult.' %
#                         max_rows, query=query)
#                 res.resultset.append(deepcopy(list(row)))
#         except InternalError as ex:
#             raise ex
#         except DriverError as ex:
#             raise ex
#         except Exception as ex:
#             msg = unicode(ex)
#             if msg == ('Operation did not complete within '
#                        'the designated timeout.'):
#                 raise QueryTimeout(
#                     "Query timeout: %s" % query,
#                     query=query, source=ex)
#             raise QueryError(
#                 "Could not execute a query: %s" % msg,
#                 query=query, source=ex)
# 
#         log.trace('query_to_resultset: end')
#         return res
# 
#     def disconnect(self):
#         if self.conn is None:
#             return False
# 
#         self.conn = None
#         return True


# Path: tests/testBigQueryDriver.py
import os
import sys
import unittest
from hecatoncheir.QueryResult import QueryResult
from hecatoncheir.exception import DriverError, InternalError, QueryError, QueryTimeout
from hecatoncheir.bigquery import BigQueryDriver

        self.dbpass = "dqwbuser"

    def test_BigQueryDriver_001(self):
        bq = BigQueryDriver.BigQueryDriver('a', 'b', 'c')
        self.assertTrue(bq is not None)
        self.assertEqual('a', bq.project)
        self.assertEqual('b', bq.dbuser)
        self.assertEqual('c', bq.dbpass)

    def test_connect_001(self):
        # connection success
        bq = BigQueryDriver.BigQueryDriver(self.dbname, self.dbuser, self.dbpass)
        try:
            bq.connect()
        except DriverError as e:
            self.fail()
        self.assertIsNotNone(bq.conn)

    def test_connect_002(self):
        # connection failure
        # FIXME:
        bq = BigQueryDriver.BigQueryDriver(self.dbname, "nosuchuser", '')
        with self.assertRaises(DriverError) as cm:
            bq.connect()
        self.assertEqual('', cm.exception.value)

    def test_query_to_resultset_001(self):
        bq = BigQueryDriver.BigQueryDriver(self.dbname, self.dbuser, self.dbpass)
        try:
            bq.connect()

        except DriverError as e: