public async Task InsertAsync(string tableName, MobileServiceTableKind tableKind, string id, JObject item)
        {
            var operation = new InsertOperation(tableName, tableKind, id)
            {
                Table = await this.GetTable(tableName)
            };

            await this.ExecuteOperationAsync(operation, item);
        }
Пример #2
0
        internal static MobileServiceTableOperation Deserialize(JObject obj)
        {
            if (obj == null)
            {
                return(null);
            }

            var    kind      = (MobileServiceTableOperationKind)obj.Value <int>("kind");
            string tableName = obj.Value <string>("tableName");
            var    tableKind = (MobileServiceTableKind)obj.Value <int?>("tableKind").GetValueOrDefault();
            string itemId    = obj.Value <string>("itemId");


            MobileServiceTableOperation operation = null;

            switch (kind)
            {
            case MobileServiceTableOperationKind.Insert:
                operation = new InsertOperation(tableName, tableKind, itemId); break;

            case MobileServiceTableOperationKind.Update:
                operation = new UpdateOperation(tableName, tableKind, itemId); break;

            case MobileServiceTableOperationKind.Delete:
                operation = new DeleteOperation(tableName, tableKind, itemId); break;
            }

            if (operation != null)
            {
                operation.Id       = obj.Value <string>(MobileServiceSystemColumns.Id);
                operation.Sequence = obj.Value <long?>("sequence").GetValueOrDefault();
                operation.Version  = obj.Value <long?>("version").GetValueOrDefault();
                string itemJson = obj.Value <string>("item");
                operation.Item  = !String.IsNullOrEmpty(itemJson) ? JObject.Parse(itemJson) : null;
                operation.State = (MobileServiceTableOperationState)obj.Value <int?>("state").GetValueOrDefault();
            }

            return(operation);
        }
        public async Task AbortPush_AbortsThePush()
        {
            MobileServiceTableOperation op = new InsertOperation("abc", MobileServiceTableKind.Table, "abc") { Item = new JObject() }; // putting an item so it won't load it
            // picks up the operation
            this.opQueue.Setup(q => q.PeekAsync(0, MobileServiceTableKind.Table, It.IsAny<IEnumerable<string>>())).Returns(() => Task.FromResult(op));
            // executes the operation via handler
            this.handler.Setup(h => h.ExecuteTableOperationAsync(op))
                        .Callback<IMobileServiceTableOperation>(o =>
                        {
                            o.AbortPush();
                        });

            // loads sync errors
            string syncError = @"[]";
            this.store.Setup(s => s.ReadAsync(It.Is<MobileServiceTableQueryDescription>(q => q.TableName == MobileServiceLocalSystemTables.SyncErrors))).Returns(Task.FromResult(JToken.Parse(syncError)));
            // calls push complete
            this.handler.Setup(h => h.OnPushCompleteAsync(It.IsAny<MobileServicePushCompletionResult>())).Returns(Task.FromResult(0))
                        .Callback<MobileServicePushCompletionResult>(result =>
                        {
                            Assert.AreEqual(result.Status, MobileServicePushStatus.CancelledByOperation);
                            Assert.AreEqual(result.Errors.Count(), 0);
                        });

            // deletes the errors
            this.store.Setup(s => s.DeleteAsync(It.Is<MobileServiceTableQueryDescription>(q => q.TableName == MobileServiceLocalSystemTables.SyncErrors))).Returns(Task.FromResult(0));

            await this.action.ExecuteAsync();

            this.store.VerifyAll();
            this.opQueue.VerifyAll();
            this.handler.VerifyAll();

            var ex = await AssertEx.Throws<MobileServicePushFailedException>(async () => await action.CompletionTask);
            Assert.AreEqual(ex.PushResult.Status, MobileServicePushStatus.CancelledByOperation);
            Assert.AreEqual(ex.PushResult.Errors.Count(), 0);
        }
        public async Task InsertAsync(string tableName, MobileServiceTableKind tableKind, string id, JObject item)
        {
            var operation = new InsertOperation(tableName, tableKind, id)
            {
                Table = await this.GetTable(tableName)
            };

            await this.ExecuteOperationAsync(operation, item);
        }
 public void Validate_Throws_WithInsertOperation()
 {
     var newOperation = new InsertOperation("test", MobileServiceTableKind.Table, "abc");
     var ex = AssertEx.Throws<InvalidOperationException>(() => this.operation.Validate(newOperation));
     Assert.AreEqual("An update operation on the item is already in the queue.", ex.Message);
 }
 public void Validate_Throws_WithInsertOperation()
 {
     var tableOperation = new InsertOperation("test", MobileServiceTableKind.Table, "abc");
     TestDeleteValidateThrows(tableOperation);
 }
 public void Initialize()
 {
     this.operation = new InsertOperation("test", MobileServiceTableKind.Table, "abc");
 }
        internal static MobileServiceTableOperation Deserialize(JObject obj)
        {
            if (obj == null)
            {
                return null;
            }

            var kind = (MobileServiceTableOperationKind)obj.Value<int>("kind");
            string tableName = obj.Value<string>("tableName");
            var tableKind = (MobileServiceTableKind)obj.Value<int?>("tableKind").GetValueOrDefault();
            string itemId = obj.Value<string>("itemId");


            MobileServiceTableOperation operation = null;
            switch (kind)
            {
                case MobileServiceTableOperationKind.Insert:
                    operation = new InsertOperation(tableName, tableKind, itemId); break;
                case MobileServiceTableOperationKind.Update:
                    operation = new UpdateOperation(tableName, tableKind, itemId); break;
                case MobileServiceTableOperationKind.Delete:
                    operation = new DeleteOperation(tableName, tableKind, itemId); break;
            }

            if (operation != null)
            {
                operation.Id = obj.Value<string>(MobileServiceSystemColumns.Id);
                operation.Sequence = obj.Value<long?>("sequence").GetValueOrDefault();
                operation.Version = obj.Value<long?>("version").GetValueOrDefault();
                string itemJson = obj.Value<string>("item");
                operation.Item = !String.IsNullOrEmpty(itemJson) ? JObject.Parse(itemJson) : null;
                operation.State = (MobileServiceTableOperationState)obj.Value<int?>("state").GetValueOrDefault();
            }

            return operation;
        }
 public async Task ExecuteAsync_SavesTheResult_IfExecuteTableOperationDoesNotThrow()
 {
     var op = new InsertOperation("table", MobileServiceTableKind.Table, "id") { Item = new JObject() };
     await TestResultSave(op, status: null, resultId: "id", saved: true);
 }
 public async Task ExecuteAsync_LoadsTheItem_IfItIsNotPresent()
 {
     var op = new InsertOperation("table", MobileServiceTableKind.Table, "id");
     this.store.Setup(s => s.LookupAsync("table", "id")).Returns(Task.FromResult(new JObject()));
     await this.TestExecuteAsync(op, null, null);
 }
 public async Task ExecuteAsync_DeletesTheErrors()
 {
     var op = new InsertOperation("table", MobileServiceTableKind.Table, "id") { Item = new JObject() }; // putting an item so it won't load it
     await this.TestExecuteAsync(op, null, null);
 }
 public async Task ExecuteAsync_DoesNotSaveTheResult_IfPresentButResultDoesNotHaveId()
 {
     this.action = new PushAction(this.opQueue.Object, this.store.Object, MobileServiceTableKind.Table, null, this.handler.Object, this.client.Object, this.context.Object, CancellationToken.None);
     var op = new InsertOperation("table", MobileServiceTableKind.Table, "id") { Item = new JObject() };
     await TestResultSave(op, status: null, resultId: null, saved: false);
 }
 public async Task ExecuteAsync_DoesNotSaveTheResult_IfExecuteTableOperationThrows()
 {
     this.store.Setup(s => s.UpsertAsync(MobileServiceLocalSystemTables.SyncErrors, It.IsAny<JObject[]>(), false)).Returns(Task.FromResult(0));
     var op = new InsertOperation("table", MobileServiceTableKind.Table, "id") { Item = new JObject() };
     await TestResultSave(op, status: HttpStatusCode.PreconditionFailed, resultId: "id", saved: false);
 }