-
Notifications
You must be signed in to change notification settings - Fork 1
/
SqlIntegration.cs
153 lines (128 loc) · 4.86 KB
/
SqlIntegration.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
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
using Dapper;
using Dapper.CX.SqlServer;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SqlServer.LocalDb;
using System;
using System.Data;
using System.Linq;
using Tests.Models;
namespace Tests.Base
{
[TestClass]
public class SqlIntegration
{
private SqlServerCrudProvider<int> GetProvider() => new SqlServerCrudProvider<int>(id => Convert.ToInt32(id));
[ClassInitialize]
public static void Initialize(TestContext context)
{
LocalDb.TryDropDatabase("DapperCX", out _);
using (var cn = LocalDb.GetConnection("DapperCX"))
{
LocalDb.ExecuteInitializeStatements(cn, DbObjects.CreateObjects());
}
}
private IDbConnection GetConnection()
{
return LocalDb.GetConnection("DapperCX");
}
[TestMethod]
public void UpdateWithLambda()
{
var emp = new Employee() { FirstName = "Janzy", LastName = "Horzenyadle", Id = 249578 };
var provider = GetProvider();
using (var cn = GetConnection())
{
provider.UpdateAsync(cn, emp, m => m.FirstName, m => m.LastName).Wait();
}
}
[TestMethod]
public void PlainInsertAsync()
{
var emp = new Employee() { FirstName = "Janzy", LastName = "Horzenyadle" };
var provider = GetProvider();
using (var cn = GetConnection())
{
var result = provider.InsertAsync(cn, emp, getIdentity: false).Result;
Assert.IsTrue(result == default);
}
}
[TestMethod]
public void PlainInsert()
{
var emp = new Employee() { FirstName = "Janzy", LastName = "Horzenyadle" };
var provider = GetProvider();
using (var cn = GetConnection())
{
var result = provider.Insert(cn, emp, getIdentity: false);
Assert.IsTrue(result == default);
}
}
[TestMethod]
public void DeleteByModel()
{
var dummyValue = new string[] { "this", "that" };
var emp = new Employee()
{
FirstName = "Loozy",
LastName = "Vorschindle",
Something = dummyValue
};
var provider = GetProvider();
using (var cn = GetConnection())
{
var result = provider.Insert(cn, emp);
provider.DeleteAsync(cn, emp).Wait();
Assert.IsTrue(emp.Something.SequenceEqual(dummyValue));
Assert.IsTrue(!provider.ExistsAsync<Employee>(cn, emp.Id).Result);
Assert.IsTrue(emp.Value == OtherEnum.Other); // set by ITrigger
}
}
[TestMethod]
public void DeleteById()
{
using (var cn = GetConnection())
{
var emp = new Employee()
{
FirstName = "Loozy",
LastName = "Vorschindle"
};
var provider = GetProvider();
var id = provider.InsertAsync(cn, emp).Result;
provider.DeleteAsync<Employee>(cn, id).Wait();
emp = provider.GetAsync<Employee>(cn, id).Result;
Assert.IsNull(emp);
}
}
[TestMethod]
public void DbDictionaryCoreOperations()
{
var sampleDictionary = new SessionDbDictionary(GetConnection);
var input = new Employee()
{
FirstName = "whoever",
LastName = "nobody",
Status = Status.Active
};
var id = sampleDictionary.SetAsync("hello", input).Result;
var entry = sampleDictionary.GetAsync<Employee>("hello").Result;
Assert.IsTrue(input.FirstName.Equals("whoever"));
Assert.IsTrue(input.LastName.Equals("nobody"));
Assert.IsTrue(input.Status == Status.Active);
entry.FirstName = "Django";
sampleDictionary.SetAsync("hello", entry).Wait();
entry = sampleDictionary.GetAsync<Employee>("hello").Result;
Assert.IsTrue(entry.FirstName.Equals("Django"));
Assert.IsTrue(sampleDictionary.KeyExistsAsync("hello").Result);
var getById = sampleDictionary.GetByIdAsync<Employee>(id).Result;
Assert.IsTrue(getById.FirstName.Equals(entry.FirstName));
Assert.IsTrue(getById.LastName.Equals(entry.LastName));
sampleDictionary.DeleteAsync("hello").Wait();
using (var cn = GetConnection())
{
cn.Execute("DROP TABLE [session].[User]");
cn.Execute("DROP SCHEMA [session]");
}
}
}
}