Пример #1
0
        public virtual async Task CreateAndDeleteLinkToDerivedNavigationPropertyOnBaseEntitySet()
        {
            // clear respository
            await this.ClearRepositoryAsync("InheritanceTests_Vehicles");

            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 <MiniSportBike>(r, creatorSettings);
            DataServiceContext ctx = WriterClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4);

            ctx.AddObject("InheritanceTests_Vehicles", car);
            ctx.AddObject("InheritanceTests_Vehicles", vehicle);
            await ctx.SaveChangesAsync();

            ctx.SetLink(car, "SingleNavigationProperty", vehicle);
            await ctx.SaveChangesAsync();

            ctx = ReaderClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4);
            var cars   = (await ctx.CreateQuery <Vehicle>("InheritanceTests_Vehicles").ExecuteAsync()).ToList().OfType <Car>();
            var actual = cars.First();
            await ctx.LoadPropertyAsync(actual, "SingleNavigationProperty");

            AssertExtension.PrimitiveEqual(vehicle, actual.SingleNavigationProperty);

            await this.ClearRepositoryAsync("InheritanceTests_Vehicles");
        }
Пример #2
0
        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);
        }
Пример #3
0
        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);
        }
Пример #4
0
 public ODataModelTypeCreator()
 {
     _valueToken          = new Token(new CharactorSet().Append('a', 'z').Append('A', 'Z').Append('0', '9').ToString());
     _zeroNullProbability = new CreatorSettings()
     {
         NullValueProbability = 0.0
     };
 }
Пример #5
0
 public CreatorSettings(CreatorSettings other)
 {
     this.MaxArrayLength       = other.MaxArrayLength;
     this.MinStringLength      = other.MinStringLength;
     this.MaxStringLength      = other.MaxArrayLength;
     this.MaxListLength        = other.MaxListLength;
     this.CreateOnlyAsciiChars = other.CreateOnlyAsciiChars;
     this.CreateDateTimeWithSubMilliseconds = other.CreateDateTimeWithSubMilliseconds;
     this.DontCreateSurrogateChars          = other.DontCreateSurrogateChars;
     this.NullValueProbability = other.NullValueProbability;
     this.AllowEmptyCollection = other.AllowEmptyCollection;
     this.MaxGraphDepth        = other.MaxGraphDepth;
 }
Пример #6
0
 public CreatorSettings(CreatorSettings other)
 {
     this.MaxArrayLength = other.MaxArrayLength;
     this.MinStringLength = other.MinStringLength;
     this.MaxStringLength = other.MaxArrayLength;
     this.MaxListLength = other.MaxListLength;
     this.CreateOnlyAsciiChars = other.CreateOnlyAsciiChars;
     this.CreateDateTimeWithSubMilliseconds = other.CreateDateTimeWithSubMilliseconds;
     this.DontCreateSurrogateChars = other.DontCreateSurrogateChars;
     this.NullValueProbability = other.NullValueProbability;
     this.AllowEmptyCollection = other.AllowEmptyCollection;
     this.MaxGraphDepth = other.MaxGraphDepth;
 }
Пример #7
0
        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());
        }
Пример #8
0
        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");
        }
Пример #9
0
        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");
        }
Пример #10
0
        private bool GenerateRandomDataForClient(
            Type clientType,
            Type serverType,
            Random rand,
            out object value,
            int?depth = null,
            CreatorSettings settings = null)
        {
            if (clientType == serverType)
            {
                value = InstanceCreator.CreateInstanceOf(clientType, rand, settings);

                return(true);
            }
            else if (this.EntityClientTypes.Contains(clientType) ||
                     this.ComplexClientTypes.Contains(clientType))
            {
                value = GenerateClientStructureRandomData(
                    clientType,
                    serverType,
                    rand,
                    depth);

                return(true);
            }
            else if (serverType.IsEnum)
            {
                value = InstanceCreator.CreateInstanceOf(serverType, rand).ToString();

                return(true);
            }
            else if (clientType.IsGenericType)
            {
                if (clientType.GetGenericTypeDefinition() == typeof(ObservableCollection <>))
                {
                    var values = Activator.CreateInstance(clientType);

                    if (depth <= 2)
                    {
                        var  addMethod         = clientType.GetMethod("Add");
                        Type elementType       = clientType.GetGenericArguments()[0];
                        Type serverElementType = serverType.GetGenericArguments()[0];
                        int  length            = rand.Next(10);
                        for (int j = 0; j < length; j++)
                        {
                            object elementValue;
                            if (GenerateRandomDataForClient(elementType, serverElementType, rand, out elementValue, depth, new CreatorSettings()
                            {
                                NullValueProbability = 0.0
                            }))
                            {
                                addMethod.Invoke(values, new object[] { elementValue });
                            }
                        }
                    }

                    value = values;
                    return(true);
                }
            }
            else if (this.primitiveTypes.Contains(clientType))
            {
                if (serverType == typeof(ushort) ||
                    serverType == typeof(uint) ||
                    serverType == typeof(ulong))
                {
                    value = InstanceCreator.CreateInstanceOf <ushort>(rand);
                    return(true);
                }
                else if (serverType == typeof(char))
                {
                    value = InstanceCreator.CreateInstanceOf <char>(rand).ToString();
                    return(true);
                }
            }

            value = null;
            return(false);
        }
Пример #11
0
        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");
        }