def test_get_multi_hit_w_transaction(self): from gcloud.datastore.key import Key TXN_ID = "123" KIND = "Kind" ID = 1234 PATH = [{"kind": KIND, "id": ID}] # Make a found entity pb to be returned from mock backend. entity_pb = _make_entity_pb(self.PROJECT, KIND, ID, "foo", "Foo") # Make a connection to return the entity pb. creds = object() client = self._makeOne(credentials=creds) client.connection._add_lookup_result([entity_pb]) key = Key(KIND, ID, project=self.PROJECT) txn = client.transaction() txn._id = TXN_ID result, = client.get_multi([key], transaction=txn) new_key = result.key # Check the returned value is as expected. self.assertFalse(new_key is key) self.assertEqual(new_key.project, self.PROJECT) self.assertEqual(new_key.path, PATH) self.assertEqual(list(result), ["foo"]) self.assertEqual(result["foo"], "Foo") cw = client.connection._lookup_cw self.assertEqual(len(cw), 1) _, _, _, transaction_id = cw[0] self.assertEqual(transaction_id, TXN_ID)
def test_get_multi_hit_w_transaction(self): from gcloud.datastore.key import Key TXN_ID = '123' KIND = 'Kind' ID = 1234 PATH = [{'kind': KIND, 'id': ID}] # Make a found entity pb to be returned from mock backend. entity_pb = _make_entity_pb(self.PROJECT, KIND, ID, 'foo', 'Foo') # Make a connection to return the entity pb. creds = object() client = self._makeOne(credentials=creds) client.connection._add_lookup_result([entity_pb]) key = Key(KIND, ID, project=self.PROJECT) txn = client.transaction() txn._id = TXN_ID result, = client.get_multi([key], transaction=txn) new_key = result.key # Check the returned value is as expected. self.assertFalse(new_key is key) self.assertEqual(new_key.project, self.PROJECT) self.assertEqual(new_key.path, PATH) self.assertEqual(list(result), ['foo']) self.assertEqual(result['foo'], 'Foo') cw = client.connection._lookup_cw self.assertEqual(len(cw), 1) _, _, _, transaction_id = cw[0] self.assertEqual(transaction_id, TXN_ID)
def test_transaction(self): from gcloud.datastore import client as MUT from gcloud._testing import _Monkey client = self._makeOne() with _Monkey(MUT, Transaction=_Dummy): xact = client.transaction() self.assertTrue(isinstance(xact, _Dummy)) self.assertEqual(xact.args, (client,)) self.assertEqual(xact.kwargs, {})
def test_transaction_explicit(self): from gcloud.datastore import client as MUT from gcloud._testing import _Monkey creds = object() client = self._makeOne(credentials=creds) with _Monkey(MUT, Transaction=_Dummy): xact = client.transaction(serializable=True) self.assertTrue(isinstance(xact, _Dummy)) self.assertEqual(xact.args, (client,)) self.assertEqual(xact.kwargs, {'serializable': True})
def test__push_batch_and__pop_batch(self): conn = object() client = self._makeOne(connection=conn) batch = client.batch() xact = client.transaction() client._push_batch(batch) self.assertEqual(list(client._batch_stack), [batch]) self.assertTrue(client.current_batch is batch) self.assertTrue(client.current_transaction is None) client._push_batch(xact) self.assertTrue(client.current_batch is xact) self.assertTrue(client.current_transaction is xact) # list(_LocalStack) returns in reverse order. self.assertEqual(list(client._batch_stack), [xact, batch]) self.assertTrue(client._pop_batch() is xact) self.assertEqual(list(client._batch_stack), [batch]) self.assertTrue(client._pop_batch() is batch) self.assertEqual(list(client._batch_stack), [])