Skip to content

Commit 5d000c2

Browse files
add unit tests for streamLike stored procedure, plus some refactoring
1 parent 348f621 commit 5d000c2

File tree

1 file changed

+138
-32
lines changed

1 file changed

+138
-32
lines changed

tests/UnitTest/iscru/util/StreamUtils/TestStreamLike.cls

+138-32
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,103 @@
11
Class UnitTest.iscru.util.StreamUtils.TestStreamLike Extends %UnitTest.TestCase
22
{
33

4+
/// Name of the persistent class for testing streamLike stored procedure
5+
Parameter TEMPCLASSNAME = {..%ClassName(1) _ ".TempClass"};
6+
7+
Method OnBeforeAllTests() As %Status
8+
{
9+
do ..dropTempClass(..#TEMPCLASSNAME)
10+
11+
// create and compile a persistent class with just one stream property
12+
quit ..createTempClass(..#TEMPCLASSNAME)
13+
}
14+
15+
Method OnAfterAllTests() As %Status
16+
{
17+
quit ..dropTempClass(..#TEMPCLASSNAME)
18+
}
19+
420
Method TestWithoutPPGCache()
521
{
6-
do ..internal($$$NO)
22+
do ..internal($$$NO, $$$NO)
723
}
824

925
Method TestWithPPGCache()
1026
{
11-
do ..internal($$$YES)
27+
do ..internal($$$YES, $$$NO)
1228
}
1329

14-
Method internal(cacheToPPG As %Boolean) [ Private ]
30+
Method TestStoredProcWithoutPPGCache()
1531
{
16-
// temp global name
17-
#dim global As %String = "^||" _ $classname()
18-
19-
do ..populateGlobal(global)
32+
do ..internal($$$NO, $$$YES)
33+
}
34+
35+
Method TestStoredProcWithPPGCache()
36+
{
37+
do ..internal($$$YES, $$$YES)
38+
}
39+
40+
Method internal(cacheToPPG As %Boolean, testStoredProc As %Boolean) [ Private ]
41+
{
42+
if testStoredProc && '$$$comClassDefined(..#TEMPCLASSNAME)
43+
{
44+
$$$ThrowStatus($$$ERROR($$$GeneralError, "Persistent class " _ ..#TEMPCLASSNAME _ " not compiled"))
45+
}
2046

21-
// now each node of the global contains a list:
47+
// populate temp global, assuming each node has a list of items:
2248
// edgesStr*, stuffingStringCount*, pattern, escape char, case insensitive flag, expected result
2349
// * - edgesStr and stuffingStringCount are arguments for ..createStream() method
50+
#dim globalName As %String = ..populateGlobal()
2451

25-
// loop through the items and test streamLike()
26-
for i = 1:1:@global
52+
// loop through the nodes and test streamLike() method or stored procedure (testStoredProc=1)
53+
for i = 1:1:@globalName
2754
{
28-
#dim list As %List = @global@(i)
55+
#dim list As %List = @globalName@(i)
2956
#dim result As %Boolean
3057
#dim expected As %Boolean
31-
do ..getResultAndExpected(cacheToPPG, list, .result, .expected)
58+
do ..doTest(cacheToPPG, testStoredProc, ..#TEMPCLASSNAME, list, .result, .expected)
3259

3360
do $$$AssertEquals(result, expected, expected _ " = " _ result)
3461
}
3562

36-
kill @global
63+
kill @globalName
3764
}
3865

39-
/// do ##class(UnitTest.iscru.util.StreamUtils.TestStreamLike).debugTest(0)
40-
ClassMethod debugTest(cacheToPPG As %Boolean)
66+
/// do ##class(UnitTest.iscru.util.StreamUtils.TestStreamLike).debugTest(0, 0)
67+
ClassMethod debugTest(cacheToPPG As %Boolean, testStoredProc As %Boolean)
4168
{
42-
// temp global name
43-
#dim global As %String = "^||" _ $classname()
69+
if testStoredProc
70+
{
71+
do ..dropTempClass(..#TEMPCLASSNAME)
72+
#dim sc As %Status = ..createTempClass(..#TEMPCLASSNAME)
73+
$$$ThrowOnError(sc)
74+
}
4475

45-
do ..populateGlobal(global)
76+
#dim globalName As %String = ..populateGlobal()
4677

47-
for i = 1:1:@global
78+
for i = 1:1:@globalName
4879
{
49-
#dim list As %List = @global@(i)
80+
#dim list As %List = @globalName@(i)
5081
#dim result As %Boolean
5182
#dim expected As %Boolean
52-
do ..getResultAndExpected(cacheToPPG, list, .result, .expected)
83+
do ..doTest(cacheToPPG, testStoredProc, ..#TEMPCLASSNAME, list, .result, .expected)
5384
if (expected '= result) write expected _ " '= " _ result _ " / " zw list
5485
}
5586

56-
kill @global
87+
kill @globalName
88+
if testStoredProc do ..dropTempClass(..#TEMPCLASSNAME)
5789
}
5890

59-
ClassMethod populateGlobal(global As %String) [ Private ]
91+
/// Return name of the global.
92+
/// Each node of the global contains a list:
93+
/// edgesStr*, stuffingStringCount*, pattern, escape char, case insensitive flag, expected result
94+
/// * - edgesStr and stuffingStringCount are arguments for ..createStream() method.
95+
ClassMethod populateGlobal() As %String [ Private ]
6096
{
61-
kill @global
97+
#dim global As %String = "^||" _ $classname()
6298

63-
// each node of the global will contain a list:
64-
// edgesStr*, stuffingStringCount*, pattern, escape char, case insensitive flag, expected result
65-
// * - edgesStr and stuffingStringCount are arguments for ..createStream() method
66-
99+
kill @global
100+
67101
// I. test with no escape char, case insensitive
68102
// 1) stream = "abcabc"
69103
set @global@($i(@global)) = $lb("abcabc", 0, "ABCABC", "", $$$YES, $$$YES)
@@ -173,6 +207,8 @@ ClassMethod populateGlobal(global As %String) [ Private ]
173207
set @global@($i(@global)) = $lb("abcabc", 0, $c(0), "", $$$YES, $$$NO)
174208
set @global@($i(@global)) = $lb("abcabc", 0, $c(0), "~", $$$NO, $$$NO)
175209
set @global@($i(@global)) = $lb("abcabc", 0, $c(0), "~", $$$YES, $$$NO)
210+
211+
quit global
176212
}
177213

178214
/// If <var>stuffingStringCount</var> equals zero then return <var>edgesStr</var> wrapped in a %Stream.TmpCharacter.
@@ -198,18 +234,88 @@ ClassMethod createStream(edgesStr As %String, stuffingStringCount As %Integer(MI
198234
quit s
199235
}
200236

201-
/// Call ##class(iscru.util.StreamUtils).streamLike() with data from <var>list</var>.
202-
/// We assume that <var>list</var> contains the following items:
237+
/// Using data from <var>list</var>: test either ##class(iscru.util.StreamUtils).streamLike() method
238+
/// or iscru_util.FunctionSet_streamLike stored procedure (if <var>testStoredProc</var> equals 1)
239+
/// assuming that <var>list</var> contains the following items:
203240
/// edgesStr*, stuffingStringCount*, pattern, escape char, case insensitive flag, expected result<br/>
204241
/// * - edgesStr and stuffingStringCount are arguments for ..createStream() method.
205-
ClassMethod getResultAndExpected(cacheToPPG As %Boolean, list As %List, Output result As %Boolean, Output expected As %Boolean) [ Private ]
242+
ClassMethod doTest(cacheToPPG As %Boolean, testStoredProc As %Boolean, className As %String, list As %List, Output result As %Boolean, Output expected As %Boolean) [ Private ]
206243
{
207244
#dim stream As %Stream.Object = ..createStream($list(list, 1), $list(list, 2))
208245
#dim pattern As %String = $list(list, 3)
209246
#dim escape As %String = $list(list, 4)
210247
#dim caseInsens As %Boolean = +$list(list, 5)
211248
set expected = +$list(list, 6)
212-
set result = ##class(iscru.util.StreamUtils).streamLike(stream, pattern, escape, caseInsens, cacheToPPG)
249+
250+
if 'testStoredProc
251+
{
252+
// test streamLike() method
253+
set result = ##class(iscru.util.StreamUtils).streamLike(stream, pattern, escape, caseInsens, cacheToPPG)
254+
}
255+
else
256+
{
257+
// test iscru_util.FunctionSet_streamLike stored procedure
258+
259+
// truncate table and add just one row
260+
do $classmethod(className, "%KillExtent")
261+
#dim obj As %Persistent = $classmethod(className, "%New")
262+
do obj.stream.CopyFrom(stream)
263+
#dim sc As %Status = obj.%Save()
264+
kill stream, obj
265+
$$$ThrowOnError(sc)
266+
267+
// query table
268+
#dim tableName As %String = $$$comClassKeyGet(className, $$$cCLASSsqlschemaname) _ "." _ $$$comClassKeyGet(className, $$$cCLASSsqltablename)
269+
#dim sql As %String = "SELECT 1 FROM " _ tableName _ " WHERE 1 = iscru_util.FunctionSet_streamLike(stream, ?, ?, ?, ?)"
270+
#dim statement As %SQL.Statement = ##class(%SQL.Statement).%New()
271+
set sc = statement.%Prepare(sql)
272+
$$$ThrowOnError(sc)
273+
274+
#dim stResult As %SQL.StatementResult = statement.%Execute(pattern, escape, caseInsens, cacheToPPG)
275+
#dim SQLCODE As %Integer = stResult.%SQLCODE
276+
if (SQLCODE < 0) $$$ThrowStatus($$$ERROR($$$SQLError, SQLCODE, stResult.%Message))
277+
278+
set result = stResult.%Next()
279+
}
280+
}
281+
282+
/// Create persistent class with the given name. Add just one stream property to the class.
283+
ClassMethod createTempClass(className As %String) [ Private ]
284+
{
285+
#dim c As %Dictionary.ClassDefinition = ##class(%Dictionary.ClassDefinition).%New()
286+
set c.Name = className
287+
set c.ProcedureBlock = $$$YES
288+
set c.Super = "%Persistent"
289+
290+
#dim p As %Dictionary.PropertyDefinition = ##class(%Dictionary.PropertyDefinition).%New()
291+
set p.Name = "stream"
292+
set p.Type = "%GlobalCharacterStream"
293+
do c.Properties.Insert(p)
294+
295+
#dim sc As %Status = c.%Save()
296+
if $$$ISERR(sc) quit sc
297+
298+
set sc = $System.OBJ.Compile(c.Name)
299+
if $$$ISERR(sc) quit sc
300+
301+
quit $$$OK
302+
}
303+
304+
/// Delete data and drop persistent class.
305+
ClassMethod dropTempClass(className As %String) [ Private ]
306+
{
307+
try
308+
{
309+
// if there is a compiled class with the given name then kill data using %KillExtent() method
310+
if $$$comClassDefined(className) do $classmethod(className, "%KillExtent")
311+
312+
// drop the class
313+
if ##class(%Dictionary.ClassDefinition).%ExistsId(className) do ##class(%Dictionary.ClassDefinition).%DeleteId(className)
314+
}
315+
catch
316+
{}
317+
318+
quit $$$OK
213319
}
214320

215321
}

0 commit comments

Comments
 (0)