-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathTestQueryParents.fs
231 lines (211 loc) · 9.57 KB
/
TestQueryParents.fs
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
module Rezoom.SQL.Test.QueryParents
open NUnit.Framework
open FsUnit
open Rezoom.SQL.Mapping
open Rezoom.SQL.Mapping.CodeGeneration
type RecordFolder =
{
Id : int
Name : string
ChildFolders : RecordFolder array
ParentFolder : RecordFolder
}
[<AllowNullLiteral>]
type ClassFolder() =
member val Id = 0 with get, set
member val Name = "" with get, set
member val ChildFolders = null : ClassFolder array with get, set
member val ParentFolder = null : ClassFolder with get, set
// Not a test: expected to always stack overflow and crash VS test engine. Fun to run only as a curiousity.
// [<Test>]
let ``self-referential record equality stack overflows`` () =
let colMap =
[|
"Id", ColumnType.Int32
"Name", ColumnType.String
"ChildFolders$Id", ColumnType.Int32
"ChildFolders$Name", ColumnType.String
"ChildFolders$ChildFolders$Id", ColumnType.Int32
"ChildFolders$ChildFolders$Name", ColumnType.String
|] |> ColumnMap.Parse
let reader = ReaderTemplate<RecordFolder list>.Template().CreateReader()
reader.ProcessColumns(colMap)
for objectRow in
[|
ObjectRow(1, "A", 2, "A.1", 7, "A.1.1")
ObjectRow(1, "A", 3, "A.2", 8, "A.2.1")
ObjectRow(4, "B", 5, "B.1", 9, "B.1.1")
ObjectRow(4, "B", 6, "B.2", 10, "B.2.1")
|] do reader.Read(objectRow)
let folders1 = reader.ToEntity()
let reader = ReaderTemplate<RecordFolder list>.Template().CreateReader()
reader.ProcessColumns(colMap)
for objectRow in
[|
ObjectRow(1, "A", 2, "A.1", 7, "A.1.1")
ObjectRow(1, "A", 3, "A.2", 8, "A.2.1")
ObjectRow(4, "B", 5, "B.1", 9, "B.1.1")
ObjectRow(4, "B", 6, "B.2", 10, "B.2.1")
|] do reader.Read(objectRow)
let folders2 = reader.ToEntity()
let bottom1 = folders1.Head.ChildFolders.[0].ChildFolders.[0]
let bottom2 = folders2.Head.ChildFolders.[0].ChildFolders.[0]
Assert.AreEqual(bottom1, bottom2)
// This will stack overflow, because the equality comparison goes:
// are we equal? -->
// ^ are our Ids equal? yes.
// | are our Names equal? yes.
// ^ are our Children equal? yes.
// | are our Parents equal? -->
// ^ are their Ids equal? yes.
// | are their Names equal? yes.
// ^ are their Children equal? -->+
// | |
// +-<--<--<--<--<--<--<--<--<--<--<--<--+
// There is nothing we can do about this other than advise against using records this way.
// This test serves as a handy way of demonstrating the problem.
[<Test>]
let ``read record folders with parent backreferences`` () =
let colMap =
[|
"Id", ColumnType.Int32
"Name", ColumnType.String
"ChildFolders$Id", ColumnType.Int32
"ChildFolders$Name", ColumnType.String
|] |> ColumnMap.Parse
let reader = ReaderTemplate<RecordFolder array>.Template().CreateReader()
reader.ProcessColumns(colMap)
for objectRow in
[|
ObjectRow(1, "A", 2, "A.1")
ObjectRow(1, "A", 3, "A.2")
ObjectRow(4, "B", 5, "B.1")
ObjectRow(4, "B", 6, "B.2")
|] do reader.Read(objectRow)
let folders = reader.ToEntity()
Assert.IsNotNull(folders)
Assert.AreEqual(2, folders.Length)
Assert.IsNotNull(folders.[0])
Assert.IsNull(folders.[0].ParentFolder)
Assert.AreEqual(1, folders.[0].Id)
Assert.AreEqual("A", folders.[0].Name)
Assert.IsNotNull(folders.[0].ChildFolders)
Assert.AreEqual(2, folders.[0].ChildFolders.Length)
Assert.IsNotNull(folders.[0].ChildFolders.[0])
Assert.AreEqual(2, folders.[0].ChildFolders.[0].Id)
Assert.AreEqual("A.1", folders.[0].ChildFolders.[0].Name)
Assert.AreEqual(0, folders.[0].ChildFolders.[0].ChildFolders.Length)
Assert.IsNotNull(folders.[0].ChildFolders.[0].ParentFolder)
Assert.IsTrue(obj.ReferenceEquals(folders.[0], folders.[0].ChildFolders.[0].ParentFolder))
Assert.IsNotNull(folders.[0].ChildFolders.[1])
Assert.AreEqual(3, folders.[0].ChildFolders.[1].Id)
Assert.AreEqual("A.2", folders.[0].ChildFolders.[1].Name)
Assert.AreEqual(0, folders.[0].ChildFolders.[1].ChildFolders.Length)
Assert.IsNotNull(folders.[0].ChildFolders.[1].ParentFolder)
Assert.IsTrue(obj.ReferenceEquals(folders.[0], folders.[0].ChildFolders.[1].ParentFolder))
Assert.IsNotNull(folders.[1])
Assert.IsNull(folders.[1].ParentFolder)
Assert.AreEqual(4, folders.[1].Id)
Assert.AreEqual("B", folders.[1].Name)
Assert.IsNotNull(folders.[1].ChildFolders)
Assert.AreEqual(2, folders.[1].ChildFolders.Length)
Assert.IsNotNull(folders.[1].ChildFolders.[0])
Assert.AreEqual(5, folders.[1].ChildFolders.[0].Id)
Assert.AreEqual("B.1", folders.[1].ChildFolders.[0].Name)
Assert.AreEqual(0, folders.[1].ChildFolders.[0].ChildFolders.Length)
Assert.IsNotNull(folders.[1].ChildFolders.[0].ParentFolder)
Assert.IsTrue(obj.ReferenceEquals(folders.[1], folders.[1].ChildFolders.[0].ParentFolder))
Assert.IsNotNull(folders.[1].ChildFolders.[1])
Assert.AreEqual(6, folders.[1].ChildFolders.[1].Id)
Assert.AreEqual("B.2", folders.[1].ChildFolders.[1].Name)
Assert.AreEqual(0, folders.[1].ChildFolders.[1].ChildFolders.Length)
Assert.IsNotNull(folders.[1].ChildFolders.[1].ParentFolder)
Assert.IsTrue(obj.ReferenceEquals(folders.[1], folders.[1].ChildFolders.[1].ParentFolder))
[<Test>]
let ``read class folders with parent backreferences`` () =
let colMap =
[|
"Id", ColumnType.Int32
"Name", ColumnType.String
"ChildFolders$Id", ColumnType.Int32
"ChildFolders$Name", ColumnType.String
|] |> ColumnMap.Parse
let reader = ReaderTemplate<ClassFolder array>.Template().CreateReader()
reader.ProcessColumns(colMap)
let next = ReaderTemplate<ClassFolder array>.Template().CreateReader()
reader.ImpartKnowledgeToNext(next)
for objectRow in
[|
ObjectRow(1, "A", 2, "A.1")
ObjectRow(1, "A", 3, "A.2")
ObjectRow(4, "B", 5, "B.1")
ObjectRow(4, "B", 6, "B.2")
|] do reader.Read(objectRow)
let folders = reader.ToEntity()
Assert.IsNotNull(folders)
Assert.AreEqual(2, folders.Length)
Assert.IsNotNull(folders.[0])
Assert.IsNull(folders.[0].ParentFolder)
Assert.AreEqual(1, folders.[0].Id)
Assert.AreEqual("A", folders.[0].Name)
Assert.IsNotNull(folders.[0].ChildFolders)
Assert.AreEqual(2, folders.[0].ChildFolders.Length)
Assert.IsNotNull(folders.[0].ChildFolders.[0])
Assert.AreEqual(2, folders.[0].ChildFolders.[0].Id)
Assert.AreEqual("A.1", folders.[0].ChildFolders.[0].Name)
Assert.AreEqual(0, folders.[0].ChildFolders.[0].ChildFolders.Length)
Assert.IsNotNull(folders.[0].ChildFolders.[0].ParentFolder)
Assert.IsTrue(obj.ReferenceEquals(folders.[0], folders.[0].ChildFolders.[0].ParentFolder))
Assert.IsNotNull(folders.[0].ChildFolders.[1])
Assert.AreEqual(3, folders.[0].ChildFolders.[1].Id)
Assert.AreEqual("A.2", folders.[0].ChildFolders.[1].Name)
Assert.AreEqual(0, folders.[0].ChildFolders.[1].ChildFolders.Length)
Assert.IsNotNull(folders.[0].ChildFolders.[1].ParentFolder)
Assert.IsTrue(obj.ReferenceEquals(folders.[0], folders.[0].ChildFolders.[1].ParentFolder))
Assert.IsNotNull(folders.[1])
Assert.IsNull(folders.[1].ParentFolder)
Assert.AreEqual(4, folders.[1].Id)
Assert.AreEqual("B", folders.[1].Name)
Assert.IsNotNull(folders.[1].ChildFolders)
Assert.AreEqual(2, folders.[1].ChildFolders.Length)
Assert.IsNotNull(folders.[1].ChildFolders.[0])
Assert.AreEqual(5, folders.[1].ChildFolders.[0].Id)
Assert.AreEqual("B.1", folders.[1].ChildFolders.[0].Name)
Assert.AreEqual(0, folders.[1].ChildFolders.[0].ChildFolders.Length)
Assert.IsNotNull(folders.[1].ChildFolders.[0].ParentFolder)
Assert.IsTrue(obj.ReferenceEquals(folders.[1], folders.[1].ChildFolders.[0].ParentFolder))
Assert.IsNotNull(folders.[1].ChildFolders.[1])
Assert.AreEqual(6, folders.[1].ChildFolders.[1].Id)
Assert.AreEqual("B.2", folders.[1].ChildFolders.[1].Name)
Assert.AreEqual(0, folders.[1].ChildFolders.[1].ChildFolders.Length)
Assert.IsNotNull(folders.[1].ChildFolders.[1].ParentFolder)
Assert.IsTrue(obj.ReferenceEquals(folders.[1], folders.[1].ChildFolders.[1].ParentFolder))
[<Test>]
let ``record folder joined parents shouldn't have children populated`` () =
let colMap =
[|
"Id", ColumnType.Int32
"Name", ColumnType.String
"ParentFolder$Id", ColumnType.Int32
"ParentFolder$Name", ColumnType.String
|] |> ColumnMap.Parse
let reader = ReaderTemplate<RecordFolder array>.Template().CreateReader()
reader.ProcessColumns(colMap)
for objectRow in
[|
ObjectRow(2, "A.1", 1, "A")
ObjectRow(3, "A.2", 1, "A")
ObjectRow(5, "B.1", 4, "B")
ObjectRow(6, "B.2", 4, "B")
|] do reader.Read(objectRow)
let folders = reader.ToEntity()
Assert.IsNotNull(folders)
Assert.AreEqual(4, folders.Length)
Assert.AreEqual(2, folders.[0].Id)
Assert.AreEqual("A.1", folders.[0].Name)
Assert.AreEqual(0, folders.[0].ChildFolders.Length)
Assert.IsNotNull(folders.[0].ParentFolder)
Assert.AreEqual(1, folders.[0].ParentFolder.Id)
Assert.AreEqual("A", folders.[0].ParentFolder.Name)
Assert.IsNull(folders.[0].ParentFolder.ParentFolder)
Assert.AreEqual(0, folders.[0].ParentFolder.ChildFolders.Length)