public void SearchUsesServiceMethodIfServerSupportsIt() { // Arrange var client = new Mock<IHttpClient>(); var context = new Mock<IDataServiceContext>(); var repository = new Mock<DataServicePackageRepository>(client.Object) { CallBase = true }; var packages = new[] { PackageUtility.CreatePackage("A", description:"New and aweseome"), PackageUtility.CreatePackage("B", description:"old and bad"), PackageUtility.CreatePackage("C", description:"rich") }; repository.Setup(m => m.GetPackages()).Returns(packages.AsQueryable()); repository.Object.Context = context.Object; context.Setup(m => m.SupportsServiceMethod("Search")).Returns(true); context.Setup(m => m.CreateQuery<DataServicePackage>(It.IsAny<string>(), It.IsAny<IDictionary<string, object>>())) .Callback<string, IDictionary<string, object>>((entitySet, parameters) => { // Assert Assert.Equal("Search", entitySet); Assert.Equal(2, parameters.Count); Assert.Equal("'old'", parameters["searchTerm"]); Assert.Equal("''", parameters["targetFramework"]); }) .Returns(new Mock<IDataServiceQuery<DataServicePackage>>().Object); // Act repository.Object.Search("old", allowPrereleaseVersions: false); }
public void SortByPorpertyPath() { var source = new[] { new TestSource{ Inner = new InnerType{Complex = new TestSource {String = "foo", Int = 1}}}, new TestSource{ Inner = new InnerType{Complex = new TestSource {String = "bar", Int = 99}}}, new TestSource{ Inner = new InnerType{Complex = new TestSource {String = "baz", Int = 3}}}, new TestSource{ Inner = new InnerType{Complex = new TestSource {String = "foo", Int = 8}}}, new TestSource{ Inner = new InnerType{Complex = new TestSource {String = "foo", Int = 5}}}, new TestSource{ Inner = new InnerType{Complex = new TestSource {String = "zap", Int = 16}}}, new TestSource{ Inner = new InnerType{Complex = new TestSource {String = "foo", Int = 7}}} }; var sortSettings = new List<SortSetting> { new SortSetting {PropertyPath = "Inner.Complex.String", SortOrder = SortOrder.Ascending}, new SortSetting {PropertyPath = "Inner.Complex.Int", SortOrder = SortOrder.Descending} }; IQueryable<TestSource> queryable = source.AsQueryable(); IOrderedQueryable<TestSource> expectedSortedQueryable = queryable.OrderBy(s => s.Inner.Complex.String).ThenByDescending(s => s.Inner.Complex.Int); var result = _builder.Sort(queryable, sortSettings); TestUtil.AssertModels(expectedSortedQueryable.ToList(), result.ToList()); }
public void CreateUsingFeatureClass() { var city1 = new City { Geometry = new Point(0, 0, 0), Name = "Amsterdam", Population = 1500000 }; var city2 = new City { Geometry = new Point(1, 1, 1), Name = "The Hague", Population = 9000000 }; var cities = new [] { city1, city2 }; var dataSource = new MemoryFeatureDataSource { Features = cities.AsQueryable() }; dataSource.FeatureAttributes.Count .Should("query attributes, automatically reconstructed from added features").Be.EqualTo(2); }
public IQueryable<SampleDataItem> Get() { var dataItems = new[] { new SampleDataItem { SomeProperty = 1 }, new SampleDataItem { SomeProperty = 2 }, new SampleDataItem { SomeProperty = 3 }, new SampleDataItem { SomeProperty = 4 }, }; return dataItems.AsQueryable(); }
public static IRepository<Content> CreateContentRepository() { var repository = MockRepository.GenerateStub<IRepository<Content>>(); var content = new[] { new Content { Name = "Page 1"}, new Content { Name = "Page 2"} }; repository.Expect(x => x.GetAll()).Return(content.AsQueryable()); return repository; }
private IQueryable<ISlot> CreateSlots([NotNull] CallInfo arg) { var one = Substitute.For <ISlot>(); one.DayId.Returns(1); var two = Substitute.For <ISlot>(); two.DayId.Returns(2); var list = new[] { one, two }; return list.AsQueryable(); }
public void NullComparer() { CustomerRec[] outer = new [] { new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } }; AnagramRec[] inner = new [] { new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } }; JoinRec[] expected = new [] { new JoinRec{ name = "Prakash", orderID = 323232, total = 9 } }; Assert.Equal(expected, outer.AsQueryable().Join(inner.AsQueryable(), e => e.name, e => e.name, (cr, or) => new JoinRec { name = cr.name, orderID = or.orderID, total = or.total }, null)); }
public void KeySelectorCalled() { var source = new[] { new { Name = "Tim", Score = 90 }, new { Name = "Robert", Score = 45 }, new { Name = "Prakash", Score = 99 } }; var expected = new[] { new { Name = "Prakash", Score = 99 }, new { Name = "Robert", Score = 45 }, new { Name = "Tim", Score = 90 } }; Assert.Equal(expected, source.AsQueryable().OrderBy(e => e.Name, null)); }
public void Should_use_nested_expression() { var items = new[] { new Parent { Children = new List<Child>() { new Child() { Value = 4 }, new Child() { Value = 5 }, } } }; var projected = items.AsQueryable().Project().To<ParentDto>().ToList(); projected[0].TotalSum.ShouldEqual(9); }
public void TestStartsWithOperatorString() { const string f = "f"; var source = new[] { new TestSource {String = "foo", Int = 1}, new TestSource {String = "bar", Int = 2}, new TestSource {String = "baz", Int = 3}, new TestSource {String = "fii", Int = 4}, new TestSource {String = "fuu", Int = 5}, new TestSource {String = "zap", Int = 6}, new TestSource {String = "faa", Int = 7} }; var expectedResultList = source.Where(t => t.String.StartsWith(f)).ToList(); var filter = new FilterSetting { OperatorName = FilterOperators.StartsWithOperator, PropertyPath = "String", Value = f }; var result = _builder.Filter(source.AsQueryable(), new[] { filter }); TestUtil.AssertModels(expectedResultList, result.ToList()); }
public void ExpressionTests_Sum2() { //Arrange var initValues = new[] { new SimpleValuesModel { FloatValue = 1 }, new SimpleValuesModel { FloatValue = 2 }, new SimpleValuesModel { FloatValue = 3 }, }; var qry = initValues.AsQueryable(); //Act var result = qry.Select("FloatValue").Sum(); var result2 = ((IQueryable<float>)qry.Select("FloatValue")).Sum(); //Assert Assert.AreEqual(6.0f, result); Assert.AreEqual(6.0f, result2); }
public void AscendingKeyThenDescendingKey() { var source = new[] { new { Name = "Jim", City = "Minneapolis", Country = "USA" }, new { Name = "Tim", City = "Seattle", Country = "USA" }, new { Name = "Philip", City = "Orlando", Country = "USA" }, new { Name = "Chris", City = "London", Country = "UK" }, new { Name = "Rob", City = "Kent", Country = "UK" } }; var expected = new[] { new { Name = "Chris", City = "London", Country = "UK" }, new { Name = "Rob", City = "Kent", Country = "UK" }, new { Name = "Tim", City = "Seattle", Country = "USA" }, new { Name = "Philip", City = "Orlando", Country = "USA" }, new { Name = "Jim", City = "Minneapolis", Country = "USA" } }; Assert.Equal(expected, source.AsQueryable().OrderBy(e => e.Country).ThenByDescending(e => e.City)); }
public void FirstOuterMatchesLastInnerLastOuterMatchesFirstInnerSameNumberElements() { CustomerRec[] outer = new [] { new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } }; OrderRec[] inner = new [] { new OrderRec{ orderID = 45321, custID = 99022, total = 50 }, new OrderRec{ orderID = 43421, custID = 29022, total = 20 }, new OrderRec{ orderID = 95421, custID = 98022, total = 9 } }; JoinRec[] expected = new [] { new JoinRec{ name = "Prakash", orderID = 95421, total = 9 }, new JoinRec{ name = "Robert", orderID = 45321, total = 50 } }; Assert.Equal(expected, outer.AsQueryable().Join(inner.AsQueryable(), e => e.custID, e => e.custID, (cr, or) => new JoinRec { name = cr.name, orderID = or.orderID, total = or.total })); }
public void SearchUsesDefaultSearchLogicIfServerDoesnotSupportServiceMethod() { // Arrange var client = new Mock<IHttpClient>(); var context = new Mock<IDataServiceContext>(); var repository = new Mock<DataServicePackageRepository>(client.Object) { CallBase = true }; var packages = new[] { PackageUtility.CreatePackage("A", description:"New and aweseome"), PackageUtility.CreatePackage("B", description:"old and bad"), PackageUtility.CreatePackage("C", description:"rich") }; repository.Setup(m => m.GetPackages()).Returns(packages.AsQueryable()); repository.Object.Context = context.Object; context.Setup(m => m.SupportsServiceMethod("Search")).Returns(false); // Act var results = repository.Object.Search("old", allowPrereleaseVersions: false).ToList(); // Assert Assert.Equal(1, results.Count); Assert.Equal("B", results[0].Id); }
public void SortByImmediateProperties() { var source = new[] { new TestSource {String = "foo", Int = 1}, new TestSource {String = "bar", Int = 2}, new TestSource {String = "baz", Int = 2}, new TestSource {String = "foo", Int = 8}, new TestSource {String = "foo", Int = 99}, new TestSource {String = "zap", Int = 6}, new TestSource {String = "foo", Int = 7} }; var sortSettings = new List<SortSetting> { new SortSetting {PropertyPath = "String", SortOrder = SortOrder.Ascending}, new SortSetting {PropertyPath = "Int", SortOrder = SortOrder.Descending} }; IQueryable<TestSource> queryable = source.AsQueryable(); IOrderedQueryable<TestSource> expectedSortedQueryable = queryable.OrderBy(s => s.String).ThenByDescending(s => s.Int); var result = _builder.Sort(queryable, sortSettings); TestUtil.AssertModels(expectedSortedQueryable.ToList(), result.ToList()); }
public void TestIdioticWayToSpecifyOrFilters() { var source = new[] { new TestSource {String = "foo", Int = 1}, new TestSource {String = "bar", Int = 2}, new TestSource {String = "baz", Int = 3}, new TestSource {String = "fii", Int = 4}, new TestSource {String = "baz", Int = 5}, new TestSource {String = "fuu", Int = 6}, new TestSource {String = "zap", Int = 7} }; var expectedResultList = source.Where(t => t.String == "foo" || t.String == "bar" || t.String == "zap").ToList(); var fooFilter = new FilterSetting { OperatorName = FilterOperators.EqualsOperator, PropertyPath = "String", Value = "foo" }; var barFilter = new FilterSetting { OperatorName = FilterOperators.EqualsOperator, PropertyPath = "String", Value = "bar" }; var zapFilter = new FilterSetting { OperatorName = FilterOperators.EqualsOperator, PropertyPath = "String", Value = "zap" }; var zapOrContiner = new FilterSetting {OrConnectedFilters = new List<FilterSetting> {zapFilter}}; var barOrContainer = new FilterSetting { OrConnectedFilters = new List<FilterSetting> { new FilterSetting {OrConnectedFilters = new List<FilterSetting> {barFilter}} } }; var fooOrContainer = new FilterSetting { OrConnectedFilters = new List<FilterSetting> { new FilterSetting { OrConnectedFilters = new List<FilterSetting> { new FilterSetting { OrConnectedFilters = new List<FilterSetting> {fooFilter} } } } } }; var orFilter = new FilterSetting { OrConnectedFilters = new List<FilterSetting> { fooOrContainer, barOrContainer, zapOrContiner } }; var result = _builder.Filter(source.AsQueryable(), new[] { orFilter }); TestUtil.AssertModels(expectedResultList, result.ToList()); }
public void TestInOperatorEnumerale() { var value = new[] {"foo", "baz"}; var source = new[] { new TestSource {String = "foo", Int = 1}, new TestSource {String = "bar", Int = 2}, new TestSource {String = "baz", Int = 3}, new TestSource {String = "fii", Int = 4}, new TestSource {String = "baz", Int = 5}, new TestSource {String = "fuu", Int = 6}, new TestSource {String = "zap", Int = 7} }; var expectedResultList = source.Where(t => value.Contains(t.String)).ToList(); var filter = new FilterSetting { OperatorName = FilterOperators.InOperator, PropertyPath = "String", Value = value }; var result = _builder.Filter(source.AsQueryable(), new[] { filter }); TestUtil.AssertModels(expectedResultList, result.ToList()); }
public void TestInOperatorString() { const string value = "goosfraba"; var source = new[] { new TestSource {String = "oos", Int = 1}, new TestSource {String = "fra", Int = 2}, new TestSource {String = "ba", Int = 3}, new TestSource {String = "fii", Int = 4}, new TestSource {String = "goos", Int = 5}, new TestSource {String = "osfra", Int = 6}, new TestSource {String = "faa", Int = 7} }; var expectedResultList = source.Where(t => value.Contains(t.String)).ToList(); var filter = new FilterSetting { OperatorName = FilterOperators.InOperator, PropertyPath = "String", Value = value }; var result = _builder.Filter(source.AsQueryable(), new[] { filter }); TestUtil.AssertModels(expectedResultList, result.ToList()); }
public void OuterNullNoComparer() { IQueryable<CustomerRec> outer = null; AnagramRec[] inner = new[] { new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } }; Assert.Throws<ArgumentNullException>("outer", () => outer.Join(inner.AsQueryable(), e => e.name, e => e.name, (cr, or) => new JoinRec { name = cr.name, orderID = or.orderID, total = or.total })); }
public void InnerNullNoComparer() { CustomerRec[] outer = new[] { new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } }; IQueryable<AnagramRec> inner = null; Assert.Throws<ArgumentNullException>("inner", () => outer.AsQueryable().Join(inner, e => e.name, e => e.name, (cr, or) => new JoinRec { name = cr.name, orderID = or.orderID, total = or.total })); }
public void TestPropertyPathFilteringWithNullCheck() { const string foo = "foo"; var source = new[] { new TestSource{ Inner = new InnerType{Complex = new TestSource {String = "foo", Int = 1}}}, new TestSource{ Inner = new InnerType{Complex = new TestSource {String = null, Int = 2}}},//Final property is null new TestSource{ Inner = new InnerType{Complex = null}}, //Null in between new TestSource{ Inner = new InnerType{Complex = new TestSource {String = "foo", Int = 4}}}, new TestSource{ Inner = null}, //Null in between null,//Whole item is null new TestSource{ Inner = new InnerType{Complex = new TestSource {String = "foo", Int = 7}}} }; var expectedResultList = source.AsQueryable().Where(t => t != null && t.Inner != null && t.Inner.Complex != null && t.Inner.Complex.String == foo); var filter = new FilterSetting { OperatorName = FilterOperators.EqualsOperator, PropertyPath = "Inner.Complex.String", Value = foo }; var result = _builder.Filter(source.AsQueryable(), new[] { filter }); TestUtil.AssertModels(expectedResultList.ToList(), result.ToList()); }
public void Should_serialize_inner_groups() { var firstGroup = new Mock<IGroup>(); firstGroup.SetupGet(g => g.Subgroups) .Returns( new ReadOnlyCollection<IGroup>(new []{ new AggregateFunctionsGroup { Key = 42, HasSubgroups = false, Subgroups = {}, ItemCount = 1, AggregateFunctionsProjection = null, Items = Enumerable.Empty<DataRowView>() } })); var secondGroup = new Mock<IGroup>(); secondGroup.SetupGet(g => g.Subgroups) .Returns( new ReadOnlyCollection<IGroup>(new[]{ new AggregateFunctionsGroup { Key = 42, HasSubgroups = false, Subgroups = {}, ItemCount = 1, AggregateFunctionsProjection = null, Items = Enumerable.Empty<DataRowView>() } })); var groups = new[] { firstGroup.Object, secondGroup.Object }; var result = groups.AsQueryable().SerializeToDictionary(dataTable); var firstGroupItem = result.Cast<Dictionary<string, object>>().First(); Assert.True(((IEnumerable<Dictionary<string, object>>)firstGroupItem["Subgroups"]).Any()); var secondGroupItem = result.Cast<Dictionary<string, object>>().ElementAt(1); Assert.True(((IEnumerable<Dictionary<string, object>>)secondGroupItem["Subgroups"]).Any()); }
public void InnerKeySelectorNullNoComparer() { CustomerRec[] outer = new[] { new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } }; AnagramRec[] inner = new[] { new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } }; Assert.Throws<ArgumentNullException>("innerKeySelector", () => outer.AsQueryable().Join(inner.AsQueryable(), e => e.name, null, (cr, or) => new JoinRec { name = cr.name, orderID = or.orderID, total = or.total })); }
public void ResultSelectorNullNoComparer() { CustomerRec[] outer = new[] { new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } }; AnagramRec[] inner = new[] { new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } }; Assert.Throws<ArgumentNullException>("resultSelector", () => outer.AsQueryable().Join(inner.AsQueryable(), e => e.name, e => e.name, (Expression<Func<CustomerRec, AnagramRec, JoinRec>>)null)); }
private static IPackageRepository GetRepository() { Mock<IPackageRepository> repository = new Mock<IPackageRepository>(); var packages = new[] { GetPackage("A", desc: "testing"), GetPackage("B", version: "1.1", requiresLicense: true), GetPackage("C", requiresLicense: true, dependencies: new[] { new PackageDependency("B", new VersionSpec { MinVersion = new SemanticVersion("1.0") }) }) }; repository.Setup(c => c.GetPackages()).Returns(packages.AsQueryable()); return repository.Object; }
public void TestOrAndCombination() { var source = new[] { new TestSource {String = "foo", Int = 1}, new TestSource {String = "bar", Int = 2}, new TestSource {String = "baz", Int = 3}, new TestSource {String = "fii", Int = 4}, new TestSource {String = "baz", Int = 5}, new TestSource {String = "fuu", Int = 6}, new TestSource {String = "zap", Int = 7}, new TestSource {String = "faa", Int = 8} }; var expectedResultList = source.Where(t => t.String.StartsWith("f") && (t.String.EndsWith("i") || t.String.EndsWith("a"))).ToList(); var startsWithFilter = new FilterSetting { OperatorName = FilterOperators.StartsWithOperator, PropertyPath = "String", Value = "f" }; var endsWith1Filter = new FilterSetting { OperatorName = FilterOperators.EndsWithOperator, PropertyPath = "String", Value = "i" }; var endsWith2Filter = new FilterSetting { OperatorName = FilterOperators.EndsWithOperator, PropertyPath = "String", Value = "a" }; var orFilter = new FilterSetting { OrConnectedFilters = new List<FilterSetting> { endsWith1Filter, endsWith2Filter } }; var result = _builder.Filter(source.AsQueryable(), new[] { startsWithFilter, orFilter }); TestUtil.AssertModels(expectedResultList, result.ToList()); }
public void TestPropertyPathFiltering() { const string foo = "foo"; var source = new[] { new TestSource{ Inner = new InnerType{Complex = new TestSource {String = "foo", Int = 1}}}, new TestSource{ Inner = new InnerType{Complex = new TestSource {String = "bar", Int = 2}}}, new TestSource{ Inner = new InnerType{Complex = new TestSource {String = "baz", Int = 3}}}, new TestSource{ Inner = new InnerType{Complex = new TestSource {String = "foo", Int = 4}}}, new TestSource{ Inner = new InnerType{Complex = new TestSource {String = "foo", Int = 5}}}, new TestSource{ Inner = new InnerType{Complex = new TestSource {String = "zap", Int = 6}}}, new TestSource{ Inner = new InnerType{Complex = new TestSource {String = "foo", Int = 7}}} }; var expectedResultList = source.Where(t => t.Inner.Complex.String == foo).ToList(); var filter = new FilterSetting { OperatorName = FilterOperators.EqualsOperator, PropertyPath = "Inner.Complex.String", Value = foo }; var result = _builder.Filter(source.AsQueryable(), new[] { filter }); TestUtil.AssertModels(expectedResultList, result.ToList()); }
public void Should_serialize_empty_list_if_no_group_items() { var groups = new[] { new AggregateFunctionsGroup { Key = 42, HasSubgroups = false, Subgroups = {}, ItemCount = 1, AggregateFunctionsProjection = null, Items = Enumerable.Empty<DataRowView>() } }; var result = groups.AsQueryable().SerializeToDictionary(dataTable); var firstGroupItem = result.Cast<Dictionary<string, object>>().First(); Assert.False(((IEnumerable<Dictionary<string, object>>)firstGroupItem["Items"]).Any()); }
public void ExpressionTests_ContextKeywordsAndSymbols() { try { //Arrange int[] values = new[] { 1, 2, 3, 4, 5 }; //Act GlobalConfig.AreContextKeywordsEnabled = false; Helper.ExpectException<ParseException>(() => values.AsQueryable().Where("it = 2")); Helper.ExpectException<ParseException>(() => values.AsQueryable().Where("root = 2")); values.AsQueryable().Where("$ = 2"); values.AsQueryable().Where("~ = 2"); GlobalConfig.AreContextKeywordsEnabled = true; var qry1 = values.AsQueryable().Where("it = 2"); var qry2 = values.AsQueryable().Where("$ = 2"); //Assert Assert.AreEqual(2, qry1.Single()); Assert.AreEqual(2, qry2.Single()); } finally { GlobalConfig.AreContextKeywordsEnabled = true; } }
private static IVsPackageManager GetPackageManager() { var fileSystem = new Mock<IFileSystem>(); var localRepo = new Mock<ISharedPackageRepository>(); var localPackages = new[] { PackageUtility.CreatePackage("P1", "0.9"), PackageUtility.CreatePackage("Pack2") }; localRepo.Setup(c => c.GetPackages()).Returns(localPackages.AsQueryable()); var remotePackages = new[] { PackageUtility.CreatePackage("P0", "1.1"), PackageUtility.CreatePackage("P1", "1.1"), PackageUtility.CreatePackage("Pack2", "1.2"), PackageUtility.CreatePackage("P3") }; var remoteRepo = new Mock<IPackageRepository>(); remoteRepo.Setup(c => c.GetPackages()).Returns(remotePackages.AsQueryable()); return new VsPackageManager(TestUtils.GetSolutionManager(), remoteRepo.Object, new Mock<IFileSystemProvider>().Object, fileSystem.Object, localRepo.Object, new Mock<IRecentPackageRepository>().Object, new Mock<VsPackageInstallerEvents>().Object); }