Example #1
0
        public void Deserialize_Succeeds_WhenVersionSequenceOrStateIsNull()
        {
            var serializedOperation = JObject.Parse(@"
            {""id"":""70cf6cc2-5981-4a32-ae6c-249572917a46"",
            ""kind"": 2,
            ""tableName"":""test"",
            ""itemId"":""abc"",
            ""version"":null,
            ""sequence"":null,
            ""state"":null,
            ""item"":""{\""id\"":\""abc\"",\""text\"":\""example\""}"",
            ""__createdAt"":""2014-03-11T20:37:10.3366689Z"",
            ""sequence"":0
            }");
            var operation           = MobileServiceTableOperation.Deserialize(serializedOperation);

            Assert.AreEqual(serializedOperation["id"], operation.Id);
            Assert.AreEqual(serializedOperation["itemId"], operation.ItemId);
            Assert.AreEqual(serializedOperation["tableName"], operation.TableName);
            Assert.AreEqual(MobileServiceTableOperationKind.Delete, operation.Kind);
            Assert.AreEqual(serializedOperation["sequence"], operation.Sequence);
            Assert.AreEqual(0, operation.Version);
            Assert.AreEqual("abc", operation.Item["id"]);
            Assert.AreEqual("example", operation.Item["text"]);
        }
Example #2
0
        public void Deserialize_Succeeds()
        {
            var serializedOperation = JObject.Parse("{\"id\":\"70cf6cc2-5981-4a32-ae6c-249572917a46\",\"kind\": 0,\"tableName\":\"test\",\"itemId\":\"abc\",\"item\":null,\"__createdAt\":\"2014-03-11T20:37:10.3366689Z\",\"sequence\":0}");

            var operation = MobileServiceTableOperation.Deserialize(serializedOperation);

            Assert.AreEqual(serializedOperation["id"], operation.Id);
            Assert.AreEqual(serializedOperation["itemId"], operation.ItemId);
            Assert.AreEqual(serializedOperation["tableName"], operation.TableName);
            Assert.AreEqual(MobileServiceTableOperationKind.Insert, operation.Kind);
            Assert.IsNull(operation.Item);
            Assert.AreEqual(serializedOperation["sequence"], operation.Sequence);
        }
Example #3
0
        private async Task TestExecuteAsync(MobileServiceTableOperation op, JObject result, HttpStatusCode?errorCode)
        {
            op.Sequence = 1;

            // picks up the operation
            this.opQueue.Setup(q => q.PeekAsync(0, MobileServiceTableKind.Table, It.IsAny <IEnumerable <string> >())).Returns(() => Task.FromResult(op));
            this.opQueue.Setup(q => q.PeekAsync(op.Sequence, MobileServiceTableKind.Table, It.IsAny <IEnumerable <string> >())).Returns(() => Task.FromResult <MobileServiceTableOperation>(null));

            // executes the operation via handler
            if (errorCode == null)
            {
                this.handler.Setup(h => h.ExecuteTableOperationAsync(op)).Returns(Task.FromResult(result));
            }
            else
            {
                this.handler.Setup(h => h.ExecuteTableOperationAsync(op))
                .Throws(new MobileServiceInvalidOperationException("",
                                                                   null,
                                                                   new HttpResponseMessage(errorCode.Value)
                {
                    Content = new StringContent(result.ToString())
                }));
            }
            // removes the operation from queue only if there is no error
            if (errorCode == null)
            {
                this.opQueue.Setup(q => q.DeleteAsync(It.IsAny <string>(), It.IsAny <long>())).Returns(Task.FromResult(true));
            }
            // 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>(r =>
            {
                Assert.AreEqual(r.Status, MobileServicePushStatus.Complete);
                Assert.AreEqual(r.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();

            await action.CompletionTask;
        }
Example #4
0
        private async Task TestResultSave(MobileServiceTableOperation op, HttpStatusCode?status, string resultId, bool saved)
        {
            var result = new JObject()
            {
                { "id", resultId }
            };

            if (saved)
            {
                this.store.Setup(s => s.UpsertAsync("table", It.Is <JObject[]>(list => list.Any(o => o.ToString() == result.ToString())), true))
                .Returns(Task.FromResult(0));
            }
            await this.TestExecuteAsync(op, result, status);
        }
Example #5
0
        private void TestDeleteValidateThrows(MobileServiceTableOperation tableOperation)
        {
            var ex = Assert.Throws <InvalidOperationException>(() => this.operation.Validate(tableOperation));

            Assert.Equal("A delete operation on the item is already in the queue.", ex.Message);
        }