1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Query.LinqToEntities
{
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.TestModels.ArubaModel;
using System.Linq;
using System.Reflection;
using Xunit;
public class MaterializationTests : FunctionalTestBase
{
[Fact]
public void Can_materialize_list_of_entity_properties()
{
using (var context = new ArubaContext())
{
var query = context.Runs.Where(r => r.Id == 1).Select(r => new List<int> { r.Id, r.RunOwner.Id });
// materializing results
var results = query.ToList();
var runId = context.Runs.Select(r => r.Id).First(r => r == 1);
var ownerId = context.Runs.Select(r => r.RunOwner.Id).First(r => r == 1);
Assert.Equal(1, results.Count);
Assert.Equal(2, results[0].Count);
Assert.Equal(runId, results[0][0]);
Assert.Equal(ownerId, results[0][1]);
}
}
[Fact]
public void Materialize_array_of_entity_properties_throws()
{
using (var context = new ArubaContext())
{
var query = context.Runs.Where(r => r.Id == 1).Select(r => new int[] { r.Id, r.RunOwner.Id });
var innerException = Assert.Throws<TargetInvocationException>(() => query.ToList())
.InnerException;
Assert.IsType<InvalidOperationException>(innerException);
innerException.ValidateMessage(
typeof(DbContext).Assembly,
"ObjectQuery_UnableToMaterializeArray",
null,
"System.Int32[]",
"System.Collections.Generic.List`1[System.Int32]");
}
}
[Fact]
public void Materializing_empty_list_throws()
{
using (var context = new ArubaContext())
{
var query = context.Runs.Select(r => new List<int> { });
Assert.Throws<NotSupportedException>(() => query.ToList()).
ValidateMessage(
typeof(DbContext).Assembly,
"ELinq_UnsupportedEnumerableType",
null,
"System.Collections.Generic.List`1");
}
}
[Fact]
public void Can_materialize_list_inside_anonymous_type()
{
using (var context = new ArubaContext())
{
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]);
}
}
[Fact]
public void Can_materialize_collection()
{
using (var context = new ArubaContext())
{
var query = context.Failures.Where(f => f.Id == 1).Select(f => 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].Count);
}
}
[Fact]
public void Can_materialize_collection_inside_anonymous_type()
{
using (var context = new ArubaContext())
{
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);
}
}
[Fact]
public void Can_materialize_properties_into_non_mapped_type()
{
using (var context = new ArubaContext())
{
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);
}
}
[Fact]
public void Can_materialize_nested_anonymous_types()
{
using (var context = new ArubaContext())
{
var query = context.Owners.Where(o => o.Id == 1).Select(o => new
{
o.Id,
Name = new { First = o.FirstName, Last = o.LastName }
});
var results = query.ToList();
var id = context.Owners.Select(o => o.Id).First(r => r == 1);
var firstName = context.Owners.Where(o => o.Id == 1).Select(o => o.FirstName).First();
var lastName = context.Owners.Where(o => o.Id == 1).Select(o => o.LastName).First();
Assert.Equal(1, results.Count);
Assert.Equal(id, results[0].Id);
Assert.Equal(firstName, results[0].Name.First);
Assert.Equal(lastName, results[0].Name.Last);
}
}
[Fact]
public void Can_materialize_null_complex_type()
{
using (var context = new ArubaContext())
{
var query = context.Runs.Select(r => r.Tasks.Where(t => t.Id < 0).Select(t => t.TaskInfo).FirstOrDefault());
var results = query.ToList();
Assert.IsType<List<ArubaTaskInfo>>(results);
foreach (var result in results)
{
Assert.Null(result);
}
}
}
[Fact]
public void Materialize_DbSet_throws()
{
using (var context = new ArubaContext())
{
Assert.Throws<ArgumentException>(() => context.Runs.Select(r => context.Owners));
context.Runs.Select(r => context.Owners.AsEnumerable()).ToList();
}
}
[Fact]
public void Materialize_ObjectQuery_throws()
{
using(var context = new ArubaContext())
{
var objectContext = ((IObjectContextAdapter)context).ObjectContext;
var owners = objectContext.CreateQuery<ArubaOwner>("Owners");
Assert.Throws<InvalidOperationException>(() => context.Runs.Select(r => owners).ToList());
context.Runs.Select(r => owners.AsEnumerable()).ToList();
}
}
public class MyNonMappedType
{
public int Id { get; set; }
public string Name { get; set; }
}
}
}
|