Exemplo n.º 1
0
        public void IntegrationTestFirstAndLast()
        {
            var first = this.CustomersODataQuery().OrderBy(c => c.Name).First();

            first.Name.ShouldEqual("A");
            UnitTestHelpers.AssertThrows <InvalidOperationException>(() => this.CustomersODataQuery().Take(0).First());

            var firstOrDefault = this.CustomersODataQuery().OrderBy(c => c.Name).FirstOrDefault();

            firstOrDefault.Name.ShouldEqual("A");
            var firstOrDefault2 = this.CustomersODataQuery().Take(0).FirstOrDefault();

            firstOrDefault2.ShouldEqual(null);

            var firstPred = this.CustomersODataQuery().Where(c => c.Name.Contains("ert")).First(c => c.Name.Contains("A"));

            firstPred.Name.ShouldEqual("Albert");
            UnitTestHelpers.AssertThrows <InvalidOperationException>(() => this.CustomersODataQuery().First(c => c.Name == "no customer has this name"));

            var firstOrDefaultPred = this.CustomersODataQuery().Where(c => c.Name.Contains("ert")).FirstOrDefault(c => c.Name.Contains("A"));

            firstOrDefaultPred.Name.ShouldEqual("Albert");
            var firstOrDefaultPred2 = this.CustomersODataQuery().FirstOrDefault(c => c.Name == "no customer has this name");

            firstOrDefaultPred2.ShouldEqual(null);

            UnitTestHelpers.AssertThrows <ODataCompileException>(() => this.CustomersODataQuery().Last());
            UnitTestHelpers.AssertThrows <ODataCompileException>(() => this.CustomersODataQuery().Last(c => c.Name == "Dominic"));
            UnitTestHelpers.AssertThrows <ODataCompileException>(() => this.CustomersODataQuery().LastOrDefault());
            UnitTestHelpers.AssertThrows <ODataCompileException>(() => this.CustomersODataQuery().LastOrDefault(c => c.Name == "Dominic"));
        }
        public void TestConsumeTwice()
        {
            var enumerable = new MergedLinesEnumerable(new StringReader("a"), new StringReader("b"));

            enumerable.GetEnumerator();
            UnitTestHelpers.AssertThrows <InvalidOperationException>(() => enumerable.GetEnumerator());
        }
Exemplo n.º 3
0
 public void IntegrationTestSumAndAverage()
 {
     UnitTestHelpers.AssertThrows <ODataCompileException>(() => this.CustomersODataQuery().Select(c => c.DateCreated.Year).Sum());
     UnitTestHelpers.AssertThrows <ODataCompileException>(() => this.CustomersODataQuery().Select(c => c.DateCreated.Year).Average());
     UnitTestHelpers.AssertThrows <ODataCompileException>(() => this.CustomersODataQuery().Sum(c => c.DateCreated.Year));
     UnitTestHelpers.AssertThrows <ODataCompileException>(() => this.CustomersODataQuery().Average(c => c.DateCreated.Year));
 }
Exemplo n.º 4
0
        public void TestCloseReadSide()
        {
            var pipe = new Pipe();

            pipe.WriteText("abc");
            pipe.ReadTextAsync(2).Result.ShouldEqual("ab");
            pipe.OutputStream.Dispose();
            UnitTestHelpers.AssertThrows <ObjectDisposedException>(() => pipe.OutputStream.ReadByte());

            var largeBytes    = new byte[10 * 1024];
            var initialMemory = GC.GetTotalMemory(forceFullCollection: true);

            for (var i = 0; i < int.MaxValue / 1024; ++i)
            {
                pipe.InputStream.Write(largeBytes, 0, largeBytes.Length);
            }
            var finalMemory = GC.GetTotalMemory(forceFullCollection: true);

            (finalMemory - initialMemory < 10 * largeBytes.Length)
            .ShouldEqual(true, "final = " + finalMemory + " initial = " + initialMemory);

            UnitTestHelpers.AssertThrows <ObjectDisposedException>(() => pipe.OutputStream.ReadByte());

            pipe.InputStream.Dispose();
        }
