def test_get_iam_policy(self):
        # Setup Expected Response
        version = 351608024
        etag = b'21'
        expected_response = {'version': version, 'etag': etag}
        expected_response = policy_pb2.Policy(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = spanner_admin_database_v1.DatabaseAdminClient(channel=channel)

        # Setup Request
        resource = client.database_path('[PROJECT]', '[INSTANCE]',
                                        '[DATABASE]')

        response = client.get_iam_policy(resource)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = iam_policy_pb2.GetIamPolicyRequest(
            resource=resource)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_get_database(self):
        # Setup Expected Response
        name_2 = "name2-1052831874"
        expected_response = {"name": name_2}
        expected_response = spanner_database_admin_pb2.Database(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = spanner_admin_database_v1.DatabaseAdminClient()

        # Setup Request
        name = client.database_path("[PROJECT]", "[INSTANCE]", "[DATABASE]")

        response = client.get_database(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = spanner_database_admin_pb2.GetDatabaseRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_test_iam_permissions(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = iam_policy_pb2.TestIamPermissionsResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = spanner_admin_database_v1.DatabaseAdminClient(channel=channel)

        # Setup Request
        resource = client.database_path('[PROJECT]', '[INSTANCE]',
                                        '[DATABASE]')
        permissions = []

        response = client.test_iam_permissions(resource, permissions)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = iam_policy_pb2.TestIamPermissionsRequest(
            resource=resource, permissions=permissions)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_create_backup(self):
        # Setup Expected Response
        database = "database1789464955"
        name = "name3373707"
        size_bytes = 1796325715
        expected_response = {
            "database": database,
            "name": name,
            "size_bytes": size_bytes,
        }
        expected_response = backup_pb2.Backup(**expected_response)
        operation = operations_pb2.Operation(
            name="operations/test_create_backup", done=True)
        operation.response.Pack(expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = spanner_admin_database_v1.DatabaseAdminClient()

        # Setup Request
        parent = client.instance_path("[PROJECT]", "[INSTANCE]")
        backup_id = "backupId1355353272"
        backup = {}

        response = client.create_backup(parent, backup_id, backup)
        result = response.result()
        assert expected_response == result

        assert len(channel.requests) == 1
        expected_request = backup_pb2.CreateBackupRequest(parent=parent,
                                                          backup_id=backup_id,
                                                          backup=backup)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Example #5
0
    def test_list_databases(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = spanner_admin_database_v1.DatabaseAdminClient()

        # Mock request
        parent = client.instance_path('[PROJECT]', '[INSTANCE]')

        # Mock response
        next_page_token = ''
        databases_element = {}
        databases = [databases_element]
        expected_response = {
            'next_page_token': next_page_token,
            'databases': databases
        }
        expected_response = spanner_database_admin_pb2.ListDatabasesResponse(
            **expected_response)
        grpc_stub.ListDatabases.return_value = expected_response

        paged_list_response = client.list_databases(parent)
        resources = list(paged_list_response)
        self.assertEqual(1, len(resources))
        self.assertEqual(expected_response.databases[0], resources[0])

        grpc_stub.ListDatabases.assert_called_once()
        args, kwargs = grpc_stub.ListDatabases.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = spanner_database_admin_pb2.ListDatabasesRequest(
            parent=parent)
        self.assertEqual(expected_request, actual_request)
    def test_get_iam_policy(self):
        # Setup Expected Response
        version = 351608024
        etag = b"21"
        expected_response = {"version": version, "etag": etag}
        expected_response = policy_pb2.Policy(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = spanner_admin_database_v1.DatabaseAdminClient()

        # Setup Request
        resource = "resource-341064690"

        response = client.get_iam_policy(resource)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = iam_policy_pb2.GetIamPolicyRequest(resource=resource)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request