Mock answer of Big Query Client response

Viewed 259

I have a simple function that connects to Big Query and returns the client (inside a tuple) and I want to run a unit test on it. This is what I have done so far:

bq_related_functions.py

def connect_bq(bq_proj, bq_path_cred):
    """
     Connection to GBQ
    :param bq_proj: Project's name we want to stablish connection to
    :param bq_path_cred: Path towards the credentials to acces GBQ
    :return: Returns a tuple client, bool depending on the success of the task
    """
    try:
        bq_cred = service_account.Credentials.from_service_account_file(bq_path_cred)
        bigquery_client = bigquery.Client(project=bq_proj, credentials=bq_cred)
        return [bigquery_client, True]
    except OperationalError as exc:
        logging.error("Problems while connecting to BigQuery")
        logging.error(exc)
        traceback.print_exc()
        return [None, False]
    except ValueError as exc:
        logging.error("Problems with the credentials")
        logging.error(exc)
        traceback.print:exc()
        return [None, False]

I have writen the following test:

test_bq_related_functions.py

class TestBqRelatedFunctions(TestCase):

    @patch("modules.bq_related_functions.service_account")
    @patch("modules.bq_related_functions.bigquery.Client")
    def test_connect_bq_1(self, client, srv_acc):
        bq_proj = "fake_proj"
        bq_path_cred = "fake_path_cred"
        srv_acc.Credentials.from_service_account_file.return_value = {
            "access_token":"MockOauth2TokenForLocaldevelopmentnTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI3",
            "token_type":"bearer",
            "expires_in":35999,
            "scope":"read write",
            "jti":"4d540b94-1854-45fa-b1d6-c2039d94b681"}
        client.return_value = "bq_query_client"
        result = connect_bq(bq_proj, bq_path_cred)
        self.assertEqual(result, [client, True])

And the result of the test raises and error:

======================================================================
FAIL: test_connect_bq_1 (test_bq_related_functions.TestBqRelatedFunctions)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.8/unittest/mock.py", line 1325, in patched
    return func(*newargs, **newkeywargs)
  File "/home/sergijustiniano/Documentos/Basetis/EL/extract_load_spreadsheet/data/test/test_bq_related_functions.py", line 30, in test_connect_bq_1
    self.assertEqual(result, [client, True])
AssertionError: Lists differ: ['bq_query_client', True] != [<MagicMock name='Client' id='140011750193952'>, True]

First differing element 0:
'bq_query_client'
<MagicMock name='Client' id='140011750193952'>

- ['bq_query_client', True]
+ [<MagicMock name='Client' id='140011750193952'>, True]

----------------------------------------------------------------------

  • What am I doing wrong?
  • What type of object should be return from "from_service_account_file()" and "bigquery.Client()"?
  • Do I need to Mock the bq_path_cred since I am already mocking the return of bigquery.Client?
  • How could, for example, test the function in case the credentials are wrong? How do I send an error of such type as a response for from_service_account_file
0 Answers
Related