Beispiel #1
0
            public void QueryRowCountUriBuilder()
            {
                var baseQuery = ctx.CreateQuery <northwindClient.Orders>("Orders").IncludeTotalCount();

                // having both $count=inline & $count=value - should fail
                try
                {
                    long sc2 = baseQuery.LongCount();
                    Assert.Fail("Duplicating $count did not throw");
                }
                catch (NotSupportedException ex)
                {
                    string expectedMsg = DataServicesClientResourceUtil.GetString("ALinq_CannotAddCountOptionConflict");
                    Assert.AreEqual(expectedMsg, ex.Message);
                }

                try
                {
                    var customQuery = baseQuery.AddQueryOption("$count", "true");
                    customQuery.Execute();
                    Assert.Fail("Duplicating query $count did not throw");
                }
                catch (NotSupportedException ex)
                {
                    string expectedMsg = DataServicesClientResourceUtil.GetString("ALinq_CantAddAstoriaQueryOption", "$count");
                    Assert.AreEqual(expectedMsg, ex.Message);
                }
            }
Beispiel #2
0
            public void QueryRowCountWithOptions()
            {
                IQueryable[] queries = new IQueryable[] {
                    // case 0: no count - should throw at get_CountValue
                    (from c in ctx.CreateQuery <northwindClient.Customers>("Customers") select c).Take(1),

                    // case 1: has count - should equal to 15
                    (from c in ctx.CreateQuery <northwindClient.Customers>("Customers").IncludeTotalCount() select c).Take(1),

                    // case 2: has count with expand - should equal to 15
                    (from c in ctx.CreateQuery <northwindClient.Customers>("Customers").Expand("Orders").IncludeTotalCount() select c).Take(1),

                    // case 3: has count with ordering, skipping - should equal to 15
                    (from c in ctx.CreateQuery <northwindClient.Customers>("Customers").IncludeTotalCount().OrderBy(cc => cc.ContactTitle).Skip(2).Take(1) select c),

                    // case 4: has count with filtering - should equal to 4
                    (from c in ctx.CreateQuery <northwindClient.Customers>("Customers").IncludeTotalCount().Where(cc => cc.ContactTitle.Equals("owner")).Take(1) select c),

                    // case 5: has count with filtering & expanding - should equal to 4
                    (from c in ctx.CreateQuery <northwindClient.Customers>("Customers").IncludeTotalCount().Expand("Orders").Where(cc => cc.ContactTitle.Equals("owner")).Take(1) select c),

                    // case 6: has count from Custom Query Option - should equal to 15
                    (from c in ctx.CreateQuery <northwindClient.Customers>("Customers").AddQueryOption("$count", "true").Take(1) select c)
                };

                for (int i = 0; i < queries.Length; ++i)
                {
                    IEnumerable ie = ((DataServiceQuery <northwindClient.Customers>)queries[i]).Execute();
                    QueryOperationResponse <northwindClient.Customers> qor = ie as QueryOperationResponse <northwindClient.Customers>;
                    Assert.IsNotNull(qor);

                    try
                    {
                        long count = qor.TotalCount;
                        Assert.AreNotEqual(i, 0);
                        if (i == 4 || i == 5)
                        {
                            Assert.AreEqual(4, count);
                        }
                        else
                        {
                            Assert.AreEqual(15, count);
                        }
                    }
                    catch (InvalidOperationException ex)
                    {
                        Assert.AreEqual(i, 0);  // only happens in the first case
                        string expectedMsg = DataServicesClientResourceUtil.GetString("MaterializeFromAtom_CountNotPresent");
                        Assert.AreEqual(expectedMsg, ex.Message);
                    }
                }
            }
Beispiel #3
0
            public void QueryRowCountCustomRequest()
            {
                // .Execute() with direct URI should never sent version
                ctx.SendingRequest2 += new EventHandler <SendingRequest2EventArgs>((o, e) =>
                {
                    string dsv = e.RequestMessage.GetHeader("OData-Version");
                    Assert.IsNull(dsv);
                });

                IEnumerable[] responses = new IEnumerable[]
                {
                    ctx.Execute <northwindClient.Customers>("/Customers?$top=1"),
                    ctx.Execute <northwindClient.Customers>("/Customers?$count=true"),
                    ctx.Execute <northwindClient.Customers>("/Customers?$count=true&$filter=ContactTitle eq 'owner'&$top=1"),
                    ctx.Execute <northwindClient.Customers>("/Customers?$orderby=ContactTitle&$count=true"),
                    ctx.Execute <northwindClient.Customers>("/Customers?$expand=Orders&$count=true")
                };

                for (int i = 0; i < responses.Length; ++i)
                {
                    QueryOperationResponse <northwindClient.Customers> qor = (QueryOperationResponse <northwindClient.Customers>)responses[i];
                    try
                    {
                        long count = qor.TotalCount;
                        Assert.AreNotEqual(i, 0);
                        if (i == 2)
                        {
                            Assert.AreEqual(4, count);
                        }
                        else
                        {
                            Assert.AreEqual(15, count);
                        }
                    }
                    catch (InvalidOperationException ex)
                    {
                        Assert.AreEqual(i, 0);
                        string expectedMsg = DataServicesClientResourceUtil.GetString("MaterializeFromAtom_CountNotPresent");
                        Assert.AreEqual(expectedMsg, ex.Message);
                    }
                }
            }
