-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSmokeTests.fsx
310 lines (276 loc) · 13 KB
/
SmokeTests.fsx
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#r "Artifacts/FlexLucene.dll"
#r "ikvm/IKVM.OpenJDK.Core.dll"
#r "ikvm/IKVM.OpenJDK.text.dll"
#r "ikvm/IKVM.Runtime.dll"
#r "dll/xunit.dll"
open FlexLucene.Analysis.Standard
open FlexLucene.Codecs
open FlexLucene.Document
open FlexLucene.Index
open FlexLucene.Queryparser.Classic
open FlexLucene.Search
open FlexLucene.Spatial.Prefix
open FlexLucene.Spatial.Prefix.Tree
open FlexLucene.Store
open Xunit
open System
open System.IO
open System.Collections.Generic
let mutable hasErrors = false
let RootDirectory = __SOURCE_DIRECTORY__
let TempDirectory = Path.Combine(RootDirectory, "work")
let exceptionWrapper (meth : unit -> unit) =
try
meth()
()
with e ->
hasErrors <- true
printfn "------------------------------------------------"
printfn "Test case failed: %s" (meth.GetType().FullName)
printfn "%A" e
printfn "------------------------------------------------"
let ShouldHaveCodec50() = Assert.True(Codec.AvailableCodecs().contains("Lucene50"), sprintf "Lucene50Codec not found. Available Codecs: %A" (Codec.AvailableCodecs()))
let ShouldHaveCodec410() = Assert.True(Codec.AvailableCodecs().contains("Lucene60"), sprintf "Lucene60Codec not found. Available Codecs: %A" (Codec.AvailableCodecs()))
let IndexingTest(directory : FlexLucene.Store.Directory) =
let analyzer = new StandardAnalyzer()
let config = new IndexWriterConfig(analyzer)
let iwriter = new IndexWriter(directory, config)
let doc = new Document()
let text = "This is the text to be indexed."
doc.Add(new Field("fieldname", text, TextField.TYPE_STORED))
iwriter.AddDocument(doc) |> ignore
iwriter.Close()
// Now search the index:
let ireader = DirectoryReader.Open(directory)
let isearcher = new IndexSearcher(ireader)
// Parse a simple query that searches for "text":
let parser = new QueryParser("fieldname", analyzer)
let query = parser.Parse("text")
let topDocs = isearcher.Search(query, 1000)
let hits : ScoreDoc [] = topDocs.ScoreDocs
Assert.Equal<int>(1, hits.Length)
// Iterate through the results:
for i = 0 to hits.Length - 1 do
let hitDoc = isearcher.Doc(hits.[i].Doc)
Assert.Equal<string>("This is the text to be indexed.", hitDoc.Get("fieldname"))
ireader.Close()
directory.Close()
let GetRandomPath() =
let dir = Path.Combine(TempDirectory, Guid.NewGuid().ToString())
(new java.io.File(dir)).toPath()
let CodecsShouldLoadProperly() =
let codecs = Codec.AvailableCodecs()
printfn "Available Codecs: %A" (codecs)
ShouldHaveCodec410 |> exceptionWrapper
ShouldHaveCodec50 |> exceptionWrapper
let PostingsFormatShouldLoadProperly() =
printfn "Postings Format : %A" (PostingsFormat.AvailablePostingsFormats())
Assert.True
(PostingsFormat.AvailablePostingsFormats().contains("Lucene50"), "Lucene50 Postings format not found.")
let IndexingTests() =
(fun _ -> IndexingTest(new RAMDirectory())) |> exceptionWrapper
(fun _ -> IndexingTest(new SimpleFSDirectory(GetRandomPath(), NativeFSLockFactory.INSTANCE))) |> exceptionWrapper
(fun _ -> IndexingTest(new MMapDirectory(GetRandomPath(), NativeFSLockFactory.GetDefault()))) |> exceptionWrapper
(fun _ -> IndexingTest(FSDirectory.Open(GetRandomPath()))) |> exceptionWrapper
let BooleanQueryCreationTests() =
let query = new BooleanQueryBuilder()
query.Add(new BooleanClause(new TermQuery(new Term("dummy")), BooleanClauseOccur.MUST)) |> ignore
()
let RangeQueryCreationTests() =
let query = LegacyNumericRangeQuery.NewDoubleRange("test", java.lang.Double(32.0), java.lang.Double(33.0), true, true)
()
open FlexLucene.Analysis
open FlexLucene.Analysis.Core
// Simple analyzer can be written and the base class methods are
// PascalCased as per C# convention
type SimpleAnalyzer() =
inherit Analyzer()
override __.CreateComponents (_) =
let src = new StandardTokenizer()
let filter = new LowerCaseFilter(src)
new AnalyzerTokenStreamComponents(src, filter)
let SimpleAnalyzerInitTest() =
let analyzer = new SimpleAnalyzer()
()
let flexCharTermAttribute =
lazy java.lang.Class.forName
(typeof<FlexLucene.Analysis.Tokenattributes.CharTermAttribute>.AssemblyQualifiedName)
/// Utility function to get tokens from the search string based upon the passed analyzer
/// This will enable us to avoid using the Lucene query parser
/// We cannot use simple white space based token generation as it really depends
/// upon the analyzer used
let ParseTextUsingAnalyzer (analyzer : FlexLucene.Analysis.Analyzer, fieldName, queryText) =
let tokens = new List<string>()
let source : TokenStream = analyzer.TokenStream(fieldName, new java.io.StringReader(queryText))
// Get the CharTermAttribute from the TokenStream
let termAtt = source.AddAttribute(flexCharTermAttribute.Value)
try
try
source.Reset()
while source.IncrementToken() do
tokens.Add(termAtt.ToString())
source.End()
with _ -> ()
finally
source.Close()
tokens
// An advance test which tests usage of CharTermAttribute
let TokenizationTest() =
let analyzer = new SimpleAnalyzer()
let result = ParseTextUsingAnalyzer (analyzer, "", "Hello World")
let expected = new List<string>([|"hello"; "world"|])
printfn "Tokenization Test"
printfn "Expected : %A" expected
printfn "Actual : %A" result
Assert.Equal<List<string>>(expected, result)
open Org.Locationtech.Spatial4j.Context
let SimpleSpatialTests() =
let ctx = SpatialContext.GEO
let grid = new GeohashPrefixTree(ctx, 11)
let strategy = new RecursivePrefixTreeStrategy(grid, "myGeoField")
let doc = new Document()
doc.Add(new LegacyIntField("id", 1, FieldStore.YES))
let pt = ctx.MakePoint(10.0, 10.0)
doc.Add(new StoredField(strategy.GetFieldName(), pt.GetX().ToString() + " " + pt.GetY().ToString()))
let MutableValueDoesNotHaveDuplicateMethods() =
let value = new FlexLucene.Util.Mutable.MutableValueBool()
value._Exists <- true;
Assert.True(value.Exists() = true, "Should be able to use the _ method in case of naming conflicts.")
let ToStringWorksCorrectly() =
let query = LegacyNumericRangeQuery.NewDoubleRange("test", java.lang.Double(32.0), java.lang.Double(33.0), true, true)
printfn "RangeQuery ToString : %s" (query.ToString())
Assert.Equal<string>(query.toString(), query.ToString())
let query = new TermQuery(new Term("Hello"))
printfn "TermQuery ToString : %s" (query.ToString())
Assert.Equal<string>(query.toString(), query.ToString())
open Org.Locationtech.Spatial4j.Shape.Impl
open Org.Locationtech.Spatial4j.Shape
let JavaToStringOverrideIsPickedUpByGeneratedToString() =
let baseClass = new RectangleImpl(1.0, 1.0, 1.0, 1.0, SpatialContext.GEO)
printfn "RectangleImpl ToString : %s" <| baseClass.ToString()
Assert.Equal<string>(baseClass.toString(), baseClass.ToString())
// Override java's toString()
let javaOverrideClass = { new RectangleImpl(1.0, 1.0, 1.0, 1.0, SpatialContext.GEO) with
override this.toString() = base.toString() + "<overridden>"
override this.GetBuffered(d : float, sc : SpatialContext) : Shape = base.GetBuffered(d, sc) :> Shape }
printfn "override toString RectangleImpl ToString : %s" <| javaOverrideClass.ToString()
Assert.Equal<string>(javaOverrideClass.toString(), javaOverrideClass.ToString())
Assert.Equal<string>(javaOverrideClass.ToString(), baseClass.toString() + "<overridden>")
// Overriding FlexLucene's generated ToString() will just override System.Object's virtual ToString().
// However, when calling the derived class, the generated FlexLucene's ToString method will be called (see e.g. *1*).
// System.Object's ToString() method from the derived class will be called only if you cast the derived
// instance to object (see e.g. *2*).
let flexOverrideClass = { new RectangleImpl(1.0, 1.0, 1.0, 1.0, SpatialContext.GEO) with
override this.ToString() = base.ToString() + "<overridden>"
override this.GetBuffered(d : float, sc : SpatialContext) : Shape = base.GetBuffered(d, sc) :> Shape }
printfn "RectangleImpl ToString without casting: %s" <| flexOverrideClass.ToString()
printfn "RectangleImpl ToString with casting: %s" <| (flexOverrideClass :> obj).ToString()
// E.g. *1*
Assert.Equal<string>(baseClass.toString(), flexOverrideClass.ToString())
// E.g. *2*
Assert.Equal<string>(baseClass.toString() + "<overridden>", (flexOverrideClass :> obj).ToString())
Assert.Equal<string>(flexOverrideClass.toString(), baseClass.toString())
let EqualityWorksCorrectly() =
let query1 = LegacyNumericRangeQuery.NewDoubleRange("test", java.lang.Double(32.0), java.lang.Double(33.0), true, true)
let query2 = LegacyNumericRangeQuery.NewDoubleRange("test", java.lang.Double(32.0), java.lang.Double(33.0), true, true)
let query3 = LegacyNumericRangeQuery.NewDoubleRange("test3", java.lang.Double(32.0), java.lang.Double(33.0), true, true)
Assert.Equal<Query>(query1, query2)
Assert.NotEqual<Query>(query1, query3)
let termQuery1 = new TermQuery(new Term("Hello"))
let termQuery2 = new TermQuery(new Term("Hello"))
Assert.Equal<int>(termQuery1.GetHashCode(), termQuery1.hashCode())
Assert.Equal<Query>(termQuery1, termQuery2)
type DummyIndexInput(dummy : string) =
inherit IndexInput(dummy)
override __.Length() = 0L
override __.Seek(l) = ()
override __.close() = ()
override __.GetFilePointer() = 0L
override this.Slice(a, b, c) = this :> IndexInput
override __.ReadBytes(a, b, c) = ()
override __.ReadByte() = 1uy
override this.equals(a) = a.GetHashCode() = this.GetHashCode()
override __.hashCode() = dummy.Length
let GetHashCodeCallshashCodeBehindTheScene() =
let a = new DummyIndexInput("test1")
let b = new DummyIndexInput("test02")
let c = new DummyIndexInput("test1")
printfn "Hash Code tests"
printfn "a GetHashCode: %i" <| a.GetHashCode()
printfn "a hashCode: %i" <| a.hashCode()
printfn "b GetHashCode: %i" <| b.GetHashCode()
printfn "b hashCode: %i" <| b.hashCode()
printfn "c GetHashCode: %i" <| c.GetHashCode()
printfn "c hashCode: %i" <| c.hashCode()
Assert.True(a.GetHashCode() = a.hashCode())
Assert.True(b.GetHashCode() = b.hashCode())
Assert.True(c.GetHashCode() = c.hashCode())
Assert.False(a.GetHashCode() = b.GetHashCode())
Assert.False(a.hashCode() = b.hashCode())
Assert.True(a.GetHashCode() = c.GetHashCode())
Assert.True(a.hashCode() = c.hashCode())
Assert.True(a.Equals(c))
Assert.True(a.equals(c))
Assert.True(c.Equals(a))
Assert.True(c.equals(a))
Assert.False(a.Equals(b))
Assert.False(a.equals(b))
let GeneratedEqualsCallsJavaEqualsBehindTheScenes() =
let baseClass = new RectangleImpl(1.0, 1.0, 1.0, 1.0, SpatialContext.GEO)
let baseClass2 = new RectangleImpl(1.0, 1.0, 1.0, 1.0, SpatialContext.GEO)
Assert.Equal(baseClass, baseClass)
Assert.True(baseClass.Equals(baseClass))
Assert.True((baseClass = baseClass))
Assert.Equal(baseClass, baseClass2)
let javaOverrideClass = { new RectangleImpl(1.0, 1.0, 1.0, 1.0, SpatialContext.GEO) with
override this.equals(obj) = false
override this.GetBuffered(d : float, sc : SpatialContext) : Shape = base.GetBuffered(d, sc) :> Shape }
Assert.False(javaOverrideClass.Equals(javaOverrideClass))
Assert.False((javaOverrideClass = javaOverrideClass))
type DerivedIndexInput(s) =
inherit IndexInput(s)
override __.Length() = 1L
override __.Seek(l) = ()
override __.close() = ()
override __.Clone() = new DerivedIndexInput("clone") :> IndexInput
override __.GetFilePointer() = 1L
override __.Slice(str, l1, l2) = __ :> IndexInput
override __.ReadBytes(barr, i1, i2, b) = ()
override __.ReadBytes(barr, i1, i2) = ()
override __.ReadByte() = 1uy
let OverridingCloneDoesntShowWarning() =
let x = new DerivedIndexInput("x")
let clone = x.Clone()
Assert.Equal<string>(x.ToString(), "x")
Assert.Equal<string>(clone.ToString(), "clone")
open FlexLucene.Analysis.De
let CreatingInBuiltAnalyzerThatUsesEmbeddedResourcesDoesntThrowError() =
let x = new GermanAnalyzer()
()
let executeTests() =
[|
CodecsShouldLoadProperly
PostingsFormatShouldLoadProperly
IndexingTests
BooleanQueryCreationTests
RangeQueryCreationTests
SimpleAnalyzerInitTest
TokenizationTest
SimpleSpatialTests
MutableValueDoesNotHaveDuplicateMethods
ToStringWorksCorrectly
JavaToStringOverrideIsPickedUpByGeneratedToString
EqualityWorksCorrectly
GetHashCodeCallshashCodeBehindTheScene
GeneratedEqualsCallsJavaEqualsBehindTheScenes
OverridingCloneDoesntShowWarning
CreatingInBuiltAnalyzerThatUsesEmbeddedResourcesDoesntThrowError
|]
|> Array.iter exceptionWrapper
if hasErrors then
printfn "Some Tests failed"
1
else
printfn "All tests passed"
0
executeTests()