File tree Expand file tree Collapse file tree 4 files changed +114
-72
lines changed
Expand file tree Collapse file tree 4 files changed +114
-72
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 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" />
You can’t perform that action at this time.
0 commit comments