Exemplo n.º 5
0
        public void IntegrationTestMinAndMax()
        {
            Func <IQueryable <Customer>, IQueryable <Customer> > companyFilter = this.NullCoalescingSupported
                    ? new Func <IQueryable <Customer>, IQueryable <Customer> >(q => q)
                    : q => q.Where(c => c.Company != null);

            var minDate = CustomersContext.GetCustomers().Min(c => c.DateCreated);

            this.CustomersODataQuery().Select(c => c.DateCreated).Min().ShouldEqual(minDate);
            this.CustomersODataQuery().Min(c => c.DateCreated).ShouldEqual(minDate);
            // this check is because we're doing min(valueType) of an empty list
            UnitTestHelpers.AssertThrows <InvalidOperationException>(() => this.CustomersODataQuery().Where(c => c.Name.Length == int.MaxValue).Min(c => c.DateCreated));
            if (this.AssociationsSupported)
            {
                Assert.IsNotNull(companyFilter(this.CustomersODataQuery()).Min(c => c.Company.DateClosed));
            }

            var maxDate = CustomersContext.GetCustomers().Max(c => c.DateCreated);

            this.CustomersODataQuery().Select(c => c.DateCreated).Max().ShouldEqual(maxDate);
            this.CustomersODataQuery().Max(c => c.DateCreated).ShouldEqual(maxDate);
            // this check is because we're doing max(valueType) of an empty list
            UnitTestHelpers.AssertThrows <InvalidOperationException>(() => this.CustomersODataQuery().Where(c => c.Name.Length == int.MaxValue).Max(c => c.DateCreated));
            if (this.AssociationsSupported)
            {
                Assert.IsNotNull(companyFilter(this.CustomersODataQuery()).Max(c => c.Company.DateClosed));
            }
        }
Exemplo n.º 6
0
        public void TestPassedPipelineIsUsed()
        {
            var mock      = new Mock <IODataClientQueryPipeline>(MockBehavior.Strict);
            var context   = new ODataQueryContext(mock.Object);
            var agg       = UnitTestHelpers.AssertThrows <AggregateException>(() => context.Query(new Uri("http://localhost:80")).ToArray());
            var innerMost = Traverse.Along(agg.As <Exception>(), e => e.InnerException).Last();

            Assert.That(innerMost, Is.InstanceOf <MockException>());
        }
Exemplo n.º 7
0
        public void TestConcurrentReads()
        {
            var pipe = new Pipe();

            var asyncRead = pipe.ReadTextAsync(1);

            UnitTestHelpers.AssertThrows <InvalidOperationException>(() => pipe.OutputStream.ReadByte());
            pipe.InputStream.Dispose();
            asyncRead.Wait(TimeSpan.FromSeconds(5)).ShouldEqual(true);
        }
Exemplo n.º 8
0
        public void TestGet()
        {
            var row = new ODataEntity(new Dictionary <string, object> {
                { "a", 1 }, { "b", "2" }
            });

            row.Get <int>("A").ShouldEqual(1);
            row.Get <string>("b").ShouldEqual("2");
            UnitTestHelpers.AssertThrows <InvalidCastException>(() => row.Get <string>("a"));
            UnitTestHelpers.AssertThrows <ArgumentException>(() => row.Get <string>("c"));
        }
Exemplo n.º 9
0
        public void TimeoutTest()
        {
            var pipe = new Pipe {
                OutputStream = { ReadTimeout = 0 }
            };

            UnitTestHelpers.AssertThrows <TimeoutException>(() => pipe.OutputStream.ReadByte());

            pipe.WriteText(new string('a', 2048));
            pipe.ReadTextAsync(2048).Result.ShouldEqual(new string('a', 2048));
        }
Exemplo n.º 10
0
        public void IntegrationTestContains()
        {
            this.CustomersODataQuery().Select(c => c.Name)
            .Contains("no customer has this name")
            .ShouldEqual(false);

            this.CustomersODataQuery().Select(c => c.Name)
            .Contains("Dominic")
            .ShouldEqual(true);

            UnitTestHelpers.AssertThrows <ODataCompileException>(() => this.CustomersODataQuery().Contains(new Customer()));
        }
Exemplo n.º 11
0
        public void SqlErrors()
        {
            var samples = this.Context.Query <Sample>("samples");

            UnitTestHelpers.AssertThrows <ODataCompileException>(() => samples.Join(samples, s => s.Id, s => s.Id, (s1, s2) => s1.Id + s2.Id).ToArray());
            UnitTestHelpers.AssertThrows <DbException>(() => this.Context.Query <Sample>("fake_samples").ToArray());
            UnitTestHelpers.AssertThrows <NotSupportedException>(
                () => this.Context.Query <Customer>("customers").Where(c => c.Company.Name.Contains("e")).ToArray()
                );
            // see note in ODataToSqlTranslator.VisitMemberAccess about why we can't throw NotSupported here
            UnitTestHelpers.AssertThrows <DbException>(
                () => this.Context.Query <ODataEntity>("customers").Where(c => c.Get <ODataEntity>("company") != null).ToArray()
                );
        }
Exemplo n.º 12
0
        public void TestConcurrentWrites()
        {
            var pipe = new Pipe();

            pipe.SetFixedLength();

            var longText   = new string('x', (2 * Constants.ByteBufferSize) + 1);
            var asyncWrite = pipe.WriteTextAsync(longText);

            asyncWrite.Wait(TimeSpan.FromSeconds(.01)).ShouldEqual(false);
            UnitTestHelpers.AssertThrows <InvalidOperationException>(() => pipe.InputStream.WriteByte(101));
            pipe.OutputStream.Dispose();
            asyncWrite.Wait(TimeSpan.FromSeconds(5)).ShouldEqual(true);
        }
