Esempio n. 1
0
 public async Task <T> ReadItemAsync(string id)
 {
     return(await table.LookupAsync(id));
 }
Esempio n. 2
0
        public async Task Insert_ThenPush_ThenPull_ThenRead_ThenUpdate_ThenRefresh_ThenDelete_ThenLookup_ThenPush_ThenPurge_ThenRead()
        {
            ResetDatabase(TestTable);

            var hijack = new TestHttpHandler();

            hijack.AddResponseContent("{\"id\":\"b\",\"String\":\"Hey\"}");                                       // insert response
            hijack.AddResponseContent("[{\"id\":\"b\",\"String\":\"Hey\"},{\"id\":\"a\",\"String\":\"World\"}]"); // pull response
            hijack.AddResponseContent("[]");                                                                      // pull last page

            IMobileServiceClient service = await CreateTodoClient(hijack);

            IMobileServiceSyncTable <ToDoWithStringId> table = service.GetSyncTable <ToDoWithStringId>();

            // first insert an item
            await table.InsertAsync(new ToDoWithStringId()
            {
                Id = "b", String = "Hey"
            });

            // then push it to server
            await service.SyncContext.PushAsync();

            // then pull changes from server
            await table.PullAsync(null, null);

            // order the records by id so we can assert them predictably
            IList <ToDoWithStringId> items = await table.OrderBy(i => i.Id).ToListAsync();

            // we should have 2 records
            Assert.AreEqual(items.Count, 2);

            // according to ordering a id comes first
            Assert.AreEqual(items[0].Id, "a");
            Assert.AreEqual(items[0].String, "World");

            // then comes b record
            Assert.AreEqual(items[1].Id, "b");
            Assert.AreEqual(items[1].String, "Hey");

            // we made 2 requests, one for push and two for pull
            Assert.AreEqual(hijack.Requests.Count, 3);

            // recreating the client from state in the store
            service = await CreateTodoClient(hijack);

            table = service.GetSyncTable <ToDoWithStringId>();

            // update the second record
            items[1].String = "Hello";
            await table.UpdateAsync(items[1]);

            // create an empty record with same id as modified record
            var second = new ToDoWithStringId()
            {
                Id = items[1].Id
            };
            // refresh the empty record
            await table.RefreshAsync(second);

            // make sure it is same as modified record now
            Assert.AreEqual(second.String, items[1].String);

            // now delete the record
            await table.DeleteAsync(second);

            // now try to get the deleted record
            ToDoWithStringId deleted = await table.LookupAsync(second.Id);

            // this should be null
            Assert.IsNull(deleted);

            // try to get the non-deleted record
            ToDoWithStringId first = await table.LookupAsync(items[0].Id);

            // this should still be there;
            Assert.IsNotNull(first);

            // make sure it is same as
            Assert.AreEqual(first.String, items[0].String);

            // recreating the client from state in the store
            service = await CreateTodoClient(hijack);

            table = service.GetSyncTable <ToDoWithStringId>();

            await service.SyncContext.PushAsync();

            // now purge the remaining records
            await table.PurgeAsync();

            // now read one last time
            IEnumerable <ToDoWithStringId> remaining = await table.ReadAsync();

            // There shouldn't be anything remaining
            Assert.AreEqual(remaining.Count(), 0);
        }
Esempio n. 3
0
 public async Task <T> GetSingle(string id) => await table.LookupAsync(id);
