0
|
1 using System;
|
|
2
|
|
3 using NUnit.Framework;
|
|
4
|
|
5 using BLToolkit.Data.Sql;
|
|
6 using BLToolkit.Mapping;
|
|
7 using BLToolkit.Reflection.Extension;
|
|
8
|
|
9 namespace Data.Sql
|
|
10 {
|
|
11 [TestFixture]
|
|
12 public class DefinitionTest
|
|
13 {
|
|
14 #region Table definition
|
|
15
|
|
16 static class Tables
|
|
17 {
|
|
18 public static readonly SqlTable Order = new SqlTable
|
|
19 {
|
|
20 Name = "Order",
|
|
21 PhysicalName = "Order",
|
|
22 Fields =
|
|
23 {
|
|
24 new SqlField { Name = "ID", PhysicalName = "OrderID" }
|
|
25 },
|
|
26 Joins =
|
|
27 {
|
|
28 new Join
|
|
29 {
|
|
30 TableName = "OrderItem",
|
|
31 JoinOns = { new JoinOn { Field = "ID", OtherField = "OrderID" } }
|
|
32 }
|
|
33 }
|
|
34 };
|
|
35
|
|
36 public static readonly SqlTable OrderItem = new SqlTable
|
|
37 {
|
|
38 Name = "OrderItem",
|
|
39 PhysicalName = "OrderItem",
|
|
40 Alias = "oi",
|
|
41 Fields =
|
|
42 {
|
|
43 new SqlField { Name = "OrderID" },
|
|
44 new SqlField { Name = "ID", PhysicalName = "OrderItemID" }
|
|
45 }
|
|
46 };
|
|
47 }
|
|
48
|
|
49 static class Xml
|
|
50 {
|
|
51 static readonly ExtensionList _ext = TypeExtension.GetExtensions("DefinitionData.xml");
|
|
52
|
|
53 public static readonly SqlTable Order = new SqlTable(_ext, "Order");
|
|
54 public static readonly SqlTable Order2 = new SqlTable(_ext, "Order2") { Name = "Order", PhysicalName = "Order" };
|
|
55 public static readonly SqlTable Order3 = new SqlTable(_ext, "Order3") { Name = "Order", PhysicalName = "Order" };
|
|
56 public static readonly SqlTable OrderItem = new SqlTable(_ext, "OrderItem");
|
|
57 }
|
|
58
|
|
59 public class Types
|
|
60 {
|
|
61 public class Order
|
|
62 {
|
|
63 [MapField("OrderID")] public int ID;
|
|
64 }
|
|
65
|
|
66 public class OrderItem
|
|
67 {
|
|
68 public int OrderID;
|
|
69 [MapField("OrderItemID")] public int ID;
|
|
70 }
|
|
71
|
|
72 public static SqlTable OrderTable = new SqlTable<Order>
|
|
73 {
|
|
74 Joins =
|
|
75 {
|
|
76 new Join
|
|
77 {
|
|
78 TableName = "OrderItem",
|
|
79 JoinOns = { new JoinOn { Field = "ID", OtherField = "OrderID" } }
|
|
80 }
|
|
81 }
|
|
82 };
|
|
83 public static SqlTable OrderItemTable = new SqlTable<OrderItem> { Alias = "oi" };
|
|
84 }
|
|
85
|
|
86 #endregion
|
|
87
|
|
88 static void CompareTables(SqlTable t1, SqlTable t2)
|
|
89 {
|
|
90 Assert.AreEqual(t1.Name, t2.Name);
|
|
91 Assert.AreEqual(t1.PhysicalName, t2.PhysicalName);
|
|
92 Assert.AreEqual(t1.Alias, t2.Alias);
|
|
93 Assert.AreEqual(t1.Database, t2.Database);
|
|
94 Assert.AreEqual(t1.Fields.Count, t2.Fields.Count);
|
|
95 Assert.AreEqual(t1.Joins. Count, t2.Joins. Count);
|
|
96
|
|
97 foreach (var field in t1.Fields.Values)
|
|
98 {
|
|
99 Assert.AreEqual(field.PhysicalName, t2[field.Name].PhysicalName);
|
|
100 }
|
|
101
|
|
102 for (var i = 0; i < t1.Joins.Count; i++)
|
|
103 {
|
|
104 var j1 = t1.Joins[i];
|
|
105 var j2 = t2.Joins[i];
|
|
106
|
|
107 Assert.AreEqual(j1.TableName, j2.TableName);
|
|
108 Assert.AreEqual(j1.Alias, j2.Alias);
|
|
109 Assert.AreEqual(j1.JoinOns.Count, j2.JoinOns.Count);
|
|
110
|
|
111 for (var j = 0; j < j1.JoinOns.Count; j++)
|
|
112 {
|
|
113 Assert.AreEqual(j1.JoinOns[j].Field, j2.JoinOns[j].Field);
|
|
114 Assert.AreEqual(j1.JoinOns[j].OtherField, j2.JoinOns[j].OtherField);
|
|
115 Assert.AreEqual(j1.JoinOns[j].Expression, j2.JoinOns[j].Expression);
|
|
116 }
|
|
117 }
|
|
118 }
|
|
119
|
|
120 [Test]
|
|
121 public void Test()
|
|
122 {
|
|
123 CompareTables(Tables.Order, Xml.Order);
|
|
124 CompareTables(Tables.Order, Xml.Order2);
|
|
125 CompareTables(Tables.Order, Xml.Order3);
|
|
126 CompareTables(Tables.OrderItem, Xml.OrderItem);
|
|
127 CompareTables(Tables.Order, Types.OrderTable);
|
|
128 CompareTables(Tables.OrderItem, Types.OrderItemTable);
|
|
129 }
|
|
130 }
|
|
131 }
|