Esempio n. 1
0
        public void TestTransactionalSingleEntityGroupReadOnly()
        {
            string keyName     = UpsertTaskList();
            Key    taskListKey = _db.CreateKeyFactory("TaskList")
                                 .CreateKey(keyName);
            Entity taskListEntity = new Entity()
            {
                Key = taskListKey
            };

            _db.Upsert(taskListEntity);
            // [START datastore_transactional_single_entity_group_read_only]
            Entity taskList;
            IReadOnlyList <Entity> tasks;

            using (var transaction = _db.BeginTransaction(TransactionOptions.CreateReadOnly()))
            {
                taskList = transaction.Lookup(taskListKey);
                var query = new Query("Task")
                {
                    Filter = Filter.HasAncestor(taskListKey)
                };
                tasks = transaction.RunQuery(query).Entities;
                transaction.Commit();
            }
            // [END datastore_transactional_single_entity_group_read_only]
            Assert.Equal(taskListEntity, taskList);
            Assert.Collection(tasks, task => { });
        }
        public void BeginTransaction_ReadOnly()
        {
            var db         = DatastoreDb.Create(_fixture.ProjectId, _fixture.NamespaceId);
            var keyFactory = db.CreateKeyFactory("readonly_test");
            var options    = TransactionOptions.CreateReadOnly();

            using (var transaction = db.BeginTransaction(options))
            {
                transaction.Insert(new Entity {
                    Key = keyFactory.CreateIncompleteKey(), ["x"] = 3
                });
                var exception = Assert.Throws <RpcException>(() => transaction.Commit());
                Assert.Equal(StatusCode.InvalidArgument, exception.Status.StatusCode);
            }
        }
        public async Task BeginTransactionAsync_ReadOnly()
        {
            var db         = _fixture.CreateDatastoreDb();
            var keyFactory = db.CreateKeyFactory("readonly_test");
            var options    = TransactionOptions.CreateReadOnly();

            using (var transaction = await db.BeginTransactionAsync(options))
            {
                transaction.Insert(new Entity {
                    Key = keyFactory.CreateIncompleteKey(), ["x"] = 3
                });
                var exception = await Assert.ThrowsAsync <RpcException>(() => transaction.CommitAsync());

                Assert.Equal(StatusCode.InvalidArgument, exception.Status.StatusCode);
            }
        }