From c24c27233df50144e6c7fa6c4272c00dba05be47 Mon Sep 17 00:00:00 2001 From: rowanmiller Date: Mon, 12 May 2014 11:47:41 -0700 Subject: [PATCH] First cut of testing extensions Adds async enumerable helper classes to EntityFramework.Testing Adds a MockDebSet to EntityFramework.Testing.Moq and the ability to specify seed data and setup LINQ --- .nuget/packages.config | 4 + BuildPackages.cmd | 2 + EntityFramework.Testing.sln | 48 +++++ src/EntityFramework.Testing.Moq/App.config | 13 ++ .../EntityFramework.Testing.Moq.csproj | 77 ++++++++ .../EntityFramework.Testing.Moq.nuspec | 19 ++ .../MockDbSetExtenstions.cs | 53 +++++ src/EntityFramework.Testing.Moq/MockDbSet`.cs | 37 ++++ .../Properties/AssemblyInfo.cs | 36 ++++ .../packages.config | 5 + src/EntityFramework.Testing/App.config | 13 ++ .../EntityFramework.Testing.csproj | 69 +++++++ .../EntityFramework.Testing.nuspec | 16 ++ .../Properties/AssemblyInfo.cs | 36 ++++ .../TestDbAsyncEnumerable`.cs | 34 ++++ .../TestDbAsyncEnumerator`.cs | 37 ++++ .../TestDbAsyncQueryProvider`.cs | 48 +++++ src/EntityFramework.Testing/packages.config | 4 + .../App.config | 13 ++ .../EntityFramework.Testing.Moq.Tests.csproj | 108 ++++++++++ .../FakeDbSetDataTests.cs | 32 +++ .../FakeDbSetLinqTests.cs | 187 ++++++++++++++++++ .../Properties/AssemblyInfo.cs | 36 ++++ .../packages.config | 5 + 24 files changed, 932 insertions(+) create mode 100644 .nuget/packages.config create mode 100644 BuildPackages.cmd create mode 100644 EntityFramework.Testing.sln create mode 100644 src/EntityFramework.Testing.Moq/App.config create mode 100644 src/EntityFramework.Testing.Moq/EntityFramework.Testing.Moq.csproj create mode 100644 src/EntityFramework.Testing.Moq/EntityFramework.Testing.Moq.nuspec create mode 100644 src/EntityFramework.Testing.Moq/MockDbSetExtenstions.cs create mode 100644 src/EntityFramework.Testing.Moq/MockDbSet`.cs create mode 100644 src/EntityFramework.Testing.Moq/Properties/AssemblyInfo.cs create mode 100644 src/EntityFramework.Testing.Moq/packages.config create mode 100644 src/EntityFramework.Testing/App.config create mode 100644 src/EntityFramework.Testing/EntityFramework.Testing.csproj create mode 100644 src/EntityFramework.Testing/EntityFramework.Testing.nuspec create mode 100644 src/EntityFramework.Testing/Properties/AssemblyInfo.cs create mode 100644 src/EntityFramework.Testing/TestDbAsyncEnumerable`.cs create mode 100644 src/EntityFramework.Testing/TestDbAsyncEnumerator`.cs create mode 100644 src/EntityFramework.Testing/TestDbAsyncQueryProvider`.cs create mode 100644 src/EntityFramework.Testing/packages.config create mode 100644 test/EntityFramework.Testing.Moq.Tests/App.config create mode 100644 test/EntityFramework.Testing.Moq.Tests/EntityFramework.Testing.Moq.Tests.csproj create mode 100644 test/EntityFramework.Testing.Moq.Tests/FakeDbSetDataTests.cs create mode 100644 test/EntityFramework.Testing.Moq.Tests/FakeDbSetLinqTests.cs create mode 100644 test/EntityFramework.Testing.Moq.Tests/Properties/AssemblyInfo.cs create mode 100644 test/EntityFramework.Testing.Moq.Tests/packages.config diff --git a/.nuget/packages.config b/.nuget/packages.config new file mode 100644 index 0000000..6c81016 --- /dev/null +++ b/.nuget/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/BuildPackages.cmd b/BuildPackages.cmd new file mode 100644 index 0000000..f47b7ce --- /dev/null +++ b/BuildPackages.cmd @@ -0,0 +1,2 @@ +packages\NuGet.CommandLine.2.8.1\tools\NuGet.exe pack src\EntityFramework.Testing\EntityFramework.Testing.csproj +packages\NuGet.CommandLine.2.8.1\tools\NuGet.exe pack src\EntityFramework.Testing.Moq\EntityFramework.Testing.Moq.csproj \ No newline at end of file diff --git a/EntityFramework.Testing.sln b/EntityFramework.Testing.sln new file mode 100644 index 0000000..beb231b --- /dev/null +++ b/EntityFramework.Testing.sln @@ -0,0 +1,48 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.30324.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFramework.Testing", "src\EntityFramework.Testing\EntityFramework.Testing.csproj", "{4351E4D4-C68C-4CF6-998B-AB56D8A3760D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFramework.Testing.Moq", "src\EntityFramework.Testing.Moq\EntityFramework.Testing.Moq.csproj", "{13B2C70C-A084-44AF-8F75-975D84438DEE}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{56EF6CEF-D305-42B5-B0FC-1528B670548E}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{7532A630-3026-4E1A-9E5C-13611C22C5F6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFramework.Testing.Moq.Tests", "test\EntityFramework.Testing.Moq.Tests\EntityFramework.Testing.Moq.Tests.csproj", "{1F55F581-F0D8-423D-A514-636C161ACDB4}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{A5D7A40E-6C6E-4119-A6FA-861C176A3F2A}" + ProjectSection(SolutionItems) = preProject + .nuget\packages.config = .nuget\packages.config + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4351E4D4-C68C-4CF6-998B-AB56D8A3760D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4351E4D4-C68C-4CF6-998B-AB56D8A3760D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4351E4D4-C68C-4CF6-998B-AB56D8A3760D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4351E4D4-C68C-4CF6-998B-AB56D8A3760D}.Release|Any CPU.Build.0 = Release|Any CPU + {13B2C70C-A084-44AF-8F75-975D84438DEE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {13B2C70C-A084-44AF-8F75-975D84438DEE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {13B2C70C-A084-44AF-8F75-975D84438DEE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {13B2C70C-A084-44AF-8F75-975D84438DEE}.Release|Any CPU.Build.0 = Release|Any CPU + {1F55F581-F0D8-423D-A514-636C161ACDB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1F55F581-F0D8-423D-A514-636C161ACDB4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1F55F581-F0D8-423D-A514-636C161ACDB4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1F55F581-F0D8-423D-A514-636C161ACDB4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {4351E4D4-C68C-4CF6-998B-AB56D8A3760D} = {56EF6CEF-D305-42B5-B0FC-1528B670548E} + {13B2C70C-A084-44AF-8F75-975D84438DEE} = {56EF6CEF-D305-42B5-B0FC-1528B670548E} + {1F55F581-F0D8-423D-A514-636C161ACDB4} = {7532A630-3026-4E1A-9E5C-13611C22C5F6} + EndGlobalSection +EndGlobal diff --git a/src/EntityFramework.Testing.Moq/App.config b/src/EntityFramework.Testing.Moq/App.config new file mode 100644 index 0000000..2fb423e --- /dev/null +++ b/src/EntityFramework.Testing.Moq/App.config @@ -0,0 +1,13 @@ + + + + +
+ + + + + + + + \ No newline at end of file diff --git a/src/EntityFramework.Testing.Moq/EntityFramework.Testing.Moq.csproj b/src/EntityFramework.Testing.Moq/EntityFramework.Testing.Moq.csproj new file mode 100644 index 0000000..8f7dba7 --- /dev/null +++ b/src/EntityFramework.Testing.Moq/EntityFramework.Testing.Moq.csproj @@ -0,0 +1,77 @@ + + + + + Debug + AnyCPU + {13B2C70C-A084-44AF-8F75-975D84438DEE} + Library + Properties + EntityFramework.Testing.Moq + EntityFramework.Testing.Moq + v4.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + False + ..\..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll + + + False + ..\..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll + + + ..\packages\Moq.4.2.1402.2112\lib\net40\Moq.dll + + + + + + + + + + + + + + + + + + + + + + + {4351e4d4-c68c-4cf6-998b-ab56d8a3760d} + EntityFramework.Testing + + + + + \ No newline at end of file diff --git a/src/EntityFramework.Testing.Moq/EntityFramework.Testing.Moq.nuspec b/src/EntityFramework.Testing.Moq/EntityFramework.Testing.Moq.nuspec new file mode 100644 index 0000000..79fd874 --- /dev/null +++ b/src/EntityFramework.Testing.Moq/EntityFramework.Testing.Moq.nuspec @@ -0,0 +1,19 @@ + + + + $id$ + 1.0.0-apha1 + $title$ + $author$ + $author$ + https://github.com/rowanmiller/EntityFramework.Testing/blob/master/LICENSE + https://github.com/rowanmiller/EntityFramework.Testing + false + $description$ + Copyright 2014 + Entity Framework EF EF6 Testing + + + + + \ No newline at end of file diff --git a/src/EntityFramework.Testing.Moq/MockDbSetExtenstions.cs b/src/EntityFramework.Testing.Moq/MockDbSetExtenstions.cs new file mode 100644 index 0000000..1fed4a9 --- /dev/null +++ b/src/EntityFramework.Testing.Moq/MockDbSetExtenstions.cs @@ -0,0 +1,53 @@ +using Moq; +using System.Collections.Generic; +using System.Data.Entity.Infrastructure; +using System.Linq; + +namespace EntityFramework.Testing.Moq +{ + public static class MockDbSetExtenstions + { + public static MockDbSet SetupSeedData( + this MockDbSet set, + IEnumerable data) + where TEntity : class + { + set.AddData(data); + + // Need to re-setup LINQ if the data changes + if(set.IsLinqSetup) + { + set.SetupLinq(); + } + + return set; + } + + public static MockDbSet SetupLinq(this MockDbSet set) + where TEntity : class + { + // Record so that we can re-setup linq if the data is changed + set.IsLinqSetup = true; + + // Enable direct async enumeration of set + set.As>() + .Setup(m => m.GetAsyncEnumerator()) + .Returns(new TestDbAsyncEnumerator(set.Queryable.GetEnumerator())); + + // Enable LINQ queries with async enumeration + set.As>() + .Setup(m => m.Provider) + .Returns(new TestDbAsyncQueryProvider(set.Queryable.Provider)); + + // Wire up LINQ provider to fall back to in memory LINQ provider of the data + set.As>().Setup(m => m.Expression).Returns(set.Queryable.Expression); + set.As>().Setup(m => m.ElementType).Returns(set.Queryable.ElementType); + set.As>().Setup(m => m.GetEnumerator()).Returns(set.Queryable.GetEnumerator()); + + // Enable Include directly on the DbSet (Include extension method on IQueryable is a no-op when it's not a DbSet/DbQuery) + // Include(string) and Include(Func s.Include(It.IsAny())).Returns(set.Object); + return set; + } + } +} diff --git a/src/EntityFramework.Testing.Moq/MockDbSet`.cs b/src/EntityFramework.Testing.Moq/MockDbSet`.cs new file mode 100644 index 0000000..fd78dcc --- /dev/null +++ b/src/EntityFramework.Testing.Moq/MockDbSet`.cs @@ -0,0 +1,37 @@ +using Moq; +using System.Collections.Generic; +using System.Data.Entity; +using System.Linq; + +namespace EntityFramework.Testing.Moq +{ + public class MockDbSet : Mock> + where TEntity : class + { + private IQueryable _queryable; + private List _data; + + public MockDbSet() + { + _data = new List(); + _queryable = _data.AsQueryable(); + } + + public IEnumerable Data + { + get { return _data; } + } + + internal bool IsLinqSetup { get; set; } + + internal IQueryable Queryable + { + get { return _queryable; } + } + + internal void AddData(IEnumerable data) + { + _data.AddRange(data); + } + } +} diff --git a/src/EntityFramework.Testing.Moq/Properties/AssemblyInfo.cs b/src/EntityFramework.Testing.Moq/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..0c1b3d0 --- /dev/null +++ b/src/EntityFramework.Testing.Moq/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("EntityFramework.Testing.Moq")] +[assembly: AssemblyDescription("Testing helpers for using Moq with Entity Framework")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Rowan Miller")] +[assembly: AssemblyProduct("EntityFramework.Testing.Moq")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("43b16b3a-167f-4961-84dc-3545ad860de8")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/EntityFramework.Testing.Moq/packages.config b/src/EntityFramework.Testing.Moq/packages.config new file mode 100644 index 0000000..896927f --- /dev/null +++ b/src/EntityFramework.Testing.Moq/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/src/EntityFramework.Testing/App.config b/src/EntityFramework.Testing/App.config new file mode 100644 index 0000000..2fb423e --- /dev/null +++ b/src/EntityFramework.Testing/App.config @@ -0,0 +1,13 @@ + + + + +
+ + + + + + + + \ No newline at end of file diff --git a/src/EntityFramework.Testing/EntityFramework.Testing.csproj b/src/EntityFramework.Testing/EntityFramework.Testing.csproj new file mode 100644 index 0000000..7fbcc47 --- /dev/null +++ b/src/EntityFramework.Testing/EntityFramework.Testing.csproj @@ -0,0 +1,69 @@ + + + + + Debug + AnyCPU + {4351E4D4-C68C-4CF6-998B-AB56D8A3760D} + Library + Properties + EntityFramework.Testing + EntityFramework.Testing + v4.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + False + ..\..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll + + + False + ..\..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/EntityFramework.Testing/EntityFramework.Testing.nuspec b/src/EntityFramework.Testing/EntityFramework.Testing.nuspec new file mode 100644 index 0000000..7b6e0a4 --- /dev/null +++ b/src/EntityFramework.Testing/EntityFramework.Testing.nuspec @@ -0,0 +1,16 @@ + + + + $id$ + 1.0.0-alpha1 + $title$ + $author$ + $author$ + https://github.com/rowanmiller/EntityFramework.Testing/blob/master/LICENSE + https://github.com/rowanmiller/EntityFramework.Testing + false + $description$ + Copyright 2014 + Entity Framework EF EF6 Testing + + \ No newline at end of file diff --git a/src/EntityFramework.Testing/Properties/AssemblyInfo.cs b/src/EntityFramework.Testing/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..3ed28f2 --- /dev/null +++ b/src/EntityFramework.Testing/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("EntityFramework.Testing")] +[assembly: AssemblyDescription("Testing helpers for Entity Framework")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Rowan Miller")] +[assembly: AssemblyProduct("EntityFramework.Testing")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("0ab6adbd-153e-4e33-bae3-920e54a9766a")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/EntityFramework.Testing/TestDbAsyncEnumerable`.cs b/src/EntityFramework.Testing/TestDbAsyncEnumerable`.cs new file mode 100644 index 0000000..373b4aa --- /dev/null +++ b/src/EntityFramework.Testing/TestDbAsyncEnumerable`.cs @@ -0,0 +1,34 @@ +using System.Collections.Generic; +using System.Data.Entity.Infrastructure; +using System.Linq; +using System.Linq.Expressions; + +namespace EntityFramework.Testing +{ + public class TestDbAsyncEnumerable : EnumerableQuery, IDbAsyncEnumerable, IQueryable + { + public TestDbAsyncEnumerable(IEnumerable enumerable) + : base(enumerable) + { } + + public TestDbAsyncEnumerable(Expression expression) + : base(expression) + { } + + public IDbAsyncEnumerator GetAsyncEnumerator() + { + return new TestDbAsyncEnumerator(this.AsEnumerable().GetEnumerator()); + } + + IDbAsyncEnumerator IDbAsyncEnumerable.GetAsyncEnumerator() + { + return GetAsyncEnumerator(); + } + + IQueryProvider IQueryable.Provider + { + get { return new TestDbAsyncQueryProvider(this); } + } + } + +} diff --git a/src/EntityFramework.Testing/TestDbAsyncEnumerator`.cs b/src/EntityFramework.Testing/TestDbAsyncEnumerator`.cs new file mode 100644 index 0000000..a5ffc44 --- /dev/null +++ b/src/EntityFramework.Testing/TestDbAsyncEnumerator`.cs @@ -0,0 +1,37 @@ +using System.Collections.Generic; +using System.Data.Entity.Infrastructure; +using System.Threading; +using System.Threading.Tasks; + +namespace EntityFramework.Testing +{ + public class TestDbAsyncEnumerator : IDbAsyncEnumerator + { + private readonly IEnumerator _inner; + + public TestDbAsyncEnumerator(IEnumerator inner) + { + _inner = inner; + } + + public void Dispose() + { + _inner.Dispose(); + } + + public Task MoveNextAsync(CancellationToken cancellationToken) + { + return Task.FromResult(_inner.MoveNext()); + } + + public T Current + { + get { return _inner.Current; } + } + + object IDbAsyncEnumerator.Current + { + get { return Current; } + } + } +} diff --git a/src/EntityFramework.Testing/TestDbAsyncQueryProvider`.cs b/src/EntityFramework.Testing/TestDbAsyncQueryProvider`.cs new file mode 100644 index 0000000..b01cbc0 --- /dev/null +++ b/src/EntityFramework.Testing/TestDbAsyncQueryProvider`.cs @@ -0,0 +1,48 @@ +using System.Data.Entity.Infrastructure; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; + +namespace EntityFramework.Testing +{ + public class TestDbAsyncQueryProvider : IDbAsyncQueryProvider + { + private readonly IQueryProvider _inner; + + public TestDbAsyncQueryProvider(IQueryProvider inner) + { + _inner = inner; + } + + public IQueryable CreateQuery(Expression expression) + { + return new TestDbAsyncEnumerable(expression); + } + + public IQueryable CreateQuery(Expression expression) + { + return new TestDbAsyncEnumerable(expression); + } + + public object Execute(Expression expression) + { + return _inner.Execute(expression); + } + + public TResult Execute(Expression expression) + { + return _inner.Execute(expression); + } + + public Task ExecuteAsync(Expression expression, CancellationToken cancellationToken) + { + return Task.FromResult(Execute(expression)); + } + + public Task ExecuteAsync(Expression expression, CancellationToken cancellationToken) + { + return Task.FromResult(Execute(expression)); + } + } +} \ No newline at end of file diff --git a/src/EntityFramework.Testing/packages.config b/src/EntityFramework.Testing/packages.config new file mode 100644 index 0000000..7256e34 --- /dev/null +++ b/src/EntityFramework.Testing/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/test/EntityFramework.Testing.Moq.Tests/App.config b/test/EntityFramework.Testing.Moq.Tests/App.config new file mode 100644 index 0000000..2fb423e --- /dev/null +++ b/test/EntityFramework.Testing.Moq.Tests/App.config @@ -0,0 +1,13 @@ + + + + +
+ + + + + + + + \ No newline at end of file diff --git a/test/EntityFramework.Testing.Moq.Tests/EntityFramework.Testing.Moq.Tests.csproj b/test/EntityFramework.Testing.Moq.Tests/EntityFramework.Testing.Moq.Tests.csproj new file mode 100644 index 0000000..49d6fff --- /dev/null +++ b/test/EntityFramework.Testing.Moq.Tests/EntityFramework.Testing.Moq.Tests.csproj @@ -0,0 +1,108 @@ + + + + Debug + AnyCPU + {1F55F581-F0D8-423D-A514-636C161ACDB4} + Library + Properties + EntityFramework.Testing.Moq.Tests + EntityFramework.Testing.Moq.Tests + v4.5 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll + + + ..\..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll + + + ..\..\packages\Moq.4.2.1402.2112\lib\net40\Moq.dll + + + + + + + + + + + + + + + + + + + + + + + + + + + + {13b2c70c-a084-44af-8f75-975d84438dee} + EntityFramework.Testing.Moq + + + {4351e4d4-c68c-4cf6-998b-ab56d8a3760d} + EntityFramework.Testing + + + + + + + False + + + False + + + False + + + False + + + + + + + + \ No newline at end of file diff --git a/test/EntityFramework.Testing.Moq.Tests/FakeDbSetDataTests.cs b/test/EntityFramework.Testing.Moq.Tests/FakeDbSetDataTests.cs new file mode 100644 index 0000000..101e96d --- /dev/null +++ b/test/EntityFramework.Testing.Moq.Tests/FakeDbSetDataTests.cs @@ -0,0 +1,32 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EntityFramework.Testing.Moq.Tests +{ + [TestClass] + public class FakeDbSetDataTests + { + public void Data_is_addded_to_set() + { + var data = new List { new Blog(), new Blog() }; + + var set = new MockDbSet() + .SetupSeedData(data); + + var result = set.Data.ToArray(); + Assert.AreEqual(2, result.Length); + Assert.AreSame(data[0], result[0]); + Assert.AreSame(data[1], result[1]); + } + + public class Blog + { + public int BlogId { get; set; } + public string Url { get; set; } + } + } +} diff --git a/test/EntityFramework.Testing.Moq.Tests/FakeDbSetLinqTests.cs b/test/EntityFramework.Testing.Moq.Tests/FakeDbSetLinqTests.cs new file mode 100644 index 0000000..1082bbc --- /dev/null +++ b/test/EntityFramework.Testing.Moq.Tests/FakeDbSetLinqTests.cs @@ -0,0 +1,187 @@ +using System; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace EntityFramework.Testing.Moq.Tests +{ + [TestClass] + public class FakeDbSetLinqTests + { + [TestMethod] + public void Can_enumerate_set() + { + var data = new List { new Blog {}, new Blog {} }; + + var set = new MockDbSet() + .SetupSeedData(data) + .SetupLinq(); + + var count = 0; + foreach (var item in set.Object) + { + count++; + } + + Assert.AreEqual(2, count); + } + + [TestMethod] + public async Task Can_enumerate_set_async() + { + var data = new List { new Blog(), new Blog() }; + + var set = new MockDbSet() + .SetupSeedData(data) + .SetupLinq(); + + var count = 0; + await set.Object.ForEachAsync(b => count++); + + Assert.AreEqual(2, count); + } + + [TestMethod] + public void Can_use_linq_materializer_directly_on_set() + { + var data = new List { new Blog(), new Blog() }; + + var set = new MockDbSet() + .SetupSeedData(data) + .SetupLinq(); + + var result = set.Object.ToList(); + + Assert.AreEqual(2, result.Count); + } + + [TestMethod] + public async Task Can_use_linq_materializer_directly_on_set_async() + { + var data = new List { new Blog(), new Blog() }; + + var set = new MockDbSet() + .SetupSeedData(data) + .SetupLinq(); + + var result = await set.Object.ToListAsync(); + + Assert.AreEqual(2, result.Count); + } + + [TestMethod] + public void Can_use_linq_opeartors() + { + var data = new List + { + new Blog { BlogId = 1 }, + new Blog { BlogId = 2 }, + new Blog { BlogId = 3} + }; + + var set = new MockDbSet() + .SetupSeedData(data) + .SetupLinq(); + + var result = set.Object + .Where(b => b.BlogId > 1) + .OrderByDescending(b => b.BlogId) + .ToList(); + + Assert.AreEqual(2, result.Count); + Assert.AreEqual(3, result[0].BlogId); + Assert.AreEqual(2, result[1].BlogId); + } + + [TestMethod] + public async Task Can_use_linq_opeartors_async() + { + var data = new List + { + new Blog { BlogId = 1 }, + new Blog { BlogId = 2 }, + new Blog { BlogId = 3} + }; + + var set = new MockDbSet() + .SetupSeedData(data) + .SetupLinq(); + + var result = await set.Object + .Where(b => b.BlogId > 1) + .OrderByDescending(b => b.BlogId) + .ToListAsync(); + + Assert.AreEqual(2, result.Count); + Assert.AreEqual(3, result[0].BlogId); + Assert.AreEqual(2, result[1].BlogId); + } + + [TestMethod] + public void Can_use_include_directly_on_set() + { + var data = new List { new Blog(), new Blog() }; + + var set = new MockDbSet() + .SetupSeedData(data) + .SetupLinq(); + + var result = set.Object + .Include(b => b.Posts) + .ToList(); + + Assert.AreEqual(2, result.Count); + } + + [TestMethod] + public void Can_use_include_after_linq_operator() + { + var data = new List { new Blog(), new Blog() }; + + var set = new MockDbSet() + .SetupSeedData(data) + .SetupLinq(); + + var result = set.Object + .OrderBy(b => b.BlogId) + .Include(b => b.Posts) + .ToList(); + + Assert.AreEqual(2, result.Count); + } + + [TestMethod] + public void Can_add_data_after_setting_up_linq() + { + var data = new List { new Blog(), new Blog() }; + + var set = new MockDbSet() + .SetupLinq() + .SetupSeedData(data); + + var result = set.Object.ToList(); + + Assert.AreEqual(2, result.Count); + } + + public class Blog + { + public int BlogId { get; set; } + public string Url { get; set; } + + public List Posts { get; set; } + } + + public class Post + { + public int PostId { get; set; } + public string Title { get; set; } + public string Content { get; set; } + + public int BlogId { get; set; } + public Blog Blog { get; set; } + } + } +} diff --git a/test/EntityFramework.Testing.Moq.Tests/Properties/AssemblyInfo.cs b/test/EntityFramework.Testing.Moq.Tests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..ea87935 --- /dev/null +++ b/test/EntityFramework.Testing.Moq.Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("EntityFramework.Testing.Moq.Tests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("EntityFramework.Testing.Moq.Tests")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("45fe240b-c6b8-47d1-bb06-4e8b27c7f625")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/test/EntityFramework.Testing.Moq.Tests/packages.config b/test/EntityFramework.Testing.Moq.Tests/packages.config new file mode 100644 index 0000000..896927f --- /dev/null +++ b/test/EntityFramework.Testing.Moq.Tests/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file