public async Task CreateFilterTestWithMultipleRequests_WithTypedTable() { var client = this.GetClient(); int numberOfRequests = new Random().Next(2, 5); var handler = new HandlerWithMultipleRequests(this, numberOfRequests); var filteredClient = new MobileServiceClient(client.MobileAppUri, handler); var typedTable = filteredClient.GetTable <RoundTripTableItem>(); var untypedTable = filteredClient.GetTable("RoundTripTable"); var uniqueId = Guid.NewGuid().ToString("N"); var item = new RoundTripTableItem { Name = uniqueId }; await typedTable.InsertAsync(item); Assert.False(handler.TestFailed); // Cleanup handler.NumberOfRequests = 1; // no need to send it multiple times anymore var items = await untypedTable.ReadAsync("$select=name,id&$filter=name eq '" + uniqueId + "'") as JArray; items.ForEach(async t => await untypedTable.DeleteAsync(t as JObject)); Assert.True(items.Count == numberOfRequests); }
private static ZumoTest CreateSetupSchemaTest() { return(new ZumoTest("Setup dynamic schema", async delegate(ZumoTest test) { var client = ZumoTestGlobals.Instance.Client; var table = client.GetTable <RoundTripTableItem>(); Random rndGen = new Random(1); try { RoundTripTableItem item = new RoundTripTableItem { Bool1 = true, ComplexType1 = new ComplexType[] { new ComplexType(rndGen) }, ComplexType2 = new ComplexType2(rndGen), Date1 = DateTime.Now, Double1 = 123.456, EnumType = EnumType.First, Int1 = 1, Long1 = 1, String1 = "hello", }; await table.InsertAsync(item); test.AddLog("Inserted item to create schema"); return true; } catch (Exception ex) { test.AddLog("Error setting up the dynamic schema: {0}", ex); return false; } })); }
private static ZumoTest CreateTypedUpdateTest <TExpectedException>( string testName, RoundTripTableItem itemToInsert, RoundTripTableItem itemToUpdate, bool setUpdatedId = true) where TExpectedException : Exception { return(new ZumoTest(testName, async delegate(ZumoTest test) { var client = ZumoTestGlobals.Instance.Client; var table = client.GetTable <RoundTripTableItem>(); try { await table.InsertAsync(itemToInsert); test.AddLog("Inserted item with id {0}", itemToInsert.Id); if (setUpdatedId) { itemToUpdate.Id = itemToInsert.Id; } var expectedItem = itemToUpdate.Clone(); await table.UpdateAsync(itemToUpdate); test.AddLog("Updated item; now retrieving it to compare with the expected value"); var retrievedItem = await table.LookupAsync(itemToInsert.Id); test.AddLog("Retrieved item"); if (!expectedItem.Equals(retrievedItem)) { test.AddLog("Error, retrieved item is different than the expected value. Expected: {0}; actual: {1}", expectedItem, retrievedItem); return false; } // cleanup await table.DeleteAsync(retrievedItem); if (typeof(TExpectedException) == typeof(ExceptionTypeWhichWillNeverBeThrown)) { return true; } else { test.AddLog("Error, test should have failed with {0}, but succeeded.", typeof(TExpectedException).FullName); return false; } } catch (TExpectedException ex) { test.AddLog("Caught expected exception - {0}: {1}", ex.GetType().FullName, ex.Message); return true; } })); }
private static ZumoTest CreateSetupSchemaTest() { return(new ZumoTest("Setup dynamic schema", async delegate(ZumoTest test) { var client = ZumoTestGlobals.Instance.Client; Random rndGen = new Random(1); try { if (!ZumoTestGlobals.Instance.IsNetRuntime) { var table = client.GetTable <RoundTripTableItem>(); RoundTripTableItem item = new RoundTripTableItem { Bool1 = true, ComplexType1 = new ComplexType[] { new ComplexType(rndGen) }, ComplexType2 = new ComplexType2(rndGen), Date1 = DateTime.Now, Double1 = 123.456, EnumType = EnumType.First, Int1 = 1, Long1 = 1, String1 = "hello", }; await table.InsertAsync(item); test.AddLog("Inserted item to create schema on the int id table"); } var table2 = client.GetTable <StringIdRoundTripTableItem>(); var item2 = new StringIdRoundTripTableItem { Bool = true, Name = "hello", Number = 1.23, ComplexType = "a b c".Split(), Date = DateTime.UtcNow }; await table2.InsertAsync(item2); test.AddLog("Inserted item to create schema on the string id table"); return true; } catch (Exception ex) { test.AddLog("Error setting up the dynamic schema: {0}", ex); return false; } }, ZumoTestGlobals.RuntimeFeatureNames.INT_ID_TABLES, ZumoTestGlobals.RuntimeFeatureNames.STRING_ID_TABLES)); }
private static ZumoTest CreateUserAgentValidationTest() { return(new ZumoTest("Validation User-Agent header", async delegate(ZumoTest test) { var client = ZumoTestGlobals.Instance.Client; var filter = new FilterToCaptureHttpTraffic(); client = client.WithFilter(filter); var table = client.GetTable <RoundTripTableItem>(); var item = new RoundTripTableItem { String1 = "hello" }; await table.InsertAsync(item); Action <string> dumpHeaders = delegate(string operation) { test.AddLog("Headers for {0}:", operation); test.AddLog(" Request:"); foreach (var header in filter.RequestHeaders.Keys) { test.AddLog(" {0}: {1}", header, filter.RequestHeaders[header]); } test.AddLog(" Response:"); foreach (var header in filter.ResponseHeaders.Keys) { test.AddLog(" {0}: {1}", header, filter.ResponseHeaders[header]); } }; dumpHeaders("Insert"); item.Double1 = 123; await table.UpdateAsync(item); dumpHeaders("Update"); var item2 = await table.LookupAsync(item.Id); dumpHeaders("Read"); await table.DeleteAsync(item); dumpHeaders("Delete"); return true; })); }
private async Task CreateUserAgentValidationTest() { var handler = new HandlerToCaptureHttpTraffic(); MobileServiceClient client = new MobileServiceClient(MobileServiceRuntimeUrl, handler); var table = client.GetTable <RoundTripTableItem>(); var item = new RoundTripTableItem { Name = "hello" }; await table.InsertAsync(item); Action <string> dumpAndValidateHeaders = delegate(string operation) { if (!handler.RequestHeaders.TryGetValue("User-Agent", out string userAgent)) { throw new InvalidOperationException("This will fail the test"); } else { Regex expected = new Regex(@"^ZUMO\/\d.\d"); if (!expected.IsMatch(userAgent)) { throw new InvalidOperationException("This will fail the test"); } } }; dumpAndValidateHeaders("Insert"); item.Number = 123; await table.UpdateAsync(item); dumpAndValidateHeaders("Update"); var item2 = await table.LookupAsync(item.Id); dumpAndValidateHeaders("Read"); await table.DeleteAsync(item); dumpAndValidateHeaders("Delete"); }
private async Task CreateFilterTestWithMultipleRequests(bool typed) { var client = this.GetClient(); int numberOfRequests = new Random().Next(2, 5); var handler = new HandlerWithMultipleRequests(this, numberOfRequests); var filteredClient = new MobileServiceClient(client.MobileAppUri, handler); var typedTable = filteredClient.GetTable <RoundTripTableItem>(); var untypedTable = filteredClient.GetTable("RoundTripTable"); var uniqueId = Guid.NewGuid().ToString("N"); if (typed) { var item = new RoundTripTableItem { Name = uniqueId }; await typedTable.InsertAsync(item); } else { var item = new JObject(new JProperty("name", uniqueId)); await untypedTable.InsertAsync(item); } Assert.False(handler.TestFailed); handler.NumberOfRequests = 1; // no need to send it multiple times anymore var items = await untypedTable.ReadAsync("$select=name,id&$filter=name eq '" + uniqueId + "'"); var array = (JArray)items; bool passed = (array.Count == numberOfRequests); // Cleanup foreach (var item in array) { await untypedTable.DeleteAsync(item as JObject); } Assert.True(passed); }
private async Task CreateFilterTestWithMultipleRequests(bool typed) { Log(string.Format(CultureInfo.InvariantCulture, "### Filter which maps one requests to many - {0} client", typed ? "typed" : "untyped")); var client = this.GetClient(); int numberOfRequests = new Random().Next(2, 5); var handler = new HandlerWithMultipleRequests(this, numberOfRequests); Log("Created a filter which will replay the request {0} times", numberOfRequests); var filteredClient = new MobileServiceClient(client.MobileAppUri, handler); var typedTable = filteredClient.GetTable<RoundTripTableItem>(); var untypedTable = filteredClient.GetTable("RoundTripTable"); var uniqueId = Guid.NewGuid().ToString("N"); if (typed) { var item = new RoundTripTableItem { Name = uniqueId }; await typedTable.InsertAsync(item); } else { var item = new JObject(new JProperty("name", uniqueId)); await untypedTable.InsertAsync(item); } if (handler.TestFailed) { Assert.Fail("Filter reported a test failure. Aborting."); } Log("Inserted the data; now retrieving it to see how many items we have inserted."); handler.NumberOfRequests = 1; // no need to send it multiple times anymore var items = await untypedTable.ReadAsync("$select=name,id&$filter=name eq '" + uniqueId + "'"); var array = (JArray)items; bool passed; if (array.Count == numberOfRequests) { Log("Filter inserted correct number of items."); passed = true; } else { Log("Error, filtered client should have inserted {0} items, but there are {1}", numberOfRequests, array.Count); passed = false; } // Cleanup foreach (var item in array) { await untypedTable.DeleteAsync(item as JObject); } Log("Cleanup: removed added items."); if (!passed) { Assert.Fail(""); } }
private async Task CreateUserAgentValidationTest() { Log("Validation User-Agent header"); var handler = new HandlerToCaptureHttpTraffic(); MobileServiceClient client = new MobileServiceClient( this.GetTestSetting("MobileServiceRuntimeUrl"), handler); var table = client.GetTable<RoundTripTableItem>(); var item = new RoundTripTableItem { Name = "hello" }; await table.InsertAsync(item); Action<string> dumpAndValidateHeaders = delegate(string operation) { Log("Headers for {0}:", operation); Log(" Request:"); foreach (var header in handler.RequestHeaders.Keys) { Log(" {0}: {1}", header, handler.RequestHeaders[header]); } Log(" Response:"); foreach (var header in handler.ResponseHeaders.Keys) { Log(" {0}: {1}", header, handler.ResponseHeaders[header]); } string userAgent; if (!handler.RequestHeaders.TryGetValue("User-Agent", out userAgent)) { Log("No user-agent header in the request"); throw new InvalidOperationException("This will fail the test"); } else { Regex expected = new Regex(@"^ZUMO\/\d.\d"); if (expected.IsMatch(userAgent)) { Log("User-Agent validated correclty"); } else { Log("User-Agent didn't validate properly."); throw new InvalidOperationException("This will fail the test"); } } }; dumpAndValidateHeaders("Insert"); item.Number = 123; await table.UpdateAsync(item); dumpAndValidateHeaders("Update"); var item2 = await table.LookupAsync(item.Id); dumpAndValidateHeaders("Read"); await table.DeleteAsync(item); dumpAndValidateHeaders("Delete"); }
private static ZumoTest CreateFilterTestWithMultipleRequests(bool typed) { string testName = string.Format(CultureInfo.InvariantCulture, "Filter which maps one requests to many - {0} client", typed ? "typed" : "untyped"); return new ZumoTest(testName, async delegate(ZumoTest test) { var client = ZumoTestGlobals.Instance.Client; int numberOfRequests = new Random().Next(2, 5); var handler = new HandlerWithMultipleRequests(test, numberOfRequests); test.AddLog("Created a filter which will replay the request {0} times", numberOfRequests); var filteredClient = new MobileServiceClient(client.ApplicationUri, client.ApplicationKey, handler); var typedTable = filteredClient.GetTable<RoundTripTableItem>(); var untypedTable = filteredClient.GetTable(ZumoTestGlobals.RoundTripTableName); var uniqueId = Guid.NewGuid().ToString("N"); if (typed) { var item = new RoundTripTableItem { String1 = uniqueId }; await typedTable.InsertAsync(item); } else { var item = new JObject(new JProperty("string1", uniqueId)); await untypedTable.InsertAsync(item); } if (handler.TestFailed) { test.AddLog("Filter reported a test failure. Aborting."); return false; } test.AddLog("Inserted the data; now retrieving it to see how many items we have inserted."); handler.NumberOfRequests = 1; // no need to send it multiple times anymore var items = await untypedTable.ReadAsync("$select=string1,id&$filter=string1 eq '" + uniqueId + "'"); var array = (JArray)items; bool passed; if (array.Count == numberOfRequests) { test.AddLog("Filter inserted correct number of items."); passed = true; } else { test.AddLog("Error, filtered client should have inserted {0} items, but there are {1}", numberOfRequests, array.Count); passed = false; } // Cleanup foreach (var item in array) { await untypedTable.DeleteAsync(item as JObject); } test.AddLog("Cleanup: removed added items."); return passed; }); }
private static ZumoTest CreateUserAgentValidationTest() { return new ZumoTest("Validation User-Agent header", async delegate(ZumoTest test) { var handler = new HandlerToCaptureHttpTraffic(); MobileServiceClient client = new MobileServiceClient( ZumoTestGlobals.Instance.Client.ApplicationUri, ZumoTestGlobals.Instance.Client.ApplicationKey, handler); var table = client.GetTable<RoundTripTableItem>(); var item = new RoundTripTableItem { String1 = "hello" }; await table.InsertAsync(item); Action<string> dumpAndValidateHeaders = delegate(string operation) { test.AddLog("Headers for {0}:", operation); test.AddLog(" Request:"); foreach (var header in handler.RequestHeaders.Keys) { test.AddLog(" {0}: {1}", header, handler.RequestHeaders[header]); } test.AddLog(" Response:"); foreach (var header in handler.ResponseHeaders.Keys) { test.AddLog(" {0}: {1}", header, handler.ResponseHeaders[header]); } string userAgent; if (!handler.RequestHeaders.TryGetValue("User-Agent", out userAgent)) { test.AddLog("No user-agent header in the request"); throw new InvalidOperationException("This will fail the test"); } else { Regex expected = new Regex(@"^ZUMO\/\d.\d"); if (expected.IsMatch(userAgent)) { test.AddLog("User-Agent validated correclty"); } else { test.AddLog("User-Agent didn't validate properly."); throw new InvalidOperationException("This will fail the test"); } } }; dumpAndValidateHeaders("Insert"); item.Double1 = 123; await table.UpdateAsync(item); dumpAndValidateHeaders("Update"); var item2 = await table.LookupAsync(item.Id); dumpAndValidateHeaders("Read"); await table.DeleteAsync(item); dumpAndValidateHeaders("Delete"); return true; }); }
private static ZumoTest CreateSimpleTypedRoundTripTest <TExpectedException>( string testName, RoundTripTestType type, object value) where TExpectedException : Exception { return(new ZumoTest(testName, async delegate(ZumoTest test) { var client = ZumoTestGlobals.Instance.Client; var table = client.GetTable <RoundTripTableItem>(); var item = new RoundTripTableItem(); switch (type) { case RoundTripTestType.Bool: item.Bool1 = (bool?)value; break; case RoundTripTestType.ComplexWithConverter: item.ComplexType1 = (ComplexType[])value; break; case RoundTripTestType.ComplexWithCustomSerialization: item.ComplexType2 = (ComplexType2)value; break; case RoundTripTestType.Date: item.Date1 = (DateTime?)value; break; case RoundTripTestType.Double: item.Double1 = (double)value; break; case RoundTripTestType.Enum: item.EnumType = (EnumType)value; break; case RoundTripTestType.Int: item.Int1 = (int)value; break; case RoundTripTestType.Long: item.Long1 = (long)value; break; case RoundTripTestType.String: item.String1 = (string)value; break; case RoundTripTestType.Id: item.Id = (int)value; break; default: throw new ArgumentException("Invalid type"); } RoundTripTableItem originalItem = item.Clone(); try { await table.InsertAsync(item); test.AddLog("Inserted item, id = {0}", item.Id); if (item.Id <= 0) { test.AddLog("Error, insert didn't succeed (id == 0)"); return false; } RoundTripTableItem roundTripped = await table.LookupAsync(item.Id); test.AddLog("Retrieved the item from the service"); if (!originalItem.Equals(roundTripped)) { test.AddLog("Round-tripped item is different! Expected: {0}; actual: {1}", originalItem, roundTripped); return false; } if (type == RoundTripTestType.String && item.String1 != null && item.String1.Length < 50) { test.AddLog("Now querying the table for the item (validating characters on query)"); var queried = await table.Where(i => i.Id > (item.Id - 40) && i.String1 == item.String1).ToListAsync(); var lastItem = queried.Where(i => i.Id == item.Id).First(); if (originalItem.Equals(lastItem)) { test.AddLog("Query for item succeeded"); } else { test.AddLog("Round-tripped (queried) item is different! Expected: {0}; actual: {1}", originalItem, lastItem); return false; } } if (typeof(TExpectedException) == typeof(ExceptionTypeWhichWillNeverBeThrown)) { return true; } else { test.AddLog("Error, test should have failed with {0}, but succeeded.", typeof(TExpectedException).FullName); return false; } } catch (TExpectedException ex) { test.AddLog("Caught expected exception - {0}: {1}", ex.GetType().FullName, ex.Message); return true; } })); }
private static ZumoTest CreateFilterTestWithMultipleRequests(bool typed) { string testName = string.Format(CultureInfo.InvariantCulture, "Filter which maps one requests to many - {0} client", typed ? "typed" : "untyped"); return(new ZumoTest(testName, async delegate(ZumoTest test) { var client = ZumoTestGlobals.Instance.Client; int numberOfRequests = new Random().Next(2, 5); var handler = new HandlerWithMultipleRequests(test, numberOfRequests); test.AddLog("Created a filter which will replay the request {0} times", numberOfRequests); var filteredClient = new MobileServiceClient(client.ApplicationUri, client.ApplicationKey, handler); var typedTable = filteredClient.GetTable <RoundTripTableItem>(); var untypedTable = filteredClient.GetTable(ZumoTestGlobals.RoundTripTableName); var uniqueId = Guid.NewGuid().ToString("N"); if (typed) { var item = new RoundTripTableItem { String1 = uniqueId }; await typedTable.InsertAsync(item); } else { var item = new JObject(new JProperty("string1", uniqueId)); await untypedTable.InsertAsync(item); } if (handler.TestFailed) { test.AddLog("Filter reported a test failure. Aborting."); return false; } test.AddLog("Inserted the data; now retrieving it to see how many items we have inserted."); handler.NumberOfRequests = 1; // no need to send it multiple times anymore var items = await untypedTable.ReadAsync("$select=string1,id&$filter=string1 eq '" + uniqueId + "'"); var array = (JArray)items; bool passed; if (array.Count == numberOfRequests) { test.AddLog("Filter inserted correct number of items."); passed = true; } else { test.AddLog("Error, filtered client should have inserted {0} items, but there are {1}", numberOfRequests, array.Count); passed = false; } // Cleanup foreach (var item in array) { await untypedTable.DeleteAsync(item as JObject); } test.AddLog("Cleanup: removed added items."); return passed; })); }
private static ZumoTest CreateUserAgentValidationTest() { return(new ZumoTest("Validation User-Agent header", async delegate(ZumoTest test) { var handler = new HandlerToCaptureHttpTraffic(); MobileServiceClient client = new MobileServiceClient( ZumoTestGlobals.Instance.Client.ApplicationUri, ZumoTestGlobals.Instance.Client.ApplicationKey, handler); var table = client.GetTable <RoundTripTableItem>(); var item = new RoundTripTableItem { String1 = "hello" }; await table.InsertAsync(item); Action <string> dumpAndValidateHeaders = delegate(string operation) { test.AddLog("Headers for {0}:", operation); test.AddLog(" Request:"); foreach (var header in handler.RequestHeaders.Keys) { test.AddLog(" {0}: {1}", header, handler.RequestHeaders[header]); } test.AddLog(" Response:"); foreach (var header in handler.ResponseHeaders.Keys) { test.AddLog(" {0}: {1}", header, handler.ResponseHeaders[header]); } string userAgent; if (!handler.RequestHeaders.TryGetValue("User-Agent", out userAgent)) { test.AddLog("No user-agent header in the request"); throw new InvalidOperationException("This will fail the test"); } else { Regex expected = new Regex(@"^ZUMO\/\d.\d"); if (expected.IsMatch(userAgent)) { test.AddLog("User-Agent validated correclty"); } else { test.AddLog("User-Agent didn't validate properly."); throw new InvalidOperationException("This will fail the test"); } } }; dumpAndValidateHeaders("Insert"); item.Double1 = 123; await table.UpdateAsync(item); dumpAndValidateHeaders("Update"); var item2 = await table.LookupAsync(item.Id); dumpAndValidateHeaders("Read"); await table.DeleteAsync(item); dumpAndValidateHeaders("Delete"); return true; })); }
private static ZumoTest CreateTypedUpdateTest( string testName, RoundTripTableItem itemToInsert, RoundTripTableItem itemToUpdate) { return(CreateTypedUpdateTest <ExceptionTypeWhichWillNeverBeThrown>(testName, itemToInsert, itemToUpdate)); }
internal static ZumoTestGroup CreateTests() { ZumoTestGroup result = new ZumoTestGroup("Update / delete tests"); DateTime now = DateTime.UtcNow; int seed = now.Year * 10000 + now.Month * 100 + now.Day; Random rndGen = new Random(seed); result.AddTest(CreateTypedUpdateTest("Update typed item", new RoundTripTableItem(rndGen), new RoundTripTableItem(rndGen))); result.AddTest(CreateTypedUpdateTest( "Update typed item, setting values to null", new RoundTripTableItem(rndGen), new RoundTripTableItem(rndGen) { Bool1 = null, ComplexType2 = null, Int1 = null, String1 = null, ComplexType1 = null })); result.AddTest(CreateTypedUpdateTest <MobileServiceInvalidOperationException>("(Neg) Update typed item, non-existing item id", new RoundTripTableItem(rndGen), new RoundTripTableItem(rndGen) { Id = 1000000000 }, false)); result.AddTest(CreateTypedUpdateTest <ArgumentException>("(Neg) Update typed item, id = 0", new RoundTripTableItem(rndGen), new RoundTripTableItem(rndGen) { Id = 0 }, false)); string toInsertJsonString = @"{ ""string1"":""hello"", ""bool1"":true, ""int1"":-1234, ""double1"":123.45, ""long1"":1234, ""date1"":""2012-12-13T09:23:12.000Z"", ""complexType1"":[{""Name"":""John Doe"",""Age"":33}, null], ""complexType2"":{""Name"":""John Doe"",""Age"":33,""Friends"":[""Jane""]} }"; string toUpdateJsonString = @"{ ""string1"":""world"", ""bool1"":false, ""int1"":9999, ""double1"":888.88, ""long1"":77777777, ""date1"":""1999-05-23T19:15:54.000Z"", ""complexType1"":[{""Name"":""Jane Roe"",""Age"":23}, null], ""complexType2"":{""Name"":""Jane Roe"",""Age"":23,""Friends"":[""John""]} }"; result.AddTest(CreateUntypedUpdateTest("Update untyped item", toInsertJsonString, toUpdateJsonString)); JsonValue nullValue = JsonValue.Parse("null"); JsonObject toUpdate = JsonObject.Parse(toUpdateJsonString); toUpdate["string1"] = nullValue; toUpdate["bool1"] = nullValue; toUpdate["complexType2"] = nullValue; toUpdate["complexType1"] = nullValue; toUpdate["int1"] = nullValue; result.AddTest(CreateUntypedUpdateTest("Update untyped item, setting values to null", toInsertJsonString, toUpdate.Stringify())); toUpdate["id"] = JsonValue.CreateNumberValue(1000000000); result.AddTest(CreateUntypedUpdateTest <MobileServiceInvalidOperationException>("(Neg) Update untyped item, non-existing item id", toInsertJsonString, toUpdate.Stringify(), false)); toUpdate["id"] = JsonValue.CreateNumberValue(0); result.AddTest(CreateUntypedUpdateTest <ArgumentException>("(Neg) Update typed item, id = 0", toInsertJsonString, toUpdateJsonString, false)); // Delete tests result.AddTest(CreateDeleteTest("Delete typed item", true, DeleteTestType.ValidDelete)); result.AddTest(CreateDeleteTest("(Neg) Delete typed item with non-existing id", true, DeleteTestType.NonExistingId)); result.AddTest(CreateDeleteTest("Delete untyped item", false, DeleteTestType.ValidDelete)); result.AddTest(CreateDeleteTest("(Neg) Delete untyped item with non-existing id", false, DeleteTestType.NonExistingId)); result.AddTest(CreateDeleteTest("(Neg) Delete untyped item without id field", false, DeleteTestType.NoIdField)); result.AddTest(new ZumoTest("Refresh - updating item with server modifications", async delegate(ZumoTest test) { var client = ZumoTestGlobals.Instance.Client; var table = client.GetTable <RoundTripTableItem>(); int randomSeed = Environment.TickCount; test.AddLog("Using random seed {0}", randomSeed); Random random = new Random(randomSeed); var item = new RoundTripTableItem(random); test.AddLog("Item to be inserted: {0}", item); await table.InsertAsync(item); test.AddLog("Added item with id = {0}", item.Id); var item2 = new RoundTripTableItem(random); item2.Id = item.Id; test.AddLog("Item to update: {0}", item2); await table.UpdateAsync(item2); test.AddLog("Updated item"); test.AddLog("Now refreshing first object"); await table.RefreshAsync(item); test.AddLog("Refreshed item: {0}", item); if (item.Equals(item2)) { test.AddLog("Item was refreshed successfully"); return(true); } else { test.AddLog("Error, refresh didn't happen successfully"); return(false); } })); result.AddTest(new ZumoTest("Refresh item without id does not send request", async delegate(ZumoTest test) { var client = ZumoTestGlobals.Instance.Client.WithFilter(new FilterWhichThrows()); var table = client.GetTable <RoundTripTableItem>(); var item = new RoundTripTableItem(new Random()); item.Id = 0; await table.RefreshAsync(item); test.AddLog("Call to RefreshAsync didn't go to the network, as expected."); return(true); })); return(result); }
private static ZumoTest CreateDeleteTest(string testName, bool useTypedTable, DeleteTestType testType) { if (useTypedTable && testType == DeleteTestType.NoIdField) { throw new ArgumentException("Cannot send a delete request without an id field on a typed table."); } return(new ZumoTest(testName, async delegate(ZumoTest test) { var client = ZumoTestGlobals.Instance.Client; var typedTable = client.GetTable <RoundTripTableItem>(); var untypedTable = client.GetTable(ZumoTestGlobals.RoundTripTableName); RoundTripTableItem itemToInsert = new RoundTripTableItem { String1 = "hello", Bool1 = true, Int1 = 123, }; await typedTable.InsertAsync(itemToInsert); test.AddLog("Inserted item to be deleted"); int id = itemToInsert.Id; switch (testType) { case DeleteTestType.ValidDelete: if (useTypedTable) { await typedTable.DeleteAsync(itemToInsert); } else { await untypedTable.DeleteAsync(JsonObject.Parse("{\"id\":" + id + "}")); } test.AddLog("Delete succeeded; verifying that object isn't in the service anymore."); try { var response = await untypedTable.LookupAsync(id); test.AddLog("Error, delete succeeded, but item was returned by the service: {0}", response.Stringify()); return false; } catch (MobileServiceInvalidOperationException msioe) { return Validate404Response(test, msioe); } case DeleteTestType.NonExistingId: try { if (useTypedTable) { itemToInsert.Id = 1000000000; await typedTable.DeleteAsync(itemToInsert); } else { JsonObject jo = new JsonObject(); jo.Add("id", JsonValue.CreateNumberValue(1000000000)); await untypedTable.DeleteAsync(jo); } test.AddLog("Error, deleting item with non-existing id should fail, but succeeded"); return false; } catch (MobileServiceInvalidOperationException msioe) { return Validate404Response(test, msioe); } default: try { JsonObject jo = new JsonObject(); jo.Add("Name", JsonValue.CreateStringValue("hello")); await untypedTable.DeleteAsync(jo); test.AddLog("Error, deleting item with non-existing id should fail, but succeeded"); return false; } catch (ArgumentException ex) { test.AddLog("Caught expected exception - {0}: {1}", ex.GetType().FullName, ex.Message); return true; } } })); }
private static ZumoTest CreateUserAgentValidationTest() { return new ZumoTest("Validation User-Agent header", async delegate(ZumoTest test) { var client = ZumoTestGlobals.Instance.Client; var filter = new FilterToCaptureHttpTraffic(); client = client.WithFilter(filter); var table = client.GetTable<RoundTripTableItem>(); var item = new RoundTripTableItem { String1 = "hello" }; await table.InsertAsync(item); Action<string> dumpHeaders = delegate(string operation) { test.AddLog("Headers for {0}:", operation); test.AddLog(" Request:"); foreach (var header in filter.RequestHeaders.Keys) { test.AddLog(" {0}: {1}", header, filter.RequestHeaders[header]); } test.AddLog(" Response:"); foreach (var header in filter.ResponseHeaders.Keys) { test.AddLog(" {0}: {1}", header, filter.ResponseHeaders[header]); } }; dumpHeaders("Insert"); item.Double1 = 123; await table.UpdateAsync(item); dumpHeaders("Update"); var item2 = await table.LookupAsync(item.Id); dumpHeaders("Read"); await table.DeleteAsync(item); dumpHeaders("Delete"); return true; }); }