public void Non_generic_AsStreaming_can_be_used_directly_on_DbQuery() { try { ProviderAgnosticConfiguration.SuspendExecutionStrategy = true; using (var context = new ProviderAgnosticContext()) { var expression = context.Gears.Where(g => g.Rank > MilitaryRank.Private).Expression; var query = (DbQuery)((IQueryable)context.Set(typeof(Gear))).Provider.CreateQuery(expression); var count = 0; foreach (var result in query) { Assert.NotNull(result); Assert.True(context.Database.Connection.State == ConnectionState.Open); count++; } Assert.Equal(3, count); count = 0; foreach (var gear in query.AsStreaming()) { Assert.NotNull(gear); Assert.True(context.Database.Connection.State == ConnectionState.Open); count++; } Assert.Equal(3, count); } } finally { ProviderAgnosticConfiguration.SuspendExecutionStrategy = false; } }
public void Generic_AsStreaming_can_be_used_directly_on_DbQuery() { try { ProviderAgnosticConfiguration.SuspendExecutionStrategy = true; using (var context = new ProviderAgnosticContext()) { var nonPrivates = (DbQuery <Gear>)context.Gears.Where(g => g.Rank > MilitaryRank.Private); var count = 0; foreach (var nonPrivate in nonPrivates) { Assert.NotNull(nonPrivate); Assert.True(context.Database.Connection.State == ConnectionState.Open); count++; } Assert.Equal(3, count); count = 0; foreach (var nonPrivate in nonPrivates.AsStreaming()) { Assert.NotNull(nonPrivate); Assert.True(context.Database.Connection.State == ConnectionState.Open); count++; } Assert.Equal(3, count); } } finally { ProviderAgnosticConfiguration.SuspendExecutionStrategy = false; } }
public void Generic_AsStreaming_can_be_used_directly_on_DbSet() { try { ProviderAgnosticConfiguration.SuspendExecutionStrategy = true; using (var context = new ProviderAgnosticContext()) { var gears = context.Gears; var count = 0; foreach (var gear in gears) { Assert.NotNull(gear); Assert.True(context.Database.Connection.State == ConnectionState.Open); count++; } Assert.Equal(5, count); count = 0; foreach (var gear in gears.AsStreaming()) { Assert.NotNull(gear); Assert.True(context.Database.Connection.State == ConnectionState.Open); count++; } Assert.Equal(5, count); } } finally { ProviderAgnosticConfiguration.SuspendExecutionStrategy = false; } }
public void Enum_in_Join_clause() { using (var context = new ProviderAgnosticContext()) { var query = context.AllTypes.Take(1).Join( context.AllTypes, o => o.EnumProperty, i => i.EnumProperty, (o, i) => new { OuterKey = o.Id, OuterEnum = o.EnumProperty, InnerKey = i.Id, InnerEnum = i.EnumProperty }); // verify that all all entities with matching enum values are joined // and that correct enum values get joined var firstEnum = context.AllTypes.ToList().Take(1).First().EnumProperty; var allTypesWithFirstEnumIds = context.AllTypes.ToList().Where(a => a.EnumProperty == firstEnum).Select(a => a.Id).ToList(); var results = query.ToList(); Assert.Equal(allTypesWithFirstEnumIds.Count(), results.Count()); foreach (var result in results) { Assert.Equal(firstEnum, result.InnerEnum); Assert.Equal(firstEnum, result.OuterEnum); Assert.True(allTypesWithFirstEnumIds.Contains(result.OuterKey)); } } }
public void ObjectSet_AsStreaming_can_be_used_before_the_rest_of_the_query() { try { ProviderAgnosticConfiguration.SuspendExecutionStrategy = true; using (var context = new ProviderAgnosticContext()) { var gears = CreateObjectSet <Gear>(context); var count = 0; foreach (var nonPrivate in gears.Where(g => g.Rank > MilitaryRank.Private)) { Assert.NotNull(nonPrivate); Assert.True(context.Database.Connection.State == ConnectionState.Open); count++; } Assert.Equal(3, count); count = 0; foreach (var nonPrivate in gears.Where(g => g.Rank > MilitaryRank.Private).AsStreaming()) { Assert.NotNull(nonPrivate); Assert.True(context.Database.Connection.State == ConnectionState.Open); count++; } Assert.Equal(3, count); } } finally { ProviderAgnosticConfiguration.SuspendExecutionStrategy = false; } }
public void Materialize_DbSet_throws() { using (var context = new ProviderAgnosticContext()) { Assert.Throws <ArgumentException>(() => context.Runs.Select(r => context.Owners)); context.Runs.Select(r => context.Owners.AsEnumerable()).ToList(); } }
public void Include_propagation_over_first_or_default_with_predicate() { using (var context = new ProviderAgnosticContext()) { ((IObjectContextAdapter)context).ObjectContext.ContextOptions.LazyLoadingEnabled = false; var results = context.Configs.Include(c => c.Failures).FirstOrDefault(o => o.Id > 0); Assert.NotNull(results.Failures); Assert.True(results.Failures.Count > 0); } }
public void Casting_to_enum_undeclared_in_model_works() { using (var context = new ProviderAgnosticContext()) { var query = context.AllTypes.Select(a => (FileAccess)a.Id); // not much to verify here, just make sure we don't throw query.ToList(); } }
public void Include_propagation_over_sort() { using (var context = new ProviderAgnosticContext()) { ((IObjectContextAdapter)context).ObjectContext.ContextOptions.LazyLoadingEnabled = false; var query = context.Owners.Include(o => o.OwnedRun).OrderBy(o => o.Id == 1); var results = query.ToList(); Assert.NotNull(results.First().OwnedRun); } }
public void Materialize_ObjectQuery_throws() { using (var context = new ProviderAgnosticContext()) { var objectContext = ((IObjectContextAdapter)context).ObjectContext; var owners = objectContext.CreateQuery <Owner>("Owners"); Assert.Throws <InvalidOperationException>(() => context.Runs.Select(r => owners).ToList()); context.Runs.Select(r => owners.AsEnumerable()).ToList(); } }
public void Include_propagation_over_type_filter() { using (var context = new ProviderAgnosticContext()) { ((IObjectContextAdapter)context).ObjectContext.ContextOptions.LazyLoadingEnabled = false; var query = context.Configs.Include(c => c.Failures).OfType <MachineConfig>(); var results = query.ToList(); Assert.True(results.Any(r => r.Failures.Count > 0)); } }
public void TrimEnd_properly_translated_to_function() { using (var context = new ProviderAgnosticContext()) { var orderedOwners = context.Owners.OrderBy(o => o.Id); var expected = orderedOwners.ToList().Select(o => o.Alias.TrimEnd()).ToList(); var actual = orderedOwners.Select(o => o.Alias.TrimEnd()).ToList(); QueryTestHelpers.VerifyQueryResult(expected, actual, (e, a) => e == a); } }
public void Include_one_to_one_relationship() { using (var context = new ProviderAgnosticContext()) { ((IObjectContextAdapter)context).ObjectContext.ContextOptions.LazyLoadingEnabled = false; var query = context.Owners.OrderBy(o => o.Id).Include(o => o.OwnedRun); var results = query.ToList(); var runsForOwners = context.Owners.OrderBy(o => o.Id).Select(o => o.OwnedRun).ToList(); Enumerable.SequenceEqual(runsForOwners, results.Select(r => r.OwnedRun)); } }
public void DateTime_Year_properly_translated_to_function() { using (var context = new ProviderAgnosticContext()) { var orderedFailures = context.Failures.OrderBy(f => f.Id); var expected = orderedFailures.ToList().Select(o => o.Changed.Year).ToList(); var actual = orderedFailures.Select(o => o.Changed.Year).ToList(); QueryTestHelpers.VerifyQueryResult(expected, actual, (e, a) => e == a); } }
public void Floor_properly_translated_to_function() { using (var context = new ProviderAgnosticContext()) { var orderedAllTypes = context.AllTypes.OrderBy(a => a.Id); var expected = orderedAllTypes.ToList().Select(a => Math.Floor(a.DecimalProperty)).ToList(); var actual = orderedAllTypes.Select(a => Math.Floor(a.DecimalProperty)).ToList(); QueryTestHelpers.VerifyQueryResult(expected, actual, (e, a) => e == a); } }
public void String_Replace_properly_translated_to_function() { using (var context = new ProviderAgnosticContext()) { var orderedOwners = context.Owners.OrderBy(o => o.Id); var expected = orderedOwners.ToList().Select(o => o.LastName.Replace("Name", "Foo")).ToList(); var actual = orderedOwners.Select(o => o.LastName.Replace("Name", "Foo")).ToList(); QueryTestHelpers.VerifyQueryResult(expected, actual, (e, a) => e == a); } }
public void String_Concat_translated_properly_to_plus_operator() { using (var context = new ProviderAgnosticContext()) { var orderedOwners = context.Owners.OrderBy(o => o.Id); var expected = orderedOwners.ToList().Select(o => string.Concat(o.LastName, o.FirstName)).ToList(); var actual = orderedOwners.Select(o => string.Concat(o.LastName, o.FirstName)).ToList(); QueryTestHelpers.VerifyQueryResult(expected, actual, (e, a) => e == a); } }
public void Include_from_union_combined() { using (var context = new ProviderAgnosticContext()) { ((IObjectContextAdapter)context).ObjectContext.ContextOptions.LazyLoadingEnabled = false; var query = context.Failures.Include(f => f.Bugs).Union(context.Failures.Include(f => f.Configs)); var results = query.ToList(); Assert.True(results.Any(r => r.Bugs.Count > 0)); Assert.True(results.Any(r => r.Configs.Count > 0)); } }
public void Can_materialize_collection_inside_anonymous_type() { using (var context = new ProviderAgnosticContext()) { var query = context.Failures.Where(f => f.Id == 1).Select(f => new { f.Bugs }); var results = query.ToList(); var bugs = context.Bugs.Where(b => b.Failure.Id == 1).ToList(); Assert.Equal(1, results.Count); Assert.Equal(bugs.Count, results[0].Bugs.Count); } }
public void Unnamed_enum_constant_in_Where_clause() { using (var context = new ProviderAgnosticContext()) { context.Configuration.UseDatabaseNullSemantics = true; var query = context.AllTypes.Where(a => a.EnumProperty == (AllTypesEnum)42).Select(p => p.EnumProperty); // verify all results are filtered out var results = query.ToList(); Assert.False(results.Any()); } }
public void OrderBy_ThenBy_Take_lifted_above_projection() { using (var context = new ProviderAgnosticContext()) { var query = context.Owners.OrderByDescending(p => p.FirstName).ThenBy(p => p.Id).Take(10).Select(p => new { p.FirstName, p.Id, Foo = p.Id * 5 }); var baseline = context.Owners.Select(p => new { p.FirstName, p.Id, Foo = p.Id * 5 }).OrderByDescending(p => p.FirstName).ThenBy(p => p.Id).Take(10); Assert.Equal(baseline.ToString(), query.ToString()); var results = query.ToList(); var expected = context.Owners.ToList().OrderByDescending(p => p.FirstName).ThenBy(p => p.Id).Take(10).Select(p => new { p.FirstName, p.Id, Foo = p.Id * 5 }).ToList(); QueryTestHelpers.VerifyQueryResult(expected, results, (o, i) => o.Id == i.Id); } }
public void OrderBy_ThenBy_lifted_above_type_filter() { using (var context = new ProviderAgnosticContext()) { var query = context.Configs.OrderByDescending(p => p.Arch).ThenBy(p => p.Id).OfType <MachineConfig>(); var baseline = context.Configs.OfType <MachineConfig>().OrderByDescending(p => p.Arch).ThenBy(p => p.Id); Assert.Equal(baseline.ToString(), query.ToString()); var results = query.ToList(); var expected = context.Configs.ToList().OrderByDescending(p => p.Arch).ThenBy(p => p.Id).OfType <MachineConfig>().ToList(); QueryTestHelpers.VerifyQueryResult(expected, results, (o, i) => o.Id == i.Id); } }
public void Cast_constant_to_enum() { using (var context = new ProviderAgnosticContext()) { context.Configuration.UseDatabaseNullSemantics = true; var query = context.AllTypes.Where(a => a.EnumProperty == (AllTypesEnum)1).Select(a => a.EnumProperty); // verify that correct enums are filtered out (verify count and value) var results = query.ToList(); var expectedCount = context.AllTypes.ToList().Count(a => a.EnumProperty == (AllTypesEnum)1); Assert.True(results.All(r => r == AllTypesEnum.EnumValue1)); Assert.Equal(expectedCount, results.Count); } }
public void Substring_properly_translated_to_function() { using (var context = new ProviderAgnosticContext()) { var orderedOwners = context.Owners.OrderBy(o => o.Id); var expected1 = orderedOwners.ToList().Select(o => o.LastName.Substring(1)).ToList(); var expected2 = orderedOwners.ToList().Select(o => o.LastName.Substring(1, 2)).ToList(); var actual1 = orderedOwners.Select(o => o.LastName.Substring(1)).ToList(); var actual2 = orderedOwners.Select(o => o.LastName.Substring(1, 2)).ToList(); QueryTestHelpers.VerifyQueryResult(expected1, actual1, (e, a) => e == a); QueryTestHelpers.VerifyQueryResult(expected2, actual2, (e, a) => e == a); } }
public void OrderBy_ThenBy_lifted_above_filter_without_clr_null_semantics() { using (var context = new ProviderAgnosticContext()) { ((IObjectContextAdapter)context).ObjectContext.ContextOptions.UseCSharpNullComparisonBehavior = false; var query = context.Owners.OrderByDescending(p => p.FirstName).ThenBy(p => p.Id).Where(p => p.Id % 2 == 0); var baseline = context.Owners.Where(p => p.Id % 2 == 0).OrderByDescending(p => p.FirstName).ThenBy(p => p.Id); Assert.Equal(baseline.ToString(), query.ToString()); var results = query.ToList(); var expected = context.Owners.ToList().OrderByDescending(p => p.FirstName).ThenBy(p => p.Id).Where(p => p.Id % 2 == 0).ToList(); QueryTestHelpers.VerifyQueryResult(expected, results, (o, i) => o.Id == i.Id); } }
public void Enum_in_OrderBy_clause() { using (var context = new ProviderAgnosticContext()) { var query = context.AllTypes.OrderBy(a => a.EnumProperty).Select(a => a.EnumProperty); // verify order is correct var results = query.ToList(); var highest = (int)results[0]; foreach (var result in results) { Assert.True((int)result >= highest); highest = (int)result; } } }
public void Can_materialize_properties_into_non_mapped_type() { using (var context = new ProviderAgnosticContext()) { var query = context.Owners.Where(o => o.Id == 1).Select(o => new MyNonMappedType { Id = o.Id, Name = o.FirstName }); var results = query.ToList(); var id = context.Owners.Select(o => o.Id).First(r => r == 1); var name = context.Owners.Where(o => o.Id == 1).Select(o => o.FirstName).First(); Assert.Equal(1, results.Count); Assert.Equal(id, results[0].Id); Assert.Equal(name, results[0].Name); } }
public void Include_with_string_overload() { using (var context = new ProviderAgnosticContext()) { ((IObjectContextAdapter)context).ObjectContext.ContextOptions.LazyLoadingEnabled = false; var query = context.Runs.OrderBy(r => r.Id).Include("Tasks"); var results = query.ToList(); var tasksForRuns = context.Runs.OrderBy(r => r.Id).Select(r => r.Tasks).ToList(); for (var i = 0; i < results.Count; i++) { Assert.Equal(tasksForRuns[i].Count, results[i].Tasks.Count); var expectedTasks = tasksForRuns[i].Select(t => t.Id).ToList().OrderBy(t => t); var actualTasks = results[i].Tasks.Select(t => t.Id).ToList().OrderBy(t => t); Enumerable.SequenceEqual(expectedTasks, actualTasks); } } }
public void Include_on_many_to_many_relationship() { using (var context = new ProviderAgnosticContext()) { ((IObjectContextAdapter)context).ObjectContext.ContextOptions.LazyLoadingEnabled = false; var query = context.Configs.OrderBy(c => c.Id).Include(c => c.Failures); var results = query.ToList(); var failuresForConfigs = context.Configs.Select(c => c.Failures).ToList(); for (var i = 0; i < results.Count; i++) { Assert.Equal(failuresForConfigs[i].Count, results[i].Failures.Count); var expectedFailures = failuresForConfigs[i].Select(t => t.Id).ToList().OrderBy(f => f); var actualFailures = results[i].Failures.Select(t => t.Id).ToList().OrderBy(f => f); Assert.True(Enumerable.SequenceEqual(expectedFailures, actualFailures)); } } }
public void Can_materialize_list_inside_anonymous_type() { using (var context = new ProviderAgnosticContext()) { var query = context.Runs.Where(r => r.Id == 1).Select(r => new { r.Id, List = new List <string> { r.Name, "a", "b" } }); var results = query.ToList(); var runId = context.Runs.Select(r => r.Id).First(r => r == 1); var runName = context.Runs.Where(r => r.Id == 1).Select(r => r.Name).First(); Assert.Equal(1, results.Count); Assert.Equal(runId, results[0].Id); Assert.Equal(3, results[0].List.Count); Assert.Equal(runName, results[0].List[0]); Assert.Equal("a", results[0].List[1]); Assert.Equal("b", results[0].List[2]); } }