public async Task ShouldSupportDerivedComplexTypeAsync() { var settings = new CreatorSettings() { NullValueProbability = 0.0 }; var uri = new Uri(this.BaseAddress); var entitySetName = "ComplexTypeTests_Entity"; // clear respository await this.ClearRepositoryAsync("ComplexTypeTests_Entity"); var rand = new Random(RandomSeedGenerator.GetRandomSeed()); // post new entity to repository var baseline = InstanceCreator.CreateInstanceOf <ComplexTypeTests_Entity>(rand, settings); await PostNewEntityAsync(uri, entitySetName, baseline); int id = baseline.ID; var actual = (await GetEntitiesAsync(uri, entitySetName)).Where(t => t.ID == id).First(); AssertExtension.DeepEqual(baseline, actual); await UpdateEntityAsync(uri, entitySetName, actual, data => { data.ComplexType = InstanceCreator.CreateInstanceOf <ComplexTypeTests_ComplexType>(rand, settings); }); var afterUpdate = (await GetEntitiesAsync(uri, entitySetName)).Where(t => t.ID == id).First(); AssertExtension.DeepEqual(actual, afterUpdate); }
public async Task SupportPostCollectionPropertyByEntityPayload() { var settings = new CreatorSettings() { NullValueProbability = 0.0 }; var uri = new Uri(this.BaseAddress); var entitySetName = "CollectionProperty_Entity"; // clear respository this.ClearRepository("CollectionProperty_Entity"); var rand = new Random(RandomSeedGenerator.GetRandomSeed()); // post new entity to repository var baseline = InstanceCreator.CreateInstanceOf <CollectionProperty_Entity>(rand, settings); await PostNewEntityAsync(uri, entitySetName, baseline); int id = baseline.ID; var actual = (await GetEntitiesAsync(uri, entitySetName)).Where(t => t.ID == id).First(); AssertExtension.DeepEqual(baseline, actual); await UpdateEntityAsync(uri, entitySetName, actual, data => { data.StringList = InstanceCreator.CreateInstanceOf <List <string> >(rand, settings); data.ComplexTypeCollection = InstanceCreator.CreateInstanceOf <Collection <CollectionProperty_ComplexType> >(rand, settings); }); var afterUpdate = (await GetEntitiesAsync(uri, entitySetName)).Where(t => t.ID == id).First(); AssertExtension.DeepEqual(actual, afterUpdate); }
public async Task TestRandomEntityTypesAsync <T>(string entitySetName) { // clear respository await this.ClearRepositoryAsync(entitySetName); // TODO: Get ride of random generator in test codes. It's bad idea to introduce random factors in functional test var rand = new Random(RandomSeedGenerator.GetRandomSeed()); T entityBaseline = await PostNewEntityAsync <T>(entitySetName, rand); T entityBeforeUpdate = await ReadFirstEntityAsync <T>(entitySetName); AssertExtension.PrimitiveEqual(entityBaseline, entityBeforeUpdate); DataServiceResponse responseUpdate = await UpdateEntityAsync <T>(entitySetName, entityBeforeUpdate, rand); T entityAfterUpdate = await ReadFirstEntityAsync <T>(entitySetName); AssertExtension.PrimitiveEqual(entityBeforeUpdate, entityAfterUpdate); DataServiceResponse responseDelete = await DeleteEntityAsync <T>(entitySetName, entityAfterUpdate); T[] entities = await ReadAllEntitiesAsync <T>(entitySetName); Assert.Empty(entities); }
public virtual void CreateAndDeleteLinkToDerivedNavigationPropertyOnBaseEntitySet() { // clear respository this.ClearRepository("InheritanceTests_Vehicles"); Random r = new Random(RandomSeedGenerator.GetRandomSeed()); // post new entity to repository var car = InstanceCreator.CreateInstanceOf <Car>(r); var vehicle = InstanceCreator.CreateInstanceOf <MiniSportBike>(r, new CreatorSettings() { NullValueProbability = 0.0 }); DataServiceContext ctx = WriterClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4); ctx.AddObject("InheritanceTests_Vehicles", car); ctx.AddObject("InheritanceTests_Vehicles", vehicle); ctx.SaveChangesAsync().Wait(); ctx.SetLink(car, "SingleNavigationProperty", vehicle); ctx.SaveChangesAsync().Wait(); ctx = ReaderClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4); var cars = ctx.CreateQuery <Vehicle>("InheritanceTests_Vehicles").ExecuteAsync().Result.ToList().OfType <Car>(); var actual = cars.First(); ctx.LoadPropertyAsync(actual, "SingleNavigationProperty").Wait(); AssertExtension.PrimitiveEqual(vehicle, actual.SingleNavigationProperty); this.ClearRepository("InheritanceTests_Vehicles"); }
public static T CreateInstances <T>() { var results = InstanceCreator.CreateInstanceOf <T>(new Random(RandomSeedGenerator.GetRandomSeed()), new CreatorSettings { NullValueProbability = 0, AllowEmptyCollection = false }); return(results); }
public void RunQueryableOnAllPossibleTypes(Type type, string queryString) { int seed = RandomSeedGenerator.GetRandomSeed(); Random r = new Random(seed); Type generic = typeof(IEnumerable <>); var collectionType = generic.MakeGenericType(type); Type listType = typeof(List <>).MakeGenericType(type); var array = Activator.CreateInstance(listType); EnableQueryAttribute q = new EnableQueryAttribute(); var configuration = new HttpConfiguration(); configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; configuration.Count().Filter().OrderBy().Expand().MaxTop(null).Select(); configuration.Routes.MapHttpRoute("ApiDefault", "api/{controller}/{id}", new { id = RouteParameter.Optional }); configuration.EnableDependencyInjection(); var request = new HttpRequestMessage(HttpMethod.Get, "http://test/api/Objects?" + queryString); request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, configuration); var controllerContext = new HttpControllerContext( configuration, configuration.Routes.GetRouteData(request), request); var actionContext = new HttpActionContext(controllerContext, new ReflectedHttpActionDescriptor() { Configuration = configuration }); var context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(System.Net.HttpStatusCode.OK); context.Response.Content = new ObjectContent(collectionType, array, new JsonMediaTypeFormatter()); try { q.OnActionExecuted(context); Console.WriteLine(context.Response.Content.ReadAsStringAsync().Result); Assert.Equal(HttpStatusCode.OK, context.Response.StatusCode); } catch (ArgumentException ae) { // For example: // The type 'System.DateTime' of property 'NotAfter' in the // 'System.Security.Cryptography.X509Certificates.X509Certificate2' type is not a supported type. // Change to use 'System.DateTimeOffset' or ignore this type by calling // Ignore<System.Security.Cryptography.X509Certificates.X509Certificate2>() // on 'System.Web.OData.Builder.ODataModelBuilder'. Assert.True(ae.Message.Contains("The type 'System.DateTime' of property") || ae.Message.Contains("System.Windows.Forms.AxHost") || ae.Message.Contains("Found more than one dynamic property container in type"), "The exception should contains \"The type 'System.DateTime' of property\", or " + "\"System.Windows.Forms.AxHost\" or" + "\"Found more than one dynamic property container in type\", but actually, it is:" + ae.Message); } }
public async Task PostGetUpdateAndDelete(Type entityType, string entitySetNam) { var testMethod = this.GetType().GetMethods() .Where(method => method.IsGenericMethod) .Where(method => method.Name == "PostGetUpdateAndDelete") .FirstOrDefault(); var concreteTestMethod = testMethod.MakeGenericMethod(entityType); var rand = new Random(RandomSeedGenerator.GetRandomSeed()); await(Task) concreteTestMethod.Invoke(this, new object[] { entitySetNam, rand }); }
public virtual void PostGetUpdateAndDelete(Type entityType, string entitySetName) { // clear respository this.ClearRepository(entitySetName); Random r = new Random(RandomSeedGenerator.GetRandomSeed()); // post new entity to repository var value = InstanceCreator.CreateInstanceOf(entityType, r, new CreatorSettings() { NullValueProbability = 0.0 }); DataServiceContext ctx = WriterClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.AddObject(entitySetName, value); ctx.SaveChanges(); // get collection of entities from repository ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); var entities = ctx.CreateQuery <Vehicle>(entitySetName); var beforeUpdate = entities.ToList().First(); AssertExtension.PrimitiveEqual(value, beforeUpdate); // update entity and verify if it's saved ctx = WriterClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, beforeUpdate); beforeUpdate.Name = InstanceCreator.CreateInstanceOf <string>(r); ctx.UpdateObject(beforeUpdate); ctx.SaveChanges(); // retrieve the updated entity ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); entities = ctx.CreateQuery <Vehicle>(entitySetName); var afterUpdate = entities.Where(e => e.Id == beforeUpdate.Id).First(); Assert.Equal(beforeUpdate.Name, afterUpdate.Name); // delete entity ctx = WriterClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, afterUpdate); ctx.DeleteObject(afterUpdate); ctx.SaveChanges(); // ensure that the entity has been deleted ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); entities = ctx.CreateQuery <Vehicle>(entitySetName); Assert.Equal(0, entities.ToList().Count()); // clear repository this.ClearRepository(entitySetName); }
public void TestRandomEntityTypes(Type entityType, string entitySetName) { this.BaseAddress = this.BaseAddress.Replace("localhost", Environment.MachineName); //var entitySetName = entityType.Name; // clear respository this.ClearRepository(entitySetName); Random r = new Random(RandomSeedGenerator.GetRandomSeed()); // post new entity to repository var value = Creator.GenerateClientRandomData(entityType, r); var ctx = WriterClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.AddObject(entitySetName, value); ctx.SaveChanges(); // get collection of entities from repository ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); IEnumerable <object> entities = ctx.CreateQuery(entityType, entitySetName); var beforeUpdate = entities.ToList().First(); AssertExtension.PrimitiveEqual(value, beforeUpdate); // update entity and verify if it's saved ctx = WriterClient(new Uri(BaseAddress), DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, beforeUpdate); var updatedProperty = UpdateNonIDProperty(beforeUpdate, r); ctx.UpdateObject(beforeUpdate); ctx.SaveChanges(); ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); entities = ctx.CreateQuery(entityType, entitySetName); var afterUpdate = entities.ToList().First(); AssertExtension.PrimitiveEqual(beforeUpdate, afterUpdate); //var afterUpdate = entities.Where(FilterByPk(entityType, GetIDValue(beforeUpdate))).First(); // delete entity ctx = WriterClient(new Uri(BaseAddress), DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, afterUpdate); ctx.DeleteObject(afterUpdate); ctx.SaveChanges(); ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); entities = ctx.CreateQuery(entityType, entitySetName); Assert.Equal(0, entities.ToList().Count()); // clear repository this.ClearRepository(entitySetName); }
public static IEnumerable <Product> CreateRandomProducts() { int seed = RandomSeedGenerator.GetRandomSeed(); var r = new Random(seed); var products = new List <Product>(); for (int i = 0; i < r.Next(5000); i++) { products.Add(new Product { ID = r.Next(1000), Name = InstanceCreator.CreateInstanceOf <string>(r), Price = InstanceCreator.CreateInstanceOf <Decimal>(r), Amount = InstanceCreator.CreateInstanceOf <double>(r), Rating = r.Next(5), ReleaseDate = InstanceCreator.CreateInstanceOf <DateTime?>(r), Taxable = InstanceCreator.CreateInstanceOf <bool?>(r) }); if (r.NextDouble() > .7) { products.Last().Supplier = new Supplier { ID = r.Next(1000), Name = InstanceCreator.CreateInstanceOf <string>(r), Address = new Address { City = InstanceCreator.CreateInstanceOf <string>(r), State = InstanceCreator.CreateInstanceOf <string>(r) } }; products.Last().Supplier.Products.Add(products.Last()); } else if (r.NextDouble() > .3) { products.Last().Supplier = new Supplier { ID = r.Next(1000), Name = InstanceCreator.CreateInstanceOf <string>(r), Address = null }; products.Last().Supplier.Products.Add(products.Last()); } } return(products); }
public async Task ODataCRUDShouldWork() { var rand = new Random(RandomSeedGenerator.GetRandomSeed()); var entitySetName = "MixScenarioTests_OData"; var uri = new Uri(this.BaseAddress + "/odata"); this.ClearRepository(entitySetName); // post new entity to repository var baseline = InstanceCreator.CreateInstanceOf <Vehicle>(rand); await PostNewEntityAsync(uri, baseline, entitySetName); // get collection of entities from repository var entities = await GetEntitiesAsync(uri, entitySetName); var firstVersion = entities.ToList().FirstOrDefault(); Assert.NotNull(firstVersion); AssertExtension.PrimitiveEqual(baseline, firstVersion); // update entity and verify if it's saved await UpdateEntityAsync( uri, firstVersion, data => { data.Model = InstanceCreator.CreateInstanceOf <string>(rand); data.Name = InstanceCreator.CreateInstanceOf <string>(rand); data.WheelCount = InstanceCreator.CreateInstanceOf <int>(rand); return(data); }, entitySetName); var entitiesAgain = await GetEntitiesAsync(uri, entitySetName); var secondVersion = entitiesAgain.ToList().FirstOrDefault(); Assert.NotNull(secondVersion); // firstVersion is updated in UpdatedEntityAsync AssertExtension.PrimitiveEqual(firstVersion, secondVersion); // delete entity await DeleteEntityAsync(uri, secondVersion, entitySetName); var entitiesFinal = await GetEntitiesAsync(uri, entitySetName); Assert.Equal(0, entitiesFinal.ToList().Count()); }
public void Test() { Random rndGen = new Random(RandomSeedGenerator.GetRandomSeed()); CreateTypes(20, rndGen); foreach (var type in this.entityTypes) { Console.WriteLine(type.FullName); foreach (var p in type.GetProperties()) { Console.WriteLine(" {0} {1}", p.PropertyType, p.Name); } Activator.CreateInstance(type); } }
public void ODataCRUDShouldWork() { Random r = new Random(RandomSeedGenerator.GetRandomSeed()); var entitySetName = "MixScenarioTests_OData"; var uri = new Uri(this.BaseAddress + "/odata"); this.ClearRepository(entitySetName); // post new entity to repository var value = InstanceCreator.CreateInstanceOf <Vehicle>(r); var ctx = WriterClient(uri, DataServiceProtocolVersion.V3); ctx.AddObject(entitySetName, value); ctx.SaveChanges(); // get collection of entities from repository ctx = ReaderClient(uri, DataServiceProtocolVersion.V3); IEnumerable <Vehicle> entities = ctx.CreateQuery <Vehicle>(entitySetName); var beforeUpdate = entities.ToList().First(); AssertExtension.PrimitiveEqual(value, beforeUpdate); // update entity and verify if it's saved ctx = WriterClient(uri, DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, beforeUpdate); beforeUpdate.Model = InstanceCreator.CreateInstanceOf <string>(r); beforeUpdate.Name = InstanceCreator.CreateInstanceOf <string>(r); beforeUpdate.WheelCount = InstanceCreator.CreateInstanceOf <int>(r); ctx.UpdateObject(beforeUpdate); ctx.SaveChanges(); ctx = ReaderClient(uri, DataServiceProtocolVersion.V3); entities = ctx.CreateQuery <Vehicle>(entitySetName); var afterUpdate = entities.ToList().First(); AssertExtension.PrimitiveEqual(beforeUpdate, afterUpdate); //var afterUpdate = entities.Where(FilterByPk(entityType, GetIDValue(beforeUpdate))).First(); // delete entity ctx = WriterClient(uri, DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, afterUpdate); ctx.DeleteObject(afterUpdate); ctx.SaveChanges(); ctx = ReaderClient(uri, DataServiceProtocolVersion.V3); entities = ctx.CreateQuery <Vehicle>(entitySetName); Assert.Equal(0, entities.ToList().Count()); }
public void CRUDEntitySetShouldWork() { Random r = new Random(RandomSeedGenerator.GetRandomSeed()); var entitySetName = "UnicodeRouteTests_Todoü"; var uri = new Uri(this.BaseAddress + "/odataü"); // post new entity to repository var value = InstanceCreator.CreateInstanceOf <UnicodeRouteTests_Todoü>(r); var ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); ctx.AddObject(entitySetName, value); ctx.SaveChanges(); // get collection of entities from repository ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); IEnumerable <UnicodeRouteTests_Todoü> entities = ctx.CreateQuery <UnicodeRouteTests_Todoü>(entitySetName); var beforeUpdate = entities.ToList().First(); AssertExtension.PrimitiveEqual(value, beforeUpdate); // update entity and verify if it's saved ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, beforeUpdate); beforeUpdate.Nameü = InstanceCreator.CreateInstanceOf <string>(r); ctx.UpdateObject(beforeUpdate); ctx.SaveChanges(); ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); entities = ctx.CreateQuery <UnicodeRouteTests_Todoü>(entitySetName); var afterUpdate = entities.ToList().First(); AssertExtension.PrimitiveEqual(beforeUpdate, afterUpdate); //var afterUpdate = entities.Where(FilterByPk(entityType, GetIDValue(beforeUpdate))).First(); var response = ctx.LoadProperty(afterUpdate, "Nameü"); Assert.Equal(200, response.StatusCode); // delete entity ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, afterUpdate); ctx.DeleteObject(afterUpdate); ctx.SaveChanges(); ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); entities = ctx.CreateQuery <UnicodeRouteTests_Todoü>(entitySetName); Assert.Equal(0, entities.ToList().Count()); }
public void SupportPostCollectionPropertyByEntityPayload() { CreatorSettings settings = new CreatorSettings() { NullValueProbability = 0.0 }; // clear respository this.ClearRepository("CollectionProperty_Entity"); //this.Client.GetStringAsync(this.BaseAddress + "/$metadata").Wait(); Random r = new Random(RandomSeedGenerator.GetRandomSeed()); // post new entity to repository var expected = InstanceCreator.CreateInstanceOf <CollectionProperty_Entity>(r, settings); DataServiceContext ctx = WriterClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.AddObject("CollectionProperty_Entity", expected); ctx.SaveChanges(); int id = expected.ID; ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); var actual = ctx.CreateQuery <CollectionProperty_Entity>("CollectionProperty_Entity").Where(t => t.ID == id).First(); AssertExtension.DeepEqual(expected, actual); expected = actual; ctx = WriterClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.AttachTo("CollectionProperty_Entity", expected); expected.StringList = InstanceCreator.CreateInstanceOf <List <string> >(r, settings); expected.ComplexTypeCollection = InstanceCreator.CreateInstanceOf <Collection <CollectionProperty_ComplexType> >(r, settings); ctx.UpdateObject(expected); ctx.SaveChanges(); ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); actual = ctx.CreateQuery <CollectionProperty_Entity>("CollectionProperty_Entity").Where(t => t.ID == id).First(); AssertExtension.DeepEqual(expected, actual); // clear respository this.ClearRepository("CollectionProperty_Entity"); }
public async Task CRUDEntitySetShouldWork() { var rand = new Random(RandomSeedGenerator.GetRandomSeed()); var entitySetName = "UnicodeRouteTests_Todoü"; var uri = new Uri(this.BaseAddress + "/odataü"); var context = new DataServiceContext(uri, ODataProtocolVersion.V4); // post new entity to repository CreatorSettings creatorSettings = new CreatorSettings() { NullValueProbability = 0, }; var baseline = InstanceCreator.CreateInstanceOf <UnicodeRouteTests_Todoü>(rand, creatorSettings); await PostNewEntityAsync(uri, entitySetName, baseline); // get collection of entities from repository var firstVersion = await GetFirstEntityAsync(uri, entitySetName); Assert.NotNull(firstVersion); AssertExtension.PrimitiveEqual(baseline, firstVersion); // update entity and verify if it's saved await UpdateEntityAsync(uri, entitySetName, firstVersion, data => { data.Nameü = InstanceCreator.CreateInstanceOf <string>(rand); }); var secondVersion = await GetFirstEntityAsync(uri, entitySetName); Assert.NotNull(secondVersion); AssertExtension.PrimitiveEqual(firstVersion, secondVersion); var response = await LoadPropertyAsync(uri, entitySetName, secondVersion, "Nameü"); Assert.Equal(200, response.StatusCode); // delete entity await DeleteEntityAsync(uri, entitySetName, secondVersion); var entities = await GetEntitiesAsync(uri, entitySetName); Assert.Empty(entities.ToList()); }
public virtual void AddAndRemoveBaseNavigationPropertyInDerivedType() { // clear respository this.ClearRepository("InheritanceTests_Cars"); Random r = new Random(RandomSeedGenerator.GetRandomSeed()); // post new entity to repository CreatorSettings creatorSettings = new CreatorSettings() { NullValueProbability = 0, }; var car = InstanceCreator.CreateInstanceOf <Car>(r, creatorSettings); var vehicle = InstanceCreator.CreateInstanceOf <Vehicle>(r, creatorSettings); DataServiceContext ctx = WriterClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4); ctx.AddObject("InheritanceTests_Cars", car); ctx.AddRelatedObject(car, "BaseTypeNavigationProperty", vehicle); ctx.SaveChangesAsync().Wait(); ctx = ReaderClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4); var cars = ctx.CreateQuery <Car>("InheritanceTests_Cars"); var actual = cars.ExecuteAsync().Result.First(); ctx.LoadPropertyAsync(actual, "BaseTypeNavigationProperty").Wait(); AssertExtension.PrimitiveEqual(vehicle, actual.BaseTypeNavigationProperty[0]); ctx = WriterClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4); ctx.AttachTo("InheritanceTests_Cars", actual); ctx.AttachTo("InheritanceTests_Vehicles", actual.BaseTypeNavigationProperty[0]); ctx.DeleteLink(actual, "BaseTypeNavigationProperty", actual.BaseTypeNavigationProperty[0]); ctx.SaveChangesAsync().Wait(); ctx = ReaderClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4); cars = ctx.CreateQuery <Car>("InheritanceTests_Cars"); actual = cars.ExecuteAsync().Result.First(); ctx.LoadPropertyAsync(actual, "BaseTypeNavigationProperty").Wait(); Assert.Empty(actual.BaseTypeNavigationProperty); this.ClearRepository("InheritanceTests_Cars"); }
public void RunQueryableOnAllPossibleTypes(Type type, string queryString) { int seed = RandomSeedGenerator.GetRandomSeed(); Random r = new Random(seed); Type generic = typeof(IEnumerable <>); var collectionType = generic.MakeGenericType(type); Type listType = typeof(List <>).MakeGenericType(type); var array = Activator.CreateInstance(listType); EnableQueryAttribute q = new EnableQueryAttribute(); var configuration = new HttpConfiguration(); configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; configuration.Routes.MapHttpRoute("ApiDefault", "api/{controller}/{id}", new { id = RouteParameter.Optional }); var request = new HttpRequestMessage(HttpMethod.Get, "http://test/api/Objects?" + queryString); request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, configuration); var controllerContext = new HttpControllerContext( configuration, configuration.Routes.GetRouteData(request), request); var actionContext = new HttpActionContext(controllerContext, new ReflectedHttpActionDescriptor() { Configuration = configuration }); var context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(System.Net.HttpStatusCode.OK); context.Response.Content = new ObjectContent(collectionType, array, new JsonMediaTypeFormatter()); q.OnActionExecuted(context); Console.WriteLine(context.Response.Content.ReadAsStringAsync().Result); Assert.Equal(HttpStatusCode.OK, context.Response.StatusCode); }
public void TestApplyPatchOnIndividualProperty() { // clear respository this.ClearRepository("DeltaTests_Todoes"); this.Client.GetStringAsync(this.BaseAddress + "/$metadata").Wait(); Random r = new Random(RandomSeedGenerator.GetRandomSeed()); var s = new CreatorSettings() { NullValueProbability = 0.0, MaxArrayLength = 100 }; // post new entity to repository var todo = InstanceCreator.CreateInstanceOf <DeltaTests_TodoClient>(r, s); todo.NullableBool = true; todo.NullableInt = 100000; todo.Enum = "One"; todo.Estimation = new DeltaTests_Estimation() { CompletedBy = new DateTime(2012, 10, 18), EstimatedTime = TimeSpan.FromDays(1) }; todo.XElement = @"<a><b/></a>"; DataServiceContext ctx = WriterClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.ResolveName = ResolveName; ctx.ResolveType = ResolveType; ctx.AddObject("DeltaTests_Todoes", todo); ctx.SaveChanges(); int id = todo.ID; todo.ID = InstanceCreator.CreateInstanceOf <int>(r, s); todo.Name = InstanceCreator.CreateInstanceOf <string>(r, s); todo.Enum = "Two"; todo.NullableBool = null; todo.Items = InstanceCreator.CreateInstanceOf <DeltaTests_TodoItems>(r, s); todo.Tags = InstanceCreator.CreateInstanceOf <List <DeltaTests_TodoTag> >(r, s); todo.Estimation.CompletedBy = new DateTime(2012, 11, 18); todo.NullableInt = 999999; todo.Bool = InstanceCreator.CreateInstanceOf <bool>(r, s); todo.Byte = InstanceCreator.CreateInstanceOf <Byte>(r, s); todo.ByteArray = InstanceCreator.CreateInstanceOf <byte[]>(r, s); todo.DateTime = InstanceCreator.CreateInstanceOf <DateTime>(r, s); todo.DateTimeOffset = InstanceCreator.CreateInstanceOf <DateTimeOffset>(r, s); todo.Decimal = InstanceCreator.CreateInstanceOf <Decimal>(r, s); todo.Double = InstanceCreator.CreateInstanceOf <Double>(r, s); todo.Float = InstanceCreator.CreateInstanceOf <float>(r, s); todo.Guid = InstanceCreator.CreateInstanceOf <Guid>(r, s); todo.Integer = InstanceCreator.CreateInstanceOf <Int32>(r, s); todo.Long = InstanceCreator.CreateInstanceOf <long>(r, s); todo.Short = InstanceCreator.CreateInstanceOf <short>(r, s); todo.String = InstanceCreator.CreateInstanceOf <string>(r, s); todo.TimeSpan = InstanceCreator.CreateInstanceOf <TimeSpan>(r, s); todo.XElement = @"<b><a/></b>"; ctx.UpdateObject(todo); ctx.SaveChanges(); ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.ResolveName = ResolveName; ctx.ResolveType = ResolveType; var actual = ctx.CreateQuery <DeltaTests_TodoClient>("DeltaTests_Todoes").Where(t => t.ID == id).First(); //Assert.Equal(id, actual.ID); Assert.Equal(todo.Name, actual.Name); Assert.Equal(todo.Estimation.CompletedBy, actual.Estimation.CompletedBy); Assert.Equal(todo.Estimation.EstimatedTime, actual.Estimation.EstimatedTime); Assert.Equal(todo.NullableBool, actual.NullableBool); Assert.Equal(todo.NullableInt, actual.NullableInt); Assert.Equal(todo.Bool, actual.Bool); Assert.Equal(todo.Byte, actual.Byte); Assert.Equal(todo.ByteArray, actual.ByteArray); Assert.Equal(todo.DateTime, actual.DateTime); Assert.Equal(todo.DateTimeOffset, actual.DateTimeOffset); Assert.Equal(todo.Decimal, actual.Decimal); Assert.Equal(todo.Double, actual.Double); Assert.Equal(todo.Float, actual.Float); Assert.Equal(todo.Guid, actual.Guid); Assert.Equal(todo.Integer, actual.Integer); Assert.Equal(todo.Long, actual.Long); Assert.Equal(todo.Short, actual.Short); Assert.Equal(todo.String, actual.String); Assert.Equal(todo.TimeSpan, actual.TimeSpan); Assert.Equal(todo.XElement, actual.XElement.Replace(" ", string.Empty).Replace(Environment.NewLine, string.Empty)); // clear respository this.ClearRepository("DeltaTests_Todoes"); }
public static IEnumerable <Product> CreateRandomProducts() { int seed = RandomSeedGenerator.GetRandomSeed(); var r = new Random(seed); var products = new List <Product>(); try { for (int i = 0; i < r.Next(5000); i++) { products.Add(new Product { ID = r.Next(1000), Name = InstanceCreator.CreateInstanceOf <string>(r), Price = InstanceCreator.CreateInstanceOf <Decimal>(r), Rating = r.Next(5), PublishDate = InstanceCreator.CreateInstanceOf <DateTimeOffset>(r), ReleaseDate = InstanceCreator.CreateInstanceOf <DateTimeOffset?>(r), Date = InstanceCreator.CreateInstanceOf <DateTime>(r), NullableDate = r.Next(9) % 3 == 0 ? (Date?)null : InstanceCreator.CreateInstanceOf <DateTime>(r), TimeOfDay = InstanceCreator.CreateInstanceOf <DateTimeOffset>(r).TimeOfDay, NullableTimeOfDay = r.Next(19) % 3 == 0 ? (TimeOfDay?)null : InstanceCreator.CreateInstanceOf <DateTimeOffset>(r).TimeOfDay, Taxable = InstanceCreator.CreateInstanceOf <bool?>(r) }); if (r.NextDouble() > .7) { products.Last().Supplier = new Supplier { ID = r.Next(1000), Name = InstanceCreator.CreateInstanceOf <string>(r), Address = new Address { City = InstanceCreator.CreateInstanceOf <string>(r), State = InstanceCreator.CreateInstanceOf <string>(r) } }; products.Last().Supplier.Products.Add(products.Last()); } else if (r.NextDouble() > .3) { products.Last().Supplier = new Supplier { ID = r.Next(1000), Name = InstanceCreator.CreateInstanceOf <string>(r), Address = null }; products.Last().Supplier.Products.Add(products.Last()); } } } catch (Exception e) { Console.WriteLine(e.Message); } return(products); }