-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityFrameworkDatabaseInitializerExtensionsTest.cs
108 lines (81 loc) · 3.95 KB
/
EntityFrameworkDatabaseInitializerExtensionsTest.cs
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
//-----------------------------------------------------------------------
// <copyright file="EntityFrameworkDatabaseInitializerExtensionsTest.cs" company="P.O.S Informatique">
// Copyright (c) P.O.S Informatique. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace PosInformatique.Testing.Databases.SqlServer.Tests
{
using Microsoft.EntityFrameworkCore;
[Collection("PosInformatique.Testing.Databases.SqlServer.Tests")]
public class EntityFrameworkDatabaseInitializerExtensionsTest : IClassFixture<SqlServerDatabaseInitializer>
{
private const string ConnectionString = $"Data Source=(localDB)\\posinfo-tests; Initial Catalog={nameof(EntityFrameworkDatabaseInitializerExtensionsTest)}; Integrated Security=True";
private readonly SqlServerDatabase database;
public EntityFrameworkDatabaseInitializerExtensionsTest(SqlServerDatabaseInitializer initializer)
{
var optionsBuilder = new DbContextOptionsBuilder<DbContextTest>()
.UseSqlServer(ConnectionString);
using var context = new DbContextTest(optionsBuilder.Options);
this.database = initializer.Initialize(context);
var table = this.database.ExecuteQuery("SELECT * FROM MyTable");
table.Rows.Should().BeEmpty();
this.database.InsertInto("MyTable", new { Id = 1, Name = "Name 1" });
this.database.InsertInto("MyTable", new { Id = 2, Name = "Name 2" });
}
[Fact]
public void Test1()
{
var currentUser = this.database.ExecuteQuery("SELECT SUSER_NAME()");
currentUser.Rows[0][0].Should().Be($"{Environment.UserDomainName}\\{Environment.UserName}");
// Check the constructor has been called
var table = this.database.ExecuteQuery("SELECT * FROM MyTable");
table.Rows.Should().HaveCount(2);
table.Rows[0]["Id"].Should().Be(1);
table.Rows[0]["Name"].Should().Be("Name 1");
table.Rows[1]["Id"].Should().Be(2);
table.Rows[1]["Name"].Should().Be("Name 2");
// Insert a row which should not be use in other tests.
this.database.InsertInto("MyTable", new { Id = 99, Name = "Should not be here for the next test" });
}
[Fact]
public void Test2()
{
var currentUser = this.database.ExecuteQuery("SELECT SUSER_NAME()");
currentUser.Rows[0][0].Should().Be($"{Environment.UserDomainName}\\{Environment.UserName}");
// Check the constructor has been called
var table = this.database.ExecuteQuery("SELECT * FROM MyTable");
table.Rows.Should().HaveCount(2);
table.Rows[0]["Id"].Should().Be(1);
table.Rows[0]["Name"].Should().Be("Name 1");
table.Rows[1]["Id"].Should().Be(2);
table.Rows[1]["Name"].Should().Be("Name 2");
// Insert a row which should not be use in other tests.
this.database.InsertInto("MyTable", new { Id = 99, Name = "Should not be here for the next test" });
}
private sealed class DbContextTest : DbContext
{
public DbContextTest(DbContextOptions<DbContextTest> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Table>(t =>
{
t.ToTable("MyTable");
t.Property(t => t.Id)
.ValueGeneratedNever();
t.Property(t => t.Name)
.IsRequired()
.HasMaxLength(50)
.IsUnicode(false);
});
}
}
private sealed class Table
{
public int Id { get; set; }
public string Name { get; set; }
}
}
}