Skip to content

Commit 9a4fff2

Browse files
authored
Merge pull request #131 from msallin/#127
#127
2 parents 04f6db1 + 3af12e3 commit 9a4fff2

File tree

5 files changed

+115
-72
lines changed

5 files changed

+115
-72
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,4 @@ FakesAssemblies/
183183

184184
# Custom
185185
*.GhostDoc.xml
186+
/.vs/*
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using SQLite.CodeFirst.Console;
5+
using SQLite.CodeFirst.Console.Entity;
6+
7+
namespace SQLite.CodeFirst.Test.IntegrationTests
8+
{
9+
10+
[TestClass]
11+
public class InMemoryDbCreationTest : InMemoryDbTest<FootballDbContext>
12+
{
13+
[TestMethod]
14+
public void CreateInMemoryDatabaseTest()
15+
{
16+
using (var context = GetDbContext())
17+
{
18+
context.Set<Team>().Add(new Team
19+
{
20+
Name = "New",
21+
Coach = new Coach
22+
{
23+
City = "New",
24+
FirstName = "New",
25+
LastName = "New",
26+
Street = "New"
27+
},
28+
Players = new List<Player>
29+
{
30+
new Player
31+
{
32+
City = "New",
33+
FirstName = "New",
34+
LastName = "New",
35+
Street = "New",
36+
Number = 1
37+
},
38+
new Player
39+
{
40+
City = "New",
41+
FirstName = "New",
42+
LastName = "New",
43+
Street = "New",
44+
Number = 2
45+
}
46+
},
47+
Stadion = new Stadion
48+
{
49+
Name = "New",
50+
City = "New",
51+
Street = "New"
52+
}
53+
});
54+
55+
context.SaveChanges();
56+
}
57+
58+
using (var context = GetDbContext())
59+
{
60+
Assert.AreEqual(1, context.Set<Team>().Count());
61+
}
62+
}
63+
}
64+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Data.Common;
3+
using System.Data.Entity;
4+
using System.Data.SQLite;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
7+
namespace SQLite.CodeFirst.Test.IntegrationTests
8+
{
9+
/// <summary>
10+
/// Inherit this class to create a test which uses a SQLite in memory database.
11+
/// This class provides the necessary logic to run multiple tests again the in memory database in a row.
12+
/// </summary>
13+
public abstract class InMemoryDbTest<TDbContext>
14+
where TDbContext : DbContext
15+
{
16+
private bool dbInitialized;
17+
18+
protected DbConnection Connection { get; private set; }
19+
20+
protected DbContext GetDbContext()
21+
{
22+
TDbContext context = (TDbContext)Activator.CreateInstance(typeof(TDbContext), Connection, false);
23+
if (!dbInitialized)
24+
{
25+
context.Database.Initialize(true);
26+
dbInitialized = true;
27+
}
28+
return context;
29+
}
30+
31+
[TestInitialize]
32+
public void Initialize()
33+
{
34+
Connection = new SQLiteConnection("data source=:memory:");
35+
36+
// This is important! Else the in memory database will not work.
37+
Connection.Open();
38+
39+
dbInitialized = false;
40+
}
41+
42+
[TestCleanup]
43+
public void Cleanup()
44+
{
45+
Connection.Dispose();
46+
}
47+
}
48+
}

SQLite.CodeFirst.Test/IntegrationTests/MemoryDbTest.cs

Lines changed: 0 additions & 71 deletions
This file was deleted.

SQLite.CodeFirst.Test/SQLite.CodeFirst.Test.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@
8686
<Compile Include="..\Shared\AssemblySharedInfo.cs">
8787
<Link>Properties\AssemblySharedInfo.cs</Link>
8888
</Compile>
89+
<Compile Include="IntegrationTests\InMemoryDbCreationTest.cs" />
90+
<Compile Include="IntegrationTests\InMemoryDbTest.cs" />
8991
<Compile Include="IntegrationTests\SqlGenerationTest.cs" />
9092
<Compile Include="UnitTests\Builder\NameCreators\IndexNameCreatorTest.cs" />
9193
<Compile Include="UnitTests\Builder\NameCreators\NameCreatorTest.cs" />
92-
<Compile Include="IntegrationTests\MemoryDbTest.cs" />
9394
<Compile Include="Properties\AssemblyInfo.cs" />
9495
<Compile Include="UnitTests\Statement\ColumnConstraint\MaxLengthConstraint.cs" />
9596
<Compile Include="UnitTests\Statement\ColumnConstraint\PrimaryKeyConstraintTest.cs" />

0 commit comments

Comments
 (0)