Exemple #1
0
        public void FilterNavigationWithAnyAll()
        {
            ocs.PopulateData.EntityConnection = null;
            using (OpenWebDataServiceHelper.AcceptAnyAllRequests.Restore())
                using (System.Data.EntityClient.EntityConnection connection = ocs.PopulateData.CreateTableAndPopulateData())
                {
                    OpenWebDataServiceHelper.AcceptAnyAllRequests.Value = true;
                    Type[] types = new Type[] { typeof(ocs.CustomObjectContext), typeof(CustomDataContext), typeof(CustomRowBasedContext) };

                    var testCases = new[]
                    {
                        new
                        {
                            filters = new string[]
                            {
                                "Orders/any() eq false",
                                "Orders/any() and ID eq 100",
                                "Orders/any(o: o/ID eq 500)",
                                "Orders/any(o: o/ID eq 100 and o/$(Customer)/ID ne 0)",
                                "Orders/any(o: isof(o/$(Customer),'$(CustomerWithBirthDayType)') and o/$(Customer)/ID ne 1)",
                                "Orders/all(o: o/$(Customer)/ID eq 1 and $it/ID ne 1)",
                                "Orders/all(o: o/ID eq 100)",
                                "Orders/all(o: o/ID eq $it/ID)",
                                "Orders/all(o: o/$(Customer)/Orders/any() eq false)",
                                "Orders/all(o: $it/ID eq 1 and o/$(Customer)/Orders/any(o1: o1/ID eq 500))",
                                "Orders/all(o: isof(o/$(Customer),'$(CustomerWithBirthDayType)') and o/$(Customer)/ID ne 1)",
                            },
                            xpaths = new string[]
                            {
                                "count(//atom:entry)=0"
                            }
                        },

                        new
                        {
                            filters = new string[]
                            {
                                "Orders/any()",
                                "Orders/any() and ID lt 100",
                                "ID lt 100 and Orders/any()",
                                "Orders/all(o: o/$(Customer)/Orders/any())",
                            },
                            xpaths = new string[]
                            {
                                "count(//atom:entry)=3"
                            }
                        },

                        new
                        {
                            filters = new string[]
                            {
                                "Orders/any() and (ID eq 1)",
                                "Orders/any() and isof('$(CustomerWithBirthDayType)')",
                                "Orders/any(o: $it/ID eq 1)",
                                "Orders/any(o: o/$(Customer)/ID eq 1)",
                                "Orders/any(o: isof(o/$(Customer), '$(CustomerWithBirthDayType)'))",

                                "Orders/all(o: $it/ID eq 1)",
                                "Orders/all(o: o/$(Customer)/ID eq 1)",
                                "Orders/all(o: isof(o/$(Customer), '$(CustomerWithBirthDayType)'))",

                                // using the same range variable name in multiple not-nested predicates
                                "Orders/any(o: o/$(Customer)/ID eq 1) and Orders/all(o: o/$(Customer)/ID eq 1)",

                                // nested queries
                                "Orders/any(o: o/$(Customer)/Orders/any(o1: o1/$(Customer)/Orders/all(o2: o2/$(Customer)/ID eq 1)) or $it/ID eq 1)",
                                "Orders/all(o: $it/ID eq 1 and o/$(Customer)/Orders/all(o1: o1/$(Customer)/Orders/any(o2: o2/$(Customer)/ID eq 1)))",

                                // keywords
                                "Orders/any(event: event/$(Customer)/ID eq 1)",
                                "Orders/any(while: while/$(Customer)/ID eq 1)",
                            },
                            xpaths = new string[]
                            {
                                "count(//atom:entry)=1",
                                "boolean(//atom:entry/atom:category[contains(@term,'CustomerWithBirthday')])"
                            }
                        }
                    };

                    TestUtil.RunCombinations(types, (type) =>
                    {
                        using (TestWebRequest request = TestWebRequest.CreateForInProcess())
                        {
                            request.DataServiceType = type;
                            request.StartService();

                            TestUtil.RunCombinations(testCases, (testCase) =>
                            {
                                foreach (var str in testCase.filters)
                                {
                                    string filter = UnitTestsUtil.ProcessStringVariables(str, (variable) =>
                                    {
                                        if (type == typeof(ocs.CustomObjectContext))
                                        {
                                            switch (variable)
                                            {
                                            case "Customer":
                                                return("Customers");

                                            case "CustomerWithBirthDayType":
                                                return("AstoriaUnitTests.ObjectContextStubs.Types.CustomerWithBirthday");
                                            }
                                        }
                                        else if (type == typeof(CustomDataContext))
                                        {
                                            switch (variable)
                                            {
                                            case "CustomerWithBirthDayType":
                                                return("AstoriaUnitTests.Stubs.CustomerWithBirthday");
                                            }
                                        }
                                        else if (type == typeof(CustomRowBasedContext))
                                        {
                                            switch (variable)
                                            {
                                            case "CustomerWithBirthDayType":
                                                return("AstoriaUnitTests.Stubs.CustomerWithBirthday");
                                            }
                                        }

                                        return(variable);
                                    });

                                    request.RequestUriString = "/Customers?$format=atom&$filter=" + filter;
                                    request.SendRequest();
                                    var response = request.GetResponseStreamAsXDocument();
                                    UnitTestsUtil.VerifyXPaths(response, testCase.xpaths);
                                }
                            });
                        }
                    });
                }
        }