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
157 lines (121 loc) · 4.68 KB
/
BulkInsertTest.cs
File metadata and controls
157 lines (121 loc) · 4.68 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using Simple.Data.Ado;
namespace Simple.Data.SqlTest
{
using System.Collections.Generic;
using System.Diagnostics;
using NUnit.Framework;
[TestFixture]
public class BulkInsertTest
{
[TestFixtureSetUp]
public void Setup()
{
DatabaseHelper.Reset();
}
[Test]
public void BulkInsertUsesSchema()
{
var db = DatabaseHelper.Open();
List<dynamic> list;
Promise<int> count;
using (var tx = db.BeginTransaction())
{
tx.test.SchemaTable.DeleteAll();
tx.test.SchemaTable.Insert(GenerateItems());
list = tx.test.SchemaTable.All().WithTotalCount(out count).ToList();
tx.Rollback();
}
Assert.AreEqual(1000, count.Value);
Assert.AreEqual(1000, list.Count);
}
[Test]
public void BulkInsertUsesSchemaAndFireTriggers()
{
var db = DatabaseHelper.Open();
using (var tx = db.BeginTransaction())
{
tx.WithOptions(new AdoOptions(commandTimeout: 60000, fireTriggersOnBulkInserts: true));
tx.test.SchemaTable.DeleteAll();
tx.test.SchemaTable.Insert(GenerateItems());
tx.Commit();
}
int rowsWhichWhereUpdatedByTrigger = db.test.SchemaTable.GetCountBy(Optional: "Modified By Trigger");
Assert.AreEqual(1000, rowsWhichWhereUpdatedByTrigger);
}
[Test]
public void BulkInsertRecordsWithDifferentColumnsProperlyInsertsData()
{
DatabaseHelper.Reset();
var db = DatabaseHelper.Open();
dynamic r1 = new SimpleRecord();
r1.FirstName = "Bob";
r1.LastName = "Dole";
dynamic r2 = new SimpleRecord();
r2.FirstName = "Bob";
r2.MiddleInitial = "L";
r2.LastName = "Saget";
db.OptionalColumnTest.Insert(new[] { r2, r1 });
var objs = db.OptionalColumnTest.All().ToList<OptionalColumnTestObject>();
var expected = new[] {new OptionalColumnTestObject("Bob", "Dole"), new OptionalColumnTestObject("Bob", "Saget", "L"),};
Assert.That(objs, Is.EquivalentTo(expected));
}
[Test]
public void BulkInsertRecordsWithDifferentColumnsAndFewerColumnsInFirstRecordProperlyInsertsData()
{
DatabaseHelper.Reset();
var db = DatabaseHelper.Open();
dynamic r1 = new SimpleRecord();
r1.FirstName = "Bob";
r1.LastName = "Dole";
dynamic r2 = new SimpleRecord();
r2.FirstName = "Bob";
r2.MiddleInitial = "L";
r2.LastName = "Saget";
db.OptionalColumnTest.Insert(new[] { r1, r2 });
var objs = db.OptionalColumnTest.All().ToList<OptionalColumnTestObject>();
var expected = new[] { new OptionalColumnTestObject("Bob", "Dole"), new OptionalColumnTestObject("Bob", "Saget", "L"), };
Assert.That(objs, Is.EquivalentTo(expected));
}
private static IEnumerable<SchemaItem> GenerateItems()
{
for (int i = 0; i < 1000; i++)
{
yield return new SchemaItem(i, i.ToString());
}
}
}
class OptionalColumnTestObject
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleInitial { get; set; }
public OptionalColumnTestObject() {}
public OptionalColumnTestObject(string first, string last, string middle = null)
{
FirstName = first;
LastName = last;
MiddleInitial = middle;
}
public override string ToString()
{
return string.Format("<FirstName={0}, LastName={1}, MiddleInitial={2}>", FirstName, LastName, MiddleInitial);
}
public override bool Equals(object obj)
{
var other = obj as OptionalColumnTestObject;
if (other == null) return false;
return other.FirstName == FirstName && other.LastName == LastName && other.MiddleInitial == MiddleInitial;
}
}
class SchemaItem
{
public SchemaItem(int id, string description)
{
Id = id;
Description = description;
}
public int Id { get; set; }
public string Description { get; set; }
}
}