Exemplo n.º 1
0
    def test_api_connection(self, client):
        connection = self.mock_connection(client)
        api.connect('', '', '')
        self.assertEqual(api.spanner_api()._connection, connection)

        api.hangup()
        with self.assertRaises(error.SpannerError):
            api.spanner_api()
Exemplo n.º 2
0
def transactional_write(func: Callable[..., T]) -> Callable[..., T]:
    """Injects a write transaction object as first argument to given function.

    For example:

    @transactional_write
    def save_book(book_id, transaction=None):
      ...

    Client would then call this by skipping the 'transaction' argument:
    book_reader.save_book(123)

    To call a decorated function if you already have a transaction:

    @transactional_write
    def save_books(book_ids, transaction=None):
      for book_id in book_ids:
        save_book(book_id, transaction=transaction)

    save_books method would call save_book method using same transaction

  Args:
    func: Callable which will be called with write transaction and original
      arguments. Decorated `func` can also be passed an optional 'transaction'
      kwarg to use a given transaction, instead of creating a new one.

  Returns:
    decorated function
  """
    api_method_lambda = lambda: api.spanner_api().run_write
    return _transactional(api_method_lambda, func)
Exemplo n.º 3
0
def transactional_read(func: Callable[..., T]) -> Callable[..., T]:
    """Injects a read-only transaction as keyword argument to given function.

    For example:

    @transactional_read
    def get_book(book_id, transaction=None):
      return Book(book_id)

    Client would then call this by skipping the 'transaction' argument:
    book_reader.get_book(123)

    To call a decorated function if you already have a transaction:

    @transactional_read
    def list_books(book_ids, transaction=None):
      for book_id in book_ids:
        get_book(book_id, transaction=transaction)

    list_books method would call get_book method using same transaction

  Args:
    func: Callable which will be called with read-only transaction and original
      arguments. Decorated `func` can also be passed an optional 'transaction'
      kwarg to use a given transaction, instead of creating a new one.

  Returns:
    decorated function
  """
    api_method_lambda = lambda: api.spanner_api().run_read_only
    return _transactional(api_method_lambda, func)
Exemplo n.º 4
0
 def spanner_api(cls) -> api.SpannerApi:
     if not cls.table:
         raise error.SpannerError('Class must define a table for API calls')
     return api.spanner_api()
Exemplo n.º 5
0
 def test_api_error_when_not_connected(self):
     with self.assertRaises(error.SpannerError):
         api.spanner_api()