Exemplo n.º 1
0
            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);
                }
            }
Exemplo n.º 2
0
 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));
     }
 }
Exemplo n.º 3
0
            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 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);
            }
        }
Exemplo n.º 5
0
        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_Skip_lifted_above_projection()
        {
            using (var context = new ProviderAgnosticContext())
            {
                var query = context.Owners.OrderByDescending(p => p.FirstName).ThenBy(p => p.Id).Skip(5).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).Skip(5);
                Assert.Equal(baseline.ToString(), query.ToString());

                var results = query.ToList();
                var expected = context.Owners.ToList().OrderByDescending(p => p.FirstName).ThenBy(p => p.Id).Skip(5).Select(p => new { p.FirstName, p.Id, Foo = p.Id * 5 }).ToList();
                QueryTestHelpers.VerifyQueryResult(expected, results, (o, i) => o.Id == i.Id);
            }
        }
Exemplo n.º 7
0
        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 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);
            }
        }
Exemplo n.º 9
0
        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;
                }
            }
        }
Exemplo n.º 10
0
 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));
         }
     }
 }
Exemplo n.º 11
0
        public void Include_on_one_to_many_relationship()
        {
            using (var context = new ProviderAgnosticContext())
            {
                ((IObjectContextAdapter)context).ObjectContext.ContextOptions.LazyLoadingEnabled = false;
                var query = context.Runs.OrderBy(r => r.Id).Include(c => c.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);

                    Assert.True(Enumerable.SequenceEqual(expectedTasks, actualTasks));
                }
            }
        }
Exemplo n.º 12
0
        public void Enum_in_GroupBy_clause()
        {
            using (var context = new ProviderAgnosticContext())
            {
                var query = context.AllTypes.GroupBy(a => a.EnumProperty).Select(
                    g => new
                    {
                        g.Key,
                        Count = g.Count()
                    });

                var results = query.ToList().OrderBy(r => r.Key).ToList();
                var expected = context.AllTypes.ToList().GroupBy(a => a.EnumProperty).Select(
                    g => new
                    {
                        g.Key,
                        Count = g.Count()
                    }).OrderBy(r => r.Key).ToList();
                QueryTestHelpers.VerifyQueryResult(expected, results, (o, i) => o.Key == i.Key && o.Count == i.Count);
            }
        }
Exemplo n.º 13
0
 public void Multiple_includes()
 {
     using (var context = new ProviderAgnosticContext())
     {
         ((IObjectContextAdapter)context).ObjectContext.ContextOptions.LazyLoadingEnabled = false;
         var query = context.Failures.OrderBy(f => f.Id).Include(f => f.Configs).Include(f => f.Bugs);
         var results = query.ToList();
         var configsForFailures = context.Failures.OrderBy(f => f.Id).Select(r => r.Configs).ToList();
         var bugsForFailures = context.Failures.OrderBy(f => f.Id).Select(r => r.Bugs).ToList();
         for (var i = 0; i < results.Count; i++)
         {
             Assert.Equal(bugsForFailures[i].Count, results[i].Bugs.Count);
             Assert.Equal(configsForFailures[i].Count, results[i].Configs.Count);
             var expectedBugs = bugsForFailures[i].Select(b => b.Id).ToList().OrderBy(b => b);
             var expectedConfigs = configsForFailures[i].Select(c => c.Id).OrderBy(c => c).ToList();
             var actualBugs = results[i].Bugs.Select(b => b.Id).ToList().OrderBy(b => b);
             var actualConfigs = results[i].Configs.Select(c => c.Id).ToList().OrderBy(c => c);
             Enumerable.SequenceEqual(expectedBugs, actualBugs);
             Enumerable.SequenceEqual(expectedConfigs, actualConfigs);
         }
     }
 }
Exemplo n.º 14
0
 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));
     }
 }
Exemplo n.º 15
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();
            }
        }
Exemplo n.º 16
0
 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);
     }
 }
Exemplo n.º 17
0
        public void Enum_with_coalesce_operator()
        {
            using (var context = new ProviderAgnosticContext())
            {
                context.Configuration.UseDatabaseNullSemantics = true;

                var query = context.AllTypes.Select(p => p.Id % 2 == 0 ? p.EnumProperty : (AllTypesEnum?)null)
                                   .Select(a => a ?? AllTypesEnum.EnumValue1);

                var results = query.ToList();
                var expected = context.AllTypes.ToList().Select(p => p.Id % 2 == 0 ? p.EnumProperty : (AllTypesEnum?)null)
                                      .Select(a => a ?? AllTypesEnum.EnumValue1).ToList();

                Assert.Equal(expected.Count, results.Count);
                for (var i = 0; i < results.Count; i++)
                {
                    Assert.Equal(expected[i], results[i]);
                }
            }
        }
Exemplo n.º 18
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);
                }
            }
Exemplo n.º 19
0
            public void Ceiling_properly_translated_to_function()
            {
                using (var context = new ProviderAgnosticContext())
                {
                    var orderedAllTypes = context.AllTypes.OrderBy(a => a.Id);
                    var expected = orderedAllTypes.ToList().Select(a => Math.Ceiling(a.DecimalProperty)).ToList();
                    var actual = orderedAllTypes.Select(a => Math.Ceiling(a.DecimalProperty)).ToList();

                    QueryTestHelpers.VerifyQueryResult(expected, actual, (e, a) => e == a);
                }
            }
Exemplo n.º 20
0
            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);
                }
            }
Exemplo n.º 21
0
 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);
     }
 }
Exemplo n.º 22
0
            public void Truncates_properly_translated_to_function()
            {
                using (var context = new ProviderAgnosticContext())
                {
                    var orderedAllTypes = context.AllTypes.OrderBy(a => a.Id);

                    var expected1 = orderedAllTypes.ToList().Select(a => Math.Truncate(a.DecimalProperty)).ToList();
                    var expected2 = orderedAllTypes.ToList().Select(a => Math.Truncate(a.FloatProperty)).ToList();
                    var actual1 = orderedAllTypes.Select(a => Math.Truncate(a.DecimalProperty)).ToList();
                    var actual2 = orderedAllTypes.Select(a => Math.Truncate(a.FloatProperty)).ToList();

                    QueryTestHelpers.VerifyQueryResult(expected1, actual1, (e, a) => e == a);
                    QueryTestHelpers.VerifyQueryResult(expected2, actual2, (e, a) => e == a);
                }
            }
Exemplo n.º 23
0
 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));
     }
 }
Exemplo n.º 24
0
            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);
                }
            }
Exemplo n.º 25
0
        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));
                }
            }
        }