-
Notifications
You must be signed in to change notification settings - Fork 0
/
SqlServer.cs
121 lines (102 loc) · 4.75 KB
/
SqlServer.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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SqlSchema.Library.Models;
using SqlSchema.SqlServer;
using SqlServer.LocalDb;
using System.Linq;
using System.Threading.Tasks;
using SqlSchema.SqlServer.Extensions;
namespace Testing
{
[TestClass]
public class SqlServer
{
[TestMethod]
public async Task Analyze()
{
using (var cn = LocalDb.GetConnection("Ginseng8"))
{
var a = new SqlServerAnalyzer();
var objects = await a.GetDbObjectsAsync(cn);
Assert.IsTrue(objects.Any());
var oneToOne = objects.OfType<ForeignKey>().Where(fk => fk.Cardinality == JoinCardinality.OneToOne);
Assert.IsTrue(oneToOne.Select(fk => fk.ReferencingTable.Name).SequenceEqual(new[] { "LabelInstructions", "WorkItemDevelopment", "WorkItemPriority" }));
var oneToMany = objects.OfType<ForeignKey>().Where(fk => fk.Cardinality == JoinCardinality.OneToMany);
}
}
[TestMethod]
public async Task GetForeignKeys()
{
using (var cn = LocalDb.GetConnection("CycleLog2"))
{
var a = new SqlServerAnalyzer();
var objects = await a.GetDbObjectsAsync(cn);
var rideTable = objects.ToDictionary(row => $"{row.Schema}.{row.Name}")["dbo.Ride"] as Table;
var parentFks = rideTable.GetParentForeignKeys(objects);
Assert.IsTrue(parentFks.Count() == 2);
Assert.IsTrue(parentFks.Select(fk => fk.Name).SequenceEqual(new string[] { "FK_Ride_BicycleID", "FK_Ride_LocationID" }));
var fkDictionary = parentFks.ToDictionary(row => row.Name);
Assert.IsTrue(fkDictionary["FK_Ride_BicycleID"].ReferencedTable.Equals(new Table() { Schema = "dbo", Name = "Bicycle" }));
Assert.IsTrue(fkDictionary["FK_Ride_BicycleID"].ReferencingTable.Equals(new Table() { Schema = "dbo", Name = "Ride" }));
Assert.IsTrue(fkDictionary["FK_Ride_LocationID"].Columns.First().ReferencedName.Equals("ID"));
Assert.IsTrue(fkDictionary["FK_Ride_LocationID"].Columns.First().ReferencingName.Equals("LocationID"));
var childFks = rideTable.GetChildForeignKeys(objects);
Assert.IsTrue(childFks.Count() == 1);
Assert.IsTrue(childFks.First().ReferencingTable.Equals(new Table() { Schema = "dbo", Name = "RideTag" }));
Assert.IsTrue(childFks.First().ReferencedTable.Equals(new Table() { Schema = "dbo", Name = "Ride" }));
var tableDictionary = objects.OfType<Table>().ToDictionary(item => item.Name);
Assert.IsTrue(tableDictionary["RideTag"].UniqueConstraintColumns.SequenceEqual(new string[] { "RideID", "TagID" }));
}
}
[TestMethod]
public async Task GetViews()
{
using (var cn = LocalDb.GetConnection("Ginseng8"))
{
var a = new SqlServerAnalyzer();
var views = (await a.GetDbObjectsAsync(cn)).OfType<View>();
Assert.IsTrue(views.Any());
}
}
[TestMethod]
public async Task GetTableFunctions()
{
using (var cn = LocalDb.GetConnection("Ginseng8"))
{
var a = new SqlServerAnalyzer();
var functions = (await a.GetDbObjectsAsync(cn)).OfType<TableFunction>();
Assert.IsTrue(functions.Any());
}
}
[TestMethod]
public async Task GetProcedures()
{
using (var cn = LocalDb.GetConnection("Ginseng8"))
{
var a = new SqlServerAnalyzer();
var procs = (await a.GetDbObjectsAsync(cn)).OfType<Procedure>();
Assert.IsTrue(procs.Any());
var dictionary = procs.ToDictionary(item => item.Name);
Assert.IsTrue(dictionary["PostInvoice"].Arguments.Any(arg => arg.Name.Equals("@orgId")));
}
}
[TestMethod]
public async Task GetSynonyms()
{
using (var cn = LocalDb.GetConnection("ZingerSample"))
{
var a = new SqlServerAnalyzer();
var synonyms = (await a.GetDbObjectsAsync(cn)).OfType<Synonym>();
Assert.IsTrue(synonyms.Count() == 3);
}
}
[TestMethod]
public async Task DbExists()
{
using var cn = LocalDb.GetConnection("ZingerSample");
var exists = await cn.DatabaseExistsAsync("hello");
Assert.IsFalse(exists);
exists = await cn.DatabaseExistsAsync("ZingerSample");
Assert.IsTrue(exists);
}
}
}