Exemplo n.º 13
0
        public void IntegrationTestSingle()
        {
            UnitTestHelpers.AssertThrows <InvalidOperationException>(() => this.CustomersODataQuery().Take(0).Single());
            this.CustomersODataQuery().Take(0).SingleOrDefault().ShouldEqual(null);

            UnitTestHelpers.AssertThrows <InvalidOperationException>(() => this.CustomersODataQuery().Single(c => c.Name == "no customer has this name"));
            this.CustomersODataQuery().SingleOrDefault(c => c.Name == "no customer has this name").ShouldEqual(null);

            UnitTestHelpers.AssertThrows <InvalidOperationException>(() => this.CustomersODataQuery().Where(c => c.Name.Contains("A")).Single());
            UnitTestHelpers.AssertThrows <InvalidOperationException>(() => this.CustomersODataQuery().Where(c => c.Name.Contains("A")).SingleOrDefault());

            this.CustomersODataQuery().Where(c => c.Name == "Dominic").Single().Name.ShouldEqual("Dominic");
            this.CustomersODataQuery().Where(c => c.Name == "Dominic").SingleOrDefault().Name.ShouldEqual("Dominic");
            this.CustomersODataQuery().Single(c => c.Name == "Dominic").Name.ShouldEqual("Dominic");
            this.CustomersODataQuery().SingleOrDefault(c => c.Name == "Dominic").Name.ShouldEqual("Dominic");
        }
Exemplo n.º 14
0
        public void TestFromObject()
        {
            var v1 = ODataValue.FromObject(1);

            v1.Value.ShouldEqual(1);

            var v2 = ODataValue.FromObject(null);

            v2.ShouldEqual(null);

            var v3 = ODataValue.FromObject(v1);

            ReferenceEquals(v1, v3).ShouldEqual(true);

            UnitTestHelpers.AssertThrows <ArgumentException>(() => ODataValue.FromObject(new { a = 2 }));
            UnitTestHelpers.AssertThrows <ArgumentException>(() => ODataValue.FromObject(new ODataEntity(Enumerable.Empty <KeyValuePair <string, object> >())));
        }
Exemplo n.º 15
0
        public void TestCloseWriteSide()
        {
            var pipe = new Pipe();

            pipe.WriteText("123456");
            pipe.InputStream.Dispose();
            UnitTestHelpers.AssertThrows <ObjectDisposedException>(() => pipe.InputStream.WriteByte(1));

            pipe.ReadTextAsync(5).Result.ShouldEqual("12345");
            pipe.ReadTextAsync(2).Result.ShouldEqual(null);

            pipe = new Pipe();
            var asyncRead = pipe.ReadTextAsync(1);

            asyncRead.Wait(TimeSpan.FromSeconds(.01)).ShouldEqual(false);
            pipe.InputStream.Dispose();
            asyncRead.Wait(TimeSpan.FromSeconds(5)).ShouldEqual(true);
        }
        public void TestOneThrows()
        {
            void testOneThrows(bool reverse)
            {
                var reader1    = new StringReader("a\nb\nc");
                var count      = 0;
                var mockReader = new Mock <TextReader>(MockBehavior.Strict);

                mockReader.Setup(r => r.ReadLineAsync())
                .ReturnsAsync(() => ++ count < 3 ? "LINE" : throw new TimeZoneNotFoundException());

                UnitTestHelpers.AssertThrows <TimeZoneNotFoundException>(
                    () => new MergedLinesEnumerable(
                        reverse ? mockReader.Object : reader1,
                        reverse ? reader1 : mockReader.Object
                        )
                    .ToList()
                    );
            }

            testOneThrows(reverse: false);
            testOneThrows(reverse: true);
        }
Exemplo n.º 17
0
        public void TestGetValuesFromODataEntity()
        {
            var entity = new ODataEntity(new Dictionary <string, object>
            {
                { "A", null },
                { "B", 1 },
                { "C", new ODataEntity(new Dictionary <string, object> {
                        { "X", "abc" }
                    }) },
                { "D", ODataValue.FromObject(-1) },
                { "F", 1.5 },
            });

            entity.Get <string>("A").ShouldEqual(null);
            entity.Get <int>("b").ShouldEqual(1);
            entity.Get <int?>("b").ShouldEqual(1);
            entity.Get <ODataValue>("B").Value.ShouldEqual(1);
            entity.Get <ODataEntity>("c").Get <string>("x").ShouldEqual("abc");
            entity.Get <ODataObject>("C").GetType().ShouldEqual(typeof(ODataEntity));
            entity.Get <int>("d").ShouldEqual(-1);

            UnitTestHelpers.AssertThrows <InvalidCastException>(() => entity.Get <int>("a"));
            UnitTestHelpers.AssertThrows <InvalidCastException>(() => entity.Get <long>("F"));
        }