Esempio n. 4
0
 public async Task <Recipe> GetSingleRecipe(string id)
 {
     return(await recipeTable.LookupAsync(id));
 }
        public async Task PushAsync_RetriesOperation_WhenConflictOccursInLastPush()
        {
            ResetDatabase(TestTable);

            var    hijack         = new TestHttpHandler();
            string conflictResult = "{\"id\":\"b\",\"String\":\"Hey\",\"version\":\"def\"}";

            hijack.Responses.Add(new HttpResponseMessage(HttpStatusCode.PreconditionFailed)
            {
                Content = new StringContent(conflictResult)
            });                                                                                                                               // first push
            string successResult = "{\"id\":\"b\",\"String\":\"Wow\",\"version\":\"def\"}";

            hijack.Responses.Add(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(successResult)
            });                                                                                                              // second push

            var store = new MobileServiceSQLiteStore(TestDbName);

            store.DefineTable <ToDoWithSystemPropertiesType>();

            IMobileServiceClient service = await CreateClient(hijack, store);

            IMobileServiceSyncTable <ToDoWithSystemPropertiesType> table = service.GetSyncTable <ToDoWithSystemPropertiesType>();

            // first insert an item
            var updatedItem = new ToDoWithSystemPropertiesType()
            {
                Id = "b", String = "Hey", Version = "abc"
            };
            await table.UpdateAsync(updatedItem);

            // then push it to server
            var ex = await Assert.ThrowsAsync <MobileServicePushFailedException>(service.SyncContext.PushAsync);

            Assert.NotNull(ex.PushResult);
            Assert.Equal(MobileServicePushStatus.Complete, ex.PushResult.Status);
            Assert.Single(ex.PushResult.Errors);
            MobileServiceTableOperationError error = ex.PushResult.Errors.FirstOrDefault();

            Assert.NotNull(error);
            Assert.False(error.Handled);
            Assert.Equal(MobileServiceTableOperationKind.Update, error.OperationKind);
            Assert.Equal(error.RawResult, conflictResult);
            Assert.Equal(error.TableName, TestTable);
            Assert.Equal(HttpStatusCode.PreconditionFailed, error.Status);

            var errorItem = error.Item.ToObject <ToDoWithSystemPropertiesType>(JsonSerializer.Create(service.SerializerSettings));

            Assert.Equal(errorItem.Id, updatedItem.Id);
            Assert.Equal(errorItem.String, updatedItem.String);
            Assert.Equal(errorItem.Version, updatedItem.Version);
            Assert.Equal(errorItem.CreatedAt, updatedItem.CreatedAt);
            Assert.Equal(errorItem.UpdatedAt, updatedItem.UpdatedAt);

            Assert.Equal(error.Result.ToString(Formatting.None), conflictResult);

            Assert.Equal(1L, service.SyncContext.PendingOperations); // operation not removed
            updatedItem = await table.LookupAsync("b");

            Assert.Equal("Hey", updatedItem.String); // item is not updated

            await service.SyncContext.PushAsync();

            Assert.Equal(0L, service.SyncContext.PendingOperations); // operation now succeeds

            updatedItem = await table.LookupAsync("b");

            Assert.Equal("Wow", updatedItem.String); // item is updated
        }
        public async Task Insert_AllTypes_ThenRead_ThenPush_ThenLookup()
        {
            ResetDatabase("AllBaseTypesWithAllSystemPropertiesType");

            var hijack = new TestHttpHandler();
            var store  = new MobileServiceSQLiteStore(TestDbName);

            store.DefineTable <AllBaseTypesWithAllSystemPropertiesType>();

            IMobileServiceClient service = await CreateClient(hijack, store);

            IMobileServiceSyncTable <AllBaseTypesWithAllSystemPropertiesType> table = service.GetSyncTable <AllBaseTypesWithAllSystemPropertiesType>();

            // first insert an item
            var inserted = new AllBaseTypesWithAllSystemPropertiesType()
            {
                Id               = "abc",
                Bool             = true,
                Byte             = 11,
                SByte            = -11,
                UShort           = 22,
                Short            = -22,
                UInt             = 33,
                Int              = -33,
                ULong            = 44,
                Long             = -44,
                Float            = 55.66f,
                Double           = 66.77,
                Decimal          = 77.88M,
                String           = "EightyEight",
                Char             = '9',
                DateTime         = new DateTime(2010, 10, 10, 10, 10, 10, DateTimeKind.Utc),
                DateTimeOffset   = new DateTimeOffset(2011, 11, 11, 11, 11, 11, 11, TimeSpan.Zero),
                Nullable         = 12.13,
                NullableDateTime = new DateTime(2010, 10, 10, 10, 10, 10, DateTimeKind.Utc),
                TimeSpan         = new TimeSpan(0, 12, 12, 15, 95),
                Uri              = new Uri("http://example.com"),
                Enum1            = Enum1.Enum1Value2,
                Enum2            = Enum2.Enum2Value2,
                Enum3            = Enum3.Enum3Value2,
                Enum4            = Enum4.Enum4Value2,
                Enum5            = Enum5.Enum5Value2,
                Enum6            = Enum6.Enum6Value2
            };

            await table.InsertAsync(inserted);

            IList <AllBaseTypesWithAllSystemPropertiesType> records = await table.ToListAsync();

            Assert.Equal(1, records.Count);

            Assert.Equal(records.First(), inserted);

            // now push
            hijack.AddResponseContent(@"
{""id"":""abc"",
""bool"":true,
""byte"":11,
""sByte"":-11,
""uShort"":22,
""short"":-22,
""uInt"":33,
""int"":-33,
""uLong"":44,
""long"":-44,
""float"":55.66,
""double"":66.77,
""decimal"":77.88,
""string"":""EightyEight"",
""char"":""9"",
""dateTime"":""2010-10-10T10:10:10.000Z"",
""dateTimeOffset"":""2011-11-11T11:11:11.011Z"",
""nullableDateTime"":""2010-10-10T10:10:10.000Z"",
""timeSpan"":""12:12:15.095"",
""nullable"":12.13,
""uri"":""http://example.com/"",
""enum1"":""Enum1Value2"",
""enum2"":""Enum2Value2"",
""enum3"":""Enum3Value2"",
""enum4"":""Enum4Value2"",
""enum5"":""Enum5Value2"",
""enum6"":""Enum6Value2"",
""version"":""XYZ""}");
            await service.SyncContext.PushAsync();

            AllBaseTypesWithAllSystemPropertiesType lookedUp = await table.LookupAsync("abc");

            inserted.Version = "XYZ";
            Assert.Equal(inserted, lookedUp);
        }
 public Task <Ticket> GetTicket(string id)
 {
     return(_ticketsTable.LookupAsync(id));
 }
Esempio n. 8
0
        public async Task <Movie> GetMovieAsync(string id)
        {
            await InitAndSync();

            return(await _moviesTable.LookupAsync(id));
        }
Esempio n. 9
0
 public async Task <Idea> GetIdeaAsync(string id) =>
 await PerformNetworkOperationAsync(async() =>
 {
     await _table.PullAsync("Idea", _table.Where(i => i.Id == id));
     return(await _table.LookupAsync(id));
 });
        public async Task SystemPropertiesArePreserved_OnlyWhenReturnedFromServer()
        {
            ResetDatabase(TestTable);

            var hijack = new TestHttpHandler();
            var store  = new MobileServiceSQLiteStore(TestDbName);

            store.DefineTable <ToDoWithSystemPropertiesType>();

            IMobileServiceClient service = await CreateClient(hijack, store);

            IMobileServiceSyncTable <ToDoWithSystemPropertiesType> table = service.GetSyncTable <ToDoWithSystemPropertiesType>();

            // first insert an item
            var updatedItem = new ToDoWithSystemPropertiesType()
            {
                Id        = "b",
                String    = "Hey",
                Version   = "abc",
                CreatedAt = new DateTime(2013, 1, 1, 1, 1, 1, DateTimeKind.Utc),
                UpdatedAt = new DateTime(2013, 1, 1, 1, 1, 2, DateTimeKind.Utc)
            };
            await table.UpdateAsync(updatedItem);

            var lookedupItem = await table.LookupAsync("b");

            Assert.AreEqual(lookedupItem.String, "Hey");
            Assert.AreEqual(lookedupItem.Version, "abc");
            // we ignored the sys properties on the local object
            Assert.AreEqual(lookedupItem.CreatedAt, new DateTime(0, DateTimeKind.Utc));
            Assert.AreEqual(lookedupItem.UpdatedAt, new DateTime(0, DateTimeKind.Utc));

            Assert.AreEqual(service.SyncContext.PendingOperations, 1L); // operation pending

            hijack.OnSendingRequest = async req =>
            {
                // we request all the system properties present on DefineTable<> object
                Assert.AreEqual(req.RequestUri.Query, "?__systemproperties=__createdAt%2C__updatedAt%2C__version%2C__deleted");

                string content = await req.Content.ReadAsStringAsync();

                Assert.AreEqual(content, @"{""id"":""b"",""String"":""Hey""}"); // the system properties are not sent to server
                return(req);
            };
            string updateResult = "{\"id\":\"b\",\"String\":\"Wow\",\"__version\":\"def\",\"__createdAt\":\"2014-01-29T23:01:33.444Z\", \"__updatedAt\":\"2014-01-30T23:01:33.444Z\"}";

            hijack.Responses.Add(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(updateResult)
            });                                                                                                             // push
            await service.SyncContext.PushAsync();

            Assert.AreEqual(service.SyncContext.PendingOperations, 0L); // operation removed

            lookedupItem = await table.LookupAsync("b");

            Assert.AreEqual(lookedupItem.String, "Wow");
            Assert.AreEqual(lookedupItem.Version, "def");
            // we preserved the system properties returned from server on update
            Assert.AreEqual(lookedupItem.CreatedAt.ToUniversalTime(), new DateTime(2014, 01, 29, 23, 1, 33, 444, DateTimeKind.Utc));
            Assert.AreEqual(lookedupItem.UpdatedAt.ToUniversalTime(), new DateTime(2014, 01, 30, 23, 1, 33, 444, DateTimeKind.Utc));
        }
Esempio n. 11
0
 public async Task <T> ReadItemAsync(string id)
 => await table.LookupAsync(id);