forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBulkInsertTest.cs
More file actions
141 lines (128 loc) · 6.2 KB
/
BulkInsertTest.cs
File metadata and controls
141 lines (128 loc) · 6.2 KB
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
namespace Simple.Data.IntegrationTest
{
using System;
using System.Dynamic;
using Mocking.Ado;
using NUnit.Framework;
[TestFixture]
public class BulkInsertTest : DatabaseIntegrationContext
{
protected override void SetSchema(MockSchemaProvider schemaProvider)
{
schemaProvider.SetTables(new[] { "dbo", "Users", "BASE TABLE" },
new[] { "dbo", "NoIdentityColumnUsers", "BASE TABLE" },
new[] { "foo", "Users", "BASE TABLE" });
schemaProvider.SetColumns(new object[] { "dbo", "Users", "Id", true },
new[] { "dbo", "Users", "Name" },
new[] { "dbo", "Users", "Password" },
new[] { "dbo", "Users", "Age" },
new object[] { "foo", "Users", "Id", true },
new[] { "foo", "Users", "Name" },
new[] { "foo", "Users", "Password" },
new[] { "foo", "Users", "Age" },
new[] { "dbo", "NoIdentityColumnUsers", "Id" },
new[] { "dbo", "NoIdentityColumnUsers", "Name" },
new[] { "dbo", "NoIdentityColumnUsers", "Password" },
new[] { "dbo", "NoIdentityColumnUsers", "Age" });
schemaProvider.SetPrimaryKeys(new object[] { "dbo", "Users", "Id", 0 });
}
[Test]
public void TestBulkInsertWithStaticTypeObjectAndIdentityColumn()
{
var users = new[] { new User { Name = "Steve", Age = 50 }, new User { Name = "Phil", Age = 42 }};
_db.Users.Insert(users);
GeneratedSqlIs("insert into [dbo].[Users] ([Name],[Password],[Age]) values (@p0,@p1,@p2)");
Parameter(0).Is("Phil");
Parameter(1).Is(DBNull.Value);
Parameter(2).Is(42);
}
[Test]
public void TestBulkInsertWithStaticTypeObjectAndIdentityColumnOnSchemaQualifiedTable()
{
var users = new[] { new User { Name = "Steve", Age = 50 }, new User { Name = "Phil", Age = 42 }};
_db.foo.Users.Insert(users);
GeneratedSqlIs("insert into [foo].[Users] ([Name],[Password],[Age]) values (@p0,@p1,@p2)");
Parameter(0).Is("Phil");
Parameter(1).Is(DBNull.Value);
Parameter(2).Is(42);
}
[Test]
public void TestBulkInsertWithStaticTypeObjectAndIdentityColumnAndIdentityFunctionThatExpectsAValueSelects()
{
_MockConnectionProvider.SetIdentityFunction("@@IDENTITY");
var users = new[] { new User { Name = "Steve", Age = 50 }, new User { Name = "Phil", Age = 42 }};
var inserted = _db.Users.Insert(users);
GeneratedSqlIs("insert into [dbo].[Users] ([Name],[Password],[Age]) values (@p0,@p1,@p2); select * from [dbo].[users] where [id] = @@identity");
Parameter(0).Is("Phil");
Parameter(1).Is(DBNull.Value);
Parameter(2).Is(42);
_MockConnectionProvider.SetIdentityFunction(null);
}
[Test]
public void TestBulkInsertWithStaticTypeObjectAndIdentityColumnAndIdentityFunctionThatDoesNotExpectAValueDoesNotSelect()
{
_MockConnectionProvider.SetIdentityFunction("@@IDENTITY");
var users = new[] { new User { Name = "Steve", Age = 50 }, new User { Name = "Phil", Age = 42 }};
_db.Users.Insert(users);
GeneratedSqlIs("insert into [dbo].[Users] ([Name],[Password],[Age]) values (@p0,@p1,@p2)");
Parameter(0).Is("Phil");
Parameter(1).Is(DBNull.Value);
Parameter(2).Is(42);
_MockConnectionProvider.SetIdentityFunction(null);
}
[Test]
public void TestBulkInsertWithDynamicObjectAndIdentityColumn()
{
dynamic steve = new ExpandoObject();
steve.Name = "Steve";
steve.Age = 50;
dynamic phil = new ExpandoObject();
phil.Name = "Phil";
phil.Age = 42;
_db.Users.Insert(new[] {steve,phil});
GeneratedSqlIs("insert into [dbo].[Users] ([Name],[Password],[Age]) values (@p0,@p1,@p2)");
Parameter(0).Is("Phil");
Parameter(1).Is(DBNull.Value);
Parameter(2).Is(42);
}
[Test]
public void TestBulkInsertWithStaticTypeObjectAndNoIdentityColumn()
{
var users = new[] { new User { Id = 1, Name = "Steve", Age = 50 }, new User { Id = 2, Name = "Phil", Age = 42 } };
_db.NoIdentityColumnUsers.Insert(users);
GeneratedSqlIs("insert into [dbo].[NoIdentityColumnUsers] ([Id],[Name],[Password],[Age]) values (@p0,@p1,@p2,@p3)");
Parameter(0).Is(2);
Parameter(1).Is("Phil");
Parameter(2).Is(DBNull.Value);
Parameter(3).Is(42);
}
[Test]
public void TestBulkInsertWithDynamicObjectAndNoIdentityColumn()
{
dynamic steve = new ExpandoObject();
steve.Id = 1;
steve.Name = "Steve";
steve.Age = 50;
dynamic phil = new ExpandoObject();
phil.Id = 2;
phil.Name = "Phil";
phil.Age = 42;
_db.NoIdentityColumnUsers.Insert(new[] { steve, phil });
GeneratedSqlIs("insert into [dbo].[NoIdentityColumnUsers] ([Id],[Name],[Password],[Age]) values (@p0,@p1,@p2,@p3)");
Parameter(0).Is(2);
Parameter(1).Is("Phil");
Parameter(2).Is(DBNull.Value);
Parameter(3).Is(42);
}
[Test]
// ReSharper disable InconsistentNaming
public void TestThatInsertUsesDBNull()
// ReSharper restore InconsistentNaming
{
dynamic person = new ExpandoObject();
person.Name = null;
_db.Users.Insert(person);
Parameter(0).Is(DBNull.Value);
}
}
}