Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
[client][managed][offline] insert should throw if the item already ex…
Browse files Browse the repository at this point in the history
…ists #491
  • Loading branch information
hasankhan committed Nov 15, 2014
1 parent 1ff0420 commit fc13891
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ private Task ExecuteOperationAsync(MobileServiceTableOperation operation, JObjec
}
catch (Exception ex)
{
if (ex is MobileServiceLocalStoreException)
{
throw;
}
throw new MobileServiceLocalStoreException(Resources.SyncStore_OperationFailed, ex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ public override void Collapse(MobileServiceTableOperation newOperation)
}
}

public override Task ExecuteLocalAsync(IMobileServiceLocalStore store, JObject item)
public override async Task ExecuteLocalAsync(IMobileServiceLocalStore store, JObject item)
{
return store.UpsertAsync(this.TableName, item, fromServer: false);
if (await store.LookupAsync(this.TableName, this.ItemId) != null)
{
throw new MobileServiceLocalStoreException(Resources.SyncContext_DuplicateInsert, null);
}

await store.UpsertAsync(this.TableName, item, fromServer: false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,33 @@ public class SQLiteStoreIntegrationTests : TestBase

public static string TestDbName = SQLiteStoreTests.TestDbName;

[AsyncTestMethod]
public async Task InsertAsync_Throws_IfItemAlreadyExistsInLocalStore()
{
ResetDatabase(TestTable);

var store = new MobileServiceSQLiteStore(TestDbName);
store.DefineTable(TestTable, new JObject()
{
{ "id", String.Empty},
{ "String", String.Empty }
});

var hijack = new TestHttpHandler();
IMobileServiceClient service = await CreateClient(hijack, store);

string pullResult = "[{\"id\":\"abc\",\"String\":\"Wow\"}]";
hijack.AddResponseContent(pullResult);
hijack.AddResponseContent("[]");

IMobileServiceSyncTable table = service.GetSyncTable(TestTable);
await table.PullAsync(null, null);

var ex = await AssertEx.Throws<MobileServiceLocalStoreException>(() => table.InsertAsync(new JObject() { { "id", "abc" } }));

Assert.AreEqual(ex.Message, "An insert operation on the item is already in the queue.");
}

[AsyncTestMethod]
public async Task ReadAsync_RoundTripsDate()
{
Expand Down

0 comments on commit fc13891

Please sign in to comment.