Beispiel #4
0
            public void QueryRowCountNotFoundException()
            {
                var    baseQuery                   = ctx.CreateQuery <northwindClient.Customers>("Customer");
                var    inlineQuery                 = baseQuery.IncludeTotalCount();
                string countNotPresentMsg          = DataServicesClientResourceUtil.GetString("MaterializeFromAtom_CountNotPresent");
                string resourceNotFoundCustomerMsg = DataServicesResourceUtil.GetString("RequestUriProcessor_ResourceNotFound", "Customer");
                string resourceNotFoundVar1Msg     = DataServicesResourceUtil.GetString("RequestUriProcessor_ResourceNotFound", "VAR1");

                for (int i = 0; i < 2; ++i)
                {
                    ctx.IgnoreResourceNotFoundException = (i == 0);

                    // case 1: $count=inline, 404 exception gives empty set
                    try
                    {
                        QueryOperationResponse <northwindClient.Customers> qor = (QueryOperationResponse <northwindClient.Customers>)inlineQuery.Execute();
                        Assert.AreEqual(i, 0);

                        // client count:
                        Assert.AreEqual(qor.Count(), 0);

                        // server count should fail:
                        long count = qor.TotalCount;

                        Assert.Fail("Server count failed to throw on 404");
                    }
                    catch (DataServiceQueryException ex)
                    {
                        Assert.AreEqual(i, 1);
                        Assert.IsNotNull(ex.InnerException);
                        Assert.IsTrue(ex.InnerException.Message.Contains(resourceNotFoundCustomerMsg));
                    }
                    catch (InvalidOperationException ex)
                    {
                        Assert.AreEqual(i, 0);
                        Assert.AreEqual(countNotPresentMsg, ex.Message);
                    }

                    // case 2: $count=value, 404 exception
                    try
                    {
                        long count = baseQuery.LongCount();
                        Assert.Fail("Server count failed to throw on 404");
                    }
                    catch (DataServiceQueryException ex)
                    {
                        Assert.IsNotNull(ex.InnerException);
                        Assert.IsTrue(ex.InnerException.Message.Contains(resourceNotFoundCustomerMsg));
                    }

                    // case 3: custom URI on context
                    try
                    {
                        QueryOperationResponse <northwindClient.Customers> qor = (QueryOperationResponse <northwindClient.Customers>)ctx.Execute <northwindClient.Customers>("/VAR1?$count=true");
                        Assert.AreEqual(i, 0);

                        // client count:
                        Assert.AreEqual(qor.Count(), 0);

                        // server count should fail:
                        long count = qor.TotalCount;

                        Assert.Fail("Server count failed to throw on 404");
                    }
                    catch (DataServiceQueryException ex)
                    {
                        Assert.AreEqual(i, 1);
                        Assert.IsNotNull(ex.InnerException);
                        Assert.IsTrue(ex.InnerException.Message.Contains(resourceNotFoundVar1Msg));
                    }
                    catch (InvalidOperationException ex)
                    {
                        Assert.AreEqual(i, 0);
                        Assert.AreEqual(countNotPresentMsg, ex.Message);
                    }


                    // case 4: $count=inline, ASYNC, 404 exception gives empty set
                    bool asyncComplete = false;
                    inlineQuery.BeginExecute(ar =>
                    {
                        try
                        {
                            QueryOperationResponse <northwindClient.Customers> qor = (QueryOperationResponse <northwindClient.Customers>)inlineQuery.EndExecute(ar);
                            Assert.AreEqual(qor.Count(), 0);
                            long count = qor.TotalCount;
                            Assert.Fail("Server count failed to throw on 404");
                        }
                        catch (DataServiceQueryException ex)
                        {
                            Assert.AreEqual(i, 1);
                            Assert.IsNotNull(ex.InnerException);
                            Assert.IsNotNull(ex.InnerException.InnerException);
                            Assert.IsTrue(ex.InnerException.InnerException.Message.Contains(resourceNotFoundCustomerMsg));
                        }
                        catch (InvalidOperationException ex)
                        {
                            Assert.AreEqual(i, 0);
                            Assert.AreEqual(countNotPresentMsg, ex.Message);
                        }
                        finally
                        {
                            asyncComplete = true;
                        }
                    }, null);

                    while (!asyncComplete)
                    {
                        Thread.Sleep(10);
                    }
                }
            }